blob: c3cd25cb17342490f693e3597fe9966a07a8ab36 [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
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000019#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "intrinsics.h"
29#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000030#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010031#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070033#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000035#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010036#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010040#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041
Vladimir Marko0a516052019-10-14 13:00:44 +000042namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010043
Roland Levillain0d5a2812015-11-13 10:07:31 +000044template<class MirrorType>
45class GcRoot;
46
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047namespace x86 {
48
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010050static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050051static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
Mark Mendell24f2dfa2015-01-14 19:51:45 -050053static constexpr int kC2ConditionMask = 0x400;
54
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000055static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000056
Aart Bik1f8d51b2018-02-15 10:42:37 -080057static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
58static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
59
Vladimir Marko3232dbb2018-07-25 15:42:46 +010060static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
61 InvokeRuntimeCallingConvention calling_convention;
62 RegisterSet caller_saves = RegisterSet::Empty();
63 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
64 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
65 // that the the kPrimNot result register is the same as the first argument register.
66 return caller_saves;
67}
68
Roland Levillain7cbd27f2016-08-11 23:53:33 +010069// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
70#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070071#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072
Andreas Gampe85b62f22015-09-09 13:15:38 -070073class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010077 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010078 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000080 if (instruction_->CanThrowIntoCatchBlock()) {
81 // Live registers will be restored in the catch block if caught.
82 SaveLiveRegisters(codegen, instruction_->GetLocations());
83 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010084 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010085 instruction_,
86 instruction_->GetDexPc(),
87 this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 }
90
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010093 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010094
Nicolas Geoffraye5038322014-07-04 09:41:32 +010095 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100103 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100104 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100106 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000107 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000108 }
109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100110 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100112 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100113
Calin Juravled0d48522014-11-04 16:40:20 +0000114 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000115 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
116};
117
Andreas Gampe85b62f22015-09-09 13:15:38 -0700118class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000119 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000120 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
121 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100123 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000124 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 if (is_div_) {
126 __ negl(reg_);
127 } else {
128 __ movl(reg_, Immediate(0));
129 }
Calin Juravled0d48522014-11-04 16:40:20 +0000130 __ jmp(GetExitLabel());
131 }
132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100133 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100134
Calin Juravled0d48522014-11-04 16:40:20 +0000135 private:
136 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Andreas Gampe85b62f22015-09-09 13:15:38 -0700141class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000143 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100145 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000149 // We're moving two locations to locations that could overlap, so we need a parallel
150 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000151 if (instruction_->CanThrowIntoCatchBlock()) {
152 // Live registers will be restored in the catch block if caught.
153 SaveLiveRegisters(codegen, instruction_->GetLocations());
154 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400155
156 // Are we using an array length from memory?
157 HInstruction* array_length = instruction_->InputAt(1);
158 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400160 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
161 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100162 HArrayLength* length = array_length->AsArrayLength();
163 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400164 Location array_loc = array_length->GetLocations()->InAt(0);
165 Address array_len(array_loc.AsRegister<Register>(), len_offset);
166 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
167 // Check for conflicts with index.
168 if (length_loc.Equals(locations->InAt(0))) {
169 // We know we aren't using parameter 2.
170 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
171 }
172 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100173 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100174 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700175 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400176 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100180 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400181 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100182 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100184 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
185 ? kQuickThrowStringBounds
186 : kQuickThrowArrayBounds;
187 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100188 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 }
191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100192 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100194 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100195
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
198};
199
Andreas Gampe85b62f22015-09-09 13:15:38 -0700200class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000203 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100205 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700206 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100207 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700209 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100210 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000211 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700212 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100213 if (successor_ == nullptr) {
214 __ jmp(GetReturnLabel());
215 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100216 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 }
219
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 Label* GetReturnLabel() {
221 DCHECK(successor_ == nullptr);
222 return &return_label_;
223 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000224
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100225 HBasicBlock* GetSuccessor() const {
226 return successor_;
227 }
228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100229 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100230
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000231 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100232 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000233 Label return_label_;
234
235 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
236};
237
Vladimir Markoaad75c62016-10-03 08:46:48 +0000238class LoadStringSlowPathX86 : public SlowPathCode {
239 public:
240 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
241
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100242 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000243 LocationSummary* locations = instruction_->GetLocations();
244 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
245
246 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
250 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000251 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
252 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000253 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
254 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
255 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
256 RestoreLiveRegisters(codegen, locations);
257
Vladimir Markoaad75c62016-10-03 08:46:48 +0000258 __ jmp(GetExitLabel());
259 }
260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100261 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000262
263 private:
264 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
265};
266
Andreas Gampe85b62f22015-09-09 13:15:38 -0700267class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100269 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
270 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100272 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 }
274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100275 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100277 Location out = locations->Out();
278 const uint32_t dex_pc = instruction_->GetDexPc();
279 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
280 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
283 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100287 if (must_resolve_type) {
288 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
289 dex::TypeIndex type_index = cls_->GetTypeIndex();
290 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100291 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
292 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100293 // If we also must_do_clinit, the resolved type is now in the correct register.
294 } else {
295 DCHECK(must_do_clinit);
296 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
297 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
298 }
299 if (must_do_clinit) {
300 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
301 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000302 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000303
304 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 if (out.IsValid()) {
306 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
307 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000308 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310 __ jmp(GetExitLabel());
311 }
312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100313 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100314
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000319 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
320};
321
Andreas Gampe85b62f22015-09-09 13:15:38 -0700322class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000324 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000325 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100327 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 DCHECK(instruction_->IsCheckCast()
330 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
333 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000335 if (kPoisonHeapReferences &&
336 instruction_->IsCheckCast() &&
337 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
338 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
339 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
340 }
341
Vladimir Marko87584542017-12-12 17:47:52 +0000342 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343 SaveLiveRegisters(codegen, locations);
344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 // We're moving two locations to locations that could overlap, so we need a parallel
347 // move resolver.
348 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800349 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100351 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800352 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800353 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100356 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 instruction_,
358 instruction_->GetDexPc(),
359 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 } else {
362 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800363 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
364 instruction_,
365 instruction_->GetDexPc(),
366 this);
367 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368 }
369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 if (!is_fatal_) {
371 if (instruction_->IsInstanceOf()) {
372 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
373 }
374 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 __ jmp(GetExitLabel());
377 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378 }
379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100380 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
381 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100382
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000383 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000385
386 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
387};
388
Andreas Gampe85b62f22015-09-09 13:15:38 -0700389class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 public:
Aart Bik42249c32016-01-07 15:33:50 -0800391 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000392 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100394 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100395 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100397 LocationSummary* locations = instruction_->GetLocations();
398 SaveLiveRegisters(codegen, locations);
399 InvokeRuntimeCallingConvention calling_convention;
400 x86_codegen->Load32BitValue(
401 calling_convention.GetRegisterAt(0),
402 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100403 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100404 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700405 }
406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100407 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100408
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700409 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
411};
412
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413class ArraySetSlowPathX86 : public SlowPathCode {
414 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000415 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100417 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100418 LocationSummary* locations = instruction_->GetLocations();
419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, locations);
421
422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100423 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 parallel_move.AddMove(
425 locations->InAt(0),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100427 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100428 nullptr);
429 parallel_move.AddMove(
430 locations->InAt(1),
431 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100432 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 nullptr);
434 parallel_move.AddMove(
435 locations->InAt(2),
436 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100437 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 nullptr);
439 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
440
441 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100442 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000443 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 RestoreLiveRegisters(codegen, locations);
445 __ jmp(GetExitLabel());
446 }
447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100448 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100449
450 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100451 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
452};
453
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100454// Slow path marking an object reference `ref` during a read
455// barrier. The field `obj.field` in the object `obj` holding this
456// reference does not get updated by this slow path after marking (see
457// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
458//
459// This means that after the execution of this slow path, `ref` will
460// always be up-to-date, but `obj.field` may not; i.e., after the
461// flip, `ref` will be a to-space reference, but `obj.field` will
462// probably still be a from-space reference (unless it gets updated by
463// another thread, or if another thread installed another object
464// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
466 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100467 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
468 Location ref,
469 bool unpoison_ref_before_marking)
470 : SlowPathCode(instruction),
471 ref_(ref),
472 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473 DCHECK(kEmitCompilerReadBarrier);
474 }
475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100476 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100478 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000479 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100482 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000483 DCHECK(instruction_->IsInstanceFieldGet() ||
484 instruction_->IsStaticFieldGet() ||
485 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100486 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 instruction_->IsLoadClass() ||
488 instruction_->IsLoadString() ||
489 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100490 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100491 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
492 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 << "Unexpected instruction in read barrier marking slow path: "
494 << instruction_->DebugName();
495
496 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 }
Roland Levillain4359e612016-07-20 11:32:19 +0100501 // No need to save live registers; it's taken care of by the
502 // entrypoint. Also, there is no need to update the stack mask,
503 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 DCHECK_NE(ref_reg, ESP);
506 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in EAX):
511 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100513 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100515 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
Roland Levillain97c46462017-05-11 14:04:03 +0100521 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100522 // This runtime call does not require a stack map.
523 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524 __ jmp(GetExitLabel());
525 }
526
527 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528 // The location (register) of the marked object reference.
529 const Location ref_;
530 // Should the reference in `ref_` be unpoisoned prior to marking it?
531 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532
533 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
534};
535
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100536// Slow path marking an object reference `ref` during a read barrier,
537// and if needed, atomically updating the field `obj.field` in the
538// object `obj` holding this reference after marking (contrary to
539// ReadBarrierMarkSlowPathX86 above, which never tries to update
540// `obj.field`).
541//
542// This means that after the execution of this slow path, both `ref`
543// and `obj.field` will be up-to-date; i.e., after the flip, both will
544// hold the same to-space reference (unless another thread installed
545// another object reference (different from `ref`) in `obj.field`).
546class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
547 public:
548 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
549 Location ref,
550 Register obj,
551 const Address& field_addr,
552 bool unpoison_ref_before_marking,
553 Register temp)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp_(temp) {
560 DCHECK(kEmitCompilerReadBarrier);
561 }
562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100565 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100566 LocationSummary* locations = instruction_->GetLocations();
567 Register ref_reg = ref_.AsRegister<Register>();
568 DCHECK(locations->CanCall());
569 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
570 // This slow path is only used by the UnsafeCASObject intrinsic.
571 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
572 << "Unexpected instruction in read barrier marking and field updating slow path: "
573 << instruction_->DebugName();
574 DCHECK(instruction_->GetLocations()->Intrinsified());
575 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
576
577 __ Bind(GetEntryLabel());
578 if (unpoison_ref_before_marking_) {
579 // Object* ref = ref_addr->AsMirrorPtr()
580 __ MaybeUnpoisonHeapReference(ref_reg);
581 }
582
583 // Save the old (unpoisoned) reference.
584 __ movl(temp_, ref_reg);
585
586 // No need to save live registers; it's taken care of by the
587 // entrypoint. Also, there is no need to update the stack mask,
588 // as this runtime call will not trigger a garbage collection.
589 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
590 DCHECK_NE(ref_reg, ESP);
591 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
592 // "Compact" slow path, saving two moves.
593 //
594 // Instead of using the standard runtime calling convention (input
595 // and output in EAX):
596 //
597 // EAX <- ref
598 // EAX <- ReadBarrierMark(EAX)
599 // ref <- EAX
600 //
601 // we just use rX (the register containing `ref`) as input and output
602 // of a dedicated entrypoint:
603 //
604 // rX <- ReadBarrierMarkRegX(rX)
605 //
Roland Levillain97c46462017-05-11 14:04:03 +0100606 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100607 // This runtime call does not require a stack map.
608 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
609
610 // If the new reference is different from the old reference,
611 // update the field in the holder (`*field_addr`).
612 //
613 // Note that this field could also hold a different object, if
614 // another thread had concurrently changed it. In that case, the
615 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
616 // operation below would abort the CAS, leaving the field as-is.
617 NearLabel done;
618 __ cmpl(temp_, ref_reg);
619 __ j(kEqual, &done);
620
621 // Update the the holder's field atomically. This may fail if
622 // mutator updates before us, but it's OK. This is achieved
623 // using a strong compare-and-set (CAS) operation with relaxed
624 // memory synchronization ordering, where the expected value is
625 // the old reference and the desired value is the new reference.
626 // This operation is implemented with a 32-bit LOCK CMPXLCHG
627 // instruction, which requires the expected value (the old
628 // reference) to be in EAX. Save EAX beforehand, and move the
629 // expected value (stored in `temp_`) into EAX.
630 __ pushl(EAX);
631 __ movl(EAX, temp_);
632
633 // Convenience aliases.
634 Register base = obj_;
635 Register expected = EAX;
636 Register value = ref_reg;
637
638 bool base_equals_value = (base == value);
639 if (kPoisonHeapReferences) {
640 if (base_equals_value) {
641 // If `base` and `value` are the same register location, move
642 // `value` to a temporary register. This way, poisoning
643 // `value` won't invalidate `base`.
644 value = temp_;
645 __ movl(value, base);
646 }
647
648 // Check that the register allocator did not assign the location
649 // of `expected` (EAX) to `value` nor to `base`, so that heap
650 // poisoning (when enabled) works as intended below.
651 // - If `value` were equal to `expected`, both references would
652 // be poisoned twice, meaning they would not be poisoned at
653 // all, as heap poisoning uses address negation.
654 // - If `base` were equal to `expected`, poisoning `expected`
655 // would invalidate `base`.
656 DCHECK_NE(value, expected);
657 DCHECK_NE(base, expected);
658
659 __ PoisonHeapReference(expected);
660 __ PoisonHeapReference(value);
661 }
662
663 __ LockCmpxchgl(field_addr_, value);
664
665 // If heap poisoning is enabled, we need to unpoison the values
666 // that were poisoned earlier.
667 if (kPoisonHeapReferences) {
668 if (base_equals_value) {
669 // `value` has been moved to a temporary register, no need
670 // to unpoison it.
671 } else {
672 __ UnpoisonHeapReference(value);
673 }
674 // No need to unpoison `expected` (EAX), as it is be overwritten below.
675 }
676
677 // Restore EAX.
678 __ popl(EAX);
679
680 __ Bind(&done);
681 __ jmp(GetExitLabel());
682 }
683
684 private:
685 // The location (register) of the marked object reference.
686 const Location ref_;
687 // The register containing the object holding the marked object reference field.
688 const Register obj_;
689 // The address of the marked reference field. The base of this address must be `obj_`.
690 const Address field_addr_;
691
692 // Should the reference in `ref_` be unpoisoned prior to marking it?
693 const bool unpoison_ref_before_marking_;
694
695 const Register temp_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
698};
699
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700// Slow path generating a read barrier for a heap reference.
701class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
702 public:
703 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
704 Location out,
705 Location ref,
706 Location obj,
707 uint32_t offset,
708 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000709 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 out_(out),
711 ref_(ref),
712 obj_(obj),
713 offset_(offset),
714 index_(index) {
715 DCHECK(kEmitCompilerReadBarrier);
716 // If `obj` is equal to `out` or `ref`, it means the initial object
717 // has been overwritten by (or after) the heap object reference load
718 // to be instrumented, e.g.:
719 //
720 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000721 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722 //
723 // In that case, we have lost the information about the original
724 // object, and the emitted read barrier cannot work properly.
725 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
726 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
727 }
728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100729 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
731 LocationSummary* locations = instruction_->GetLocations();
732 Register reg_out = out_.AsRegister<Register>();
733 DCHECK(locations->CanCall());
734 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100735 DCHECK(instruction_->IsInstanceFieldGet() ||
736 instruction_->IsStaticFieldGet() ||
737 instruction_->IsArrayGet() ||
738 instruction_->IsInstanceOf() ||
739 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700740 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000741 << "Unexpected instruction in read barrier for heap reference slow path: "
742 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000743
744 __ Bind(GetEntryLabel());
745 SaveLiveRegisters(codegen, locations);
746
747 // We may have to change the index's value, but as `index_` is a
748 // constant member (like other "inputs" of this slow path),
749 // introduce a copy of it, `index`.
750 Location index = index_;
751 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000753 if (instruction_->IsArrayGet()) {
754 // Compute the actual memory offset and store it in `index`.
755 Register index_reg = index_.AsRegister<Register>();
756 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
757 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
758 // We are about to change the value of `index_reg` (see the
759 // calls to art::x86::X86Assembler::shll and
760 // art::x86::X86Assembler::AddImmediate below), but it has
761 // not been saved by the previous call to
762 // art::SlowPathCode::SaveLiveRegisters, as it is a
763 // callee-save register --
764 // art::SlowPathCode::SaveLiveRegisters does not consider
765 // callee-save registers, as it has been designed with the
766 // assumption that callee-save registers are supposed to be
767 // handled by the called function. So, as a callee-save
768 // register, `index_reg` _would_ eventually be saved onto
769 // the stack, but it would be too late: we would have
770 // changed its value earlier. Therefore, we manually save
771 // it here into another freely available register,
772 // `free_reg`, chosen of course among the caller-save
773 // registers (as a callee-save `free_reg` register would
774 // exhibit the same problem).
775 //
776 // Note we could have requested a temporary register from
777 // the register allocator instead; but we prefer not to, as
778 // this is a slow path, and we know we can find a
779 // caller-save register that is available.
780 Register free_reg = FindAvailableCallerSaveRegister(codegen);
781 __ movl(free_reg, index_reg);
782 index_reg = free_reg;
783 index = Location::RegisterLocation(index_reg);
784 } else {
785 // The initial register stored in `index_` has already been
786 // saved in the call to art::SlowPathCode::SaveLiveRegisters
787 // (as it is not a callee-save register), so we can freely
788 // use it.
789 }
790 // Shifting the index value contained in `index_reg` by the scale
791 // factor (2) cannot overflow in practice, as the runtime is
792 // unable to allocate object arrays with a size larger than
793 // 2^26 - 1 (that is, 2^28 - 4 bytes).
794 __ shll(index_reg, Immediate(TIMES_4));
795 static_assert(
796 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
797 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
798 __ AddImmediate(index_reg, Immediate(offset_));
799 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100800 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
801 // intrinsics, `index_` is not shifted by a scale factor of 2
802 // (as in the case of ArrayGet), as it is actually an offset
803 // to an object field within an object.
804 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000805 DCHECK(instruction_->GetLocations()->Intrinsified());
806 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
807 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
808 << instruction_->AsInvoke()->GetIntrinsic();
809 DCHECK_EQ(offset_, 0U);
810 DCHECK(index_.IsRegisterPair());
811 // UnsafeGet's offset location is a register pair, the low
812 // part contains the correct offset.
813 index = index_.ToLow();
814 }
815 }
816
817 // We're moving two or three locations to locations that could
818 // overlap, so we need a parallel move resolver.
819 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 parallel_move.AddMove(ref_,
822 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100823 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000824 nullptr);
825 parallel_move.AddMove(obj_,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100827 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 nullptr);
829 if (index.IsValid()) {
830 parallel_move.AddMove(index,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 nullptr);
834 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
835 } else {
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
838 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100839 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 CheckEntrypointTypes<
841 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
842 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
843
844 RestoreLiveRegisters(codegen, locations);
845 __ jmp(GetExitLabel());
846 }
847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100848 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849
850 private:
851 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
852 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
853 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
854 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
855 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
856 return static_cast<Register>(i);
857 }
858 }
859 // We shall never fail to find a free caller-save register, as
860 // there are more than two core caller-save registers on x86
861 // (meaning it is possible to find one which is different from
862 // `ref` and `obj`).
863 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
864 LOG(FATAL) << "Could not find a free caller-save register";
865 UNREACHABLE();
866 }
867
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 const Location out_;
869 const Location ref_;
870 const Location obj_;
871 const uint32_t offset_;
872 // An additional location containing an index to an array.
873 // Only used for HArrayGet and the UnsafeGetObject &
874 // UnsafeGetObjectVolatile intrinsics.
875 const Location index_;
876
877 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
878};
879
880// Slow path generating a read barrier for a GC root.
881class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
882 public:
883 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000885 DCHECK(kEmitCompilerReadBarrier);
886 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100888 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889 LocationSummary* locations = instruction_->GetLocations();
890 Register reg_out = out_.AsRegister<Register>();
891 DCHECK(locations->CanCall());
892 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000893 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
894 << "Unexpected instruction in read barrier for GC root slow path: "
895 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 __ Bind(GetEntryLabel());
898 SaveLiveRegisters(codegen, locations);
899
900 InvokeRuntimeCallingConvention calling_convention;
901 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
902 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100903 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
908 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
909
910 RestoreLiveRegisters(codegen, locations);
911 __ jmp(GetExitLabel());
912 }
913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100914 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000915
916 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000917 const Location out_;
918 const Location root_;
919
920 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
921};
922
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100924// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
925#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926
Aart Bike9f37602015-10-09 11:15:55 -0700927inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700928 switch (cond) {
929 case kCondEQ: return kEqual;
930 case kCondNE: return kNotEqual;
931 case kCondLT: return kLess;
932 case kCondLE: return kLessEqual;
933 case kCondGT: return kGreater;
934 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700935 case kCondB: return kBelow;
936 case kCondBE: return kBelowEqual;
937 case kCondA: return kAbove;
938 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700939 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 LOG(FATAL) << "Unreachable";
941 UNREACHABLE();
942}
943
Aart Bike9f37602015-10-09 11:15:55 -0700944// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700949 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100950 case kCondLT: return kBelow;
951 case kCondLE: return kBelowEqual;
952 case kCondGT: return kAbove;
953 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700954 // Unsigned remain unchanged.
955 case kCondB: return kBelow;
956 case kCondBE: return kBelowEqual;
957 case kCondA: return kAbove;
958 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959 }
960 LOG(FATAL) << "Unreachable";
961 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700962}
963
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100964void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100965 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966}
967
968void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100969 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970}
971
Vladimir Markoa0431112018-06-25 09:32:54 +0100972const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
973 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
974}
975
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100976size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
977 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
978 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100979}
980
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100981size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
982 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
983 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100984}
985
Mark Mendell7c8d0092015-01-26 11:21:33 -0500986size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700988 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700989 } else {
990 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
991 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100992 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500993}
994
995size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700996 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700997 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700998 } else {
999 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1000 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001001 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001002}
1003
Calin Juravle175dc732015-08-25 15:42:32 +01001004void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1005 HInstruction* instruction,
1006 uint32_t dex_pc,
1007 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001008 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001009 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1010 if (EntrypointRequiresStackMap(entrypoint)) {
1011 RecordPcInfo(instruction, dex_pc, slow_path);
1012 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001013}
1014
Roland Levillaindec8f632016-07-22 17:10:06 +01001015void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1016 HInstruction* instruction,
1017 SlowPathCode* slow_path) {
1018 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001019 GenerateInvokeRuntime(entry_point_offset);
1020}
1021
1022void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001023 __ fs()->call(Address::Absolute(entry_point_offset));
1024}
1025
Mark Mendellfb8d2792015-03-31 22:16:59 -04001026CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001027 const CompilerOptions& compiler_options,
1028 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001029 : CodeGenerator(graph,
1030 kNumberOfCpuRegisters,
1031 kNumberOfXmmRegisters,
1032 kNumberOfRegisterPairs,
1033 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1034 arraysize(kCoreCalleeSaves))
1035 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001036 0,
1037 compiler_options,
1038 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001039 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001040 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001041 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 move_resolver_(graph->GetAllocator(), this),
1043 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1045 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001048 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001050 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001051 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1052 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001053 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001055 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001056 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 // Use a fake return address register to mimic Quick.
1058 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001059}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001060
David Brazdil58282f42016-01-14 12:45:10 +00001061void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001063 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001064}
1065
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001066InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001067 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001068 assembler_(codegen->GetAssembler()),
1069 codegen_(codegen) {}
1070
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001072 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073}
1074
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001075void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1076 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1077 Register reg = EAX;
1078 if (is_frame_entry) {
1079 reg = kMethodRegisterArgument;
1080 } else {
1081 __ pushl(EAX);
1082 __ movl(EAX, Address(ESP, kX86WordSize));
1083 }
1084 NearLabel overflow;
1085 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1086 Immediate(ArtMethod::MaxCounter()));
1087 __ j(kEqual, &overflow);
1088 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1089 Immediate(1));
1090 __ Bind(&overflow);
1091 if (!is_frame_entry) {
1092 __ popl(EAX);
1093 }
1094 }
1095
1096 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1097 ScopedObjectAccess soa(Thread::Current());
1098 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
1099 uint32_t address = reinterpret_cast32<uint32_t>(info);
1100 NearLabel done;
1101 if (HasEmptyFrame()) {
1102 CHECK(is_frame_entry);
1103 // Alignment
1104 __ subl(ESP, Immediate(8));
1105 __ cfi().AdjustCFAOffset(8);
1106 // We need a temporary. The stub also expects the method at bottom of stack.
1107 __ pushl(EAX);
1108 __ cfi().AdjustCFAOffset(4);
1109 __ movl(EAX, Immediate(address));
1110 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()), Immediate(1));
1111 __ j(kCarryClear, &done);
1112 GenerateInvokeRuntime(
1113 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1114 __ Bind(&done);
1115 // We don't strictly require to restore EAX, but this makes the generated
1116 // code easier to reason about.
1117 __ popl(EAX);
1118 __ cfi().AdjustCFAOffset(-4);
1119 __ addl(ESP, Immediate(8));
1120 __ cfi().AdjustCFAOffset(-8);
1121 } else {
1122 if (!RequiresCurrentMethod()) {
1123 CHECK(is_frame_entry);
1124 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1125 }
1126 // We need a temporary.
1127 __ pushl(EAX);
1128 __ cfi().AdjustCFAOffset(4);
1129 __ movl(EAX, Immediate(address));
1130 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()), Immediate(1));
1131 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1132 __ cfi().AdjustCFAOffset(-4);
1133 __ j(kCarryClear, &done);
1134 GenerateInvokeRuntime(
1135 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1136 __ Bind(&done);
1137 }
1138 }
1139}
1140
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001141void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001142 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001143 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001144 bool skip_overflow_check =
1145 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001146 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001147
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001148 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001149 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1150 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001152 }
1153
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001154 if (!HasEmptyFrame()) {
1155 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1156 Register reg = kCoreCalleeSaves[i];
1157 if (allocated_registers_.ContainsCoreRegister(reg)) {
1158 __ pushl(reg);
1159 __ cfi().AdjustCFAOffset(kX86WordSize);
1160 __ cfi().RelOffset(DWARFReg(reg), 0);
1161 }
1162 }
Mark Mendell5f874182015-03-04 15:42:45 -05001163
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001164 int adjust = GetFrameSize() - FrameEntrySpillSize();
1165 __ subl(ESP, Immediate(adjust));
1166 __ cfi().AdjustCFAOffset(adjust);
1167 // Save the current method if we need it. Note that we do not
1168 // do this in HCurrentMethod, as the instruction might have been removed
1169 // in the SSA graph.
1170 if (RequiresCurrentMethod()) {
1171 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1172 }
1173
1174 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1175 // Initialize should_deoptimize flag to 0.
1176 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001177 }
1178 }
1179
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001180 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001181}
1182
1183void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001184 __ cfi().RememberState();
1185 if (!HasEmptyFrame()) {
1186 int adjust = GetFrameSize() - FrameEntrySpillSize();
1187 __ addl(ESP, Immediate(adjust));
1188 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001189
David Srbeckyc34dc932015-04-12 09:27:43 +01001190 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1191 Register reg = kCoreCalleeSaves[i];
1192 if (allocated_registers_.ContainsCoreRegister(reg)) {
1193 __ popl(reg);
1194 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1195 __ cfi().Restore(DWARFReg(reg));
1196 }
Mark Mendell5f874182015-03-04 15:42:45 -05001197 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001198 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001199 __ ret();
1200 __ cfi().RestoreState();
1201 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001202}
1203
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001204void CodeGeneratorX86::Bind(HBasicBlock* block) {
1205 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001206}
1207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001209 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001210 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001211 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001212 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 case DataType::Type::kInt8:
1214 case DataType::Type::kUint16:
1215 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001216 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001218 return Location::RegisterLocation(EAX);
1219
Aart Bik66c158e2018-01-31 12:55:04 -08001220 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001222 return Location::RegisterPairLocation(EAX, EDX);
1223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001225 return Location::NoLocation();
1226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 case DataType::Type::kFloat64:
1228 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001229 return Location::FpuRegisterLocation(XMM0);
1230 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001231
1232 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001233}
1234
1235Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1236 return Location::RegisterLocation(kMethodRegisterArgument);
1237}
1238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001240 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001241 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001243 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001244 case DataType::Type::kInt8:
1245 case DataType::Type::kUint16:
1246 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001247 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001248 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001249 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001250 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001251 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001252 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001253 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001254 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001255 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001257 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001258 uint32_t index = gp_index_;
1259 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001260 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001262 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1263 calling_convention.GetRegisterPairAt(index));
1264 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001265 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001266 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1267 }
1268 }
1269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001271 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001272 stack_index_++;
1273 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1274 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1275 } else {
1276 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1277 }
1278 }
1279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001280 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001281 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001282 stack_index_ += 2;
1283 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1284 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1285 } else {
1286 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001287 }
1288 }
1289
Aart Bik66c158e2018-01-31 12:55:04 -08001290 case DataType::Type::kUint32:
1291 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001292 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001293 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001294 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001295 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001296 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001297}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001298
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001299void CodeGeneratorX86::Move32(Location destination, Location source) {
1300 if (source.Equals(destination)) {
1301 return;
1302 }
1303 if (destination.IsRegister()) {
1304 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 } else {
1309 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001312 } else if (destination.IsFpuRegister()) {
1313 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001315 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317 } else {
1318 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001320 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001322 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001323 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001324 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001325 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001327 } else if (source.IsConstant()) {
1328 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001329 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001330 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001331 } else {
1332 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001333 __ pushl(Address(ESP, source.GetStackIndex()));
1334 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001335 }
1336 }
1337}
1338
1339void CodeGeneratorX86::Move64(Location destination, Location source) {
1340 if (source.Equals(destination)) {
1341 return;
1342 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001343 if (destination.IsRegisterPair()) {
1344 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001345 EmitParallelMoves(
1346 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1347 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001349 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001350 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001351 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001352 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001353 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1354 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1355 __ psrlq(src_reg, Immediate(32));
1356 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001357 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001358 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001359 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001360 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1361 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1363 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001364 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001365 if (source.IsFpuRegister()) {
1366 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1367 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001368 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001369 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001370 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001371 // Create stack space for 2 elements.
1372 __ subl(ESP, Immediate(2 * elem_size));
1373 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1374 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1375 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1376 // And remove the temporary stack space we allocated.
1377 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 } else {
1379 LOG(FATAL) << "Unimplemented";
1380 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001381 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001382 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001383 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001384 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001385 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001386 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001387 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001388 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001389 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001390 } else if (source.IsConstant()) {
1391 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001392 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1393 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001394 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001395 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1396 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001398 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001399 EmitParallelMoves(
1400 Location::StackSlot(source.GetStackIndex()),
1401 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001402 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001403 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001404 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001405 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001406 }
1407 }
1408}
1409
Calin Juravle175dc732015-08-25 15:42:32 +01001410void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1411 DCHECK(location.IsRegister());
1412 __ movl(location.AsRegister<Register>(), Immediate(value));
1413}
1414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001415void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001416 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001417 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1418 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1419 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001420 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001421 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001422 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001423 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001424}
1425
1426void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1427 if (location.IsRegister()) {
1428 locations->AddTemp(location);
1429 } else if (location.IsRegisterPair()) {
1430 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1431 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1432 } else {
1433 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1434 }
1435}
1436
David Brazdilfc6a86a2015-06-26 10:33:45 +00001437void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001438 if (successor->IsExitBlock()) {
1439 DCHECK(got->GetPrevious()->AlwaysThrows());
1440 return; // no code needed
1441 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001442
1443 HBasicBlock* block = got->GetBlock();
1444 HInstruction* previous = got->GetPrevious();
1445
1446 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001447 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001448 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001449 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1450 return;
1451 }
1452
1453 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1454 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1455 }
1456 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001458 }
1459}
1460
David Brazdilfc6a86a2015-06-26 10:33:45 +00001461void LocationsBuilderX86::VisitGoto(HGoto* got) {
1462 got->SetLocations(nullptr);
1463}
1464
1465void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1466 HandleGoto(got, got->GetSuccessor());
1467}
1468
1469void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1470 try_boundary->SetLocations(nullptr);
1471}
1472
1473void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1474 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1475 if (!successor->IsExitBlock()) {
1476 HandleGoto(try_boundary, successor);
1477 }
1478}
1479
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001480void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001481 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001482}
1483
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001484void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001485}
1486
Mark Mendell152408f2015-12-31 12:28:50 -05001487template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001488void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001489 LabelType* true_label,
1490 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001491 if (cond->IsFPConditionTrueIfNaN()) {
1492 __ j(kUnordered, true_label);
1493 } else if (cond->IsFPConditionFalseIfNaN()) {
1494 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001495 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001496 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001497}
1498
Mark Mendell152408f2015-12-31 12:28:50 -05001499template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001500void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001501 LabelType* true_label,
1502 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001503 LocationSummary* locations = cond->GetLocations();
1504 Location left = locations->InAt(0);
1505 Location right = locations->InAt(1);
1506 IfCondition if_cond = cond->GetCondition();
1507
Mark Mendellc4701932015-04-10 13:18:51 -04001508 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001509 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001510 IfCondition true_high_cond = if_cond;
1511 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001512 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001513
1514 // Set the conditions for the test, remembering that == needs to be
1515 // decided using the low words.
1516 switch (if_cond) {
1517 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001518 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001519 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001520 break;
1521 case kCondLT:
1522 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001523 break;
1524 case kCondLE:
1525 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001526 break;
1527 case kCondGT:
1528 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001529 break;
1530 case kCondGE:
1531 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001532 break;
Aart Bike9f37602015-10-09 11:15:55 -07001533 case kCondB:
1534 false_high_cond = kCondA;
1535 break;
1536 case kCondBE:
1537 true_high_cond = kCondB;
1538 break;
1539 case kCondA:
1540 false_high_cond = kCondB;
1541 break;
1542 case kCondAE:
1543 true_high_cond = kCondA;
1544 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001545 }
1546
1547 if (right.IsConstant()) {
1548 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001549 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001550 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001551
Aart Bika19616e2016-02-01 18:57:58 -08001552 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001553 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001554 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001555 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001556 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001557 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001558 __ j(X86Condition(true_high_cond), true_label);
1559 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001560 }
1561 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001562 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001563 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001564 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001565 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001566
1567 __ cmpl(left_high, right_high);
1568 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001569 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001570 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001571 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001572 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001573 __ j(X86Condition(true_high_cond), true_label);
1574 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001575 }
1576 // Must be equal high, so compare the lows.
1577 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001578 } else {
1579 DCHECK(right.IsDoubleStackSlot());
1580 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1581 if (if_cond == kCondNE) {
1582 __ j(X86Condition(true_high_cond), true_label);
1583 } else if (if_cond == kCondEQ) {
1584 __ j(X86Condition(false_high_cond), false_label);
1585 } else {
1586 __ j(X86Condition(true_high_cond), true_label);
1587 __ j(X86Condition(false_high_cond), false_label);
1588 }
1589 // Must be equal high, so compare the lows.
1590 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001591 }
1592 // The last comparison might be unsigned.
1593 __ j(final_condition, true_label);
1594}
1595
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001596void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1597 Location rhs,
1598 HInstruction* insn,
1599 bool is_double) {
1600 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1601 if (is_double) {
1602 if (rhs.IsFpuRegister()) {
1603 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1604 } else if (const_area != nullptr) {
1605 DCHECK(const_area->IsEmittedAtUseSite());
1606 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1607 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001608 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1609 const_area->GetBaseMethodAddress(),
1610 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001611 } else {
1612 DCHECK(rhs.IsDoubleStackSlot());
1613 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1614 }
1615 } else {
1616 if (rhs.IsFpuRegister()) {
1617 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1618 } else if (const_area != nullptr) {
1619 DCHECK(const_area->IsEmittedAtUseSite());
1620 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1621 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001622 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1623 const_area->GetBaseMethodAddress(),
1624 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001625 } else {
1626 DCHECK(rhs.IsStackSlot());
1627 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1628 }
1629 }
1630}
1631
Mark Mendell152408f2015-12-31 12:28:50 -05001632template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001633void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001634 LabelType* true_target_in,
1635 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001636 // Generated branching requires both targets to be explicit. If either of the
1637 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001638 LabelType fallthrough_target;
1639 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1640 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001641
Mark Mendellc4701932015-04-10 13:18:51 -04001642 LocationSummary* locations = condition->GetLocations();
1643 Location left = locations->InAt(0);
1644 Location right = locations->InAt(1);
1645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001646 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001647 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001648 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001649 GenerateLongComparesAndJumps(condition, true_target, false_target);
1650 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001651 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001652 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001653 GenerateFPJumps(condition, true_target, false_target);
1654 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001656 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001657 GenerateFPJumps(condition, true_target, false_target);
1658 break;
1659 default:
1660 LOG(FATAL) << "Unexpected compare type " << type;
1661 }
1662
David Brazdil0debae72015-11-12 18:37:00 +00001663 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001664 __ jmp(false_target);
1665 }
David Brazdil0debae72015-11-12 18:37:00 +00001666
1667 if (fallthrough_target.IsLinked()) {
1668 __ Bind(&fallthrough_target);
1669 }
Mark Mendellc4701932015-04-10 13:18:51 -04001670}
1671
David Brazdil0debae72015-11-12 18:37:00 +00001672static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1673 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1674 // are set only strictly before `branch`. We can't use the eflags on long/FP
1675 // conditions if they are materialized due to the complex branching.
1676 return cond->IsCondition() &&
1677 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1679 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001680}
1681
Mark Mendell152408f2015-12-31 12:28:50 -05001682template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001683void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001684 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001685 LabelType* true_target,
1686 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001687 HInstruction* cond = instruction->InputAt(condition_input_index);
1688
1689 if (true_target == nullptr && false_target == nullptr) {
1690 // Nothing to do. The code always falls through.
1691 return;
1692 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001693 // Constant condition, statically compared against "true" (integer value 1).
1694 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (true_target != nullptr) {
1696 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001697 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001698 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001699 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001700 if (false_target != nullptr) {
1701 __ jmp(false_target);
1702 }
1703 }
1704 return;
1705 }
1706
1707 // The following code generates these patterns:
1708 // (1) true_target == nullptr && false_target != nullptr
1709 // - opposite condition true => branch to false_target
1710 // (2) true_target != nullptr && false_target == nullptr
1711 // - condition true => branch to true_target
1712 // (3) true_target != nullptr && false_target != nullptr
1713 // - condition true => branch to true_target
1714 // - branch to false_target
1715 if (IsBooleanValueOrMaterializedCondition(cond)) {
1716 if (AreEflagsSetFrom(cond, instruction)) {
1717 if (true_target == nullptr) {
1718 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1719 } else {
1720 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1721 }
1722 } else {
1723 // Materialized condition, compare against 0.
1724 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1725 if (lhs.IsRegister()) {
1726 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1727 } else {
1728 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1729 }
1730 if (true_target == nullptr) {
1731 __ j(kEqual, false_target);
1732 } else {
1733 __ j(kNotEqual, true_target);
1734 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001735 }
1736 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001737 // Condition has not been materialized, use its inputs as the comparison and
1738 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001739 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001740
1741 // If this is a long or FP comparison that has been folded into
1742 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001743 DataType::Type type = condition->InputAt(0)->GetType();
1744 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001745 GenerateCompareTestAndBranch(condition, true_target, false_target);
1746 return;
1747 }
1748
1749 Location lhs = condition->GetLocations()->InAt(0);
1750 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001752 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001753 if (true_target == nullptr) {
1754 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1755 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001756 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001757 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001758 }
David Brazdil0debae72015-11-12 18:37:00 +00001759
1760 // If neither branch falls through (case 3), the conditional branch to `true_target`
1761 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1762 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001763 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001764 }
1765}
1766
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001767void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001768 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001769 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001770 locations->SetInAt(0, Location::Any());
1771 }
1772}
1773
1774void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001775 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1776 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1777 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1778 nullptr : codegen_->GetLabelOf(true_successor);
1779 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1780 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001781 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001782}
1783
1784void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001785 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001786 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001787 InvokeRuntimeCallingConvention calling_convention;
1788 RegisterSet caller_saves = RegisterSet::Empty();
1789 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1790 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001791 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001792 locations->SetInAt(0, Location::Any());
1793 }
1794}
1795
1796void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001797 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001798 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001799 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001800 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001801 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001802}
1803
Mingyao Yang063fc772016-08-02 11:02:54 -07001804void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001805 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001806 LocationSummary(flag, LocationSummary::kNoCall);
1807 locations->SetOut(Location::RequiresRegister());
1808}
1809
1810void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1811 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1812 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1813}
1814
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001815static bool SelectCanUseCMOV(HSelect* select) {
1816 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001818 return false;
1819 }
1820
1821 // A FP condition doesn't generate the single CC that we need.
1822 // In 32 bit mode, a long condition doesn't generate a single CC either.
1823 HInstruction* condition = select->GetCondition();
1824 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 DataType::Type compare_type = condition->InputAt(0)->GetType();
1826 if (compare_type == DataType::Type::kInt64 ||
1827 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001828 return false;
1829 }
1830 }
1831
1832 // We can generate a CMOV for this Select.
1833 return true;
1834}
1835
David Brazdil74eb1b22015-12-14 11:44:01 +00001836void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001839 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001840 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001841 } else {
1842 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001843 if (SelectCanUseCMOV(select)) {
1844 if (select->InputAt(1)->IsConstant()) {
1845 // Cmov can't handle a constant value.
1846 locations->SetInAt(1, Location::RequiresRegister());
1847 } else {
1848 locations->SetInAt(1, Location::Any());
1849 }
1850 } else {
1851 locations->SetInAt(1, Location::Any());
1852 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001853 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001854 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1855 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001856 }
1857 locations->SetOut(Location::SameAsFirstInput());
1858}
1859
1860void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1861 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001862 DCHECK(locations->InAt(0).Equals(locations->Out()));
1863 if (SelectCanUseCMOV(select)) {
1864 // If both the condition and the source types are integer, we can generate
1865 // a CMOV to implement Select.
1866
1867 HInstruction* select_condition = select->GetCondition();
1868 Condition cond = kNotEqual;
1869
1870 // Figure out how to test the 'condition'.
1871 if (select_condition->IsCondition()) {
1872 HCondition* condition = select_condition->AsCondition();
1873 if (!condition->IsEmittedAtUseSite()) {
1874 // This was a previously materialized condition.
1875 // Can we use the existing condition code?
1876 if (AreEflagsSetFrom(condition, select)) {
1877 // Materialization was the previous instruction. Condition codes are right.
1878 cond = X86Condition(condition->GetCondition());
1879 } else {
1880 // No, we have to recreate the condition code.
1881 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1882 __ testl(cond_reg, cond_reg);
1883 }
1884 } else {
1885 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1887 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001888 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001889 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001890 cond = X86Condition(condition->GetCondition());
1891 }
1892 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001893 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001894 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1895 __ testl(cond_reg, cond_reg);
1896 }
1897
1898 // If the condition is true, overwrite the output, which already contains false.
1899 Location false_loc = locations->InAt(0);
1900 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001902 // 64 bit conditional move.
1903 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1904 Register false_low = false_loc.AsRegisterPairLow<Register>();
1905 if (true_loc.IsRegisterPair()) {
1906 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1907 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1908 } else {
1909 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1910 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1911 }
1912 } else {
1913 // 32 bit conditional move.
1914 Register false_reg = false_loc.AsRegister<Register>();
1915 if (true_loc.IsRegister()) {
1916 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1917 } else {
1918 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1919 }
1920 }
1921 } else {
1922 NearLabel false_target;
1923 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001924 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001925 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1926 __ Bind(&false_target);
1927 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001928}
1929
David Srbecky0cf44932015-12-09 14:09:59 +00001930void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001931 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001932}
1933
David Srbeckyd28f4a02016-03-14 17:14:24 +00001934void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1935 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001936}
1937
1938void CodeGeneratorX86::GenerateNop() {
1939 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001940}
1941
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001943 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001944 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001945 // Handle the long/FP comparisons made in instruction simplification.
1946 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001948 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001949 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001950 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001951 locations->SetOut(Location::RequiresRegister());
1952 }
1953 break;
1954 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001955 case DataType::Type::kFloat32:
1956 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001957 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001958 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1959 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1960 } else if (cond->InputAt(1)->IsConstant()) {
1961 locations->SetInAt(1, Location::RequiresFpuRegister());
1962 } else {
1963 locations->SetInAt(1, Location::Any());
1964 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001965 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001966 locations->SetOut(Location::RequiresRegister());
1967 }
1968 break;
1969 }
1970 default:
1971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001973 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001974 // We need a byte register.
1975 locations->SetOut(Location::RegisterLocation(ECX));
1976 }
1977 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001978 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979}
1980
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001982 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001983 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001984 }
Mark Mendellc4701932015-04-10 13:18:51 -04001985
1986 LocationSummary* locations = cond->GetLocations();
1987 Location lhs = locations->InAt(0);
1988 Location rhs = locations->InAt(1);
1989 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001990 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001991
1992 switch (cond->InputAt(0)->GetType()) {
1993 default: {
1994 // Integer case.
1995
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001996 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001997 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001998 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001999 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002000 return;
2001 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002003 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2004 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002006 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002007 GenerateFPJumps(cond, &true_label, &false_label);
2008 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002010 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002011 GenerateFPJumps(cond, &true_label, &false_label);
2012 break;
2013 }
2014
2015 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002016 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002017
Roland Levillain4fa13f62015-07-06 18:11:54 +01002018 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002019 __ Bind(&false_label);
2020 __ xorl(reg, reg);
2021 __ jmp(&done_label);
2022
Roland Levillain4fa13f62015-07-06 18:11:54 +01002023 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002024 __ Bind(&true_label);
2025 __ movl(reg, Immediate(1));
2026 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002027}
2028
2029void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002030 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002031}
2032
2033void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002035}
2036
2037void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002039}
2040
2041void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002042 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002043}
2044
2045void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002047}
2048
2049void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002051}
2052
2053void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002054 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002055}
2056
2057void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002058 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002059}
2060
2061void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002062 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002063}
2064
2065void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002066 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002067}
2068
2069void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002070 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002071}
2072
2073void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002074 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002075}
2076
Aart Bike9f37602015-10-09 11:15:55 -07002077void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002078 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002079}
2080
2081void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002082 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002083}
2084
2085void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002086 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002087}
2088
2089void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002090 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002091}
2092
2093void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002094 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002095}
2096
2097void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002098 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002099}
2100
2101void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002102 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002103}
2104
2105void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002107}
2108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002110 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002111 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002112 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113}
2114
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002115void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002116 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117}
2118
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002119void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002121 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002122 locations->SetOut(Location::ConstantLocation(constant));
2123}
2124
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002125void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002126 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002127}
2128
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002130 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002131 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002132 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133}
2134
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002135void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002136 // Will be generated at use site.
2137}
2138
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002139void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2140 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002141 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002142 locations->SetOut(Location::ConstantLocation(constant));
2143}
2144
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002145void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002146 // Will be generated at use site.
2147}
2148
2149void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2150 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002151 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002152 locations->SetOut(Location::ConstantLocation(constant));
2153}
2154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002155void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002156 // Will be generated at use site.
2157}
2158
Igor Murashkind01745e2017-04-05 16:40:31 -07002159void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2160 constructor_fence->SetLocations(nullptr);
2161}
2162
2163void InstructionCodeGeneratorX86::VisitConstructorFence(
2164 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2165 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2166}
2167
Calin Juravle27df7582015-04-17 19:12:31 +01002168void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2169 memory_barrier->SetLocations(nullptr);
2170}
2171
2172void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002173 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002174}
2175
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002176void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002177 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178}
2179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002180void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002181 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002182}
2183
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002184void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002186 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002187 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002188 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002190 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002191 case DataType::Type::kInt8:
2192 case DataType::Type::kUint16:
2193 case DataType::Type::kInt16:
2194 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002195 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002196 break;
2197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002199 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002200 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201 break;
2202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 case DataType::Type::kFloat32:
2204 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002205 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002207 break;
2208
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002210 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002211 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002212}
2213
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002214void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002215 if (kIsDebugBuild) {
2216 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002217 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002219 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 case DataType::Type::kInt8:
2221 case DataType::Type::kUint16:
2222 case DataType::Type::kInt16:
2223 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002224 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002225 break;
2226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002228 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2229 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002230 break;
2231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 case DataType::Type::kFloat32:
2233 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 break;
2236
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002237 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002239 }
2240 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002242}
2243
Calin Juravle175dc732015-08-25 15:42:32 +01002244void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2245 // The trampoline uses the same calling convention as dex calling conventions,
2246 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2247 // the method_idx.
2248 HandleInvoke(invoke);
2249}
2250
2251void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2252 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2253}
2254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002255void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002256 // Explicit clinit checks triggered by static invokes must have been pruned by
2257 // art::PrepareForRegisterAllocation.
2258 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002259
Mark Mendellfb8d2792015-03-31 22:16:59 -04002260 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002261 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002262 if (invoke->GetLocations()->CanCall() &&
2263 invoke->HasPcRelativeMethodLoadKind() &&
2264 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002265 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002266 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002267 return;
2268 }
2269
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002270 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002271
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002272 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002273 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002274 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002275 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276}
2277
Mark Mendell09ed1a32015-03-25 08:30:06 -04002278static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2279 if (invoke->GetLocations()->Intrinsified()) {
2280 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2281 intrinsic.Dispatch(invoke);
2282 return true;
2283 }
2284 return false;
2285}
2286
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002287void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002288 // Explicit clinit checks triggered by static invokes must have been pruned by
2289 // art::PrepareForRegisterAllocation.
2290 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002291
Mark Mendell09ed1a32015-03-25 08:30:06 -04002292 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2293 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002294 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002295
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002296 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002297 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002298 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002299}
2300
2301void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002302 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2303 if (intrinsic.TryDispatch(invoke)) {
2304 return;
2305 }
2306
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002307 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002308
2309 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002310 // Add one temporary for inline cache update.
2311 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2312 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002313}
2314
2315void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002316 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002317 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002318}
2319
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002320void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002321 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2322 return;
2323 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002324
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002325 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002326 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002327}
2328
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002329void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002330 // This call to HandleInvoke allocates a temporary (core) register
2331 // which is also used to transfer the hidden argument from FP to
2332 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002333 HandleInvoke(invoke);
2334 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002335 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002336
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002337 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002338 // Add one temporary for inline cache update.
2339 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2340 }
2341}
2342
2343void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2344 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002345 // We know the destination of an intrinsic, so no need to record inline
2346 // caches (also the intrinsic location builder doesn't request an additional
2347 // temporary).
2348 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002349 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002350 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002351 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2352 ScopedObjectAccess soa(Thread::Current());
2353 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
2354 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2355 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2356 if (kIsDebugBuild) {
2357 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2358 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2359 }
2360 Register temp = EBP;
2361 NearLabel done;
2362 __ movl(temp, Immediate(address));
2363 // Fast path for a monomorphic cache.
2364 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2365 __ j(kEqual, &done);
2366 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2367 __ Bind(&done);
2368 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002369}
2370
2371void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2372 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002373 LocationSummary* locations = invoke->GetLocations();
2374 Register temp = locations->GetTemp(0).AsRegister<Register>();
2375 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002376 Location receiver = locations->InAt(0);
2377 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2378
Roland Levillain0d5a2812015-11-13 10:07:31 +00002379 // Set the hidden argument. This is safe to do this here, as XMM7
2380 // won't be modified thereafter, before the `call` instruction.
2381 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002382 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002383 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002384
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385 if (receiver.IsStackSlot()) {
2386 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002387 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002388 __ movl(temp, Address(temp, class_offset));
2389 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002390 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002391 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002392 }
Roland Levillain4d027112015-07-01 15:41:14 +01002393 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002394 // Instead of simply (possibly) unpoisoning `temp` here, we should
2395 // emit a read barrier for the previous class reference load.
2396 // However this is not required in practice, as this is an
2397 // intermediate/temporary reference and because the current
2398 // concurrent copying collector keeps the from-space memory
2399 // intact/accessible until the end of the marking phase (the
2400 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002401 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002402
2403 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2404
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002405 // temp = temp->GetAddressOfIMT()
2406 __ movl(temp,
2407 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002408 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002409 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002410 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002411 __ movl(temp, Address(temp, method_offset));
2412 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002413 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002414 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002415
2416 DCHECK(!codegen_->IsLeafMethod());
2417 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2418}
2419
Orion Hodsonac141392017-01-13 11:53:47 +00002420void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2421 HandleInvoke(invoke);
2422}
2423
2424void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2425 codegen_->GenerateInvokePolymorphicCall(invoke);
2426}
2427
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002428void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2429 HandleInvoke(invoke);
2430}
2431
2432void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2433 codegen_->GenerateInvokeCustomCall(invoke);
2434}
2435
Roland Levillain88cb1752014-10-20 16:36:47 +01002436void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002438 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002439 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 case DataType::Type::kInt32:
2441 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002442 locations->SetInAt(0, Location::RequiresRegister());
2443 locations->SetOut(Location::SameAsFirstInput());
2444 break;
2445
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002447 locations->SetInAt(0, Location::RequiresFpuRegister());
2448 locations->SetOut(Location::SameAsFirstInput());
2449 locations->AddTemp(Location::RequiresRegister());
2450 locations->AddTemp(Location::RequiresFpuRegister());
2451 break;
2452
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002453 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002454 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002455 locations->SetOut(Location::SameAsFirstInput());
2456 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002457 break;
2458
2459 default:
2460 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2461 }
2462}
2463
2464void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2465 LocationSummary* locations = neg->GetLocations();
2466 Location out = locations->Out();
2467 Location in = locations->InAt(0);
2468 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002470 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002471 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002473 break;
2474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002476 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002477 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002478 __ negl(out.AsRegisterPairLow<Register>());
2479 // Negation is similar to subtraction from zero. The least
2480 // significant byte triggers a borrow when it is different from
2481 // zero; to take it into account, add 1 to the most significant
2482 // byte if the carry flag (CF) is set to 1 after the first NEGL
2483 // operation.
2484 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2485 __ negl(out.AsRegisterPairHigh<Register>());
2486 break;
2487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002489 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 Register constant = locations->GetTemp(0).AsRegister<Register>();
2491 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002492 // Implement float negation with an exclusive or with value
2493 // 0x80000000 (mask for bit 31, representing the sign of a
2494 // single-precision floating-point number).
2495 __ movl(constant, Immediate(INT32_C(0x80000000)));
2496 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002497 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002498 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002499 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002502 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002504 // Implement double negation with an exclusive or with value
2505 // 0x8000000000000000 (mask for bit 63, representing the sign of
2506 // a double-precision floating-point number).
2507 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002509 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002510 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002511
2512 default:
2513 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2514 }
2515}
2516
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002517void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2518 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002519 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002521 locations->SetInAt(0, Location::RequiresFpuRegister());
2522 locations->SetInAt(1, Location::RequiresRegister());
2523 locations->SetOut(Location::SameAsFirstInput());
2524 locations->AddTemp(Location::RequiresFpuRegister());
2525}
2526
2527void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2528 LocationSummary* locations = neg->GetLocations();
2529 Location out = locations->Out();
2530 DCHECK(locations->InAt(0).Equals(out));
2531
2532 Register constant_area = locations->InAt(1).AsRegister<Register>();
2533 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002535 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2536 neg->GetBaseMethodAddress(),
2537 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002538 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2539 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002540 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2541 neg->GetBaseMethodAddress(),
2542 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002543 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2544 }
2545}
2546
Roland Levillaindff1f282014-11-05 14:15:05 +00002547void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 DataType::Type result_type = conversion->GetResultType();
2549 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002550 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2551 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002552
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002553 // The float-to-long and double-to-long type conversions rely on a
2554 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002555 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2557 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002558 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002559 : LocationSummary::kNoCall;
2560 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002561 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002562
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002564 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002566 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002567 case DataType::Type::kUint8:
2568 case DataType::Type::kInt8:
2569 case DataType::Type::kUint16:
2570 case DataType::Type::kInt16:
2571 case DataType::Type::kInt32:
2572 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2573 // Make the output overlap to please the register allocator. This greatly simplifies
2574 // the validation of the linear scan implementation
2575 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2576 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002578 HInstruction* input = conversion->InputAt(0);
2579 Location input_location = input->IsConstant()
2580 ? Location::ConstantLocation(input->AsConstant())
2581 : Location::RegisterPairLocation(EAX, EDX);
2582 locations->SetInAt(0, input_location);
2583 // Make the output overlap to please the register allocator. This greatly simplifies
2584 // the validation of the linear scan implementation
2585 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2586 break;
2587 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002588
2589 default:
2590 LOG(FATAL) << "Unexpected type conversion from " << input_type
2591 << " to " << result_type;
2592 }
2593 break;
2594
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002595 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002597 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2598 locations->SetInAt(0, Location::Any());
2599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002600 break;
2601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002603 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002604 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002605 locations->SetInAt(0, Location::Any());
2606 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2607 break;
2608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002610 locations->SetInAt(0, Location::RequiresFpuRegister());
2611 locations->SetOut(Location::RequiresRegister());
2612 locations->AddTemp(Location::RequiresFpuRegister());
2613 break;
2614
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002616 locations->SetInAt(0, Location::RequiresFpuRegister());
2617 locations->SetOut(Location::RequiresRegister());
2618 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 }
2625 break;
2626
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002628 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002630 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002632 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002633 case DataType::Type::kInt16:
2634 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002635 locations->SetInAt(0, Location::RegisterLocation(EAX));
2636 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2637 break;
2638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kFloat32:
2640 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002641 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002642 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2643 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2644
Vladimir Marko949c91f2015-01-27 10:48:44 +00002645 // The runtime helper puts the result in EAX, EDX.
2646 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002647 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002648 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002649
2650 default:
2651 LOG(FATAL) << "Unexpected type conversion from " << input_type
2652 << " to " << result_type;
2653 }
2654 break;
2655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002657 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002659 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002661 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 case DataType::Type::kInt16:
2663 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002664 locations->SetInAt(0, Location::RequiresRegister());
2665 locations->SetOut(Location::RequiresFpuRegister());
2666 break;
2667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002669 locations->SetInAt(0, Location::Any());
2670 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002671 break;
2672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002674 locations->SetInAt(0, Location::RequiresFpuRegister());
2675 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002681 }
Roland Levillaincff13742014-11-17 14:32:17 +00002682 break;
2683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002685 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002687 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002689 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 case DataType::Type::kInt16:
2691 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002692 locations->SetInAt(0, Location::RequiresRegister());
2693 locations->SetOut(Location::RequiresFpuRegister());
2694 break;
2695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002697 locations->SetInAt(0, Location::Any());
2698 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002699 break;
2700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002702 locations->SetInAt(0, Location::RequiresFpuRegister());
2703 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002704 break;
2705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002710 break;
2711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716}
2717
2718void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2719 LocationSummary* locations = conversion->GetLocations();
2720 Location out = locations->Out();
2721 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 DataType::Type result_type = conversion->GetResultType();
2723 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002724 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2725 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002727 case DataType::Type::kUint8:
2728 switch (input_type) {
2729 case DataType::Type::kInt8:
2730 case DataType::Type::kUint16:
2731 case DataType::Type::kInt16:
2732 case DataType::Type::kInt32:
2733 if (in.IsRegister()) {
2734 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2735 } else {
2736 DCHECK(in.GetConstant()->IsIntConstant());
2737 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2738 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2739 }
2740 break;
2741 case DataType::Type::kInt64:
2742 if (in.IsRegisterPair()) {
2743 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2744 } else {
2745 DCHECK(in.GetConstant()->IsLongConstant());
2746 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2747 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2748 }
2749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected type conversion from " << input_type
2753 << " to " << result_type;
2754 }
2755 break;
2756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002757 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002758 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002759 case DataType::Type::kUint8:
2760 case DataType::Type::kUint16:
2761 case DataType::Type::kInt16:
2762 case DataType::Type::kInt32:
2763 if (in.IsRegister()) {
2764 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2765 } else {
2766 DCHECK(in.GetConstant()->IsIntConstant());
2767 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2768 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2769 }
2770 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002772 if (in.IsRegisterPair()) {
2773 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2774 } else {
2775 DCHECK(in.GetConstant()->IsLongConstant());
2776 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2777 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2778 }
2779 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002780
2781 default:
2782 LOG(FATAL) << "Unexpected type conversion from " << input_type
2783 << " to " << result_type;
2784 }
2785 break;
2786
2787 case DataType::Type::kUint16:
2788 switch (input_type) {
2789 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 case DataType::Type::kInt16:
2791 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002792 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002793 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2794 } else if (in.IsStackSlot()) {
2795 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002796 } else {
2797 DCHECK(in.GetConstant()->IsIntConstant());
2798 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002799 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2800 }
2801 break;
2802 case DataType::Type::kInt64:
2803 if (in.IsRegisterPair()) {
2804 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2805 } else if (in.IsDoubleStackSlot()) {
2806 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2807 } else {
2808 DCHECK(in.GetConstant()->IsLongConstant());
2809 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2810 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002811 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002812 break;
2813
2814 default:
2815 LOG(FATAL) << "Unexpected type conversion from " << input_type
2816 << " to " << result_type;
2817 }
2818 break;
2819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002821 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002823 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002824 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002826 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002827 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002828 } else {
2829 DCHECK(in.GetConstant()->IsIntConstant());
2830 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002832 }
2833 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002834 case DataType::Type::kInt64:
2835 if (in.IsRegisterPair()) {
2836 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2837 } else if (in.IsDoubleStackSlot()) {
2838 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2839 } else {
2840 DCHECK(in.GetConstant()->IsLongConstant());
2841 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2842 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2843 }
2844 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002845
2846 default:
2847 LOG(FATAL) << "Unexpected type conversion from " << input_type
2848 << " to " << result_type;
2849 }
2850 break;
2851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002852 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002853 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002854 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002855 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002857 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002859 } else {
2860 DCHECK(in.IsConstant());
2861 DCHECK(in.GetConstant()->IsLongConstant());
2862 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002863 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002864 }
2865 break;
2866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002868 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2869 Register output = out.AsRegister<Register>();
2870 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002871 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002872
2873 __ movl(output, Immediate(kPrimIntMax));
2874 // temp = int-to-float(output)
2875 __ cvtsi2ss(temp, output);
2876 // if input >= temp goto done
2877 __ comiss(input, temp);
2878 __ j(kAboveEqual, &done);
2879 // if input == NaN goto nan
2880 __ j(kUnordered, &nan);
2881 // output = float-to-int-truncate(input)
2882 __ cvttss2si(output, input);
2883 __ jmp(&done);
2884 __ Bind(&nan);
2885 // output = 0
2886 __ xorl(output, output);
2887 __ Bind(&done);
2888 break;
2889 }
2890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002892 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2893 Register output = out.AsRegister<Register>();
2894 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002895 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002896
2897 __ movl(output, Immediate(kPrimIntMax));
2898 // temp = int-to-double(output)
2899 __ cvtsi2sd(temp, output);
2900 // if input >= temp goto done
2901 __ comisd(input, temp);
2902 __ j(kAboveEqual, &done);
2903 // if input == NaN goto nan
2904 __ j(kUnordered, &nan);
2905 // output = double-to-int-truncate(input)
2906 __ cvttsd2si(output, input);
2907 __ jmp(&done);
2908 __ Bind(&nan);
2909 // output = 0
2910 __ xorl(output, output);
2911 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002912 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002913 }
Roland Levillain946e1432014-11-11 17:35:19 +00002914
2915 default:
2916 LOG(FATAL) << "Unexpected type conversion from " << input_type
2917 << " to " << result_type;
2918 }
2919 break;
2920
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002921 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002922 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002923 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002924 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002926 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 case DataType::Type::kInt16:
2928 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002929 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2930 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002931 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002932 __ cdq();
2933 break;
2934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002936 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002937 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002938 break;
2939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002941 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002942 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002943 break;
2944
2945 default:
2946 LOG(FATAL) << "Unexpected type conversion from " << input_type
2947 << " to " << result_type;
2948 }
2949 break;
2950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002952 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002954 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002956 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kInt16:
2958 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002959 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002960 break;
2961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002963 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002964
Roland Levillain232ade02015-04-20 15:14:36 +01002965 // Create stack space for the call to
2966 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2967 // TODO: enhance register allocator to ask for stack temporaries.
2968 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002970 __ subl(ESP, Immediate(adjustment));
2971 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002972
Roland Levillain232ade02015-04-20 15:14:36 +01002973 // Load the value to the FP stack, using temporaries if needed.
2974 PushOntoFPStack(in, 0, adjustment, false, true);
2975
2976 if (out.IsStackSlot()) {
2977 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2978 } else {
2979 __ fstps(Address(ESP, 0));
2980 Location stack_temp = Location::StackSlot(0);
2981 codegen_->Move32(out, stack_temp);
2982 }
2983
2984 // Remove the temporary stack space we allocated.
2985 if (adjustment != 0) {
2986 __ addl(ESP, Immediate(adjustment));
2987 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002988 break;
2989 }
2990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002992 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002993 break;
2994
2995 default:
2996 LOG(FATAL) << "Unexpected type conversion from " << input_type
2997 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002998 }
Roland Levillaincff13742014-11-17 14:32:17 +00002999 break;
3000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003002 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003004 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003005 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003006 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 case DataType::Type::kInt16:
3008 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003009 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003010 break;
3011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003013 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003014
Roland Levillain232ade02015-04-20 15:14:36 +01003015 // Create stack space for the call to
3016 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3017 // TODO: enhance register allocator to ask for stack temporaries.
3018 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01003020 __ subl(ESP, Immediate(adjustment));
3021 }
3022
3023 // Load the value to the FP stack, using temporaries if needed.
3024 PushOntoFPStack(in, 0, adjustment, false, true);
3025
3026 if (out.IsDoubleStackSlot()) {
3027 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3028 } else {
3029 __ fstpl(Address(ESP, 0));
3030 Location stack_temp = Location::DoubleStackSlot(0);
3031 codegen_->Move64(out, stack_temp);
3032 }
3033
3034 // Remove the temporary stack space we allocated.
3035 if (adjustment != 0) {
3036 __ addl(ESP, Immediate(adjustment));
3037 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003038 break;
3039 }
3040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003042 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003043 break;
3044
3045 default:
3046 LOG(FATAL) << "Unexpected type conversion from " << input_type
3047 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003048 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003049 break;
3050
3051 default:
3052 LOG(FATAL) << "Unexpected type conversion from " << input_type
3053 << " to " << result_type;
3054 }
3055}
3056
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003057void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003058 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003059 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003060 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003062 locations->SetInAt(0, Location::RequiresRegister());
3063 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3064 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3065 break;
3066 }
3067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003069 locations->SetInAt(0, Location::RequiresRegister());
3070 locations->SetInAt(1, Location::Any());
3071 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003072 break;
3073 }
3074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kFloat32:
3076 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003077 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003078 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3079 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003080 } else if (add->InputAt(1)->IsConstant()) {
3081 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003082 } else {
3083 locations->SetInAt(1, Location::Any());
3084 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003085 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003086 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003087 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003088
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003089 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003090 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003091 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003092 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003093}
3094
3095void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3096 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003097 Location first = locations->InAt(0);
3098 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003099 Location out = locations->Out();
3100
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003101 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003102 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003103 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003104 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3105 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003106 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3107 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003108 } else {
3109 __ leal(out.AsRegister<Register>(), Address(
3110 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3111 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003112 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003113 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3114 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3115 __ addl(out.AsRegister<Register>(), Immediate(value));
3116 } else {
3117 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3118 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003119 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003120 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003122 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003123 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003124 }
3125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003127 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003128 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3129 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003130 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003131 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3132 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003133 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003134 } else {
3135 DCHECK(second.IsConstant()) << second;
3136 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3137 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3138 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003139 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003140 break;
3141 }
3142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003143 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003144 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003145 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3147 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003148 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003149 __ addss(first.AsFpuRegister<XmmRegister>(),
3150 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003151 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3152 const_area->GetBaseMethodAddress(),
3153 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003154 } else {
3155 DCHECK(second.IsStackSlot());
3156 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003157 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003158 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003159 }
3160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003162 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003163 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003164 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3165 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003166 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003167 __ addsd(first.AsFpuRegister<XmmRegister>(),
3168 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003169 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3170 const_area->GetBaseMethodAddress(),
3171 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003172 } else {
3173 DCHECK(second.IsDoubleStackSlot());
3174 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003175 }
3176 break;
3177 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003178
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003179 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003180 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003181 }
3182}
3183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003184void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003186 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003187 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 case DataType::Type::kInt32:
3189 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003190 locations->SetInAt(0, Location::RequiresRegister());
3191 locations->SetInAt(1, Location::Any());
3192 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003193 break;
3194 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kFloat32:
3196 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003197 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003198 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3199 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003200 } else if (sub->InputAt(1)->IsConstant()) {
3201 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003202 } else {
3203 locations->SetInAt(1, Location::Any());
3204 }
Calin Juravle11351682014-10-23 15:38:15 +01003205 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003206 break;
Calin Juravle11351682014-10-23 15:38:15 +01003207 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003208
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003209 default:
Calin Juravle11351682014-10-23 15:38:15 +01003210 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003211 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003212}
3213
3214void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3215 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003216 Location first = locations->InAt(0);
3217 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003218 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003219 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003221 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003223 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003224 __ subl(first.AsRegister<Register>(),
3225 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003226 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003227 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003228 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003229 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003230 }
3231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003232 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003233 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003234 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3235 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003236 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003237 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003238 __ sbbl(first.AsRegisterPairHigh<Register>(),
3239 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003240 } else {
3241 DCHECK(second.IsConstant()) << second;
3242 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3243 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3244 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003245 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003246 break;
3247 }
3248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003250 if (second.IsFpuRegister()) {
3251 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3252 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3253 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003254 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003255 __ subss(first.AsFpuRegister<XmmRegister>(),
3256 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003257 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3258 const_area->GetBaseMethodAddress(),
3259 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003260 } else {
3261 DCHECK(second.IsStackSlot());
3262 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3263 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003264 break;
Calin Juravle11351682014-10-23 15:38:15 +01003265 }
3266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003267 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003268 if (second.IsFpuRegister()) {
3269 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3270 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3271 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003272 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003273 __ subsd(first.AsFpuRegister<XmmRegister>(),
3274 codegen_->LiteralDoubleAddress(
3275 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003276 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003277 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3278 } else {
3279 DCHECK(second.IsDoubleStackSlot());
3280 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3281 }
Calin Juravle11351682014-10-23 15:38:15 +01003282 break;
3283 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003284
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003285 default:
Calin Juravle11351682014-10-23 15:38:15 +01003286 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003287 }
3288}
3289
Calin Juravle34bacdf2014-10-07 20:23:36 +01003290void LocationsBuilderX86::VisitMul(HMul* mul) {
3291 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003292 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003293 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003295 locations->SetInAt(0, Location::RequiresRegister());
3296 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003297 if (mul->InputAt(1)->IsIntConstant()) {
3298 // Can use 3 operand multiply.
3299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3300 } else {
3301 locations->SetOut(Location::SameAsFirstInput());
3302 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003305 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003306 locations->SetInAt(1, Location::Any());
3307 locations->SetOut(Location::SameAsFirstInput());
3308 // Needed for imul on 32bits with 64bits output.
3309 locations->AddTemp(Location::RegisterLocation(EAX));
3310 locations->AddTemp(Location::RegisterLocation(EDX));
3311 break;
3312 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 case DataType::Type::kFloat32:
3314 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003315 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003316 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3317 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003318 } else if (mul->InputAt(1)->IsConstant()) {
3319 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003320 } else {
3321 locations->SetInAt(1, Location::Any());
3322 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003323 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003325 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003326
3327 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003328 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329 }
3330}
3331
3332void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3333 LocationSummary* locations = mul->GetLocations();
3334 Location first = locations->InAt(0);
3335 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003336 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003337
3338 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003339 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003340 // The constant may have ended up in a register, so test explicitly to avoid
3341 // problems where the output may not be the same as the first operand.
3342 if (mul->InputAt(1)->IsIntConstant()) {
3343 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3344 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3345 } else if (second.IsRegister()) {
3346 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003348 } else {
3349 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003350 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352 }
3353 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003355 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003356 Register in1_hi = first.AsRegisterPairHigh<Register>();
3357 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003358 Register eax = locations->GetTemp(0).AsRegister<Register>();
3359 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003360
3361 DCHECK_EQ(EAX, eax);
3362 DCHECK_EQ(EDX, edx);
3363
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003364 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003365 // output: in1
3366 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3367 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3368 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003369 if (second.IsConstant()) {
3370 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003371
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003372 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3373 int32_t low_value = Low32Bits(value);
3374 int32_t high_value = High32Bits(value);
3375 Immediate low(low_value);
3376 Immediate high(high_value);
3377
3378 __ movl(eax, high);
3379 // eax <- in1.lo * in2.hi
3380 __ imull(eax, in1_lo);
3381 // in1.hi <- in1.hi * in2.lo
3382 __ imull(in1_hi, low);
3383 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3384 __ addl(in1_hi, eax);
3385 // move in2_lo to eax to prepare for double precision
3386 __ movl(eax, low);
3387 // edx:eax <- in1.lo * in2.lo
3388 __ mull(in1_lo);
3389 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3390 __ addl(in1_hi, edx);
3391 // in1.lo <- (in1.lo * in2.lo)[31:0];
3392 __ movl(in1_lo, eax);
3393 } else if (second.IsRegisterPair()) {
3394 Register in2_hi = second.AsRegisterPairHigh<Register>();
3395 Register in2_lo = second.AsRegisterPairLow<Register>();
3396
3397 __ movl(eax, in2_hi);
3398 // eax <- in1.lo * in2.hi
3399 __ imull(eax, in1_lo);
3400 // in1.hi <- in1.hi * in2.lo
3401 __ imull(in1_hi, in2_lo);
3402 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3403 __ addl(in1_hi, eax);
3404 // move in1_lo to eax to prepare for double precision
3405 __ movl(eax, in1_lo);
3406 // edx:eax <- in1.lo * in2.lo
3407 __ mull(in2_lo);
3408 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3409 __ addl(in1_hi, edx);
3410 // in1.lo <- (in1.lo * in2.lo)[31:0];
3411 __ movl(in1_lo, eax);
3412 } else {
3413 DCHECK(second.IsDoubleStackSlot()) << second;
3414 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3415 Address in2_lo(ESP, second.GetStackIndex());
3416
3417 __ movl(eax, in2_hi);
3418 // eax <- in1.lo * in2.hi
3419 __ imull(eax, in1_lo);
3420 // in1.hi <- in1.hi * in2.lo
3421 __ imull(in1_hi, in2_lo);
3422 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3423 __ addl(in1_hi, eax);
3424 // move in1_lo to eax to prepare for double precision
3425 __ movl(eax, in1_lo);
3426 // edx:eax <- in1.lo * in2.lo
3427 __ mull(in2_lo);
3428 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3429 __ addl(in1_hi, edx);
3430 // in1.lo <- (in1.lo * in2.lo)[31:0];
3431 __ movl(in1_lo, eax);
3432 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003433
3434 break;
3435 }
3436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003437 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003438 DCHECK(first.Equals(locations->Out()));
3439 if (second.IsFpuRegister()) {
3440 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3441 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3442 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003443 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003444 __ mulss(first.AsFpuRegister<XmmRegister>(),
3445 codegen_->LiteralFloatAddress(
3446 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003447 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003448 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3449 } else {
3450 DCHECK(second.IsStackSlot());
3451 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3452 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003453 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003454 }
3455
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003456 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003457 DCHECK(first.Equals(locations->Out()));
3458 if (second.IsFpuRegister()) {
3459 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3460 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3461 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003462 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003463 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3464 codegen_->LiteralDoubleAddress(
3465 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003466 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003467 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3468 } else {
3469 DCHECK(second.IsDoubleStackSlot());
3470 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3471 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003472 break;
3473 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003474
3475 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003476 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003477 }
3478}
3479
Roland Levillain232ade02015-04-20 15:14:36 +01003480void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3481 uint32_t temp_offset,
3482 uint32_t stack_adjustment,
3483 bool is_fp,
3484 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003485 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003486 DCHECK(!is_wide);
3487 if (is_fp) {
3488 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3489 } else {
3490 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3491 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003492 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003493 DCHECK(is_wide);
3494 if (is_fp) {
3495 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3496 } else {
3497 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3498 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003499 } else {
3500 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003501 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003502 Location stack_temp = Location::StackSlot(temp_offset);
3503 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003504 if (is_fp) {
3505 __ flds(Address(ESP, temp_offset));
3506 } else {
3507 __ filds(Address(ESP, temp_offset));
3508 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003509 } else {
3510 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3511 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003512 if (is_fp) {
3513 __ fldl(Address(ESP, temp_offset));
3514 } else {
3515 __ fildl(Address(ESP, temp_offset));
3516 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003517 }
3518 }
3519}
3520
3521void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003522 DataType::Type type = rem->GetResultType();
3523 bool is_float = type == DataType::Type::kFloat32;
3524 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003525 LocationSummary* locations = rem->GetLocations();
3526 Location first = locations->InAt(0);
3527 Location second = locations->InAt(1);
3528 Location out = locations->Out();
3529
3530 // Create stack space for 2 elements.
3531 // TODO: enhance register allocator to ask for stack temporaries.
3532 __ subl(ESP, Immediate(2 * elem_size));
3533
3534 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003535 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003536 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3537 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003538
3539 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003540 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003541 __ Bind(&retry);
3542 __ fprem();
3543
3544 // Move FP status to AX.
3545 __ fstsw();
3546
3547 // And see if the argument reduction is complete. This is signaled by the
3548 // C2 FPU flag bit set to 0.
3549 __ andl(EAX, Immediate(kC2ConditionMask));
3550 __ j(kNotEqual, &retry);
3551
3552 // We have settled on the final value. Retrieve it into an XMM register.
3553 // Store FP top of stack to real stack.
3554 if (is_float) {
3555 __ fsts(Address(ESP, 0));
3556 } else {
3557 __ fstl(Address(ESP, 0));
3558 }
3559
3560 // Pop the 2 items from the FP stack.
3561 __ fucompp();
3562
3563 // Load the value from the stack into an XMM register.
3564 DCHECK(out.IsFpuRegister()) << out;
3565 if (is_float) {
3566 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3567 } else {
3568 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3569 }
3570
3571 // And remove the temporary stack space we allocated.
3572 __ addl(ESP, Immediate(2 * elem_size));
3573}
3574
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003575
3576void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3577 DCHECK(instruction->IsDiv() || instruction->IsRem());
3578
3579 LocationSummary* locations = instruction->GetLocations();
3580 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003581 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003582
3583 Register out_register = locations->Out().AsRegister<Register>();
3584 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003585 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586
3587 DCHECK(imm == 1 || imm == -1);
3588
3589 if (instruction->IsRem()) {
3590 __ xorl(out_register, out_register);
3591 } else {
3592 __ movl(out_register, input_register);
3593 if (imm == -1) {
3594 __ negl(out_register);
3595 }
3596 }
3597}
3598
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303599void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3600 LocationSummary* locations = instruction->GetLocations();
3601 Location second = locations->InAt(1);
3602
3603 Register out = locations->Out().AsRegister<Register>();
3604 Register numerator = locations->InAt(0).AsRegister<Register>();
3605
3606 int32_t imm = Int64FromConstant(second.GetConstant());
3607 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3608 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3609
3610 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3611 NearLabel done;
3612 __ movl(out, numerator);
3613 __ andl(out, Immediate(abs_imm-1));
3614 __ j(Condition::kZero, &done);
3615 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3616 __ testl(numerator, numerator);
3617 __ cmovl(Condition::kLess, out, tmp);
3618 __ Bind(&done);
3619}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003621void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 LocationSummary* locations = instruction->GetLocations();
3623
3624 Register out_register = locations->Out().AsRegister<Register>();
3625 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003626 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003627 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3628 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003630 Register num = locations->GetTemp(0).AsRegister<Register>();
3631
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003632 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003633 __ testl(input_register, input_register);
3634 __ cmovl(kGreaterEqual, num, input_register);
3635 int shift = CTZ(imm);
3636 __ sarl(num, Immediate(shift));
3637
3638 if (imm < 0) {
3639 __ negl(num);
3640 }
3641
3642 __ movl(out_register, num);
3643}
3644
3645void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3646 DCHECK(instruction->IsDiv() || instruction->IsRem());
3647
3648 LocationSummary* locations = instruction->GetLocations();
3649 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3650
3651 Register eax = locations->InAt(0).AsRegister<Register>();
3652 Register out = locations->Out().AsRegister<Register>();
3653 Register num;
3654 Register edx;
3655
3656 if (instruction->IsDiv()) {
3657 edx = locations->GetTemp(0).AsRegister<Register>();
3658 num = locations->GetTemp(1).AsRegister<Register>();
3659 } else {
3660 edx = locations->Out().AsRegister<Register>();
3661 num = locations->GetTemp(0).AsRegister<Register>();
3662 }
3663
3664 DCHECK_EQ(EAX, eax);
3665 DCHECK_EQ(EDX, edx);
3666 if (instruction->IsDiv()) {
3667 DCHECK_EQ(EAX, out);
3668 } else {
3669 DCHECK_EQ(EDX, out);
3670 }
3671
3672 int64_t magic;
3673 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003674 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003675
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003676 // Save the numerator.
3677 __ movl(num, eax);
3678
3679 // EAX = magic
3680 __ movl(eax, Immediate(magic));
3681
3682 // EDX:EAX = magic * numerator
3683 __ imull(num);
3684
3685 if (imm > 0 && magic < 0) {
3686 // EDX += num
3687 __ addl(edx, num);
3688 } else if (imm < 0 && magic > 0) {
3689 __ subl(edx, num);
3690 }
3691
3692 // Shift if needed.
3693 if (shift != 0) {
3694 __ sarl(edx, Immediate(shift));
3695 }
3696
3697 // EDX += 1 if EDX < 0
3698 __ movl(eax, edx);
3699 __ shrl(edx, Immediate(31));
3700 __ addl(edx, eax);
3701
3702 if (instruction->IsRem()) {
3703 __ movl(eax, num);
3704 __ imull(edx, Immediate(imm));
3705 __ subl(eax, edx);
3706 __ movl(edx, eax);
3707 } else {
3708 __ movl(eax, edx);
3709 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003710}
3711
Calin Juravlebacfec32014-11-14 15:54:36 +00003712void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3713 DCHECK(instruction->IsDiv() || instruction->IsRem());
3714
3715 LocationSummary* locations = instruction->GetLocations();
3716 Location out = locations->Out();
3717 Location first = locations->InAt(0);
3718 Location second = locations->InAt(1);
3719 bool is_div = instruction->IsDiv();
3720
3721 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003723 DCHECK_EQ(EAX, first.AsRegister<Register>());
3724 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003725
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003726 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003727 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003728
3729 if (imm == 0) {
3730 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3731 } else if (imm == 1 || imm == -1) {
3732 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303733 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3734 if (is_div) {
3735 DivByPowerOfTwo(instruction->AsDiv());
3736 } else {
3737 RemByPowerOfTwo(instruction->AsRem());
3738 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003739 } else {
3740 DCHECK(imm <= -2 || imm >= 2);
3741 GenerateDivRemWithAnyConstant(instruction);
3742 }
3743 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003744 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003745 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003746 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003747
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003748 Register second_reg = second.AsRegister<Register>();
3749 // 0x80000000/-1 triggers an arithmetic exception!
3750 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3751 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003752
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003753 __ cmpl(second_reg, Immediate(-1));
3754 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003755
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003756 // edx:eax <- sign-extended of eax
3757 __ cdq();
3758 // eax = quotient, edx = remainder
3759 __ idivl(second_reg);
3760 __ Bind(slow_path->GetExitLabel());
3761 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 break;
3763 }
3764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003766 InvokeRuntimeCallingConvention calling_convention;
3767 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3768 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3769 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3770 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3771 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3772 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3773
3774 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003775 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003776 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003777 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003778 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003779 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 break;
3782 }
3783
3784 default:
3785 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3786 }
3787}
3788
Calin Juravle7c4954d2014-10-28 16:57:40 +00003789void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003791 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003792 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003793 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003794
Calin Juravle7c4954d2014-10-28 16:57:40 +00003795 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003797 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003798 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003799 locations->SetOut(Location::SameAsFirstInput());
3800 // Intel uses edx:eax as the dividend.
3801 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003802 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3803 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3804 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003805 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003806 locations->AddTemp(Location::RequiresRegister());
3807 }
Calin Juravled0d48522014-11-04 16:40:20 +00003808 break;
3809 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003811 InvokeRuntimeCallingConvention calling_convention;
3812 locations->SetInAt(0, Location::RegisterPairLocation(
3813 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3814 locations->SetInAt(1, Location::RegisterPairLocation(
3815 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3816 // Runtime helper puts the result in EAX, EDX.
3817 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003818 break;
3819 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003820 case DataType::Type::kFloat32:
3821 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003822 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003823 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3824 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003825 } else if (div->InputAt(1)->IsConstant()) {
3826 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003827 } else {
3828 locations->SetInAt(1, Location::Any());
3829 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003830 locations->SetOut(Location::SameAsFirstInput());
3831 break;
3832 }
3833
3834 default:
3835 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3836 }
3837}
3838
3839void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3840 LocationSummary* locations = div->GetLocations();
3841 Location first = locations->InAt(0);
3842 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003843
3844 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003845 case DataType::Type::kInt32:
3846 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003847 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003848 break;
3849 }
3850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003852 if (second.IsFpuRegister()) {
3853 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3854 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3855 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003856 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003857 __ divss(first.AsFpuRegister<XmmRegister>(),
3858 codegen_->LiteralFloatAddress(
3859 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003860 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003861 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3862 } else {
3863 DCHECK(second.IsStackSlot());
3864 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3865 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003866 break;
3867 }
3868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003870 if (second.IsFpuRegister()) {
3871 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3872 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3873 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003874 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003875 __ divsd(first.AsFpuRegister<XmmRegister>(),
3876 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003877 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3878 const_area->GetBaseMethodAddress(),
3879 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003880 } else {
3881 DCHECK(second.IsDoubleStackSlot());
3882 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3883 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003884 break;
3885 }
3886
3887 default:
3888 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3889 }
3890}
3891
Calin Juravlebacfec32014-11-14 15:54:36 +00003892void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003893 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003896 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003897 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003898 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003899
Calin Juravled2ec87d2014-12-08 14:24:46 +00003900 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003902 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003903 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003904 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003905 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3906 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3907 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003908 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003909 locations->AddTemp(Location::RequiresRegister());
3910 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003911 break;
3912 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003913 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003914 InvokeRuntimeCallingConvention calling_convention;
3915 locations->SetInAt(0, Location::RegisterPairLocation(
3916 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3917 locations->SetInAt(1, Location::RegisterPairLocation(
3918 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3919 // Runtime helper puts the result in EAX, EDX.
3920 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3921 break;
3922 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003923 case DataType::Type::kFloat64:
3924 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003925 locations->SetInAt(0, Location::Any());
3926 locations->SetInAt(1, Location::Any());
3927 locations->SetOut(Location::RequiresFpuRegister());
3928 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003929 break;
3930 }
3931
3932 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003933 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003934 }
3935}
3936
3937void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003938 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003939 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003940 case DataType::Type::kInt32:
3941 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003942 GenerateDivRemIntegral(rem);
3943 break;
3944 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 case DataType::Type::kFloat32:
3946 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003947 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003948 break;
3949 }
3950 default:
3951 LOG(FATAL) << "Unexpected rem type " << type;
3952 }
3953}
3954
Aart Bik1f8d51b2018-02-15 10:42:37 -08003955static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3956 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3957 switch (minmax->GetResultType()) {
3958 case DataType::Type::kInt32:
3959 locations->SetInAt(0, Location::RequiresRegister());
3960 locations->SetInAt(1, Location::RequiresRegister());
3961 locations->SetOut(Location::SameAsFirstInput());
3962 break;
3963 case DataType::Type::kInt64:
3964 locations->SetInAt(0, Location::RequiresRegister());
3965 locations->SetInAt(1, Location::RequiresRegister());
3966 locations->SetOut(Location::SameAsFirstInput());
3967 // Register to use to perform a long subtract to set cc.
3968 locations->AddTemp(Location::RequiresRegister());
3969 break;
3970 case DataType::Type::kFloat32:
3971 locations->SetInAt(0, Location::RequiresFpuRegister());
3972 locations->SetInAt(1, Location::RequiresFpuRegister());
3973 locations->SetOut(Location::SameAsFirstInput());
3974 locations->AddTemp(Location::RequiresRegister());
3975 break;
3976 case DataType::Type::kFloat64:
3977 locations->SetInAt(0, Location::RequiresFpuRegister());
3978 locations->SetInAt(1, Location::RequiresFpuRegister());
3979 locations->SetOut(Location::SameAsFirstInput());
3980 break;
3981 default:
3982 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3983 }
3984}
3985
Aart Bik351df3e2018-03-07 11:54:57 -08003986void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3987 bool is_min,
3988 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003989 Location op1_loc = locations->InAt(0);
3990 Location op2_loc = locations->InAt(1);
3991
3992 // Shortcut for same input locations.
3993 if (op1_loc.Equals(op2_loc)) {
3994 // Can return immediately, as op1_loc == out_loc.
3995 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3996 // a copy here.
3997 DCHECK(locations->Out().Equals(op1_loc));
3998 return;
3999 }
4000
4001 if (type == DataType::Type::kInt64) {
4002 // Need to perform a subtract to get the sign right.
4003 // op1 is already in the same location as the output.
4004 Location output = locations->Out();
4005 Register output_lo = output.AsRegisterPairLow<Register>();
4006 Register output_hi = output.AsRegisterPairHigh<Register>();
4007
4008 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4009 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4010
4011 // The comparison is performed by subtracting the second operand from
4012 // the first operand and then setting the status flags in the same
4013 // manner as the SUB instruction."
4014 __ cmpl(output_lo, op2_lo);
4015
4016 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4017 Register temp = locations->GetTemp(0).AsRegister<Register>();
4018 __ movl(temp, output_hi);
4019 __ sbbl(temp, op2_hi);
4020
4021 // Now the condition code is correct.
4022 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4023 __ cmovl(cond, output_lo, op2_lo);
4024 __ cmovl(cond, output_hi, op2_hi);
4025 } else {
4026 DCHECK_EQ(type, DataType::Type::kInt32);
4027 Register out = locations->Out().AsRegister<Register>();
4028 Register op2 = op2_loc.AsRegister<Register>();
4029
4030 // (out := op1)
4031 // out <=? op2
4032 // if out is min jmp done
4033 // out := op2
4034 // done:
4035
4036 __ cmpl(out, op2);
4037 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4038 __ cmovl(cond, out, op2);
4039 }
4040}
4041
4042void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4043 bool is_min,
4044 DataType::Type type) {
4045 Location op1_loc = locations->InAt(0);
4046 Location op2_loc = locations->InAt(1);
4047 Location out_loc = locations->Out();
4048 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4049
4050 // Shortcut for same input locations.
4051 if (op1_loc.Equals(op2_loc)) {
4052 DCHECK(out_loc.Equals(op1_loc));
4053 return;
4054 }
4055
4056 // (out := op1)
4057 // out <=? op2
4058 // if Nan jmp Nan_label
4059 // if out is min jmp done
4060 // if op2 is min jmp op2_label
4061 // handle -0/+0
4062 // jmp done
4063 // Nan_label:
4064 // out := NaN
4065 // op2_label:
4066 // out := op2
4067 // done:
4068 //
4069 // This removes one jmp, but needs to copy one input (op1) to out.
4070 //
4071 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4072
4073 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4074
4075 NearLabel nan, done, op2_label;
4076 if (type == DataType::Type::kFloat64) {
4077 __ ucomisd(out, op2);
4078 } else {
4079 DCHECK_EQ(type, DataType::Type::kFloat32);
4080 __ ucomiss(out, op2);
4081 }
4082
4083 __ j(Condition::kParityEven, &nan);
4084
4085 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4086 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4087
4088 // Handle 0.0/-0.0.
4089 if (is_min) {
4090 if (type == DataType::Type::kFloat64) {
4091 __ orpd(out, op2);
4092 } else {
4093 __ orps(out, op2);
4094 }
4095 } else {
4096 if (type == DataType::Type::kFloat64) {
4097 __ andpd(out, op2);
4098 } else {
4099 __ andps(out, op2);
4100 }
4101 }
4102 __ jmp(&done);
4103
4104 // NaN handling.
4105 __ Bind(&nan);
4106 if (type == DataType::Type::kFloat64) {
4107 // TODO: Use a constant from the constant table (requires extra input).
4108 __ LoadLongConstant(out, kDoubleNaN);
4109 } else {
4110 Register constant = locations->GetTemp(0).AsRegister<Register>();
4111 __ movl(constant, Immediate(kFloatNaN));
4112 __ movd(out, constant);
4113 }
4114 __ jmp(&done);
4115
4116 // out := op2;
4117 __ Bind(&op2_label);
4118 if (type == DataType::Type::kFloat64) {
4119 __ movsd(out, op2);
4120 } else {
4121 __ movss(out, op2);
4122 }
4123
4124 // Done.
4125 __ Bind(&done);
4126}
4127
Aart Bik351df3e2018-03-07 11:54:57 -08004128void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4129 DataType::Type type = minmax->GetResultType();
4130 switch (type) {
4131 case DataType::Type::kInt32:
4132 case DataType::Type::kInt64:
4133 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4134 break;
4135 case DataType::Type::kFloat32:
4136 case DataType::Type::kFloat64:
4137 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4138 break;
4139 default:
4140 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4141 }
4142}
4143
Aart Bik1f8d51b2018-02-15 10:42:37 -08004144void LocationsBuilderX86::VisitMin(HMin* min) {
4145 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4146}
4147
4148void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004149 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004150}
4151
4152void LocationsBuilderX86::VisitMax(HMax* max) {
4153 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4154}
4155
4156void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004157 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004158}
4159
Aart Bik3dad3412018-02-28 12:01:46 -08004160void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4161 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4162 switch (abs->GetResultType()) {
4163 case DataType::Type::kInt32:
4164 locations->SetInAt(0, Location::RegisterLocation(EAX));
4165 locations->SetOut(Location::SameAsFirstInput());
4166 locations->AddTemp(Location::RegisterLocation(EDX));
4167 break;
4168 case DataType::Type::kInt64:
4169 locations->SetInAt(0, Location::RequiresRegister());
4170 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4171 locations->AddTemp(Location::RequiresRegister());
4172 break;
4173 case DataType::Type::kFloat32:
4174 locations->SetInAt(0, Location::RequiresFpuRegister());
4175 locations->SetOut(Location::SameAsFirstInput());
4176 locations->AddTemp(Location::RequiresFpuRegister());
4177 locations->AddTemp(Location::RequiresRegister());
4178 break;
4179 case DataType::Type::kFloat64:
4180 locations->SetInAt(0, Location::RequiresFpuRegister());
4181 locations->SetOut(Location::SameAsFirstInput());
4182 locations->AddTemp(Location::RequiresFpuRegister());
4183 break;
4184 default:
4185 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4186 }
4187}
4188
4189void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4190 LocationSummary* locations = abs->GetLocations();
4191 switch (abs->GetResultType()) {
4192 case DataType::Type::kInt32: {
4193 Register out = locations->Out().AsRegister<Register>();
4194 DCHECK_EQ(out, EAX);
4195 Register temp = locations->GetTemp(0).AsRegister<Register>();
4196 DCHECK_EQ(temp, EDX);
4197 // Sign extend EAX into EDX.
4198 __ cdq();
4199 // XOR EAX with sign.
4200 __ xorl(EAX, EDX);
4201 // Subtract out sign to correct.
4202 __ subl(EAX, EDX);
4203 // The result is in EAX.
4204 break;
4205 }
4206 case DataType::Type::kInt64: {
4207 Location input = locations->InAt(0);
4208 Register input_lo = input.AsRegisterPairLow<Register>();
4209 Register input_hi = input.AsRegisterPairHigh<Register>();
4210 Location output = locations->Out();
4211 Register output_lo = output.AsRegisterPairLow<Register>();
4212 Register output_hi = output.AsRegisterPairHigh<Register>();
4213 Register temp = locations->GetTemp(0).AsRegister<Register>();
4214 // Compute the sign into the temporary.
4215 __ movl(temp, input_hi);
4216 __ sarl(temp, Immediate(31));
4217 // Store the sign into the output.
4218 __ movl(output_lo, temp);
4219 __ movl(output_hi, temp);
4220 // XOR the input to the output.
4221 __ xorl(output_lo, input_lo);
4222 __ xorl(output_hi, input_hi);
4223 // Subtract the sign.
4224 __ subl(output_lo, temp);
4225 __ sbbl(output_hi, temp);
4226 break;
4227 }
4228 case DataType::Type::kFloat32: {
4229 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4230 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4231 Register constant = locations->GetTemp(1).AsRegister<Register>();
4232 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4233 __ movd(temp, constant);
4234 __ andps(out, temp);
4235 break;
4236 }
4237 case DataType::Type::kFloat64: {
4238 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4239 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4240 // TODO: Use a constant from the constant table (requires extra input).
4241 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4242 __ andpd(out, temp);
4243 break;
4244 }
4245 default:
4246 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4247 }
4248}
4249
Calin Juravled0d48522014-11-04 16:40:20 +00004250void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004251 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004252 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004253 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004254 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004255 case DataType::Type::kInt8:
4256 case DataType::Type::kUint16:
4257 case DataType::Type::kInt16:
4258 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004259 locations->SetInAt(0, Location::Any());
4260 break;
4261 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004262 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004263 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4264 if (!instruction->IsConstant()) {
4265 locations->AddTemp(Location::RequiresRegister());
4266 }
4267 break;
4268 }
4269 default:
4270 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4271 }
Calin Juravled0d48522014-11-04 16:40:20 +00004272}
4273
4274void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004275 SlowPathCode* slow_path =
4276 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004277 codegen_->AddSlowPath(slow_path);
4278
4279 LocationSummary* locations = instruction->GetLocations();
4280 Location value = locations->InAt(0);
4281
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004282 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004283 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004284 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kInt8:
4286 case DataType::Type::kUint16:
4287 case DataType::Type::kInt16:
4288 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004289 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004290 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004291 __ j(kEqual, slow_path->GetEntryLabel());
4292 } else if (value.IsStackSlot()) {
4293 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4294 __ j(kEqual, slow_path->GetEntryLabel());
4295 } else {
4296 DCHECK(value.IsConstant()) << value;
4297 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004298 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004299 }
4300 }
4301 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004302 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004304 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004305 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004306 __ movl(temp, value.AsRegisterPairLow<Register>());
4307 __ orl(temp, value.AsRegisterPairHigh<Register>());
4308 __ j(kEqual, slow_path->GetEntryLabel());
4309 } else {
4310 DCHECK(value.IsConstant()) << value;
4311 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4312 __ jmp(slow_path->GetEntryLabel());
4313 }
4314 }
4315 break;
4316 }
4317 default:
4318 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004319 }
Calin Juravled0d48522014-11-04 16:40:20 +00004320}
4321
Calin Juravle9aec02f2014-11-18 23:06:35 +00004322void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4323 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4324
4325 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004326 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004327
4328 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004329 case DataType::Type::kInt32:
4330 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004331 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004332 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004333 // The shift count needs to be in CL or a constant.
4334 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004335 locations->SetOut(Location::SameAsFirstInput());
4336 break;
4337 }
4338 default:
4339 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4340 }
4341}
4342
4343void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4344 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4345
4346 LocationSummary* locations = op->GetLocations();
4347 Location first = locations->InAt(0);
4348 Location second = locations->InAt(1);
4349 DCHECK(first.Equals(locations->Out()));
4350
4351 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004352 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004353 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004354 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004355 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004357 DCHECK_EQ(ECX, second_reg);
4358 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004359 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004360 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004361 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004362 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004363 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004364 }
4365 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004366 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004367 if (shift == 0) {
4368 return;
4369 }
4370 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004371 if (op->IsShl()) {
4372 __ shll(first_reg, imm);
4373 } else if (op->IsShr()) {
4374 __ sarl(first_reg, imm);
4375 } else {
4376 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004377 }
4378 }
4379 break;
4380 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004381 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004382 if (second.IsRegister()) {
4383 Register second_reg = second.AsRegister<Register>();
4384 DCHECK_EQ(ECX, second_reg);
4385 if (op->IsShl()) {
4386 GenerateShlLong(first, second_reg);
4387 } else if (op->IsShr()) {
4388 GenerateShrLong(first, second_reg);
4389 } else {
4390 GenerateUShrLong(first, second_reg);
4391 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004392 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004393 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004394 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004395 // Nothing to do if the shift is 0, as the input is already the output.
4396 if (shift != 0) {
4397 if (op->IsShl()) {
4398 GenerateShlLong(first, shift);
4399 } else if (op->IsShr()) {
4400 GenerateShrLong(first, shift);
4401 } else {
4402 GenerateUShrLong(first, shift);
4403 }
4404 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004405 }
4406 break;
4407 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004408 default:
4409 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4410 }
4411}
4412
Mark P Mendell73945692015-04-29 14:56:17 +00004413void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4414 Register low = loc.AsRegisterPairLow<Register>();
4415 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004416 if (shift == 1) {
4417 // This is just an addition.
4418 __ addl(low, low);
4419 __ adcl(high, high);
4420 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004421 // Shift by 32 is easy. High gets low, and low gets 0.
4422 codegen_->EmitParallelMoves(
4423 loc.ToLow(),
4424 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004425 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004426 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4427 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004428 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004429 } else if (shift > 32) {
4430 // Low part becomes 0. High part is low part << (shift-32).
4431 __ movl(high, low);
4432 __ shll(high, Immediate(shift - 32));
4433 __ xorl(low, low);
4434 } else {
4435 // Between 1 and 31.
4436 __ shld(high, low, Immediate(shift));
4437 __ shll(low, Immediate(shift));
4438 }
4439}
4440
Calin Juravle9aec02f2014-11-18 23:06:35 +00004441void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004442 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004443 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4444 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4445 __ testl(shifter, Immediate(32));
4446 __ j(kEqual, &done);
4447 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4448 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4449 __ Bind(&done);
4450}
4451
Mark P Mendell73945692015-04-29 14:56:17 +00004452void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4453 Register low = loc.AsRegisterPairLow<Register>();
4454 Register high = loc.AsRegisterPairHigh<Register>();
4455 if (shift == 32) {
4456 // Need to copy the sign.
4457 DCHECK_NE(low, high);
4458 __ movl(low, high);
4459 __ sarl(high, Immediate(31));
4460 } else if (shift > 32) {
4461 DCHECK_NE(low, high);
4462 // High part becomes sign. Low part is shifted by shift - 32.
4463 __ movl(low, high);
4464 __ sarl(high, Immediate(31));
4465 __ sarl(low, Immediate(shift - 32));
4466 } else {
4467 // Between 1 and 31.
4468 __ shrd(low, high, Immediate(shift));
4469 __ sarl(high, Immediate(shift));
4470 }
4471}
4472
Calin Juravle9aec02f2014-11-18 23:06:35 +00004473void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004474 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004475 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4476 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4477 __ testl(shifter, Immediate(32));
4478 __ j(kEqual, &done);
4479 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4480 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4481 __ Bind(&done);
4482}
4483
Mark P Mendell73945692015-04-29 14:56:17 +00004484void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4485 Register low = loc.AsRegisterPairLow<Register>();
4486 Register high = loc.AsRegisterPairHigh<Register>();
4487 if (shift == 32) {
4488 // Shift by 32 is easy. Low gets high, and high gets 0.
4489 codegen_->EmitParallelMoves(
4490 loc.ToHigh(),
4491 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004492 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004493 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4494 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004495 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004496 } else if (shift > 32) {
4497 // Low part is high >> (shift - 32). High part becomes 0.
4498 __ movl(low, high);
4499 __ shrl(low, Immediate(shift - 32));
4500 __ xorl(high, high);
4501 } else {
4502 // Between 1 and 31.
4503 __ shrd(low, high, Immediate(shift));
4504 __ shrl(high, Immediate(shift));
4505 }
4506}
4507
Calin Juravle9aec02f2014-11-18 23:06:35 +00004508void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004509 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004510 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4511 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4512 __ testl(shifter, Immediate(32));
4513 __ j(kEqual, &done);
4514 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4515 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4516 __ Bind(&done);
4517}
4518
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004519void LocationsBuilderX86::VisitRor(HRor* ror) {
4520 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004521 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004522
4523 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004524 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004525 // Add the temporary needed.
4526 locations->AddTemp(Location::RequiresRegister());
4527 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004529 locations->SetInAt(0, Location::RequiresRegister());
4530 // The shift count needs to be in CL (unless it is a constant).
4531 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4532 locations->SetOut(Location::SameAsFirstInput());
4533 break;
4534 default:
4535 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4536 UNREACHABLE();
4537 }
4538}
4539
4540void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4541 LocationSummary* locations = ror->GetLocations();
4542 Location first = locations->InAt(0);
4543 Location second = locations->InAt(1);
4544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004545 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004546 Register first_reg = first.AsRegister<Register>();
4547 if (second.IsRegister()) {
4548 Register second_reg = second.AsRegister<Register>();
4549 __ rorl(first_reg, second_reg);
4550 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004551 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004552 __ rorl(first_reg, imm);
4553 }
4554 return;
4555 }
4556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004557 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004558 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4559 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4560 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4561 if (second.IsRegister()) {
4562 Register second_reg = second.AsRegister<Register>();
4563 DCHECK_EQ(second_reg, ECX);
4564 __ movl(temp_reg, first_reg_hi);
4565 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4566 __ shrd(first_reg_lo, temp_reg, second_reg);
4567 __ movl(temp_reg, first_reg_hi);
4568 __ testl(second_reg, Immediate(32));
4569 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4570 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4571 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004573 if (shift_amt == 0) {
4574 // Already fine.
4575 return;
4576 }
4577 if (shift_amt == 32) {
4578 // Just swap.
4579 __ movl(temp_reg, first_reg_lo);
4580 __ movl(first_reg_lo, first_reg_hi);
4581 __ movl(first_reg_hi, temp_reg);
4582 return;
4583 }
4584
4585 Immediate imm(shift_amt);
4586 // Save the constents of the low value.
4587 __ movl(temp_reg, first_reg_lo);
4588
4589 // Shift right into low, feeding bits from high.
4590 __ shrd(first_reg_lo, first_reg_hi, imm);
4591
4592 // Shift right into high, feeding bits from the original low.
4593 __ shrd(first_reg_hi, temp_reg, imm);
4594
4595 // Swap if needed.
4596 if (shift_amt > 32) {
4597 __ movl(temp_reg, first_reg_lo);
4598 __ movl(first_reg_lo, first_reg_hi);
4599 __ movl(first_reg_hi, temp_reg);
4600 }
4601 }
4602}
4603
Calin Juravle9aec02f2014-11-18 23:06:35 +00004604void LocationsBuilderX86::VisitShl(HShl* shl) {
4605 HandleShift(shl);
4606}
4607
4608void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4609 HandleShift(shl);
4610}
4611
4612void LocationsBuilderX86::VisitShr(HShr* shr) {
4613 HandleShift(shr);
4614}
4615
4616void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4617 HandleShift(shr);
4618}
4619
4620void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4621 HandleShift(ushr);
4622}
4623
4624void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4625 HandleShift(ushr);
4626}
4627
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004628void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004629 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4630 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004631 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004632 InvokeRuntimeCallingConvention calling_convention;
4633 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004634}
4635
4636void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004637 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4638 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4639 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004640}
4641
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004642void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004643 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4644 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004645 locations->SetOut(Location::RegisterLocation(EAX));
4646 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004647 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4648 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004649}
4650
4651void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004652 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4653 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004654 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004655 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004656 DCHECK(!codegen_->IsLeafMethod());
4657}
4658
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004659void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004660 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004661 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004662 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4663 if (location.IsStackSlot()) {
4664 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4665 } else if (location.IsDoubleStackSlot()) {
4666 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004667 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004668 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004669}
4670
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004671void InstructionCodeGeneratorX86::VisitParameterValue(
4672 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4673}
4674
4675void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4676 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004677 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004678 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4679}
4680
4681void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004682}
4683
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004684void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4685 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004686 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004687 locations->SetInAt(0, Location::RequiresRegister());
4688 locations->SetOut(Location::RequiresRegister());
4689}
4690
4691void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4692 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004693 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004694 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004695 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004696 __ movl(locations->Out().AsRegister<Register>(),
4697 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004698 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004699 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004700 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004701 __ movl(locations->Out().AsRegister<Register>(),
4702 Address(locations->InAt(0).AsRegister<Register>(),
4703 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4704 // temp = temp->GetImtEntryAt(method_offset);
4705 __ movl(locations->Out().AsRegister<Register>(),
4706 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004707 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004708}
4709
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004710void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004711 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004712 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004713 locations->SetInAt(0, Location::RequiresRegister());
4714 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004715}
4716
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004717void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4718 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004719 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004720 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004721 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004722 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004723 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004724 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004725 break;
4726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004727 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004728 __ notl(out.AsRegisterPairLow<Register>());
4729 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004730 break;
4731
4732 default:
4733 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4734 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004735}
4736
David Brazdil66d126e2015-04-03 16:02:44 +01004737void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4738 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004739 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004740 locations->SetInAt(0, Location::RequiresRegister());
4741 locations->SetOut(Location::SameAsFirstInput());
4742}
4743
4744void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004745 LocationSummary* locations = bool_not->GetLocations();
4746 Location in = locations->InAt(0);
4747 Location out = locations->Out();
4748 DCHECK(in.Equals(out));
4749 __ xorl(out.AsRegister<Register>(), Immediate(1));
4750}
4751
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004752void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004753 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004754 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004755 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004756 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004757 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004758 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004759 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004760 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004761 case DataType::Type::kInt32:
4762 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004763 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004764 locations->SetInAt(1, Location::Any());
4765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4766 break;
4767 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 case DataType::Type::kFloat32:
4769 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004770 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004771 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4772 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4773 } else if (compare->InputAt(1)->IsConstant()) {
4774 locations->SetInAt(1, Location::RequiresFpuRegister());
4775 } else {
4776 locations->SetInAt(1, Location::Any());
4777 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004778 locations->SetOut(Location::RequiresRegister());
4779 break;
4780 }
4781 default:
4782 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4783 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004784}
4785
4786void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004787 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004788 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004789 Location left = locations->InAt(0);
4790 Location right = locations->InAt(1);
4791
Mark Mendell0c9497d2015-08-21 09:30:05 -04004792 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004793 Condition less_cond = kLess;
4794
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004795 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004797 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004799 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004800 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004802 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004803 break;
4804 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004806 Register left_low = left.AsRegisterPairLow<Register>();
4807 Register left_high = left.AsRegisterPairHigh<Register>();
4808 int32_t val_low = 0;
4809 int32_t val_high = 0;
4810 bool right_is_const = false;
4811
4812 if (right.IsConstant()) {
4813 DCHECK(right.GetConstant()->IsLongConstant());
4814 right_is_const = true;
4815 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4816 val_low = Low32Bits(val);
4817 val_high = High32Bits(val);
4818 }
4819
Calin Juravleddb7df22014-11-25 20:56:51 +00004820 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004821 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004822 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004823 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004824 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004825 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004826 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004827 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004828 __ j(kLess, &less); // Signed compare.
4829 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004830 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004831 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004832 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004833 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004834 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004835 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004836 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004837 }
Aart Bika19616e2016-02-01 18:57:58 -08004838 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004839 break;
4840 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004841 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004842 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004843 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004844 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004845 break;
4846 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004848 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004849 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004850 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004851 break;
4852 }
4853 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004854 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004855 }
Aart Bika19616e2016-02-01 18:57:58 -08004856
Calin Juravleddb7df22014-11-25 20:56:51 +00004857 __ movl(out, Immediate(0));
4858 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004859 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004860
4861 __ Bind(&greater);
4862 __ movl(out, Immediate(1));
4863 __ jmp(&done);
4864
4865 __ Bind(&less);
4866 __ movl(out, Immediate(-1));
4867
4868 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004869}
4870
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004871void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004873 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004874 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004875 locations->SetInAt(i, Location::Any());
4876 }
4877 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004878}
4879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004880void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004881 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004882}
4883
Roland Levillain7c1559a2015-12-15 10:55:36 +00004884void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004885 /*
4886 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4887 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4888 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4889 */
4890 switch (kind) {
4891 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004892 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004893 break;
4894 }
4895 case MemBarrierKind::kAnyStore:
4896 case MemBarrierKind::kLoadAny:
4897 case MemBarrierKind::kStoreStore: {
4898 // nop
4899 break;
4900 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004901 case MemBarrierKind::kNTStoreStore:
4902 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004903 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004904 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004905 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004906}
4907
Vladimir Markodc151b22015-10-15 18:02:30 +01004908HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4909 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004910 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004911 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004912}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004913
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004914Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4915 Register temp) {
4916 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004917 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004918 if (!invoke->GetLocations()->Intrinsified()) {
4919 return location.AsRegister<Register>();
4920 }
4921 // For intrinsics we allow any location, so it may be on the stack.
4922 if (!location.IsRegister()) {
4923 __ movl(temp, Address(ESP, location.GetStackIndex()));
4924 return temp;
4925 }
4926 // For register locations, check if the register was saved. If so, get it from the stack.
4927 // Note: There is a chance that the register was saved but not overwritten, so we could
4928 // save one load. However, since this is just an intrinsic slow path we prefer this
4929 // simple and more robust approach rather that trying to determine if that's the case.
4930 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004931 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4932 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4933 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4934 __ movl(temp, Address(ESP, stack_offset));
4935 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004936 }
4937 return location.AsRegister<Register>();
4938}
4939
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004940void CodeGeneratorX86::GenerateStaticOrDirectCall(
4941 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004942 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4943 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004944 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004945 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004946 uint32_t offset =
4947 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4948 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004949 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004950 }
Vladimir Marko58155012015-08-19 12:49:41 +00004951 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004952 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004953 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004954 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004955 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004956 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4957 temp.AsRegister<Register>());
4958 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004959 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004960 break;
4961 }
Vladimir Markob066d432018-01-03 13:14:37 +00004962 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4963 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4964 temp.AsRegister<Register>());
4965 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4966 RecordBootImageRelRoPatch(
4967 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004968 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004969 break;
4970 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004971 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004972 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4973 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004974 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004975 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004976 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004977 break;
4978 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004979 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4980 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4981 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004982 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4983 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4984 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004985 }
Vladimir Marko58155012015-08-19 12:49:41 +00004986 }
4987
4988 switch (invoke->GetCodePtrLocation()) {
4989 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4990 __ call(GetFrameEntryLabel());
4991 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004992 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4993 // (callee_method + offset_of_quick_compiled_code)()
4994 __ call(Address(callee_method.AsRegister<Register>(),
4995 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004996 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004997 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004998 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004999 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04005000
5001 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005002}
5003
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005004void CodeGeneratorX86::GenerateVirtualCall(
5005 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005006 Register temp = temp_in.AsRegister<Register>();
5007 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5008 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005009
5010 // Use the calling convention instead of the location of the receiver, as
5011 // intrinsics may have put the receiver in a different register. In the intrinsics
5012 // slow path, the arguments have been moved to the right place, so here we are
5013 // guaranteed that the receiver is the first register of the calling convention.
5014 InvokeDexCallingConvention calling_convention;
5015 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005016 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005017 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005018 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005019 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005020 // Instead of simply (possibly) unpoisoning `temp` here, we should
5021 // emit a read barrier for the previous class reference load.
5022 // However this is not required in practice, as this is an
5023 // intermediate/temporary reference and because the current
5024 // concurrent copying collector keeps the from-space memory
5025 // intact/accessible until the end of the marking phase (the
5026 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005027 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005028
5029 MaybeGenerateInlineCacheCheck(invoke, temp);
5030
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005031 // temp = temp->GetMethodAt(method_offset);
5032 __ movl(temp, Address(temp, method_offset));
5033 // call temp->GetEntryPoint();
5034 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005035 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005036 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005037}
5038
Vladimir Marko6fd16062018-06-26 11:02:04 +01005039void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5040 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005041 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005042 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005043 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005044}
5045
Vladimir Markob066d432018-01-03 13:14:37 +00005046void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5047 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005048 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005049 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005050 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005051}
5052
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005053void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01005054 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005055 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005056 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005057 boot_image_method_patches_.emplace_back(
5058 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005059 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005060}
5061
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005062void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
5063 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5064 HX86ComputeBaseMethodAddress* method_address =
5065 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005066 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005067 method_bss_entry_patches_.emplace_back(
5068 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5069 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005070}
5071
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005072void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5073 HX86ComputeBaseMethodAddress* method_address =
5074 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5075 boot_image_type_patches_.emplace_back(
5076 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005077 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005078}
5079
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005080Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005081 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005082 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5083 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005084 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005085 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005086}
5087
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005088void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5089 HX86ComputeBaseMethodAddress* method_address =
5090 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5091 boot_image_string_patches_.emplace_back(
5092 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5093 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005094}
5095
Vladimir Markoaad75c62016-10-03 08:46:48 +00005096Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005097 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005098 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005099 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005100 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005101 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005102}
5103
Vladimir Markoeebb8212018-06-05 14:57:24 +01005104void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005105 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005106 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005107 if (GetCompilerOptions().IsBootImage()) {
5108 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5109 HX86ComputeBaseMethodAddress* method_address =
5110 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5111 DCHECK(method_address != nullptr);
5112 Register method_address_reg =
5113 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5114 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5115 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005116 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005117 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5118 HX86ComputeBaseMethodAddress* method_address =
5119 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5120 DCHECK(method_address != nullptr);
5121 Register method_address_reg =
5122 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5123 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005124 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005125 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005126 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005127 gc::Heap* heap = Runtime::Current()->GetHeap();
5128 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005129 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005130 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5131 }
5132}
5133
Vladimir Marko6fd16062018-06-26 11:02:04 +01005134void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5135 uint32_t boot_image_offset) {
5136 DCHECK(invoke->IsStatic());
5137 InvokeRuntimeCallingConvention calling_convention;
5138 Register argument = calling_convention.GetRegisterAt(0);
5139 if (GetCompilerOptions().IsBootImage()) {
5140 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5141 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5142 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5143 HX86ComputeBaseMethodAddress* method_address =
5144 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5145 DCHECK(method_address != nullptr);
5146 Register method_address_reg =
5147 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5148 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5149 MethodReference target_method = invoke->GetTargetMethod();
5150 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5151 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5152 __ Bind(&boot_image_type_patches_.back().label);
5153 } else {
5154 LoadBootImageAddress(argument, boot_image_offset, invoke);
5155 }
5156 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5157 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5158}
5159
Vladimir Markoaad75c62016-10-03 08:46:48 +00005160// The label points to the end of the "movl" or another instruction but the literal offset
5161// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5162constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5163
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005164template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005165inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005166 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005167 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005168 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005169 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005170 linker_patches->push_back(Factory(literal_offset,
5171 info.target_dex_file,
5172 GetMethodAddressOffset(info.method_address),
5173 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005174 }
5175}
5176
Vladimir Marko6fd16062018-06-26 11:02:04 +01005177template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5178linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5179 const DexFile* target_dex_file,
5180 uint32_t pc_insn_offset,
5181 uint32_t boot_image_offset) {
5182 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5183 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005184}
5185
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005186void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005187 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005188 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005189 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005190 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005191 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005192 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005193 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005194 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005195 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005196 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005197 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005198 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5199 boot_image_method_patches_, linker_patches);
5200 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5201 boot_image_type_patches_, linker_patches);
5202 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005203 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005204 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005205 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005206 DCHECK(boot_image_type_patches_.empty());
5207 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005208 }
5209 if (GetCompilerOptions().IsBootImage()) {
5210 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5211 boot_image_other_patches_, linker_patches);
5212 } else {
5213 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5214 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005215 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005216 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5217 method_bss_entry_patches_, linker_patches);
5218 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5219 type_bss_entry_patches_, linker_patches);
5220 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5221 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005222 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005223}
5224
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005225void CodeGeneratorX86::MarkGCCard(Register temp,
5226 Register card,
5227 Register object,
5228 Register value,
5229 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005230 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005231 if (value_can_be_null) {
5232 __ testl(value, value);
5233 __ j(kEqual, &is_null);
5234 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005235 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005236 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005237 // Calculate the offset (in the card table) of the card corresponding to
5238 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 __ movl(temp, object);
5240 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005241 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5242 // `object`'s card.
5243 //
5244 // Register `card` contains the address of the card table. Note that the card
5245 // table's base is biased during its creation so that it always starts at an
5246 // address whose least-significant byte is equal to `kCardDirty` (see
5247 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5248 // below writes the `kCardDirty` (byte) value into the `object`'s card
5249 // (located at `card + object >> kCardShift`).
5250 //
5251 // This dual use of the value in register `card` (1. to calculate the location
5252 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5253 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005254 __ movb(Address(temp, card, TIMES_1, 0),
5255 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005256 if (value_can_be_null) {
5257 __ Bind(&is_null);
5258 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259}
5260
Calin Juravle52c48962014-12-16 17:02:57 +00005261void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5262 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005263
5264 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005265 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005266 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005267 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5268 kEmitCompilerReadBarrier
5269 ? LocationSummary::kCallOnSlowPath
5270 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005271 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005272 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005273 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005274 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005277 locations->SetOut(Location::RequiresFpuRegister());
5278 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005279 // The output overlaps in case of long: we don't want the low move
5280 // to overwrite the object's location. Likewise, in the case of
5281 // an object field get with read barriers enabled, we do not want
5282 // the move to overwrite the object's location, as we need it to emit
5283 // the read barrier.
5284 locations->SetOut(
5285 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005287 Location::kOutputOverlap :
5288 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005289 }
Calin Juravle52c48962014-12-16 17:02:57 +00005290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005292 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005293 // So we use an XMM register as a temp to achieve atomicity (first
5294 // load the temp into the XMM and then copy the XMM into the
5295 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005296 locations->AddTemp(Location::RequiresFpuRegister());
5297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005298}
5299
Calin Juravle52c48962014-12-16 17:02:57 +00005300void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5301 const FieldInfo& field_info) {
5302 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005303
Calin Juravle52c48962014-12-16 17:02:57 +00005304 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305 Location base_loc = locations->InAt(0);
5306 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005307 Location out = locations->Out();
5308 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005309 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5310 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005311 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5312
Vladimir Marko61b92282017-10-11 13:23:17 +01005313 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005314 case DataType::Type::kBool:
5315 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005316 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005317 break;
5318 }
5319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005320 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005321 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005322 break;
5323 }
5324
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005325 case DataType::Type::kUint16: {
5326 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005327 break;
5328 }
5329
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005330 case DataType::Type::kInt16: {
5331 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005332 break;
5333 }
5334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005335 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005336 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005337 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005339 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005340 // /* HeapReference<Object> */ out = *(base + offset)
5341 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005342 // Note that a potential implicit null check is handled in this
5343 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5344 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005345 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005346 if (is_volatile) {
5347 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5348 }
5349 } else {
5350 __ movl(out.AsRegister<Register>(), Address(base, offset));
5351 codegen_->MaybeRecordImplicitNullCheck(instruction);
5352 if (is_volatile) {
5353 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5354 }
5355 // If read barriers are enabled, emit read barriers other than
5356 // Baker's using a slow path (and also unpoison the loaded
5357 // reference, if heap poisoning is enabled).
5358 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5359 }
5360 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005361 }
5362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005363 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005364 if (is_volatile) {
5365 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5366 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005367 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005368 __ movd(out.AsRegisterPairLow<Register>(), temp);
5369 __ psrlq(temp, Immediate(32));
5370 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5371 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005372 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005373 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005374 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005375 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5376 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005377 break;
5378 }
5379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005380 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005381 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005382 break;
5383 }
5384
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005385 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005386 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005387 break;
5388 }
5389
Aart Bik66c158e2018-01-31 12:55:04 -08005390 case DataType::Type::kUint32:
5391 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005393 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005394 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005395 }
Calin Juravle52c48962014-12-16 17:02:57 +00005396
Vladimir Marko61b92282017-10-11 13:23:17 +01005397 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005398 // Potential implicit null checks, in the case of reference or
5399 // long fields, are handled in the previous switch statement.
5400 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005401 codegen_->MaybeRecordImplicitNullCheck(instruction);
5402 }
5403
Calin Juravle52c48962014-12-16 17:02:57 +00005404 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005405 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005406 // Memory barriers, in the case of references, are also handled
5407 // in the previous switch statement.
5408 } else {
5409 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5410 }
Roland Levillain4d027112015-07-01 15:41:14 +01005411 }
Calin Juravle52c48962014-12-16 17:02:57 +00005412}
5413
5414void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5415 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5416
5417 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005418 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005419 locations->SetInAt(0, Location::RequiresRegister());
5420 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005421 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005422 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005423
5424 // The register allocator does not support multiple
5425 // inputs that die at entry with one in a specific register.
5426 if (is_byte_type) {
5427 // Ensure the value is in a byte register.
5428 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 } else if (DataType::IsFloatingPointType(field_type)) {
5430 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005431 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5432 locations->SetInAt(1, Location::RequiresFpuRegister());
5433 } else {
5434 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5435 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005436 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005437 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005438 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005439
Calin Juravle52c48962014-12-16 17:02:57 +00005440 // 64bits value can be atomically written to an address with movsd and an XMM register.
5441 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5442 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5443 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5444 // isolated cases when we need this it isn't worth adding the extra complexity.
5445 locations->AddTemp(Location::RequiresFpuRegister());
5446 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005447 } else {
5448 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5449
5450 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5451 // Temporary registers for the write barrier.
5452 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5453 // Ensure the card is in a byte register.
5454 locations->AddTemp(Location::RegisterLocation(ECX));
5455 }
Calin Juravle52c48962014-12-16 17:02:57 +00005456 }
5457}
5458
5459void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005460 const FieldInfo& field_info,
5461 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005462 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5463
5464 LocationSummary* locations = instruction->GetLocations();
5465 Register base = locations->InAt(0).AsRegister<Register>();
5466 Location value = locations->InAt(1);
5467 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005468 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005469 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005470 bool needs_write_barrier =
5471 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005472
5473 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005474 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005475 }
5476
Mark Mendell81489372015-11-04 11:30:41 -05005477 bool maybe_record_implicit_null_check_done = false;
5478
Calin Juravle52c48962014-12-16 17:02:57 +00005479 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005481 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005482 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005483 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5484 break;
5485 }
5486
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005487 case DataType::Type::kUint16:
5488 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005489 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005490 __ movw(Address(base, offset),
5491 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005492 } else {
5493 __ movw(Address(base, offset), value.AsRegister<Register>());
5494 }
Calin Juravle52c48962014-12-16 17:02:57 +00005495 break;
5496 }
5497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005498 case DataType::Type::kInt32:
5499 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005500 if (kPoisonHeapReferences && needs_write_barrier) {
5501 // Note that in the case where `value` is a null reference,
5502 // we do not enter this block, as the reference does not
5503 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005505 Register temp = locations->GetTemp(0).AsRegister<Register>();
5506 __ movl(temp, value.AsRegister<Register>());
5507 __ PoisonHeapReference(temp);
5508 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005509 } else if (value.IsConstant()) {
5510 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5511 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005512 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005513 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005514 __ movl(Address(base, offset), value.AsRegister<Register>());
5515 }
Calin Juravle52c48962014-12-16 17:02:57 +00005516 break;
5517 }
5518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005520 if (is_volatile) {
5521 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5522 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5523 __ movd(temp1, value.AsRegisterPairLow<Register>());
5524 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5525 __ punpckldq(temp1, temp2);
5526 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005527 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005528 } else if (value.IsConstant()) {
5529 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5530 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5531 codegen_->MaybeRecordImplicitNullCheck(instruction);
5532 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005533 } else {
5534 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005535 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005536 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5537 }
Mark Mendell81489372015-11-04 11:30:41 -05005538 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005539 break;
5540 }
5541
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005542 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005543 if (value.IsConstant()) {
5544 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5545 __ movl(Address(base, offset), Immediate(v));
5546 } else {
5547 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5548 }
Calin Juravle52c48962014-12-16 17:02:57 +00005549 break;
5550 }
5551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005552 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005553 if (value.IsConstant()) {
5554 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5555 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5556 codegen_->MaybeRecordImplicitNullCheck(instruction);
5557 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5558 maybe_record_implicit_null_check_done = true;
5559 } else {
5560 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5561 }
Calin Juravle52c48962014-12-16 17:02:57 +00005562 break;
5563 }
5564
Aart Bik66c158e2018-01-31 12:55:04 -08005565 case DataType::Type::kUint32:
5566 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005568 LOG(FATAL) << "Unreachable type " << field_type;
5569 UNREACHABLE();
5570 }
5571
Mark Mendell81489372015-11-04 11:30:41 -05005572 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005573 codegen_->MaybeRecordImplicitNullCheck(instruction);
5574 }
5575
Roland Levillain4d027112015-07-01 15:41:14 +01005576 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005577 Register temp = locations->GetTemp(0).AsRegister<Register>();
5578 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005579 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005580 }
5581
Calin Juravle52c48962014-12-16 17:02:57 +00005582 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005583 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005584 }
5585}
5586
5587void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5588 HandleFieldGet(instruction, instruction->GetFieldInfo());
5589}
5590
5591void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5592 HandleFieldGet(instruction, instruction->GetFieldInfo());
5593}
5594
5595void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5596 HandleFieldSet(instruction, instruction->GetFieldInfo());
5597}
5598
5599void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005600 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005601}
5602
5603void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5604 HandleFieldSet(instruction, instruction->GetFieldInfo());
5605}
5606
5607void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005608 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005609}
5610
5611void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5612 HandleFieldGet(instruction, instruction->GetFieldInfo());
5613}
5614
5615void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5616 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005617}
5618
Vladimir Marko552a1342017-10-31 10:56:47 +00005619void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5620 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5621}
5622
5623void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5624 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5625 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5626}
5627
Calin Juravlee460d1d2015-09-29 04:52:17 +01005628void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5629 HUnresolvedInstanceFieldGet* instruction) {
5630 FieldAccessCallingConventionX86 calling_convention;
5631 codegen_->CreateUnresolvedFieldLocationSummary(
5632 instruction, instruction->GetFieldType(), calling_convention);
5633}
5634
5635void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5636 HUnresolvedInstanceFieldGet* instruction) {
5637 FieldAccessCallingConventionX86 calling_convention;
5638 codegen_->GenerateUnresolvedFieldAccess(instruction,
5639 instruction->GetFieldType(),
5640 instruction->GetFieldIndex(),
5641 instruction->GetDexPc(),
5642 calling_convention);
5643}
5644
5645void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5646 HUnresolvedInstanceFieldSet* instruction) {
5647 FieldAccessCallingConventionX86 calling_convention;
5648 codegen_->CreateUnresolvedFieldLocationSummary(
5649 instruction, instruction->GetFieldType(), calling_convention);
5650}
5651
5652void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5653 HUnresolvedInstanceFieldSet* instruction) {
5654 FieldAccessCallingConventionX86 calling_convention;
5655 codegen_->GenerateUnresolvedFieldAccess(instruction,
5656 instruction->GetFieldType(),
5657 instruction->GetFieldIndex(),
5658 instruction->GetDexPc(),
5659 calling_convention);
5660}
5661
5662void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5663 HUnresolvedStaticFieldGet* instruction) {
5664 FieldAccessCallingConventionX86 calling_convention;
5665 codegen_->CreateUnresolvedFieldLocationSummary(
5666 instruction, instruction->GetFieldType(), calling_convention);
5667}
5668
5669void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5670 HUnresolvedStaticFieldGet* instruction) {
5671 FieldAccessCallingConventionX86 calling_convention;
5672 codegen_->GenerateUnresolvedFieldAccess(instruction,
5673 instruction->GetFieldType(),
5674 instruction->GetFieldIndex(),
5675 instruction->GetDexPc(),
5676 calling_convention);
5677}
5678
5679void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5680 HUnresolvedStaticFieldSet* instruction) {
5681 FieldAccessCallingConventionX86 calling_convention;
5682 codegen_->CreateUnresolvedFieldLocationSummary(
5683 instruction, instruction->GetFieldType(), calling_convention);
5684}
5685
5686void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5687 HUnresolvedStaticFieldSet* instruction) {
5688 FieldAccessCallingConventionX86 calling_convention;
5689 codegen_->GenerateUnresolvedFieldAccess(instruction,
5690 instruction->GetFieldType(),
5691 instruction->GetFieldIndex(),
5692 instruction->GetDexPc(),
5693 calling_convention);
5694}
5695
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005696void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005697 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5698 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5699 ? Location::RequiresRegister()
5700 : Location::Any();
5701 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005702}
5703
Calin Juravle2ae48182016-03-16 14:05:09 +00005704void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5705 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005706 return;
5707 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005708 LocationSummary* locations = instruction->GetLocations();
5709 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005710
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005711 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005712 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005713}
5714
Calin Juravle2ae48182016-03-16 14:05:09 +00005715void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005716 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005717 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005718
5719 LocationSummary* locations = instruction->GetLocations();
5720 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721
5722 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005723 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005724 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005725 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005726 } else {
5727 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005728 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005729 __ jmp(slow_path->GetEntryLabel());
5730 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005731 }
5732 __ j(kEqual, slow_path->GetEntryLabel());
5733}
5734
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005735void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005736 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005737}
5738
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005739void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005740 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005742 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005743 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5744 object_array_get_with_read_barrier
5745 ? LocationSummary::kCallOnSlowPath
5746 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005747 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005748 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005749 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005750 locations->SetInAt(0, Location::RequiresRegister());
5751 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005753 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5754 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 // The output overlaps in case of long: we don't want the low move
5756 // to overwrite the array's location. Likewise, in the case of an
5757 // object array get with read barriers enabled, we do not want the
5758 // move to overwrite the array's location, as we need it to emit
5759 // the read barrier.
5760 locations->SetOut(
5761 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005762 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5763 ? Location::kOutputOverlap
5764 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005765 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005766}
5767
5768void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5769 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770 Location obj_loc = locations->InAt(0);
5771 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005772 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005773 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005774 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005777 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005778 case DataType::Type::kBool:
5779 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005780 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005781 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005782 break;
5783 }
5784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005786 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005787 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005788 break;
5789 }
5790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005792 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005793 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5794 // Branch cases into compressed and uncompressed for each index's type.
5795 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5796 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005797 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005799 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5800 "Expecting 0=compressed, 1=uncompressed");
5801 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005802 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5803 __ jmp(&done);
5804 __ Bind(&not_compressed);
5805 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5806 __ Bind(&done);
5807 } else {
5808 // Common case for charAt of array of char or when string compression's
5809 // feature is turned off.
5810 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5811 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005812 break;
5813 }
5814
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005815 case DataType::Type::kInt16: {
5816 Register out = out_loc.AsRegister<Register>();
5817 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5818 break;
5819 }
5820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005822 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005823 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005824 break;
5825 }
5826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005828 static_assert(
5829 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5830 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005831 // /* HeapReference<Object> */ out =
5832 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5833 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005834 // Note that a potential implicit null check is handled in this
5835 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5836 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005837 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005838 } else {
5839 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005840 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5841 codegen_->MaybeRecordImplicitNullCheck(instruction);
5842 // If read barriers are enabled, emit read barriers other than
5843 // Baker's using a slow path (and also unpoison the loaded
5844 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005845 if (index.IsConstant()) {
5846 uint32_t offset =
5847 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005848 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5849 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005850 codegen_->MaybeGenerateReadBarrierSlow(
5851 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5852 }
5853 }
5854 break;
5855 }
5856
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005858 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005859 __ movl(out_loc.AsRegisterPairLow<Register>(),
5860 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5861 codegen_->MaybeRecordImplicitNullCheck(instruction);
5862 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5863 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005864 break;
5865 }
5866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005867 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005868 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005869 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005870 break;
5871 }
5872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005874 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005875 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005876 break;
5877 }
5878
Aart Bik66c158e2018-01-31 12:55:04 -08005879 case DataType::Type::kUint32:
5880 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005882 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005883 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005884 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005887 // Potential implicit null checks, in the case of reference or
5888 // long arrays, are handled in the previous switch statement.
5889 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005890 codegen_->MaybeRecordImplicitNullCheck(instruction);
5891 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005892}
5893
5894void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005896
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005897 bool needs_write_barrier =
5898 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005899 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005900
Vladimir Markoca6fff82017-10-03 14:49:14 +01005901 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005902 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005903 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005904
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005905 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005906 // We need the inputs to be different than the output in case of long operation.
5907 // In case of a byte operation, the register allocator does not support multiple
5908 // inputs that die at entry with one in a specific register.
5909 locations->SetInAt(0, Location::RequiresRegister());
5910 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5911 if (is_byte_type) {
5912 // Ensure the value is in a byte register.
5913 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005915 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005916 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005917 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5918 }
5919 if (needs_write_barrier) {
5920 // Temporary registers for the write barrier.
5921 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5922 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005923 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005924 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005925}
5926
5927void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5928 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005929 Location array_loc = locations->InAt(0);
5930 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005931 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005932 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005933 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005934 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005935 bool needs_write_barrier =
5936 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005937
5938 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005939 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005940 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005941 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005942 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005943 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005944 if (value.IsRegister()) {
5945 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005947 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005948 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005949 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005950 break;
5951 }
5952
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005953 case DataType::Type::kUint16:
5954 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005955 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005956 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005957 if (value.IsRegister()) {
5958 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005959 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005960 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005961 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005962 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005963 break;
5964 }
5965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005966 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005967 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005968 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005969
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005970 if (!value.IsRegister()) {
5971 // Just setting null.
5972 DCHECK(instruction->InputAt(2)->IsNullConstant());
5973 DCHECK(value.IsConstant()) << value;
5974 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005975 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005976 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005977 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005978 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005979 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005980
5981 DCHECK(needs_write_barrier);
5982 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005983 Location temp_loc = locations->GetTemp(0);
5984 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005985
5986 bool can_value_be_null = instruction->GetValueCanBeNull();
5987 NearLabel do_store;
5988 if (can_value_be_null) {
5989 __ testl(register_value, register_value);
5990 __ j(kEqual, &do_store);
5991 }
5992
5993 SlowPathCode* slow_path = nullptr;
5994 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005995 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005996 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005997
5998 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5999 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6000 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006001
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006002 // Note that when Baker read barriers are enabled, the type
6003 // checks are performed without read barriers. This is fine,
6004 // even in the case where a class object is in the from-space
6005 // after the flip, as a comparison involving such a type would
6006 // not produce a false positive; it may of course produce a
6007 // false negative, in which case we would take the ArraySet
6008 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006009
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006010 // /* HeapReference<Class> */ temp = array->klass_
6011 __ movl(temp, Address(array, class_offset));
6012 codegen_->MaybeRecordImplicitNullCheck(instruction);
6013 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006014
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006015 // /* HeapReference<Class> */ temp = temp->component_type_
6016 __ movl(temp, Address(temp, component_offset));
6017 // If heap poisoning is enabled, no need to unpoison `temp`
6018 // nor the object reference in `register_value->klass`, as
6019 // we are comparing two poisoned references.
6020 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006021
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006022 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006023 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006024 __ j(kEqual, &do_put);
6025 // If heap poisoning is enabled, the `temp` reference has
6026 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006027 __ MaybeUnpoisonHeapReference(temp);
6028
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006029 // If heap poisoning is enabled, no need to unpoison the
6030 // heap reference loaded below, as it is only used for a
6031 // comparison with null.
6032 __ cmpl(Address(temp, super_offset), Immediate(0));
6033 __ j(kNotEqual, slow_path->GetEntryLabel());
6034 __ Bind(&do_put);
6035 } else {
6036 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006037 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006038 }
6039
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006040 Register card = locations->GetTemp(1).AsRegister<Register>();
6041 codegen_->MarkGCCard(
6042 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6043
6044 if (can_value_be_null) {
6045 DCHECK(do_store.IsLinked());
6046 __ Bind(&do_store);
6047 }
6048
6049 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006050 if (kPoisonHeapReferences) {
6051 __ movl(temp, register_value);
6052 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006053 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006054 }
6055
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006056 __ movl(address, source);
6057
6058 if (can_value_be_null || !needs_type_check) {
6059 codegen_->MaybeRecordImplicitNullCheck(instruction);
6060 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006061
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006062 if (slow_path != nullptr) {
6063 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006064 }
6065
6066 break;
6067 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006069 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006070 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006071 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006072 if (value.IsRegister()) {
6073 __ movl(address, value.AsRegister<Register>());
6074 } else {
6075 DCHECK(value.IsConstant()) << value;
6076 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6077 __ movl(address, Immediate(v));
6078 }
6079 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::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006085 if (value.IsRegisterPair()) {
6086 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6087 value.AsRegisterPairLow<Register>());
6088 codegen_->MaybeRecordImplicitNullCheck(instruction);
6089 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6090 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006091 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006092 DCHECK(value.IsConstant());
6093 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6094 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6095 Immediate(Low32Bits(val)));
6096 codegen_->MaybeRecordImplicitNullCheck(instruction);
6097 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6098 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099 }
6100 break;
6101 }
6102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006104 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006105 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006106 if (value.IsFpuRegister()) {
6107 __ movss(address, value.AsFpuRegister<XmmRegister>());
6108 } else {
6109 DCHECK(value.IsConstant());
6110 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6111 __ movl(address, Immediate(v));
6112 }
6113 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006114 break;
6115 }
6116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006117 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006118 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006119 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006120 if (value.IsFpuRegister()) {
6121 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6122 } else {
6123 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006124 Address address_hi =
6125 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006126 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6127 __ movl(address, Immediate(Low32Bits(v)));
6128 codegen_->MaybeRecordImplicitNullCheck(instruction);
6129 __ movl(address_hi, Immediate(High32Bits(v)));
6130 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006131 break;
6132 }
6133
Aart Bik66c158e2018-01-31 12:55:04 -08006134 case DataType::Type::kUint32:
6135 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006136 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006137 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006138 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006139 }
6140}
6141
6142void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006143 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006144 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006145 if (!instruction->IsEmittedAtUseSite()) {
6146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6147 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006148}
6149
6150void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006151 if (instruction->IsEmittedAtUseSite()) {
6152 return;
6153 }
6154
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006155 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006156 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006157 Register obj = locations->InAt(0).AsRegister<Register>();
6158 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006159 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006160 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006161 // Mask out most significant bit in case the array is String's array of char.
6162 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006163 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006164 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006165}
6166
6167void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006168 RegisterSet caller_saves = RegisterSet::Empty();
6169 InvokeRuntimeCallingConvention calling_convention;
6170 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6171 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6172 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006173 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006174 HInstruction* length = instruction->InputAt(1);
6175 if (!length->IsEmittedAtUseSite()) {
6176 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6177 }
jessicahandojo4877b792016-09-08 19:49:13 -07006178 // Need register to see array's length.
6179 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6180 locations->AddTemp(Location::RequiresRegister());
6181 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006182}
6183
6184void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006185 const bool is_string_compressed_char_at =
6186 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006187 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006188 Location index_loc = locations->InAt(0);
6189 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006190 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006191 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006192
Mark Mendell99dbd682015-04-22 16:18:52 -04006193 if (length_loc.IsConstant()) {
6194 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6195 if (index_loc.IsConstant()) {
6196 // BCE will remove the bounds check if we are guarenteed to pass.
6197 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6198 if (index < 0 || index >= length) {
6199 codegen_->AddSlowPath(slow_path);
6200 __ jmp(slow_path->GetEntryLabel());
6201 } else {
6202 // Some optimization after BCE may have generated this, and we should not
6203 // generate a bounds check if it is a valid range.
6204 }
6205 return;
6206 }
6207
6208 // We have to reverse the jump condition because the length is the constant.
6209 Register index_reg = index_loc.AsRegister<Register>();
6210 __ cmpl(index_reg, Immediate(length));
6211 codegen_->AddSlowPath(slow_path);
6212 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006213 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006214 HInstruction* array_length = instruction->InputAt(1);
6215 if (array_length->IsEmittedAtUseSite()) {
6216 // Address the length field in the array.
6217 DCHECK(array_length->IsArrayLength());
6218 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6219 Location array_loc = array_length->GetLocations()->InAt(0);
6220 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006221 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006222 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6223 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006224 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6225 __ movl(length_reg, array_len);
6226 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006227 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006228 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006229 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006230 // Checking bounds for general case:
6231 // Array of char or string's array with feature compression off.
6232 if (index_loc.IsConstant()) {
6233 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6234 __ cmpl(array_len, Immediate(value));
6235 } else {
6236 __ cmpl(array_len, index_loc.AsRegister<Register>());
6237 }
6238 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006239 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006240 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006241 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006242 }
6243 codegen_->AddSlowPath(slow_path);
6244 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006245 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006246}
6247
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006248void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006249 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006250}
6251
6252void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006253 if (instruction->GetNext()->IsSuspendCheck() &&
6254 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6255 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6256 // The back edge will generate the suspend check.
6257 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6258 }
6259
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006260 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6261}
6262
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006263void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006264 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6265 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006266 // In suspend check slow path, usually there are no caller-save registers at all.
6267 // If SIMD instructions are present, however, we force spilling all live SIMD
6268 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006269 locations->SetCustomSlowPathCallerSaves(
6270 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006271}
6272
6273void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006274 HBasicBlock* block = instruction->GetBlock();
6275 if (block->GetLoopInformation() != nullptr) {
6276 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6277 // The back edge will generate the suspend check.
6278 return;
6279 }
6280 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6281 // The goto will generate the suspend check.
6282 return;
6283 }
6284 GenerateSuspendCheck(instruction, nullptr);
6285}
6286
6287void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6288 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006289 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006290 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6291 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006292 slow_path =
6293 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006294 instruction->SetSlowPath(slow_path);
6295 codegen_->AddSlowPath(slow_path);
6296 if (successor != nullptr) {
6297 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006298 }
6299 } else {
6300 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6301 }
6302
Andreas Gampe542451c2016-07-26 09:02:02 -07006303 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006304 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006305 if (successor == nullptr) {
6306 __ j(kNotEqual, slow_path->GetEntryLabel());
6307 __ Bind(slow_path->GetReturnLabel());
6308 } else {
6309 __ j(kEqual, codegen_->GetLabelOf(successor));
6310 __ jmp(slow_path->GetEntryLabel());
6311 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006312}
6313
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006314X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6315 return codegen_->GetAssembler();
6316}
6317
Aart Bikcfe50bb2017-12-12 14:54:12 -08006318void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006319 ScratchRegisterScope ensure_scratch(
6320 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6321 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6322 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006323
Aart Bikcfe50bb2017-12-12 14:54:12 -08006324 // Now that temp register is available (possibly spilled), move blocks of memory.
6325 for (int i = 0; i < number_of_words; i++) {
6326 __ movl(temp_reg, Address(ESP, src + stack_offset));
6327 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6328 stack_offset += kX86WordSize;
6329 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006330}
6331
6332void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006333 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006334 Location source = move->GetSource();
6335 Location destination = move->GetDestination();
6336
6337 if (source.IsRegister()) {
6338 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006339 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006340 } else if (destination.IsFpuRegister()) {
6341 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006342 } else {
6343 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006344 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006345 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006346 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006348 // Create stack space for 2 elements.
6349 __ subl(ESP, Immediate(2 * elem_size));
6350 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6351 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6352 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6353 // And remove the temporary stack space we allocated.
6354 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006355 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006356 if (destination.IsRegister()) {
6357 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6358 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006359 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006360 } else if (destination.IsRegisterPair()) {
6361 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6362 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6363 __ psrlq(src_reg, Immediate(32));
6364 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006365 } else if (destination.IsStackSlot()) {
6366 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006367 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006368 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006369 } else {
6370 DCHECK(destination.IsSIMDStackSlot());
6371 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006372 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006373 } else if (source.IsStackSlot()) {
6374 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006375 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006376 } else if (destination.IsFpuRegister()) {
6377 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006378 } else {
6379 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006380 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006381 }
6382 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006383 if (destination.IsRegisterPair()) {
6384 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6385 __ movl(destination.AsRegisterPairHigh<Register>(),
6386 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6387 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006388 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6389 } else {
6390 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006391 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006392 }
Aart Bik5576f372017-03-23 16:17:37 -07006393 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006394 if (destination.IsFpuRegister()) {
6395 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6396 } else {
6397 DCHECK(destination.IsSIMDStackSlot());
6398 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6399 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006400 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006401 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006402 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006403 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006404 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006405 if (value == 0) {
6406 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6407 } else {
6408 __ movl(destination.AsRegister<Register>(), Immediate(value));
6409 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006410 } else {
6411 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006412 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006413 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006414 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006415 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006416 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006417 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006418 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006419 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6420 if (value == 0) {
6421 // Easy handling of 0.0.
6422 __ xorps(dest, dest);
6423 } else {
6424 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006425 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6426 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6427 __ movl(temp, Immediate(value));
6428 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006429 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006430 } else {
6431 DCHECK(destination.IsStackSlot()) << destination;
6432 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6433 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006434 } else if (constant->IsLongConstant()) {
6435 int64_t value = constant->AsLongConstant()->GetValue();
6436 int32_t low_value = Low32Bits(value);
6437 int32_t high_value = High32Bits(value);
6438 Immediate low(low_value);
6439 Immediate high(high_value);
6440 if (destination.IsDoubleStackSlot()) {
6441 __ movl(Address(ESP, destination.GetStackIndex()), low);
6442 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6443 } else {
6444 __ movl(destination.AsRegisterPairLow<Register>(), low);
6445 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6446 }
6447 } else {
6448 DCHECK(constant->IsDoubleConstant());
6449 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006450 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006451 int32_t low_value = Low32Bits(value);
6452 int32_t high_value = High32Bits(value);
6453 Immediate low(low_value);
6454 Immediate high(high_value);
6455 if (destination.IsFpuRegister()) {
6456 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6457 if (value == 0) {
6458 // Easy handling of 0.0.
6459 __ xorpd(dest, dest);
6460 } else {
6461 __ pushl(high);
6462 __ pushl(low);
6463 __ movsd(dest, Address(ESP, 0));
6464 __ addl(ESP, Immediate(8));
6465 }
6466 } else {
6467 DCHECK(destination.IsDoubleStackSlot()) << destination;
6468 __ movl(Address(ESP, destination.GetStackIndex()), low);
6469 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6470 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006471 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006472 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006473 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006474 }
6475}
6476
Mark Mendella5c19ce2015-04-01 12:51:05 -04006477void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006478 Register suggested_scratch = reg == EAX ? EBX : EAX;
6479 ScratchRegisterScope ensure_scratch(
6480 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6481
6482 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6483 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6484 __ movl(Address(ESP, mem + stack_offset), reg);
6485 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006486}
6487
Mark Mendell7c8d0092015-01-26 11:21:33 -05006488void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006489 ScratchRegisterScope ensure_scratch(
6490 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6491
6492 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6493 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6494 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6495 __ movss(Address(ESP, mem + stack_offset), reg);
6496 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006497}
6498
Aart Bikcfe50bb2017-12-12 14:54:12 -08006499void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6500 size_t extra_slot = 4 * kX86WordSize;
6501 __ subl(ESP, Immediate(extra_slot));
6502 __ movups(Address(ESP, 0), XmmRegister(reg));
6503 ExchangeMemory(0, mem + extra_slot, 4);
6504 __ movups(XmmRegister(reg), Address(ESP, 0));
6505 __ addl(ESP, Immediate(extra_slot));
6506}
6507
6508void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006509 ScratchRegisterScope ensure_scratch1(
6510 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006511
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006512 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6513 ScratchRegisterScope ensure_scratch2(
6514 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006515
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006516 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6517 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006518
6519 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6520 for (int i = 0; i < number_of_words; i++) {
6521 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6522 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6523 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6524 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6525 stack_offset += kX86WordSize;
6526 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006527}
6528
6529void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006530 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006531 Location source = move->GetSource();
6532 Location destination = move->GetDestination();
6533
6534 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006535 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6536 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6537 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6538 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6539 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006540 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006541 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006542 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006543 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006544 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006545 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006546 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6547 // Use XOR Swap algorithm to avoid a temporary.
6548 DCHECK_NE(source.reg(), destination.reg());
6549 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6550 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6551 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6552 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6553 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6554 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6555 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006556 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6557 // Take advantage of the 16 bytes in the XMM register.
6558 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6559 Address stack(ESP, destination.GetStackIndex());
6560 // Load the double into the high doubleword.
6561 __ movhpd(reg, stack);
6562
6563 // Store the low double into the destination.
6564 __ movsd(stack, reg);
6565
6566 // Move the high double to the low double.
6567 __ psrldq(reg, Immediate(8));
6568 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6569 // Take advantage of the 16 bytes in the XMM register.
6570 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6571 Address stack(ESP, source.GetStackIndex());
6572 // Load the double into the high doubleword.
6573 __ movhpd(reg, stack);
6574
6575 // Store the low double into the destination.
6576 __ movsd(stack, reg);
6577
6578 // Move the high double to the low double.
6579 __ psrldq(reg, Immediate(8));
6580 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006581 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6582 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6583 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6584 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6585 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6586 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6587 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006588 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006589 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006590 }
6591}
6592
6593void ParallelMoveResolverX86::SpillScratch(int reg) {
6594 __ pushl(static_cast<Register>(reg));
6595}
6596
6597void ParallelMoveResolverX86::RestoreScratch(int reg) {
6598 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006599}
6600
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006601HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6602 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006603 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006604 case HLoadClass::LoadKind::kInvalid:
6605 LOG(FATAL) << "UNREACHABLE";
6606 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006607 case HLoadClass::LoadKind::kReferrersClass:
6608 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006609 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006610 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006611 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006612 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006613 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006614 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006615 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006616 DCHECK(Runtime::Current()->UseJitCompilation());
6617 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006618 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006619 break;
6620 }
6621 return desired_class_load_kind;
6622}
6623
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006624void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006625 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006626 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006627 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006628 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006629 cls,
6630 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006631 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006632 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006633 return;
6634 }
Vladimir Marko41559982017-01-06 14:04:23 +00006635 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006636
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006637 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6638 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006639 ? LocationSummary::kCallOnSlowPath
6640 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006641 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006642 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006643 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006644 }
6645
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006646 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006647 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006648 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006649 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006650 locations->SetInAt(0, Location::RequiresRegister());
6651 }
6652 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006653 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6654 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6655 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006656 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006657 } else {
6658 // For non-Baker read barrier we have a temp-clobbering call.
6659 }
6660 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006661}
6662
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006663Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006664 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006665 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006666 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006667 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006668 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006669 PatchInfo<Label>* info = &jit_class_patches_.back();
6670 return &info->label;
6671}
6672
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006673// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6674// move.
6675void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006676 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006677 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006678 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006679 return;
6680 }
Vladimir Marko41559982017-01-06 14:04:23 +00006681 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006682
Vladimir Marko41559982017-01-06 14:04:23 +00006683 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 Location out_loc = locations->Out();
6685 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006687 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006688 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6689 ? kWithoutReadBarrier
6690 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006691 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006692 case HLoadClass::LoadKind::kReferrersClass: {
6693 DCHECK(!cls->CanCallRuntime());
6694 DCHECK(!cls->MustGenerateClinitCheck());
6695 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6696 Register current_method = locations->InAt(0).AsRegister<Register>();
6697 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006698 cls,
6699 out_loc,
6700 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006701 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006702 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006703 break;
6704 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006705 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006706 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6707 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006708 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006709 Register method_address = locations->InAt(0).AsRegister<Register>();
6710 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006711 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006712 break;
6713 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006714 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006715 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6716 Register method_address = locations->InAt(0).AsRegister<Register>();
6717 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006718 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6719 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006720 break;
6721 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006722 case HLoadClass::LoadKind::kBssEntry: {
6723 Register method_address = locations->InAt(0).AsRegister<Register>();
6724 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6725 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6726 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006727 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006728 generate_null_check = true;
6729 break;
6730 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006731 case HLoadClass::LoadKind::kJitBootImageAddress: {
6732 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6733 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6734 DCHECK_NE(address, 0u);
6735 __ movl(out, Immediate(address));
6736 break;
6737 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006738 case HLoadClass::LoadKind::kJitTableAddress: {
6739 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6740 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006741 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006742 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006743 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006744 break;
6745 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006746 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006747 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006748 LOG(FATAL) << "UNREACHABLE";
6749 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006750 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006751
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006752 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6753 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006754 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006755 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006756
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006757 if (generate_null_check) {
6758 __ testl(out, out);
6759 __ j(kEqual, slow_path->GetEntryLabel());
6760 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006761
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006762 if (cls->MustGenerateClinitCheck()) {
6763 GenerateClassInitializationCheck(slow_path, out);
6764 } else {
6765 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006766 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006767 }
6768}
6769
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006770void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6771 InvokeRuntimeCallingConvention calling_convention;
6772 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6773 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6774}
6775
6776void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6777 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6778}
6779
Orion Hodson18259d72018-04-12 11:18:23 +01006780void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6781 InvokeRuntimeCallingConvention calling_convention;
6782 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6783 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6784}
6785
6786void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6787 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6788}
6789
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006790void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6791 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006792 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006793 locations->SetInAt(0, Location::RequiresRegister());
6794 if (check->HasUses()) {
6795 locations->SetOut(Location::SameAsFirstInput());
6796 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006797 // Rely on the type initialization to save everything we need.
6798 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006799}
6800
6801void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006802 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006803 SlowPathCode* slow_path =
6804 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006805 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006806 GenerateClassInitializationCheck(slow_path,
6807 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006808}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006809
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006810void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006811 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006812 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6813 const size_t status_byte_offset =
6814 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006815 constexpr uint32_t shifted_visibly_initialized_value =
6816 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006817
Vladimir Markobf121912019-06-04 13:49:05 +01006818 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006819 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006820 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006821}
6822
Vladimir Marko175e7862018-03-27 09:03:13 +00006823void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6824 Register temp) {
6825 uint32_t path_to_root = check->GetBitstringPathToRoot();
6826 uint32_t mask = check->GetBitstringMask();
6827 DCHECK(IsPowerOfTwo(mask + 1));
6828 size_t mask_bits = WhichPowerOf2(mask + 1);
6829
6830 if (mask_bits == 16u) {
6831 // Compare the bitstring in memory.
6832 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6833 } else {
6834 // /* uint32_t */ temp = temp->status_
6835 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6836 // Compare the bitstring bits using SUB.
6837 __ subl(temp, Immediate(path_to_root));
6838 // Shift out bits that do not contribute to the comparison.
6839 __ shll(temp, Immediate(32u - mask_bits));
6840 }
6841}
6842
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006843HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6844 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006845 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006846 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006847 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006848 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006849 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006850 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006851 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006852 case HLoadString::LoadKind::kJitTableAddress:
6853 DCHECK(Runtime::Current()->UseJitCompilation());
6854 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006855 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006856 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006857 }
6858 return desired_string_load_kind;
6859}
6860
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006861void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006862 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006863 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006864 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006865 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006866 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006867 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006868 locations->SetInAt(0, Location::RequiresRegister());
6869 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006870 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006871 locations->SetOut(Location::RegisterLocation(EAX));
6872 } else {
6873 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006874 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6875 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006876 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006877 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006878 } else {
6879 // For non-Baker read barrier we have a temp-clobbering call.
6880 }
6881 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006882 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006883}
6884
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006885Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006886 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006887 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006888 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006889 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006890 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006891 PatchInfo<Label>* info = &jit_string_patches_.back();
6892 return &info->label;
6893}
6894
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006895// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6896// move.
6897void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006898 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006899 Location out_loc = locations->Out();
6900 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006901
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006902 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006903 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006904 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6905 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006906 Register method_address = locations->InAt(0).AsRegister<Register>();
6907 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006908 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006909 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006910 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006911 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006912 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6913 Register method_address = locations->InAt(0).AsRegister<Register>();
6914 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006915 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6916 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006917 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006918 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006919 case HLoadString::LoadKind::kBssEntry: {
6920 Register method_address = locations->InAt(0).AsRegister<Register>();
6921 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6922 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006923 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006924 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006925 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006926 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006927 codegen_->AddSlowPath(slow_path);
6928 __ testl(out, out);
6929 __ j(kEqual, slow_path->GetEntryLabel());
6930 __ Bind(slow_path->GetExitLabel());
6931 return;
6932 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006933 case HLoadString::LoadKind::kJitBootImageAddress: {
6934 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6935 DCHECK_NE(address, 0u);
6936 __ movl(out, Immediate(address));
6937 return;
6938 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006939 case HLoadString::LoadKind::kJitTableAddress: {
6940 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6941 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006942 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006943 // /* GcRoot<mirror::String> */ out = *address
6944 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6945 return;
6946 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006947 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006948 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006949 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006950
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006951 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006952 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006953 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006954 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006955 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6956 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006957}
6958
David Brazdilcb1c0552015-08-04 16:22:25 +01006959static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006960 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006961}
6962
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006963void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6964 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006965 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006966 locations->SetOut(Location::RequiresRegister());
6967}
6968
6969void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006970 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6971}
6972
6973void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006974 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006975}
6976
6977void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6978 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006979}
6980
6981void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006982 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6983 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006984 InvokeRuntimeCallingConvention calling_convention;
6985 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6986}
6987
6988void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006989 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006990 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006991}
6992
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006993// Temp is used for read barrier.
6994static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6995 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006996 !kUseBakerReadBarrier &&
6997 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006998 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006999 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7000 return 1;
7001 }
7002 return 0;
7003}
7004
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007005// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7006// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007007// The other checks have one temp for loading the object's class.
7008static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007009 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007010 return 2;
7011 }
7012 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007013}
7014
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007015void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007016 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007017 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007018 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007019 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007020 case TypeCheckKind::kExactCheck:
7021 case TypeCheckKind::kAbstractClassCheck:
7022 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007023 case TypeCheckKind::kArrayObjectCheck: {
7024 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7025 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7026 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007027 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007028 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007029 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007030 case TypeCheckKind::kUnresolvedCheck:
7031 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007032 call_kind = LocationSummary::kCallOnSlowPath;
7033 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007034 case TypeCheckKind::kBitstringCheck:
7035 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007036 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007037
Vladimir Markoca6fff82017-10-03 14:49:14 +01007038 LocationSummary* locations =
7039 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007040 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007041 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007042 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007043 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007044 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7045 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7046 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7047 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7048 } else {
7049 locations->SetInAt(1, Location::Any());
7050 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007051 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7052 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007053 // When read barriers are enabled, we need a temporary register for some cases.
7054 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007055}
7056
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007057void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007058 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007059 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007060 Location obj_loc = locations->InAt(0);
7061 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007062 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007063 Location out_loc = locations->Out();
7064 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007065 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7066 DCHECK_LE(num_temps, 1u);
7067 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007068 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007069 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7070 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7071 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007072 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007073 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007074
7075 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007076 // Avoid null check if we know obj is not null.
7077 if (instruction->MustDoNullCheck()) {
7078 __ testl(obj, obj);
7079 __ j(kEqual, &zero);
7080 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007081
Roland Levillain7c1559a2015-12-15 10:55:36 +00007082 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007083 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007084 ReadBarrierOption read_barrier_option =
7085 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007086 // /* HeapReference<Class> */ out = obj->klass_
7087 GenerateReferenceLoadTwoRegisters(instruction,
7088 out_loc,
7089 obj_loc,
7090 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007091 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007092 if (cls.IsRegister()) {
7093 __ cmpl(out, cls.AsRegister<Register>());
7094 } else {
7095 DCHECK(cls.IsStackSlot()) << cls;
7096 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7097 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007098
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007099 // Classes must be equal for the instanceof to succeed.
7100 __ j(kNotEqual, &zero);
7101 __ movl(out, Immediate(1));
7102 __ jmp(&done);
7103 break;
7104 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007105
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007106 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007107 ReadBarrierOption read_barrier_option =
7108 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007109 // /* HeapReference<Class> */ out = obj->klass_
7110 GenerateReferenceLoadTwoRegisters(instruction,
7111 out_loc,
7112 obj_loc,
7113 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007114 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007115 // If the class is abstract, we eagerly fetch the super class of the
7116 // object to avoid doing a comparison we know will fail.
7117 NearLabel loop;
7118 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007119 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007120 GenerateReferenceLoadOneRegister(instruction,
7121 out_loc,
7122 super_offset,
7123 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007124 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007125 __ testl(out, out);
7126 // If `out` is null, we use it for the result, and jump to `done`.
7127 __ j(kEqual, &done);
7128 if (cls.IsRegister()) {
7129 __ cmpl(out, cls.AsRegister<Register>());
7130 } else {
7131 DCHECK(cls.IsStackSlot()) << cls;
7132 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7133 }
7134 __ j(kNotEqual, &loop);
7135 __ movl(out, Immediate(1));
7136 if (zero.IsLinked()) {
7137 __ jmp(&done);
7138 }
7139 break;
7140 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007142 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007143 ReadBarrierOption read_barrier_option =
7144 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007145 // /* HeapReference<Class> */ out = obj->klass_
7146 GenerateReferenceLoadTwoRegisters(instruction,
7147 out_loc,
7148 obj_loc,
7149 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007150 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007151 // Walk over the class hierarchy to find a match.
7152 NearLabel loop, success;
7153 __ Bind(&loop);
7154 if (cls.IsRegister()) {
7155 __ cmpl(out, cls.AsRegister<Register>());
7156 } else {
7157 DCHECK(cls.IsStackSlot()) << cls;
7158 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7159 }
7160 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007161 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007162 GenerateReferenceLoadOneRegister(instruction,
7163 out_loc,
7164 super_offset,
7165 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007166 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007167 __ testl(out, out);
7168 __ j(kNotEqual, &loop);
7169 // If `out` is null, we use it for the result, and jump to `done`.
7170 __ jmp(&done);
7171 __ Bind(&success);
7172 __ movl(out, Immediate(1));
7173 if (zero.IsLinked()) {
7174 __ jmp(&done);
7175 }
7176 break;
7177 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007178
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007179 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007180 ReadBarrierOption read_barrier_option =
7181 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007182 // /* HeapReference<Class> */ out = obj->klass_
7183 GenerateReferenceLoadTwoRegisters(instruction,
7184 out_loc,
7185 obj_loc,
7186 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007187 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007188 // Do an exact check.
7189 NearLabel exact_check;
7190 if (cls.IsRegister()) {
7191 __ cmpl(out, cls.AsRegister<Register>());
7192 } else {
7193 DCHECK(cls.IsStackSlot()) << cls;
7194 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7195 }
7196 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007197 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007198 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007199 GenerateReferenceLoadOneRegister(instruction,
7200 out_loc,
7201 component_offset,
7202 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007203 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007204 __ testl(out, out);
7205 // If `out` is null, we use it for the result, and jump to `done`.
7206 __ j(kEqual, &done);
7207 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7208 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007209 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007210 __ movl(out, Immediate(1));
7211 __ jmp(&done);
7212 break;
7213 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007214
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007215 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007216 // No read barrier since the slow path will retry upon failure.
7217 // /* HeapReference<Class> */ out = obj->klass_
7218 GenerateReferenceLoadTwoRegisters(instruction,
7219 out_loc,
7220 obj_loc,
7221 class_offset,
7222 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007223 if (cls.IsRegister()) {
7224 __ cmpl(out, cls.AsRegister<Register>());
7225 } else {
7226 DCHECK(cls.IsStackSlot()) << cls;
7227 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7228 }
7229 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007230 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007231 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 codegen_->AddSlowPath(slow_path);
7233 __ j(kNotEqual, slow_path->GetEntryLabel());
7234 __ movl(out, Immediate(1));
7235 if (zero.IsLinked()) {
7236 __ jmp(&done);
7237 }
7238 break;
7239 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007240
Calin Juravle98893e12015-10-02 21:05:03 +01007241 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007242 case TypeCheckKind::kInterfaceCheck: {
7243 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007244 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007245 // cases.
7246 //
7247 // We cannot directly call the InstanceofNonTrivial runtime
7248 // entry point without resorting to a type checking slow path
7249 // here (i.e. by calling InvokeRuntime directly), as it would
7250 // require to assign fixed registers for the inputs of this
7251 // HInstanceOf instruction (following the runtime calling
7252 // convention), which might be cluttered by the potential first
7253 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007254 //
7255 // TODO: Introduce a new runtime entry point taking the object
7256 // to test (instead of its class) as argument, and let it deal
7257 // with the read barrier issues. This will let us refactor this
7258 // case of the `switch` code as it was previously (with a direct
7259 // call to the runtime not using a type checking slow path).
7260 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007261 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007262 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007263 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007264 codegen_->AddSlowPath(slow_path);
7265 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007266 if (zero.IsLinked()) {
7267 __ jmp(&done);
7268 }
7269 break;
7270 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007271
7272 case TypeCheckKind::kBitstringCheck: {
7273 // /* HeapReference<Class> */ temp = obj->klass_
7274 GenerateReferenceLoadTwoRegisters(instruction,
7275 out_loc,
7276 obj_loc,
7277 class_offset,
7278 kWithoutReadBarrier);
7279
7280 GenerateBitstringTypeCheckCompare(instruction, out);
7281 __ j(kNotEqual, &zero);
7282 __ movl(out, Immediate(1));
7283 __ jmp(&done);
7284 break;
7285 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007286 }
7287
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007288 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007289 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007290 __ xorl(out, out);
7291 }
7292
7293 if (done.IsLinked()) {
7294 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007295 }
7296
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007297 if (slow_path != nullptr) {
7298 __ Bind(slow_path->GetExitLabel());
7299 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007300}
7301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007302void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007303 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007304 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007305 LocationSummary* locations =
7306 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007307 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007308 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7309 // Require a register for the interface check since there is a loop that compares the class to
7310 // a memory address.
7311 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007312 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7313 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7314 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7315 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007316 } else {
7317 locations->SetInAt(1, Location::Any());
7318 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007319 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007320 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7321}
7322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007323void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007324 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007325 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007326 Location obj_loc = locations->InAt(0);
7327 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007328 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 Location temp_loc = locations->GetTemp(0);
7330 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007331 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7332 DCHECK_GE(num_temps, 1u);
7333 DCHECK_LE(num_temps, 2u);
7334 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7335 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7336 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7337 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7338 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7339 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7340 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7341 const uint32_t object_array_data_offset =
7342 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007343
Vladimir Marko87584542017-12-12 17:47:52 +00007344 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007345 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007346 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7347 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007349
Roland Levillain0d5a2812015-11-13 10:07:31 +00007350 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007351 // Avoid null check if we know obj is not null.
7352 if (instruction->MustDoNullCheck()) {
7353 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007354 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007355 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007356
Roland Levillain0d5a2812015-11-13 10:07:31 +00007357 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007358 case TypeCheckKind::kExactCheck:
7359 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007360 // /* HeapReference<Class> */ temp = obj->klass_
7361 GenerateReferenceLoadTwoRegisters(instruction,
7362 temp_loc,
7363 obj_loc,
7364 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007365 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007367 if (cls.IsRegister()) {
7368 __ cmpl(temp, cls.AsRegister<Register>());
7369 } else {
7370 DCHECK(cls.IsStackSlot()) << cls;
7371 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7372 }
7373 // Jump to slow path for throwing the exception or doing a
7374 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007375 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007376 break;
7377 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007378
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007379 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007380 // /* HeapReference<Class> */ temp = obj->klass_
7381 GenerateReferenceLoadTwoRegisters(instruction,
7382 temp_loc,
7383 obj_loc,
7384 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007385 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007387 // If the class is abstract, we eagerly fetch the super class of the
7388 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007389 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007390 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007391 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007392 GenerateReferenceLoadOneRegister(instruction,
7393 temp_loc,
7394 super_offset,
7395 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007396 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007397
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007398 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7399 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007400 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007401 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007402
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007403 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007404 if (cls.IsRegister()) {
7405 __ cmpl(temp, cls.AsRegister<Register>());
7406 } else {
7407 DCHECK(cls.IsStackSlot()) << cls;
7408 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7409 }
7410 __ j(kNotEqual, &loop);
7411 break;
7412 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007413
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007414 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007415 // /* HeapReference<Class> */ temp = obj->klass_
7416 GenerateReferenceLoadTwoRegisters(instruction,
7417 temp_loc,
7418 obj_loc,
7419 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007420 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007421
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007422 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007423 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007424 __ Bind(&loop);
7425 if (cls.IsRegister()) {
7426 __ cmpl(temp, cls.AsRegister<Register>());
7427 } else {
7428 DCHECK(cls.IsStackSlot()) << cls;
7429 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7430 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007431 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007432
Roland Levillain0d5a2812015-11-13 10:07:31 +00007433 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007434 GenerateReferenceLoadOneRegister(instruction,
7435 temp_loc,
7436 super_offset,
7437 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007438 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007439
7440 // If the class reference currently in `temp` is not null, jump
7441 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007442 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007443 __ j(kNotZero, &loop);
7444 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007445 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007446 break;
7447 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007449 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007450 // /* HeapReference<Class> */ temp = obj->klass_
7451 GenerateReferenceLoadTwoRegisters(instruction,
7452 temp_loc,
7453 obj_loc,
7454 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007455 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007456
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007457 // Do an exact check.
7458 if (cls.IsRegister()) {
7459 __ cmpl(temp, cls.AsRegister<Register>());
7460 } else {
7461 DCHECK(cls.IsStackSlot()) << cls;
7462 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7463 }
7464 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007465
7466 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007467 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007468 GenerateReferenceLoadOneRegister(instruction,
7469 temp_loc,
7470 component_offset,
7471 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007472 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007473
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007474 // If the component type is null (i.e. the object not an array), jump to the slow path to
7475 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007476 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007477 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007478
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007479 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007480 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007481 break;
7482 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007483
Calin Juravle98893e12015-10-02 21:05:03 +01007484 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007485 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007486 // We cannot directly call the CheckCast runtime entry point
7487 // without resorting to a type checking slow path here (i.e. by
7488 // calling InvokeRuntime directly), as it would require to
7489 // assign fixed registers for the inputs of this HInstanceOf
7490 // instruction (following the runtime calling convention), which
7491 // might be cluttered by the potential first read barrier
7492 // emission at the beginning of this method.
7493 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007494 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007495
7496 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007497 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7498 // We can not get false positives by doing this.
7499 // /* HeapReference<Class> */ temp = obj->klass_
7500 GenerateReferenceLoadTwoRegisters(instruction,
7501 temp_loc,
7502 obj_loc,
7503 class_offset,
7504 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007505
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007506 // /* HeapReference<Class> */ temp = temp->iftable_
7507 GenerateReferenceLoadTwoRegisters(instruction,
7508 temp_loc,
7509 temp_loc,
7510 iftable_offset,
7511 kWithoutReadBarrier);
7512 // Iftable is never null.
7513 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7514 // Maybe poison the `cls` for direct comparison with memory.
7515 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7516 // Loop through the iftable and check if any class matches.
7517 NearLabel start_loop;
7518 __ Bind(&start_loop);
7519 // Need to subtract first to handle the empty array case.
7520 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7521 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7522 // Go to next interface if the classes do not match.
7523 __ cmpl(cls.AsRegister<Register>(),
7524 CodeGeneratorX86::ArrayAddress(temp,
7525 maybe_temp2_loc,
7526 TIMES_4,
7527 object_array_data_offset));
7528 __ j(kNotEqual, &start_loop);
7529 // If `cls` was poisoned above, unpoison it.
7530 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007531 break;
7532 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007533
7534 case TypeCheckKind::kBitstringCheck: {
7535 // /* HeapReference<Class> */ temp = obj->klass_
7536 GenerateReferenceLoadTwoRegisters(instruction,
7537 temp_loc,
7538 obj_loc,
7539 class_offset,
7540 kWithoutReadBarrier);
7541
7542 GenerateBitstringTypeCheckCompare(instruction, temp);
7543 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7544 break;
7545 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007546 }
7547 __ Bind(&done);
7548
Roland Levillain0d5a2812015-11-13 10:07:31 +00007549 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007550}
7551
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007552void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007553 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7554 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007555 InvokeRuntimeCallingConvention calling_convention;
7556 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7557}
7558
7559void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007560 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7561 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007562 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007563 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007564 if (instruction->IsEnter()) {
7565 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7566 } else {
7567 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7568 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007569}
7570
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307571void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7572 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7573 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7574 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7575 locations->SetInAt(0, Location::RequiresRegister());
7576 locations->SetInAt(1, Location::RequiresRegister());
7577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7578}
7579
7580void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7581 LocationSummary* locations = instruction->GetLocations();
7582 Location first = locations->InAt(0);
7583 Location second = locations->InAt(1);
7584 Location dest = locations->Out();
7585 if (instruction->GetResultType() == DataType::Type::kInt32) {
7586 __ andn(dest.AsRegister<Register>(),
7587 first.AsRegister<Register>(),
7588 second.AsRegister<Register>());
7589 } else {
7590 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7591 __ andn(dest.AsRegisterPairLow<Register>(),
7592 first.AsRegisterPairLow<Register>(),
7593 second.AsRegisterPairLow<Register>());
7594 __ andn(dest.AsRegisterPairHigh<Register>(),
7595 first.AsRegisterPairHigh<Register>(),
7596 second.AsRegisterPairHigh<Register>());
7597 }
7598}
7599
7600void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7601 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7602 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7603 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7604 locations->SetInAt(0, Location::RequiresRegister());
7605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7606}
7607
7608void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7609 HX86MaskOrResetLeastSetBit* instruction) {
7610 LocationSummary* locations = instruction->GetLocations();
7611 Location src = locations->InAt(0);
7612 Location dest = locations->Out();
7613 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7614 switch (instruction->GetOpKind()) {
7615 case HInstruction::kAnd:
7616 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7617 break;
7618 case HInstruction::kXor:
7619 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7620 break;
7621 default:
7622 LOG(FATAL) << "Unreachable";
7623 }
7624}
7625
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007626void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7627void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7628void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7629
7630void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7631 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007632 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007633 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7634 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007635 locations->SetInAt(0, Location::RequiresRegister());
7636 locations->SetInAt(1, Location::Any());
7637 locations->SetOut(Location::SameAsFirstInput());
7638}
7639
7640void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7641 HandleBitwiseOperation(instruction);
7642}
7643
7644void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7645 HandleBitwiseOperation(instruction);
7646}
7647
7648void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7649 HandleBitwiseOperation(instruction);
7650}
7651
7652void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7653 LocationSummary* locations = instruction->GetLocations();
7654 Location first = locations->InAt(0);
7655 Location second = locations->InAt(1);
7656 DCHECK(first.Equals(locations->Out()));
7657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007658 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007659 if (second.IsRegister()) {
7660 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007661 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007662 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007663 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007664 } else {
7665 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007666 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007667 }
7668 } else if (second.IsConstant()) {
7669 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007670 __ andl(first.AsRegister<Register>(),
7671 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007672 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007673 __ orl(first.AsRegister<Register>(),
7674 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007675 } else {
7676 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007677 __ xorl(first.AsRegister<Register>(),
7678 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007679 }
7680 } else {
7681 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007682 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007683 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007684 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007685 } else {
7686 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007687 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007688 }
7689 }
7690 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007691 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007692 if (second.IsRegisterPair()) {
7693 if (instruction->IsAnd()) {
7694 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7695 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7696 } else if (instruction->IsOr()) {
7697 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7698 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7699 } else {
7700 DCHECK(instruction->IsXor());
7701 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7702 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7703 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007704 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007705 if (instruction->IsAnd()) {
7706 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7707 __ andl(first.AsRegisterPairHigh<Register>(),
7708 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7709 } else if (instruction->IsOr()) {
7710 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7711 __ orl(first.AsRegisterPairHigh<Register>(),
7712 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7713 } else {
7714 DCHECK(instruction->IsXor());
7715 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7716 __ xorl(first.AsRegisterPairHigh<Register>(),
7717 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7718 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007719 } else {
7720 DCHECK(second.IsConstant()) << second;
7721 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007722 int32_t low_value = Low32Bits(value);
7723 int32_t high_value = High32Bits(value);
7724 Immediate low(low_value);
7725 Immediate high(high_value);
7726 Register first_low = first.AsRegisterPairLow<Register>();
7727 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007728 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007729 if (low_value == 0) {
7730 __ xorl(first_low, first_low);
7731 } else if (low_value != -1) {
7732 __ andl(first_low, low);
7733 }
7734 if (high_value == 0) {
7735 __ xorl(first_high, first_high);
7736 } else if (high_value != -1) {
7737 __ andl(first_high, high);
7738 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007739 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007740 if (low_value != 0) {
7741 __ orl(first_low, low);
7742 }
7743 if (high_value != 0) {
7744 __ orl(first_high, high);
7745 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007746 } else {
7747 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007748 if (low_value != 0) {
7749 __ xorl(first_low, low);
7750 }
7751 if (high_value != 0) {
7752 __ xorl(first_high, high);
7753 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007754 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007755 }
7756 }
7757}
7758
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007759void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7760 HInstruction* instruction,
7761 Location out,
7762 uint32_t offset,
7763 Location maybe_temp,
7764 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007765 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007766 if (read_barrier_option == kWithReadBarrier) {
7767 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007768 if (kUseBakerReadBarrier) {
7769 // Load with fast path based Baker's read barrier.
7770 // /* HeapReference<Object> */ out = *(out + offset)
7771 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007772 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007773 } else {
7774 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007775 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007776 // in the following move operation, as we will need it for the
7777 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007778 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007779 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007780 // /* HeapReference<Object> */ out = *(out + offset)
7781 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007782 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007783 }
7784 } else {
7785 // Plain load with no read barrier.
7786 // /* HeapReference<Object> */ out = *(out + offset)
7787 __ movl(out_reg, Address(out_reg, offset));
7788 __ MaybeUnpoisonHeapReference(out_reg);
7789 }
7790}
7791
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007792void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7793 HInstruction* instruction,
7794 Location out,
7795 Location obj,
7796 uint32_t offset,
7797 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007798 Register out_reg = out.AsRegister<Register>();
7799 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007800 if (read_barrier_option == kWithReadBarrier) {
7801 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007802 if (kUseBakerReadBarrier) {
7803 // Load with fast path based Baker's read barrier.
7804 // /* HeapReference<Object> */ out = *(obj + offset)
7805 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007806 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007807 } else {
7808 // Load with slow path based read barrier.
7809 // /* HeapReference<Object> */ out = *(obj + offset)
7810 __ movl(out_reg, Address(obj_reg, offset));
7811 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7812 }
7813 } else {
7814 // Plain load with no read barrier.
7815 // /* HeapReference<Object> */ out = *(obj + offset)
7816 __ movl(out_reg, Address(obj_reg, offset));
7817 __ MaybeUnpoisonHeapReference(out_reg);
7818 }
7819}
7820
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007821void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7822 HInstruction* instruction,
7823 Location root,
7824 const Address& address,
7825 Label* fixup_label,
7826 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007827 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007828 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007829 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007830 if (kUseBakerReadBarrier) {
7831 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7832 // Baker's read barrier are used:
7833 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007834 // root = obj.field;
7835 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7836 // if (temp != null) {
7837 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007838 // }
7839
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007840 // /* GcRoot<mirror::Object> */ root = *address
7841 __ movl(root_reg, address);
7842 if (fixup_label != nullptr) {
7843 __ Bind(fixup_label);
7844 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007845 static_assert(
7846 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7847 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7848 "have different sizes.");
7849 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7850 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7851 "have different sizes.");
7852
Vladimir Marko953437b2016-08-24 08:30:46 +00007853 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007854 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007855 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007856 codegen_->AddSlowPath(slow_path);
7857
Roland Levillaind966ce72017-02-09 16:20:14 +00007858 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7859 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007860 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007861 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7862 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007863 __ j(kNotEqual, slow_path->GetEntryLabel());
7864 __ Bind(slow_path->GetExitLabel());
7865 } else {
7866 // GC root loaded through a slow path for read barriers other
7867 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007868 // /* GcRoot<mirror::Object>* */ root = address
7869 __ leal(root_reg, address);
7870 if (fixup_label != nullptr) {
7871 __ Bind(fixup_label);
7872 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007873 // /* mirror::Object* */ root = root->Read()
7874 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7875 }
7876 } else {
7877 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007878 // /* GcRoot<mirror::Object> */ root = *address
7879 __ movl(root_reg, address);
7880 if (fixup_label != nullptr) {
7881 __ Bind(fixup_label);
7882 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007883 // Note that GC roots are not affected by heap poisoning, thus we
7884 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007885 }
7886}
7887
7888void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7889 Location ref,
7890 Register obj,
7891 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007892 bool needs_null_check) {
7893 DCHECK(kEmitCompilerReadBarrier);
7894 DCHECK(kUseBakerReadBarrier);
7895
7896 // /* HeapReference<Object> */ ref = *(obj + offset)
7897 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007898 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007899}
7900
7901void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7902 Location ref,
7903 Register obj,
7904 uint32_t data_offset,
7905 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007906 bool needs_null_check) {
7907 DCHECK(kEmitCompilerReadBarrier);
7908 DCHECK(kUseBakerReadBarrier);
7909
Roland Levillain3d312422016-06-23 13:53:42 +01007910 static_assert(
7911 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7912 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007913 // /* HeapReference<Object> */ ref =
7914 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007915 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007916 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007917}
7918
7919void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7920 Location ref,
7921 Register obj,
7922 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007923 bool needs_null_check,
7924 bool always_update_field,
7925 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007926 DCHECK(kEmitCompilerReadBarrier);
7927 DCHECK(kUseBakerReadBarrier);
7928
7929 // In slow path based read barriers, the read barrier call is
7930 // inserted after the original load. However, in fast path based
7931 // Baker's read barriers, we need to perform the load of
7932 // mirror::Object::monitor_ *before* the original reference load.
7933 // This load-load ordering is required by the read barrier.
7934 // The fast path/slow path (for Baker's algorithm) should look like:
7935 //
7936 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7937 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7938 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007939 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007940 // if (is_gray) {
7941 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7942 // }
7943 //
7944 // Note: the original implementation in ReadBarrier::Barrier is
7945 // slightly more complex as:
7946 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007947 // the high-bits of rb_state, which are expected to be all zeroes
7948 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7949 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007950 // - it performs additional checks that we do not do here for
7951 // performance reasons.
7952
7953 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007954 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7955
Vladimir Marko953437b2016-08-24 08:30:46 +00007956 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007957 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007958 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007959 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7960 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7961 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7962
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007963 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007964 // ref = ReadBarrier::Mark(ref);
7965 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7966 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007967 if (needs_null_check) {
7968 MaybeRecordImplicitNullCheck(instruction);
7969 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007970
7971 // Load fence to prevent load-load reordering.
7972 // Note that this is a no-op, thanks to the x86 memory model.
7973 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7974
7975 // The actual reference load.
7976 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007977 __ movl(ref_reg, src); // Flags are unaffected.
7978
7979 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7980 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007981 SlowPathCode* slow_path;
7982 if (always_update_field) {
7983 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007984 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007985 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007986 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007987 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007988 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007989 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007990 AddSlowPath(slow_path);
7991
7992 // We have done the "if" of the gray bit check above, now branch based on the flags.
7993 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007994
7995 // Object* ref = ref_addr->AsMirrorPtr()
7996 __ MaybeUnpoisonHeapReference(ref_reg);
7997
Roland Levillain7c1559a2015-12-15 10:55:36 +00007998 __ Bind(slow_path->GetExitLabel());
7999}
8000
8001void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8002 Location out,
8003 Location ref,
8004 Location obj,
8005 uint32_t offset,
8006 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008007 DCHECK(kEmitCompilerReadBarrier);
8008
Roland Levillain7c1559a2015-12-15 10:55:36 +00008009 // Insert a slow path based read barrier *after* the reference load.
8010 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008011 // If heap poisoning is enabled, the unpoisoning of the loaded
8012 // reference will be carried out by the runtime within the slow
8013 // path.
8014 //
8015 // Note that `ref` currently does not get unpoisoned (when heap
8016 // poisoning is enabled), which is alright as the `ref` argument is
8017 // not used by the artReadBarrierSlow entry point.
8018 //
8019 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008020 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008021 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8022 AddSlowPath(slow_path);
8023
Roland Levillain0d5a2812015-11-13 10:07:31 +00008024 __ jmp(slow_path->GetEntryLabel());
8025 __ Bind(slow_path->GetExitLabel());
8026}
8027
Roland Levillain7c1559a2015-12-15 10:55:36 +00008028void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8029 Location out,
8030 Location ref,
8031 Location obj,
8032 uint32_t offset,
8033 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008034 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008035 // Baker's read barriers shall be handled by the fast path
8036 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8037 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008038 // If heap poisoning is enabled, unpoisoning will be taken care of
8039 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008040 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008041 } else if (kPoisonHeapReferences) {
8042 __ UnpoisonHeapReference(out.AsRegister<Register>());
8043 }
8044}
8045
Roland Levillain7c1559a2015-12-15 10:55:36 +00008046void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8047 Location out,
8048 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008049 DCHECK(kEmitCompilerReadBarrier);
8050
Roland Levillain7c1559a2015-12-15 10:55:36 +00008051 // Insert a slow path based read barrier *after* the GC root load.
8052 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008053 // Note that GC roots are not affected by heap poisoning, so we do
8054 // not need to do anything special for this here.
8055 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008056 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008057 AddSlowPath(slow_path);
8058
Roland Levillain0d5a2812015-11-13 10:07:31 +00008059 __ jmp(slow_path->GetEntryLabel());
8060 __ Bind(slow_path->GetExitLabel());
8061}
8062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008063void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008064 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008065 LOG(FATAL) << "Unreachable";
8066}
8067
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008068void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008069 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008070 LOG(FATAL) << "Unreachable";
8071}
8072
Mark Mendellfe57faa2015-09-18 09:26:15 -04008073// Simple implementation of packed switch - generate cascaded compare/jumps.
8074void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8075 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008076 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008077 locations->SetInAt(0, Location::RequiresRegister());
8078}
8079
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008080void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8081 int32_t lower_bound,
8082 uint32_t num_entries,
8083 HBasicBlock* switch_block,
8084 HBasicBlock* default_block) {
8085 // Figure out the correct compare values and jump conditions.
8086 // Handle the first compare/branch as a special case because it might
8087 // jump to the default case.
8088 DCHECK_GT(num_entries, 2u);
8089 Condition first_condition;
8090 uint32_t index;
8091 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8092 if (lower_bound != 0) {
8093 first_condition = kLess;
8094 __ cmpl(value_reg, Immediate(lower_bound));
8095 __ j(first_condition, codegen_->GetLabelOf(default_block));
8096 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008097
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008098 index = 1;
8099 } else {
8100 // Handle all the compare/jumps below.
8101 first_condition = kBelow;
8102 index = 0;
8103 }
8104
8105 // Handle the rest of the compare/jumps.
8106 for (; index + 1 < num_entries; index += 2) {
8107 int32_t compare_to_value = lower_bound + index + 1;
8108 __ cmpl(value_reg, Immediate(compare_to_value));
8109 // Jump to successors[index] if value < case_value[index].
8110 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8111 // Jump to successors[index + 1] if value == case_value[index + 1].
8112 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8113 }
8114
8115 if (index != num_entries) {
8116 // There are an odd number of entries. Handle the last one.
8117 DCHECK_EQ(index + 1, num_entries);
8118 __ cmpl(value_reg, Immediate(lower_bound + index));
8119 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008120 }
8121
8122 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008123 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8124 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008125 }
8126}
8127
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008128void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8129 int32_t lower_bound = switch_instr->GetStartValue();
8130 uint32_t num_entries = switch_instr->GetNumEntries();
8131 LocationSummary* locations = switch_instr->GetLocations();
8132 Register value_reg = locations->InAt(0).AsRegister<Register>();
8133
8134 GenPackedSwitchWithCompares(value_reg,
8135 lower_bound,
8136 num_entries,
8137 switch_instr->GetBlock(),
8138 switch_instr->GetDefaultBlock());
8139}
8140
Mark Mendell805b3b52015-09-18 14:10:29 -04008141void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008143 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008144 locations->SetInAt(0, Location::RequiresRegister());
8145
8146 // Constant area pointer.
8147 locations->SetInAt(1, Location::RequiresRegister());
8148
8149 // And the temporary we need.
8150 locations->AddTemp(Location::RequiresRegister());
8151}
8152
8153void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8154 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008155 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008156 LocationSummary* locations = switch_instr->GetLocations();
8157 Register value_reg = locations->InAt(0).AsRegister<Register>();
8158 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8159
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008160 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8161 GenPackedSwitchWithCompares(value_reg,
8162 lower_bound,
8163 num_entries,
8164 switch_instr->GetBlock(),
8165 default_block);
8166 return;
8167 }
8168
Mark Mendell805b3b52015-09-18 14:10:29 -04008169 // Optimizing has a jump area.
8170 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8171 Register constant_area = locations->InAt(1).AsRegister<Register>();
8172
8173 // Remove the bias, if needed.
8174 if (lower_bound != 0) {
8175 __ leal(temp_reg, Address(value_reg, -lower_bound));
8176 value_reg = temp_reg;
8177 }
8178
8179 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008180 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008181 __ cmpl(value_reg, Immediate(num_entries - 1));
8182 __ j(kAbove, codegen_->GetLabelOf(default_block));
8183
8184 // We are in the range of the table.
8185 // Load (target-constant_area) from the jump table, indexing by the value.
8186 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8187
8188 // Compute the actual target address by adding in constant_area.
8189 __ addl(temp_reg, constant_area);
8190
8191 // And jump.
8192 __ jmp(temp_reg);
8193}
8194
Mark Mendell0616ae02015-04-17 12:49:27 -04008195void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8196 HX86ComputeBaseMethodAddress* insn) {
8197 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008198 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008199 locations->SetOut(Location::RequiresRegister());
8200}
8201
8202void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8203 HX86ComputeBaseMethodAddress* insn) {
8204 LocationSummary* locations = insn->GetLocations();
8205 Register reg = locations->Out().AsRegister<Register>();
8206
8207 // Generate call to next instruction.
8208 Label next_instruction;
8209 __ call(&next_instruction);
8210 __ Bind(&next_instruction);
8211
8212 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008213 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008214
8215 // Grab the return address off the stack.
8216 __ popl(reg);
8217}
8218
8219void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8220 HX86LoadFromConstantTable* insn) {
8221 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008222 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008223
8224 locations->SetInAt(0, Location::RequiresRegister());
8225 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8226
8227 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008228 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008229 return;
8230 }
8231
8232 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008233 case DataType::Type::kFloat32:
8234 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008235 locations->SetOut(Location::RequiresFpuRegister());
8236 break;
8237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008238 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008239 locations->SetOut(Location::RequiresRegister());
8240 break;
8241
8242 default:
8243 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8244 }
8245}
8246
8247void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008248 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008249 return;
8250 }
8251
8252 LocationSummary* locations = insn->GetLocations();
8253 Location out = locations->Out();
8254 Register const_area = locations->InAt(0).AsRegister<Register>();
8255 HConstant *value = insn->GetConstant();
8256
8257 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008258 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008259 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008260 codegen_->LiteralFloatAddress(
8261 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008262 break;
8263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008264 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008265 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008266 codegen_->LiteralDoubleAddress(
8267 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008268 break;
8269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008270 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008271 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008272 codegen_->LiteralInt32Address(
8273 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008274 break;
8275
8276 default:
8277 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8278 }
8279}
8280
Mark Mendell0616ae02015-04-17 12:49:27 -04008281/**
8282 * Class to handle late fixup of offsets into constant area.
8283 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008284class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008285 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008286 RIPFixup(CodeGeneratorX86& codegen,
8287 HX86ComputeBaseMethodAddress* base_method_address,
8288 size_t offset)
8289 : codegen_(&codegen),
8290 base_method_address_(base_method_address),
8291 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008292
8293 protected:
8294 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8295
8296 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008297 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008298
8299 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008300 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008301 // Patch the correct offset for the instruction. The place to patch is the
8302 // last 4 bytes of the instruction.
8303 // The value to patch is the distance from the offset in the constant area
8304 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008305 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008306 int32_t relative_position =
8307 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008308
8309 // Patch in the right value.
8310 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8311 }
8312
Mark Mendell0616ae02015-04-17 12:49:27 -04008313 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008314 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008315};
8316
Mark Mendell805b3b52015-09-18 14:10:29 -04008317/**
8318 * Class to handle late fixup of offsets to a jump table that will be created in the
8319 * constant area.
8320 */
8321class JumpTableRIPFixup : public RIPFixup {
8322 public:
8323 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008324 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8325 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008326
8327 void CreateJumpTable() {
8328 X86Assembler* assembler = codegen_->GetAssembler();
8329
8330 // Ensure that the reference to the jump table has the correct offset.
8331 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8332 SetOffset(offset_in_constant_table);
8333
8334 // The label values in the jump table are computed relative to the
8335 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008336 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008337
8338 // Populate the jump table with the correct values for the jump table.
8339 int32_t num_entries = switch_instr_->GetNumEntries();
8340 HBasicBlock* block = switch_instr_->GetBlock();
8341 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8342 // The value that we want is the target offset - the position of the table.
8343 for (int32_t i = 0; i < num_entries; i++) {
8344 HBasicBlock* b = successors[i];
8345 Label* l = codegen_->GetLabelOf(b);
8346 DCHECK(l->IsBound());
8347 int32_t offset_to_block = l->Position() - relative_offset;
8348 assembler->AppendInt32(offset_to_block);
8349 }
8350 }
8351
8352 private:
8353 const HX86PackedSwitch* switch_instr_;
8354};
8355
8356void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8357 // Generate the constant area if needed.
8358 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308359
Mark Mendell805b3b52015-09-18 14:10:29 -04008360 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8361 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8362 // byte values.
8363 assembler->Align(4, 0);
8364 constant_area_start_ = assembler->CodeSize();
8365
8366 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008367 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008368 jump_table->CreateJumpTable();
8369 }
8370
8371 // And now add the constant area to the generated code.
8372 assembler->AddConstantArea();
8373 }
8374
8375 // And finish up.
8376 CodeGenerator::Finalize(allocator);
8377}
8378
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008379Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8380 HX86ComputeBaseMethodAddress* method_base,
8381 Register reg) {
8382 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008383 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008384 return Address(reg, kDummy32BitOffset, fixup);
8385}
8386
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008387Address CodeGeneratorX86::LiteralFloatAddress(float v,
8388 HX86ComputeBaseMethodAddress* method_base,
8389 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008390 AssemblerFixup* fixup =
8391 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008392 return Address(reg, kDummy32BitOffset, fixup);
8393}
8394
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008395Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8396 HX86ComputeBaseMethodAddress* method_base,
8397 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008398 AssemblerFixup* fixup =
8399 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008400 return Address(reg, kDummy32BitOffset, fixup);
8401}
8402
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008403Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8404 HX86ComputeBaseMethodAddress* method_base,
8405 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008406 AssemblerFixup* fixup =
8407 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008408 return Address(reg, kDummy32BitOffset, fixup);
8409}
8410
Aart Bika19616e2016-02-01 18:57:58 -08008411void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8412 if (value == 0) {
8413 __ xorl(dest, dest);
8414 } else {
8415 __ movl(dest, Immediate(value));
8416 }
8417}
8418
8419void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8420 if (value == 0) {
8421 __ testl(dest, dest);
8422 } else {
8423 __ cmpl(dest, Immediate(value));
8424 }
8425}
8426
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008427void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8428 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008429 GenerateIntCompare(lhs_reg, rhs);
8430}
8431
8432void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008433 if (rhs.IsConstant()) {
8434 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008435 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008436 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008437 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008438 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008439 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008440 }
8441}
8442
8443Address CodeGeneratorX86::ArrayAddress(Register obj,
8444 Location index,
8445 ScaleFactor scale,
8446 uint32_t data_offset) {
8447 return index.IsConstant() ?
8448 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8449 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8450}
8451
Mark Mendell805b3b52015-09-18 14:10:29 -04008452Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8453 Register reg,
8454 Register value) {
8455 // Create a fixup to be used to create and address the jump table.
8456 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008457 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008458
8459 // We have to populate the jump tables.
8460 fixups_to_jump_tables_.push_back(table_fixup);
8461
8462 // We want a scaled address, as we are extracting the correct offset from the table.
8463 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8464}
8465
Andreas Gampe85b62f22015-09-09 13:15:38 -07008466// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008467void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008468 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008469 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008470 return;
8471 }
8472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008473 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008474
8475 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8476 if (target.Equals(return_loc)) {
8477 return;
8478 }
8479
8480 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8481 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008482 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008483 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008484 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8485 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008486 GetMoveResolver()->EmitNativeCode(&parallel_move);
8487 } else {
8488 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008489 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008490 parallel_move.AddMove(return_loc, target, type, nullptr);
8491 GetMoveResolver()->EmitNativeCode(&parallel_move);
8492 }
8493}
8494
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008495void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8496 const uint8_t* roots_data,
8497 const PatchInfo<Label>& info,
8498 uint64_t index_in_table) const {
8499 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8500 uintptr_t address =
8501 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008502 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008503 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8504 dchecked_integral_cast<uint32_t>(address);
8505}
8506
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008507void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8508 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008509 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008510 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008511 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008512 }
8513
8514 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008515 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008516 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008517 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008518 }
8519}
8520
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008521void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8522 ATTRIBUTE_UNUSED) {
8523 LOG(FATAL) << "Unreachable";
8524}
8525
8526void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8527 ATTRIBUTE_UNUSED) {
8528 LOG(FATAL) << "Unreachable";
8529}
8530
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308531bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8532 return codegen_->GetInstructionSetFeatures().HasAVX();
8533}
8534bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8535 return codegen_->GetInstructionSetFeatures().HasAVX2();
8536}
8537bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8538 return codegen_->GetInstructionSetFeatures().HasAVX();
8539}
8540bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8541 return codegen_->GetInstructionSetFeatures().HasAVX2();
8542}
8543
Roland Levillain4d027112015-07-01 15:41:14 +01008544#undef __
8545
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008546} // namespace x86
8547} // namespace art