blob: f02ab26be867cc96ec3df70f683d6d3ea94d7a52 [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 Geoffray57cacb72019-12-08 22:07:08 +00002215 switch (ret->InputAt(0)->GetType()) {
2216 case DataType::Type::kReference:
2217 case DataType::Type::kBool:
2218 case DataType::Type::kUint8:
2219 case DataType::Type::kInt8:
2220 case DataType::Type::kUint16:
2221 case DataType::Type::kInt16:
2222 case DataType::Type::kInt32:
2223 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2224 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002225
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002226 case DataType::Type::kInt64:
2227 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2228 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2229 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002230
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002231 case DataType::Type::kFloat32:
2232 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2233 if (GetGraph()->IsCompilingOsr()) {
2234 // To simplify callers of an OSR method, we put the return value in both
2235 // floating point and core registers.
2236 __ movd(EAX, XMM0);
2237 }
2238 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002239
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002240 case DataType::Type::kFloat64:
2241 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2242 if (GetGraph()->IsCompilingOsr()) {
2243 // To simplify callers of an OSR method, we put the return value in both
2244 // floating point and core registers.
2245 __ movd(EAX, XMM0);
2246 // Use XMM1 as temporary register to not clobber XMM0.
2247 __ movaps(XMM1, XMM0);
2248 __ psrlq(XMM1, Immediate(32));
2249 __ movd(EDX, XMM1);
2250 }
2251 break;
2252
2253 default:
2254 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002255 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002257}
2258
Calin Juravle175dc732015-08-25 15:42:32 +01002259void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2260 // The trampoline uses the same calling convention as dex calling conventions,
2261 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2262 // the method_idx.
2263 HandleInvoke(invoke);
2264}
2265
2266void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2267 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2268}
2269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002270void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002271 // Explicit clinit checks triggered by static invokes must have been pruned by
2272 // art::PrepareForRegisterAllocation.
2273 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002274
Mark Mendellfb8d2792015-03-31 22:16:59 -04002275 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002276 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002277 if (invoke->GetLocations()->CanCall() &&
2278 invoke->HasPcRelativeMethodLoadKind() &&
2279 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002280 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002281 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002282 return;
2283 }
2284
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002285 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002286
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002287 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002288 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002289 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002290 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002291}
2292
Mark Mendell09ed1a32015-03-25 08:30:06 -04002293static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2294 if (invoke->GetLocations()->Intrinsified()) {
2295 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2296 intrinsic.Dispatch(invoke);
2297 return true;
2298 }
2299 return false;
2300}
2301
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002302void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002303 // Explicit clinit checks triggered by static invokes must have been pruned by
2304 // art::PrepareForRegisterAllocation.
2305 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002306
Mark Mendell09ed1a32015-03-25 08:30:06 -04002307 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2308 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002309 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002310
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002311 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002312 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002313 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002314}
2315
2316void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002317 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2318 if (intrinsic.TryDispatch(invoke)) {
2319 return;
2320 }
2321
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002322 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002323
2324 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002325 // Add one temporary for inline cache update.
2326 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2327 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002328}
2329
2330void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002331 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002332 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002333}
2334
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002335void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002336 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2337 return;
2338 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002339
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002340 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002341 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002342}
2343
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002344void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002345 // This call to HandleInvoke allocates a temporary (core) register
2346 // which is also used to transfer the hidden argument from FP to
2347 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002348 HandleInvoke(invoke);
2349 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002350 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002351
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002352 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002353 // Add one temporary for inline cache update.
2354 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2355 }
2356}
2357
2358void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2359 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002360 // We know the destination of an intrinsic, so no need to record inline
2361 // caches (also the intrinsic location builder doesn't request an additional
2362 // temporary).
2363 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002364 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002365 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002366 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2367 ScopedObjectAccess soa(Thread::Current());
2368 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
2369 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2370 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2371 if (kIsDebugBuild) {
2372 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2373 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2374 }
2375 Register temp = EBP;
2376 NearLabel done;
2377 __ movl(temp, Immediate(address));
2378 // Fast path for a monomorphic cache.
2379 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2380 __ j(kEqual, &done);
2381 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2382 __ Bind(&done);
2383 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002384}
2385
2386void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2387 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002388 LocationSummary* locations = invoke->GetLocations();
2389 Register temp = locations->GetTemp(0).AsRegister<Register>();
2390 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002391 Location receiver = locations->InAt(0);
2392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2393
Roland Levillain0d5a2812015-11-13 10:07:31 +00002394 // Set the hidden argument. This is safe to do this here, as XMM7
2395 // won't be modified thereafter, before the `call` instruction.
2396 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002397 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002398 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002399
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002400 if (receiver.IsStackSlot()) {
2401 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002402 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403 __ movl(temp, Address(temp, class_offset));
2404 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002405 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002407 }
Roland Levillain4d027112015-07-01 15:41:14 +01002408 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002409 // Instead of simply (possibly) unpoisoning `temp` here, we should
2410 // emit a read barrier for the previous class reference load.
2411 // However this is not required in practice, as this is an
2412 // intermediate/temporary reference and because the current
2413 // concurrent copying collector keeps the from-space memory
2414 // intact/accessible until the end of the marking phase (the
2415 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002416 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002417
2418 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2419
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002420 // temp = temp->GetAddressOfIMT()
2421 __ movl(temp,
2422 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002423 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002424 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002425 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002426 __ movl(temp, Address(temp, method_offset));
2427 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002428 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002429 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002430
2431 DCHECK(!codegen_->IsLeafMethod());
2432 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2433}
2434
Orion Hodsonac141392017-01-13 11:53:47 +00002435void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2436 HandleInvoke(invoke);
2437}
2438
2439void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2440 codegen_->GenerateInvokePolymorphicCall(invoke);
2441}
2442
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002443void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2444 HandleInvoke(invoke);
2445}
2446
2447void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2448 codegen_->GenerateInvokeCustomCall(invoke);
2449}
2450
Roland Levillain88cb1752014-10-20 16:36:47 +01002451void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2452 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002453 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002454 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002455 case DataType::Type::kInt32:
2456 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002457 locations->SetInAt(0, Location::RequiresRegister());
2458 locations->SetOut(Location::SameAsFirstInput());
2459 break;
2460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002462 locations->SetInAt(0, Location::RequiresFpuRegister());
2463 locations->SetOut(Location::SameAsFirstInput());
2464 locations->AddTemp(Location::RequiresRegister());
2465 locations->AddTemp(Location::RequiresFpuRegister());
2466 break;
2467
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002469 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002470 locations->SetOut(Location::SameAsFirstInput());
2471 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002472 break;
2473
2474 default:
2475 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2476 }
2477}
2478
2479void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2480 LocationSummary* locations = neg->GetLocations();
2481 Location out = locations->Out();
2482 Location in = locations->InAt(0);
2483 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002484 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002485 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002486 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002487 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002488 break;
2489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002491 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002492 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002493 __ negl(out.AsRegisterPairLow<Register>());
2494 // Negation is similar to subtraction from zero. The least
2495 // significant byte triggers a borrow when it is different from
2496 // zero; to take it into account, add 1 to the most significant
2497 // byte if the carry flag (CF) is set to 1 after the first NEGL
2498 // operation.
2499 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2500 __ negl(out.AsRegisterPairHigh<Register>());
2501 break;
2502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002503 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002504 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 Register constant = locations->GetTemp(0).AsRegister<Register>();
2506 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002507 // Implement float negation with an exclusive or with value
2508 // 0x80000000 (mask for bit 31, representing the sign of a
2509 // single-precision floating-point number).
2510 __ movl(constant, Immediate(INT32_C(0x80000000)));
2511 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002513 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002514 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002517 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002519 // Implement double negation with an exclusive or with value
2520 // 0x8000000000000000 (mask for bit 63, representing the sign of
2521 // a double-precision floating-point number).
2522 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002524 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002525 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002526
2527 default:
2528 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2529 }
2530}
2531
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002532void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2533 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002534 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002536 locations->SetInAt(0, Location::RequiresFpuRegister());
2537 locations->SetInAt(1, Location::RequiresRegister());
2538 locations->SetOut(Location::SameAsFirstInput());
2539 locations->AddTemp(Location::RequiresFpuRegister());
2540}
2541
2542void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2543 LocationSummary* locations = neg->GetLocations();
2544 Location out = locations->Out();
2545 DCHECK(locations->InAt(0).Equals(out));
2546
2547 Register constant_area = locations->InAt(1).AsRegister<Register>();
2548 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002550 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2551 neg->GetBaseMethodAddress(),
2552 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002553 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2554 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002555 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2556 neg->GetBaseMethodAddress(),
2557 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002558 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2559 }
2560}
2561
Roland Levillaindff1f282014-11-05 14:15:05 +00002562void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 DataType::Type result_type = conversion->GetResultType();
2564 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002565 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2566 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002567
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002568 // The float-to-long and double-to-long type conversions rely on a
2569 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002570 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2572 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002573 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002574 : LocationSummary::kNoCall;
2575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002576 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002577
Roland Levillaindff1f282014-11-05 14:15:05 +00002578 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002581 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002582 case DataType::Type::kUint8:
2583 case DataType::Type::kInt8:
2584 case DataType::Type::kUint16:
2585 case DataType::Type::kInt16:
2586 case DataType::Type::kInt32:
2587 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2588 // Make the output overlap to please the register allocator. This greatly simplifies
2589 // the validation of the linear scan implementation
2590 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2591 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002593 HInstruction* input = conversion->InputAt(0);
2594 Location input_location = input->IsConstant()
2595 ? Location::ConstantLocation(input->AsConstant())
2596 : Location::RegisterPairLocation(EAX, EDX);
2597 locations->SetInAt(0, input_location);
2598 // Make the output overlap to please the register allocator. This greatly simplifies
2599 // the validation of the linear scan implementation
2600 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2601 break;
2602 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002603
2604 default:
2605 LOG(FATAL) << "Unexpected type conversion from " << input_type
2606 << " to " << result_type;
2607 }
2608 break;
2609
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002610 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002612 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2613 locations->SetInAt(0, Location::Any());
2614 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002615 break;
2616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002618 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002619 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002620 locations->SetInAt(0, Location::Any());
2621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2622 break;
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002625 locations->SetInAt(0, Location::RequiresFpuRegister());
2626 locations->SetOut(Location::RequiresRegister());
2627 locations->AddTemp(Location::RequiresFpuRegister());
2628 break;
2629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631 locations->SetInAt(0, Location::RequiresFpuRegister());
2632 locations->SetOut(Location::RequiresRegister());
2633 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002634 break;
2635
2636 default:
2637 LOG(FATAL) << "Unexpected type conversion from " << input_type
2638 << " to " << result_type;
2639 }
2640 break;
2641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002643 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002644 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002645 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002647 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 case DataType::Type::kInt16:
2649 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002650 locations->SetInAt(0, Location::RegisterLocation(EAX));
2651 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2652 break;
2653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kFloat32:
2655 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002656 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002657 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2658 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2659
Vladimir Marko949c91f2015-01-27 10:48:44 +00002660 // The runtime helper puts the result in EAX, EDX.
2661 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002662 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002663 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002664
2665 default:
2666 LOG(FATAL) << "Unexpected type conversion from " << input_type
2667 << " to " << result_type;
2668 }
2669 break;
2670
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002672 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002674 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002675 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002676 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 case DataType::Type::kInt16:
2678 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002679 locations->SetInAt(0, Location::RequiresRegister());
2680 locations->SetOut(Location::RequiresFpuRegister());
2681 break;
2682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002684 locations->SetInAt(0, Location::Any());
2685 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002686 break;
2687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002689 locations->SetInAt(0, Location::RequiresFpuRegister());
2690 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002691 break;
2692
2693 default:
2694 LOG(FATAL) << "Unexpected type conversion from " << input_type
2695 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002696 }
Roland Levillaincff13742014-11-17 14:32:17 +00002697 break;
2698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002700 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002702 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002704 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 case DataType::Type::kInt16:
2706 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002707 locations->SetInAt(0, Location::RequiresRegister());
2708 locations->SetOut(Location::RequiresFpuRegister());
2709 break;
2710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002712 locations->SetInAt(0, Location::Any());
2713 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002714 break;
2715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002717 locations->SetInAt(0, Location::RequiresFpuRegister());
2718 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002719 break;
2720
2721 default:
2722 LOG(FATAL) << "Unexpected type conversion from " << input_type
2723 << " to " << result_type;
2724 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002725 break;
2726
2727 default:
2728 LOG(FATAL) << "Unexpected type conversion from " << input_type
2729 << " to " << result_type;
2730 }
2731}
2732
2733void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2734 LocationSummary* locations = conversion->GetLocations();
2735 Location out = locations->Out();
2736 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002737 DataType::Type result_type = conversion->GetResultType();
2738 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002739 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2740 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002741 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002742 case DataType::Type::kUint8:
2743 switch (input_type) {
2744 case DataType::Type::kInt8:
2745 case DataType::Type::kUint16:
2746 case DataType::Type::kInt16:
2747 case DataType::Type::kInt32:
2748 if (in.IsRegister()) {
2749 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2750 } else {
2751 DCHECK(in.GetConstant()->IsIntConstant());
2752 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2753 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2754 }
2755 break;
2756 case DataType::Type::kInt64:
2757 if (in.IsRegisterPair()) {
2758 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2759 } else {
2760 DCHECK(in.GetConstant()->IsLongConstant());
2761 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2762 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2763 }
2764 break;
2765
2766 default:
2767 LOG(FATAL) << "Unexpected type conversion from " << input_type
2768 << " to " << result_type;
2769 }
2770 break;
2771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002772 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002773 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002774 case DataType::Type::kUint8:
2775 case DataType::Type::kUint16:
2776 case DataType::Type::kInt16:
2777 case DataType::Type::kInt32:
2778 if (in.IsRegister()) {
2779 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2780 } else {
2781 DCHECK(in.GetConstant()->IsIntConstant());
2782 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2783 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2784 }
2785 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002787 if (in.IsRegisterPair()) {
2788 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2789 } else {
2790 DCHECK(in.GetConstant()->IsLongConstant());
2791 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2792 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2793 }
2794 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002795
2796 default:
2797 LOG(FATAL) << "Unexpected type conversion from " << input_type
2798 << " to " << result_type;
2799 }
2800 break;
2801
2802 case DataType::Type::kUint16:
2803 switch (input_type) {
2804 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kInt16:
2806 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002807 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002808 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2809 } else if (in.IsStackSlot()) {
2810 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002811 } else {
2812 DCHECK(in.GetConstant()->IsIntConstant());
2813 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002814 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2815 }
2816 break;
2817 case DataType::Type::kInt64:
2818 if (in.IsRegisterPair()) {
2819 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2820 } else if (in.IsDoubleStackSlot()) {
2821 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2822 } else {
2823 DCHECK(in.GetConstant()->IsLongConstant());
2824 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2825 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002826 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002827 break;
2828
2829 default:
2830 LOG(FATAL) << "Unexpected type conversion from " << input_type
2831 << " to " << result_type;
2832 }
2833 break;
2834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002836 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002838 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002839 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002840 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002841 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002843 } else {
2844 DCHECK(in.GetConstant()->IsIntConstant());
2845 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002847 }
2848 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002849 case DataType::Type::kInt64:
2850 if (in.IsRegisterPair()) {
2851 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2852 } else if (in.IsDoubleStackSlot()) {
2853 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2854 } else {
2855 DCHECK(in.GetConstant()->IsLongConstant());
2856 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2857 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2858 }
2859 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002860
2861 default:
2862 LOG(FATAL) << "Unexpected type conversion from " << input_type
2863 << " to " << result_type;
2864 }
2865 break;
2866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002868 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002870 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002872 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002873 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002874 } else {
2875 DCHECK(in.IsConstant());
2876 DCHECK(in.GetConstant()->IsLongConstant());
2877 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002878 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002879 }
2880 break;
2881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002883 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2884 Register output = out.AsRegister<Register>();
2885 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002886 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002887
2888 __ movl(output, Immediate(kPrimIntMax));
2889 // temp = int-to-float(output)
2890 __ cvtsi2ss(temp, output);
2891 // if input >= temp goto done
2892 __ comiss(input, temp);
2893 __ j(kAboveEqual, &done);
2894 // if input == NaN goto nan
2895 __ j(kUnordered, &nan);
2896 // output = float-to-int-truncate(input)
2897 __ cvttss2si(output, input);
2898 __ jmp(&done);
2899 __ Bind(&nan);
2900 // output = 0
2901 __ xorl(output, output);
2902 __ Bind(&done);
2903 break;
2904 }
2905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002907 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2908 Register output = out.AsRegister<Register>();
2909 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002910 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002911
2912 __ movl(output, Immediate(kPrimIntMax));
2913 // temp = int-to-double(output)
2914 __ cvtsi2sd(temp, output);
2915 // if input >= temp goto done
2916 __ comisd(input, temp);
2917 __ j(kAboveEqual, &done);
2918 // if input == NaN goto nan
2919 __ j(kUnordered, &nan);
2920 // output = double-to-int-truncate(input)
2921 __ cvttsd2si(output, input);
2922 __ jmp(&done);
2923 __ Bind(&nan);
2924 // output = 0
2925 __ xorl(output, output);
2926 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002927 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002928 }
Roland Levillain946e1432014-11-11 17:35:19 +00002929
2930 default:
2931 LOG(FATAL) << "Unexpected type conversion from " << input_type
2932 << " to " << result_type;
2933 }
2934 break;
2935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002937 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002939 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002941 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kInt16:
2943 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002944 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2945 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002946 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002947 __ cdq();
2948 break;
2949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002951 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002952 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002953 break;
2954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002956 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002957 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002958 break;
2959
2960 default:
2961 LOG(FATAL) << "Unexpected type conversion from " << input_type
2962 << " to " << result_type;
2963 }
2964 break;
2965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002967 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002969 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002971 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002972 case DataType::Type::kInt16:
2973 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002975 break;
2976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002978 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002979
Roland Levillain232ade02015-04-20 15:14:36 +01002980 // Create stack space for the call to
2981 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2982 // TODO: enhance register allocator to ask for stack temporaries.
2983 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002985 __ subl(ESP, Immediate(adjustment));
2986 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002987
Roland Levillain232ade02015-04-20 15:14:36 +01002988 // Load the value to the FP stack, using temporaries if needed.
2989 PushOntoFPStack(in, 0, adjustment, false, true);
2990
2991 if (out.IsStackSlot()) {
2992 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2993 } else {
2994 __ fstps(Address(ESP, 0));
2995 Location stack_temp = Location::StackSlot(0);
2996 codegen_->Move32(out, stack_temp);
2997 }
2998
2999 // Remove the temporary stack space we allocated.
3000 if (adjustment != 0) {
3001 __ addl(ESP, Immediate(adjustment));
3002 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003003 break;
3004 }
3005
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003007 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003008 break;
3009
3010 default:
3011 LOG(FATAL) << "Unexpected type conversion from " << input_type
3012 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003013 }
Roland Levillaincff13742014-11-17 14:32:17 +00003014 break;
3015
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003017 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003019 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003021 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kInt16:
3023 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003025 break;
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003028 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003029
Roland Levillain232ade02015-04-20 15:14:36 +01003030 // Create stack space for the call to
3031 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3032 // TODO: enhance register allocator to ask for stack temporaries.
3033 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01003035 __ subl(ESP, Immediate(adjustment));
3036 }
3037
3038 // Load the value to the FP stack, using temporaries if needed.
3039 PushOntoFPStack(in, 0, adjustment, false, true);
3040
3041 if (out.IsDoubleStackSlot()) {
3042 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3043 } else {
3044 __ fstpl(Address(ESP, 0));
3045 Location stack_temp = Location::DoubleStackSlot(0);
3046 codegen_->Move64(out, stack_temp);
3047 }
3048
3049 // Remove the temporary stack space we allocated.
3050 if (adjustment != 0) {
3051 __ addl(ESP, Immediate(adjustment));
3052 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003053 break;
3054 }
3055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003057 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003058 break;
3059
3060 default:
3061 LOG(FATAL) << "Unexpected type conversion from " << input_type
3062 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003063 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003064 break;
3065
3066 default:
3067 LOG(FATAL) << "Unexpected type conversion from " << input_type
3068 << " to " << result_type;
3069 }
3070}
3071
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003072void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003073 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003074 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003075 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003077 locations->SetInAt(0, Location::RequiresRegister());
3078 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3080 break;
3081 }
3082
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003083 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003084 locations->SetInAt(0, Location::RequiresRegister());
3085 locations->SetInAt(1, Location::Any());
3086 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003087 break;
3088 }
3089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 case DataType::Type::kFloat32:
3091 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003092 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003093 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3094 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003095 } else if (add->InputAt(1)->IsConstant()) {
3096 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003097 } else {
3098 locations->SetInAt(1, Location::Any());
3099 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003100 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003101 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003102 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003103
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003104 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003105 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003106 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003107 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003108}
3109
3110void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3111 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003112 Location first = locations->InAt(0);
3113 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003114 Location out = locations->Out();
3115
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003116 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003118 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003119 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3120 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003121 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3122 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003123 } else {
3124 __ leal(out.AsRegister<Register>(), Address(
3125 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3126 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003127 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003128 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3129 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3130 __ addl(out.AsRegister<Register>(), Immediate(value));
3131 } else {
3132 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3133 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003134 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003135 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003136 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003137 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003138 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003139 }
3140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003142 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003143 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3144 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003145 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003146 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3147 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003148 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003149 } else {
3150 DCHECK(second.IsConstant()) << second;
3151 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3152 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3153 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003154 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003155 break;
3156 }
3157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003158 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003159 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003160 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003161 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3162 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003163 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003164 __ addss(first.AsFpuRegister<XmmRegister>(),
3165 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003166 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3167 const_area->GetBaseMethodAddress(),
3168 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003169 } else {
3170 DCHECK(second.IsStackSlot());
3171 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003172 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003173 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003174 }
3175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003176 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003177 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003179 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3180 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003181 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003182 __ addsd(first.AsFpuRegister<XmmRegister>(),
3183 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003184 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3185 const_area->GetBaseMethodAddress(),
3186 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003187 } else {
3188 DCHECK(second.IsDoubleStackSlot());
3189 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003190 }
3191 break;
3192 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003193
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003194 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003195 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003196 }
3197}
3198
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003199void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003200 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003201 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003202 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003203 case DataType::Type::kInt32:
3204 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003205 locations->SetInAt(0, Location::RequiresRegister());
3206 locations->SetInAt(1, Location::Any());
3207 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003208 break;
3209 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 case DataType::Type::kFloat32:
3211 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003212 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003213 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3214 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003215 } else if (sub->InputAt(1)->IsConstant()) {
3216 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003217 } else {
3218 locations->SetInAt(1, Location::Any());
3219 }
Calin Juravle11351682014-10-23 15:38:15 +01003220 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003221 break;
Calin Juravle11351682014-10-23 15:38:15 +01003222 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003223
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003224 default:
Calin Juravle11351682014-10-23 15:38:15 +01003225 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003226 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003227}
3228
3229void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3230 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003231 Location first = locations->InAt(0);
3232 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003233 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003234 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003235 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003236 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003238 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003239 __ subl(first.AsRegister<Register>(),
3240 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003241 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003243 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003244 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003245 }
3246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003248 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003249 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3250 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003251 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003252 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003253 __ sbbl(first.AsRegisterPairHigh<Register>(),
3254 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003255 } else {
3256 DCHECK(second.IsConstant()) << second;
3257 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3258 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3259 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003260 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003261 break;
3262 }
3263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003264 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003265 if (second.IsFpuRegister()) {
3266 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3267 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3268 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003269 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003270 __ subss(first.AsFpuRegister<XmmRegister>(),
3271 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003272 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3273 const_area->GetBaseMethodAddress(),
3274 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003275 } else {
3276 DCHECK(second.IsStackSlot());
3277 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3278 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003279 break;
Calin Juravle11351682014-10-23 15:38:15 +01003280 }
3281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003283 if (second.IsFpuRegister()) {
3284 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3285 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3286 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003287 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003288 __ subsd(first.AsFpuRegister<XmmRegister>(),
3289 codegen_->LiteralDoubleAddress(
3290 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003291 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003292 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3293 } else {
3294 DCHECK(second.IsDoubleStackSlot());
3295 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3296 }
Calin Juravle11351682014-10-23 15:38:15 +01003297 break;
3298 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003299
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003300 default:
Calin Juravle11351682014-10-23 15:38:15 +01003301 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003302 }
3303}
3304
Calin Juravle34bacdf2014-10-07 20:23:36 +01003305void LocationsBuilderX86::VisitMul(HMul* mul) {
3306 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003307 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003308 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003309 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 locations->SetInAt(0, Location::RequiresRegister());
3311 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003312 if (mul->InputAt(1)->IsIntConstant()) {
3313 // Can use 3 operand multiply.
3314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3315 } else {
3316 locations->SetOut(Location::SameAsFirstInput());
3317 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003318 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003319 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003321 locations->SetInAt(1, Location::Any());
3322 locations->SetOut(Location::SameAsFirstInput());
3323 // Needed for imul on 32bits with 64bits output.
3324 locations->AddTemp(Location::RegisterLocation(EAX));
3325 locations->AddTemp(Location::RegisterLocation(EDX));
3326 break;
3327 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003328 case DataType::Type::kFloat32:
3329 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003330 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003331 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3332 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003333 } else if (mul->InputAt(1)->IsConstant()) {
3334 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003335 } else {
3336 locations->SetInAt(1, Location::Any());
3337 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003338 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003340 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003341
3342 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003343 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003344 }
3345}
3346
3347void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3348 LocationSummary* locations = mul->GetLocations();
3349 Location first = locations->InAt(0);
3350 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003351 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352
3353 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003354 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003355 // The constant may have ended up in a register, so test explicitly to avoid
3356 // problems where the output may not be the same as the first operand.
3357 if (mul->InputAt(1)->IsIntConstant()) {
3358 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3359 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3360 } else if (second.IsRegister()) {
3361 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003363 } else {
3364 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003365 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003366 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003367 }
3368 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003369
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003370 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003371 Register in1_hi = first.AsRegisterPairHigh<Register>();
3372 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 Register eax = locations->GetTemp(0).AsRegister<Register>();
3374 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375
3376 DCHECK_EQ(EAX, eax);
3377 DCHECK_EQ(EDX, edx);
3378
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003379 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003380 // output: in1
3381 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3382 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3383 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003384 if (second.IsConstant()) {
3385 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003386
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003387 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3388 int32_t low_value = Low32Bits(value);
3389 int32_t high_value = High32Bits(value);
3390 Immediate low(low_value);
3391 Immediate high(high_value);
3392
3393 __ movl(eax, high);
3394 // eax <- in1.lo * in2.hi
3395 __ imull(eax, in1_lo);
3396 // in1.hi <- in1.hi * in2.lo
3397 __ imull(in1_hi, low);
3398 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3399 __ addl(in1_hi, eax);
3400 // move in2_lo to eax to prepare for double precision
3401 __ movl(eax, low);
3402 // edx:eax <- in1.lo * in2.lo
3403 __ mull(in1_lo);
3404 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3405 __ addl(in1_hi, edx);
3406 // in1.lo <- (in1.lo * in2.lo)[31:0];
3407 __ movl(in1_lo, eax);
3408 } else if (second.IsRegisterPair()) {
3409 Register in2_hi = second.AsRegisterPairHigh<Register>();
3410 Register in2_lo = second.AsRegisterPairLow<Register>();
3411
3412 __ movl(eax, in2_hi);
3413 // eax <- in1.lo * in2.hi
3414 __ imull(eax, in1_lo);
3415 // in1.hi <- in1.hi * in2.lo
3416 __ imull(in1_hi, in2_lo);
3417 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3418 __ addl(in1_hi, eax);
3419 // move in1_lo to eax to prepare for double precision
3420 __ movl(eax, in1_lo);
3421 // edx:eax <- in1.lo * in2.lo
3422 __ mull(in2_lo);
3423 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3424 __ addl(in1_hi, edx);
3425 // in1.lo <- (in1.lo * in2.lo)[31:0];
3426 __ movl(in1_lo, eax);
3427 } else {
3428 DCHECK(second.IsDoubleStackSlot()) << second;
3429 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3430 Address in2_lo(ESP, second.GetStackIndex());
3431
3432 __ movl(eax, in2_hi);
3433 // eax <- in1.lo * in2.hi
3434 __ imull(eax, in1_lo);
3435 // in1.hi <- in1.hi * in2.lo
3436 __ imull(in1_hi, in2_lo);
3437 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3438 __ addl(in1_hi, eax);
3439 // move in1_lo to eax to prepare for double precision
3440 __ movl(eax, in1_lo);
3441 // edx:eax <- in1.lo * in2.lo
3442 __ mull(in2_lo);
3443 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3444 __ addl(in1_hi, edx);
3445 // in1.lo <- (in1.lo * in2.lo)[31:0];
3446 __ movl(in1_lo, eax);
3447 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003448
3449 break;
3450 }
3451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003452 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003453 DCHECK(first.Equals(locations->Out()));
3454 if (second.IsFpuRegister()) {
3455 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3456 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3457 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003458 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003459 __ mulss(first.AsFpuRegister<XmmRegister>(),
3460 codegen_->LiteralFloatAddress(
3461 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003462 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003463 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3464 } else {
3465 DCHECK(second.IsStackSlot());
3466 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3467 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003468 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003469 }
3470
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003471 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003472 DCHECK(first.Equals(locations->Out()));
3473 if (second.IsFpuRegister()) {
3474 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3475 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3476 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003477 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003478 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3479 codegen_->LiteralDoubleAddress(
3480 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003481 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003482 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3483 } else {
3484 DCHECK(second.IsDoubleStackSlot());
3485 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3486 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003487 break;
3488 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003489
3490 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003491 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003492 }
3493}
3494
Roland Levillain232ade02015-04-20 15:14:36 +01003495void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3496 uint32_t temp_offset,
3497 uint32_t stack_adjustment,
3498 bool is_fp,
3499 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003500 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003501 DCHECK(!is_wide);
3502 if (is_fp) {
3503 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3504 } else {
3505 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3506 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003507 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003508 DCHECK(is_wide);
3509 if (is_fp) {
3510 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3511 } else {
3512 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3513 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003514 } else {
3515 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003516 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003517 Location stack_temp = Location::StackSlot(temp_offset);
3518 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003519 if (is_fp) {
3520 __ flds(Address(ESP, temp_offset));
3521 } else {
3522 __ filds(Address(ESP, temp_offset));
3523 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003524 } else {
3525 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3526 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003527 if (is_fp) {
3528 __ fldl(Address(ESP, temp_offset));
3529 } else {
3530 __ fildl(Address(ESP, temp_offset));
3531 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003532 }
3533 }
3534}
3535
3536void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 DataType::Type type = rem->GetResultType();
3538 bool is_float = type == DataType::Type::kFloat32;
3539 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003540 LocationSummary* locations = rem->GetLocations();
3541 Location first = locations->InAt(0);
3542 Location second = locations->InAt(1);
3543 Location out = locations->Out();
3544
3545 // Create stack space for 2 elements.
3546 // TODO: enhance register allocator to ask for stack temporaries.
3547 __ subl(ESP, Immediate(2 * elem_size));
3548
3549 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003550 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003551 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3552 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003553
3554 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003555 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003556 __ Bind(&retry);
3557 __ fprem();
3558
3559 // Move FP status to AX.
3560 __ fstsw();
3561
3562 // And see if the argument reduction is complete. This is signaled by the
3563 // C2 FPU flag bit set to 0.
3564 __ andl(EAX, Immediate(kC2ConditionMask));
3565 __ j(kNotEqual, &retry);
3566
3567 // We have settled on the final value. Retrieve it into an XMM register.
3568 // Store FP top of stack to real stack.
3569 if (is_float) {
3570 __ fsts(Address(ESP, 0));
3571 } else {
3572 __ fstl(Address(ESP, 0));
3573 }
3574
3575 // Pop the 2 items from the FP stack.
3576 __ fucompp();
3577
3578 // Load the value from the stack into an XMM register.
3579 DCHECK(out.IsFpuRegister()) << out;
3580 if (is_float) {
3581 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3582 } else {
3583 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3584 }
3585
3586 // And remove the temporary stack space we allocated.
3587 __ addl(ESP, Immediate(2 * elem_size));
3588}
3589
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590
3591void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3592 DCHECK(instruction->IsDiv() || instruction->IsRem());
3593
3594 LocationSummary* locations = instruction->GetLocations();
3595 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003596 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003597
3598 Register out_register = locations->Out().AsRegister<Register>();
3599 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003600 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601
3602 DCHECK(imm == 1 || imm == -1);
3603
3604 if (instruction->IsRem()) {
3605 __ xorl(out_register, out_register);
3606 } else {
3607 __ movl(out_register, input_register);
3608 if (imm == -1) {
3609 __ negl(out_register);
3610 }
3611 }
3612}
3613
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303614void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3615 LocationSummary* locations = instruction->GetLocations();
3616 Location second = locations->InAt(1);
3617
3618 Register out = locations->Out().AsRegister<Register>();
3619 Register numerator = locations->InAt(0).AsRegister<Register>();
3620
3621 int32_t imm = Int64FromConstant(second.GetConstant());
3622 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3623 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3624
3625 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3626 NearLabel done;
3627 __ movl(out, numerator);
3628 __ andl(out, Immediate(abs_imm-1));
3629 __ j(Condition::kZero, &done);
3630 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3631 __ testl(numerator, numerator);
3632 __ cmovl(Condition::kLess, out, tmp);
3633 __ Bind(&done);
3634}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003635
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003636void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003637 LocationSummary* locations = instruction->GetLocations();
3638
3639 Register out_register = locations->Out().AsRegister<Register>();
3640 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003641 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003642 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3643 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003644
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003645 Register num = locations->GetTemp(0).AsRegister<Register>();
3646
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003647 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 __ testl(input_register, input_register);
3649 __ cmovl(kGreaterEqual, num, input_register);
3650 int shift = CTZ(imm);
3651 __ sarl(num, Immediate(shift));
3652
3653 if (imm < 0) {
3654 __ negl(num);
3655 }
3656
3657 __ movl(out_register, num);
3658}
3659
3660void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3661 DCHECK(instruction->IsDiv() || instruction->IsRem());
3662
3663 LocationSummary* locations = instruction->GetLocations();
3664 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3665
3666 Register eax = locations->InAt(0).AsRegister<Register>();
3667 Register out = locations->Out().AsRegister<Register>();
3668 Register num;
3669 Register edx;
3670
3671 if (instruction->IsDiv()) {
3672 edx = locations->GetTemp(0).AsRegister<Register>();
3673 num = locations->GetTemp(1).AsRegister<Register>();
3674 } else {
3675 edx = locations->Out().AsRegister<Register>();
3676 num = locations->GetTemp(0).AsRegister<Register>();
3677 }
3678
3679 DCHECK_EQ(EAX, eax);
3680 DCHECK_EQ(EDX, edx);
3681 if (instruction->IsDiv()) {
3682 DCHECK_EQ(EAX, out);
3683 } else {
3684 DCHECK_EQ(EDX, out);
3685 }
3686
3687 int64_t magic;
3688 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003689 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003690
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003691 // Save the numerator.
3692 __ movl(num, eax);
3693
3694 // EAX = magic
3695 __ movl(eax, Immediate(magic));
3696
3697 // EDX:EAX = magic * numerator
3698 __ imull(num);
3699
3700 if (imm > 0 && magic < 0) {
3701 // EDX += num
3702 __ addl(edx, num);
3703 } else if (imm < 0 && magic > 0) {
3704 __ subl(edx, num);
3705 }
3706
3707 // Shift if needed.
3708 if (shift != 0) {
3709 __ sarl(edx, Immediate(shift));
3710 }
3711
3712 // EDX += 1 if EDX < 0
3713 __ movl(eax, edx);
3714 __ shrl(edx, Immediate(31));
3715 __ addl(edx, eax);
3716
3717 if (instruction->IsRem()) {
3718 __ movl(eax, num);
3719 __ imull(edx, Immediate(imm));
3720 __ subl(eax, edx);
3721 __ movl(edx, eax);
3722 } else {
3723 __ movl(eax, edx);
3724 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003725}
3726
Calin Juravlebacfec32014-11-14 15:54:36 +00003727void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3728 DCHECK(instruction->IsDiv() || instruction->IsRem());
3729
3730 LocationSummary* locations = instruction->GetLocations();
3731 Location out = locations->Out();
3732 Location first = locations->InAt(0);
3733 Location second = locations->InAt(1);
3734 bool is_div = instruction->IsDiv();
3735
3736 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 DCHECK_EQ(EAX, first.AsRegister<Register>());
3739 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003740
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003741 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003742 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003743
3744 if (imm == 0) {
3745 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3746 } else if (imm == 1 || imm == -1) {
3747 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303748 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3749 if (is_div) {
3750 DivByPowerOfTwo(instruction->AsDiv());
3751 } else {
3752 RemByPowerOfTwo(instruction->AsRem());
3753 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003754 } else {
3755 DCHECK(imm <= -2 || imm >= 2);
3756 GenerateDivRemWithAnyConstant(instruction);
3757 }
3758 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003759 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003760 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003761 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003762
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003763 Register second_reg = second.AsRegister<Register>();
3764 // 0x80000000/-1 triggers an arithmetic exception!
3765 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3766 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003767
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003768 __ cmpl(second_reg, Immediate(-1));
3769 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003770
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003771 // edx:eax <- sign-extended of eax
3772 __ cdq();
3773 // eax = quotient, edx = remainder
3774 __ idivl(second_reg);
3775 __ Bind(slow_path->GetExitLabel());
3776 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003777 break;
3778 }
3779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 InvokeRuntimeCallingConvention calling_convention;
3782 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3783 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3784 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3785 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3786 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3787 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3788
3789 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003790 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003791 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003792 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003793 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003794 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003795 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003796 break;
3797 }
3798
3799 default:
3800 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3801 }
3802}
3803
Calin Juravle7c4954d2014-10-28 16:57:40 +00003804void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003805 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003806 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003808 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003809
Calin Juravle7c4954d2014-10-28 16:57:40 +00003810 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003812 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003813 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003814 locations->SetOut(Location::SameAsFirstInput());
3815 // Intel uses edx:eax as the dividend.
3816 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003817 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3818 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3819 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003820 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003821 locations->AddTemp(Location::RequiresRegister());
3822 }
Calin Juravled0d48522014-11-04 16:40:20 +00003823 break;
3824 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003826 InvokeRuntimeCallingConvention calling_convention;
3827 locations->SetInAt(0, Location::RegisterPairLocation(
3828 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3829 locations->SetInAt(1, Location::RegisterPairLocation(
3830 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3831 // Runtime helper puts the result in EAX, EDX.
3832 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003833 break;
3834 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kFloat32:
3836 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003837 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003838 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3839 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003840 } else if (div->InputAt(1)->IsConstant()) {
3841 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003842 } else {
3843 locations->SetInAt(1, Location::Any());
3844 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003845 locations->SetOut(Location::SameAsFirstInput());
3846 break;
3847 }
3848
3849 default:
3850 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3851 }
3852}
3853
3854void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3855 LocationSummary* locations = div->GetLocations();
3856 Location first = locations->InAt(0);
3857 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003858
3859 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003860 case DataType::Type::kInt32:
3861 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003862 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003863 break;
3864 }
3865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003867 if (second.IsFpuRegister()) {
3868 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3869 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3870 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003871 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003872 __ divss(first.AsFpuRegister<XmmRegister>(),
3873 codegen_->LiteralFloatAddress(
3874 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003875 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003876 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3877 } else {
3878 DCHECK(second.IsStackSlot());
3879 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3880 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003881 break;
3882 }
3883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003884 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003885 if (second.IsFpuRegister()) {
3886 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3887 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3888 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003889 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003890 __ divsd(first.AsFpuRegister<XmmRegister>(),
3891 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003892 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3893 const_area->GetBaseMethodAddress(),
3894 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003895 } else {
3896 DCHECK(second.IsDoubleStackSlot());
3897 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3898 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003899 break;
3900 }
3901
3902 default:
3903 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3904 }
3905}
3906
Calin Juravlebacfec32014-11-14 15:54:36 +00003907void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003908 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003909
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003911 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003912 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003913 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003914
Calin Juravled2ec87d2014-12-08 14:24:46 +00003915 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003917 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003918 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003919 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003920 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3921 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3922 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003923 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003924 locations->AddTemp(Location::RequiresRegister());
3925 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003926 break;
3927 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003929 InvokeRuntimeCallingConvention calling_convention;
3930 locations->SetInAt(0, Location::RegisterPairLocation(
3931 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3932 locations->SetInAt(1, Location::RegisterPairLocation(
3933 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3934 // Runtime helper puts the result in EAX, EDX.
3935 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3936 break;
3937 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003938 case DataType::Type::kFloat64:
3939 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003940 locations->SetInAt(0, Location::Any());
3941 locations->SetInAt(1, Location::Any());
3942 locations->SetOut(Location::RequiresFpuRegister());
3943 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003944 break;
3945 }
3946
3947 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003948 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003949 }
3950}
3951
3952void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003954 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003955 case DataType::Type::kInt32:
3956 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003957 GenerateDivRemIntegral(rem);
3958 break;
3959 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kFloat32:
3961 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003962 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003963 break;
3964 }
3965 default:
3966 LOG(FATAL) << "Unexpected rem type " << type;
3967 }
3968}
3969
Aart Bik1f8d51b2018-02-15 10:42:37 -08003970static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3971 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3972 switch (minmax->GetResultType()) {
3973 case DataType::Type::kInt32:
3974 locations->SetInAt(0, Location::RequiresRegister());
3975 locations->SetInAt(1, Location::RequiresRegister());
3976 locations->SetOut(Location::SameAsFirstInput());
3977 break;
3978 case DataType::Type::kInt64:
3979 locations->SetInAt(0, Location::RequiresRegister());
3980 locations->SetInAt(1, Location::RequiresRegister());
3981 locations->SetOut(Location::SameAsFirstInput());
3982 // Register to use to perform a long subtract to set cc.
3983 locations->AddTemp(Location::RequiresRegister());
3984 break;
3985 case DataType::Type::kFloat32:
3986 locations->SetInAt(0, Location::RequiresFpuRegister());
3987 locations->SetInAt(1, Location::RequiresFpuRegister());
3988 locations->SetOut(Location::SameAsFirstInput());
3989 locations->AddTemp(Location::RequiresRegister());
3990 break;
3991 case DataType::Type::kFloat64:
3992 locations->SetInAt(0, Location::RequiresFpuRegister());
3993 locations->SetInAt(1, Location::RequiresFpuRegister());
3994 locations->SetOut(Location::SameAsFirstInput());
3995 break;
3996 default:
3997 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3998 }
3999}
4000
Aart Bik351df3e2018-03-07 11:54:57 -08004001void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4002 bool is_min,
4003 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004004 Location op1_loc = locations->InAt(0);
4005 Location op2_loc = locations->InAt(1);
4006
4007 // Shortcut for same input locations.
4008 if (op1_loc.Equals(op2_loc)) {
4009 // Can return immediately, as op1_loc == out_loc.
4010 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4011 // a copy here.
4012 DCHECK(locations->Out().Equals(op1_loc));
4013 return;
4014 }
4015
4016 if (type == DataType::Type::kInt64) {
4017 // Need to perform a subtract to get the sign right.
4018 // op1 is already in the same location as the output.
4019 Location output = locations->Out();
4020 Register output_lo = output.AsRegisterPairLow<Register>();
4021 Register output_hi = output.AsRegisterPairHigh<Register>();
4022
4023 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4024 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4025
4026 // The comparison is performed by subtracting the second operand from
4027 // the first operand and then setting the status flags in the same
4028 // manner as the SUB instruction."
4029 __ cmpl(output_lo, op2_lo);
4030
4031 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4032 Register temp = locations->GetTemp(0).AsRegister<Register>();
4033 __ movl(temp, output_hi);
4034 __ sbbl(temp, op2_hi);
4035
4036 // Now the condition code is correct.
4037 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4038 __ cmovl(cond, output_lo, op2_lo);
4039 __ cmovl(cond, output_hi, op2_hi);
4040 } else {
4041 DCHECK_EQ(type, DataType::Type::kInt32);
4042 Register out = locations->Out().AsRegister<Register>();
4043 Register op2 = op2_loc.AsRegister<Register>();
4044
4045 // (out := op1)
4046 // out <=? op2
4047 // if out is min jmp done
4048 // out := op2
4049 // done:
4050
4051 __ cmpl(out, op2);
4052 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4053 __ cmovl(cond, out, op2);
4054 }
4055}
4056
4057void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4058 bool is_min,
4059 DataType::Type type) {
4060 Location op1_loc = locations->InAt(0);
4061 Location op2_loc = locations->InAt(1);
4062 Location out_loc = locations->Out();
4063 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4064
4065 // Shortcut for same input locations.
4066 if (op1_loc.Equals(op2_loc)) {
4067 DCHECK(out_loc.Equals(op1_loc));
4068 return;
4069 }
4070
4071 // (out := op1)
4072 // out <=? op2
4073 // if Nan jmp Nan_label
4074 // if out is min jmp done
4075 // if op2 is min jmp op2_label
4076 // handle -0/+0
4077 // jmp done
4078 // Nan_label:
4079 // out := NaN
4080 // op2_label:
4081 // out := op2
4082 // done:
4083 //
4084 // This removes one jmp, but needs to copy one input (op1) to out.
4085 //
4086 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4087
4088 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4089
4090 NearLabel nan, done, op2_label;
4091 if (type == DataType::Type::kFloat64) {
4092 __ ucomisd(out, op2);
4093 } else {
4094 DCHECK_EQ(type, DataType::Type::kFloat32);
4095 __ ucomiss(out, op2);
4096 }
4097
4098 __ j(Condition::kParityEven, &nan);
4099
4100 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4101 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4102
4103 // Handle 0.0/-0.0.
4104 if (is_min) {
4105 if (type == DataType::Type::kFloat64) {
4106 __ orpd(out, op2);
4107 } else {
4108 __ orps(out, op2);
4109 }
4110 } else {
4111 if (type == DataType::Type::kFloat64) {
4112 __ andpd(out, op2);
4113 } else {
4114 __ andps(out, op2);
4115 }
4116 }
4117 __ jmp(&done);
4118
4119 // NaN handling.
4120 __ Bind(&nan);
4121 if (type == DataType::Type::kFloat64) {
4122 // TODO: Use a constant from the constant table (requires extra input).
4123 __ LoadLongConstant(out, kDoubleNaN);
4124 } else {
4125 Register constant = locations->GetTemp(0).AsRegister<Register>();
4126 __ movl(constant, Immediate(kFloatNaN));
4127 __ movd(out, constant);
4128 }
4129 __ jmp(&done);
4130
4131 // out := op2;
4132 __ Bind(&op2_label);
4133 if (type == DataType::Type::kFloat64) {
4134 __ movsd(out, op2);
4135 } else {
4136 __ movss(out, op2);
4137 }
4138
4139 // Done.
4140 __ Bind(&done);
4141}
4142
Aart Bik351df3e2018-03-07 11:54:57 -08004143void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4144 DataType::Type type = minmax->GetResultType();
4145 switch (type) {
4146 case DataType::Type::kInt32:
4147 case DataType::Type::kInt64:
4148 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4149 break;
4150 case DataType::Type::kFloat32:
4151 case DataType::Type::kFloat64:
4152 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4153 break;
4154 default:
4155 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4156 }
4157}
4158
Aart Bik1f8d51b2018-02-15 10:42:37 -08004159void LocationsBuilderX86::VisitMin(HMin* min) {
4160 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4161}
4162
4163void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004164 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004165}
4166
4167void LocationsBuilderX86::VisitMax(HMax* max) {
4168 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4169}
4170
4171void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004172 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004173}
4174
Aart Bik3dad3412018-02-28 12:01:46 -08004175void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4176 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4177 switch (abs->GetResultType()) {
4178 case DataType::Type::kInt32:
4179 locations->SetInAt(0, Location::RegisterLocation(EAX));
4180 locations->SetOut(Location::SameAsFirstInput());
4181 locations->AddTemp(Location::RegisterLocation(EDX));
4182 break;
4183 case DataType::Type::kInt64:
4184 locations->SetInAt(0, Location::RequiresRegister());
4185 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4186 locations->AddTemp(Location::RequiresRegister());
4187 break;
4188 case DataType::Type::kFloat32:
4189 locations->SetInAt(0, Location::RequiresFpuRegister());
4190 locations->SetOut(Location::SameAsFirstInput());
4191 locations->AddTemp(Location::RequiresFpuRegister());
4192 locations->AddTemp(Location::RequiresRegister());
4193 break;
4194 case DataType::Type::kFloat64:
4195 locations->SetInAt(0, Location::RequiresFpuRegister());
4196 locations->SetOut(Location::SameAsFirstInput());
4197 locations->AddTemp(Location::RequiresFpuRegister());
4198 break;
4199 default:
4200 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4201 }
4202}
4203
4204void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4205 LocationSummary* locations = abs->GetLocations();
4206 switch (abs->GetResultType()) {
4207 case DataType::Type::kInt32: {
4208 Register out = locations->Out().AsRegister<Register>();
4209 DCHECK_EQ(out, EAX);
4210 Register temp = locations->GetTemp(0).AsRegister<Register>();
4211 DCHECK_EQ(temp, EDX);
4212 // Sign extend EAX into EDX.
4213 __ cdq();
4214 // XOR EAX with sign.
4215 __ xorl(EAX, EDX);
4216 // Subtract out sign to correct.
4217 __ subl(EAX, EDX);
4218 // The result is in EAX.
4219 break;
4220 }
4221 case DataType::Type::kInt64: {
4222 Location input = locations->InAt(0);
4223 Register input_lo = input.AsRegisterPairLow<Register>();
4224 Register input_hi = input.AsRegisterPairHigh<Register>();
4225 Location output = locations->Out();
4226 Register output_lo = output.AsRegisterPairLow<Register>();
4227 Register output_hi = output.AsRegisterPairHigh<Register>();
4228 Register temp = locations->GetTemp(0).AsRegister<Register>();
4229 // Compute the sign into the temporary.
4230 __ movl(temp, input_hi);
4231 __ sarl(temp, Immediate(31));
4232 // Store the sign into the output.
4233 __ movl(output_lo, temp);
4234 __ movl(output_hi, temp);
4235 // XOR the input to the output.
4236 __ xorl(output_lo, input_lo);
4237 __ xorl(output_hi, input_hi);
4238 // Subtract the sign.
4239 __ subl(output_lo, temp);
4240 __ sbbl(output_hi, temp);
4241 break;
4242 }
4243 case DataType::Type::kFloat32: {
4244 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4245 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4246 Register constant = locations->GetTemp(1).AsRegister<Register>();
4247 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4248 __ movd(temp, constant);
4249 __ andps(out, temp);
4250 break;
4251 }
4252 case DataType::Type::kFloat64: {
4253 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4254 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4255 // TODO: Use a constant from the constant table (requires extra input).
4256 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4257 __ andpd(out, temp);
4258 break;
4259 }
4260 default:
4261 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4262 }
4263}
4264
Calin Juravled0d48522014-11-04 16:40:20 +00004265void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004266 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004267 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004268 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004269 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004270 case DataType::Type::kInt8:
4271 case DataType::Type::kUint16:
4272 case DataType::Type::kInt16:
4273 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004274 locations->SetInAt(0, Location::Any());
4275 break;
4276 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004277 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004278 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4279 if (!instruction->IsConstant()) {
4280 locations->AddTemp(Location::RequiresRegister());
4281 }
4282 break;
4283 }
4284 default:
4285 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4286 }
Calin Juravled0d48522014-11-04 16:40:20 +00004287}
4288
4289void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004290 SlowPathCode* slow_path =
4291 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004292 codegen_->AddSlowPath(slow_path);
4293
4294 LocationSummary* locations = instruction->GetLocations();
4295 Location value = locations->InAt(0);
4296
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004297 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004298 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004299 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004300 case DataType::Type::kInt8:
4301 case DataType::Type::kUint16:
4302 case DataType::Type::kInt16:
4303 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004304 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004305 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004306 __ j(kEqual, slow_path->GetEntryLabel());
4307 } else if (value.IsStackSlot()) {
4308 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4309 __ j(kEqual, slow_path->GetEntryLabel());
4310 } else {
4311 DCHECK(value.IsConstant()) << value;
4312 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004313 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004314 }
4315 }
4316 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004317 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004319 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004320 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004321 __ movl(temp, value.AsRegisterPairLow<Register>());
4322 __ orl(temp, value.AsRegisterPairHigh<Register>());
4323 __ j(kEqual, slow_path->GetEntryLabel());
4324 } else {
4325 DCHECK(value.IsConstant()) << value;
4326 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4327 __ jmp(slow_path->GetEntryLabel());
4328 }
4329 }
4330 break;
4331 }
4332 default:
4333 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004334 }
Calin Juravled0d48522014-11-04 16:40:20 +00004335}
4336
Calin Juravle9aec02f2014-11-18 23:06:35 +00004337void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4338 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4339
4340 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004341 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004342
4343 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004344 case DataType::Type::kInt32:
4345 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004346 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004347 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004348 // The shift count needs to be in CL or a constant.
4349 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004350 locations->SetOut(Location::SameAsFirstInput());
4351 break;
4352 }
4353 default:
4354 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4355 }
4356}
4357
4358void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4359 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4360
4361 LocationSummary* locations = op->GetLocations();
4362 Location first = locations->InAt(0);
4363 Location second = locations->InAt(1);
4364 DCHECK(first.Equals(locations->Out()));
4365
4366 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004368 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004369 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004370 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004371 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004372 DCHECK_EQ(ECX, second_reg);
4373 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004374 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004375 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004376 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004377 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004378 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004379 }
4380 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004381 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004382 if (shift == 0) {
4383 return;
4384 }
4385 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004386 if (op->IsShl()) {
4387 __ shll(first_reg, imm);
4388 } else if (op->IsShr()) {
4389 __ sarl(first_reg, imm);
4390 } else {
4391 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004392 }
4393 }
4394 break;
4395 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004396 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004397 if (second.IsRegister()) {
4398 Register second_reg = second.AsRegister<Register>();
4399 DCHECK_EQ(ECX, second_reg);
4400 if (op->IsShl()) {
4401 GenerateShlLong(first, second_reg);
4402 } else if (op->IsShr()) {
4403 GenerateShrLong(first, second_reg);
4404 } else {
4405 GenerateUShrLong(first, second_reg);
4406 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004407 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004408 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004409 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004410 // Nothing to do if the shift is 0, as the input is already the output.
4411 if (shift != 0) {
4412 if (op->IsShl()) {
4413 GenerateShlLong(first, shift);
4414 } else if (op->IsShr()) {
4415 GenerateShrLong(first, shift);
4416 } else {
4417 GenerateUShrLong(first, shift);
4418 }
4419 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004420 }
4421 break;
4422 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004423 default:
4424 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4425 }
4426}
4427
Mark P Mendell73945692015-04-29 14:56:17 +00004428void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4429 Register low = loc.AsRegisterPairLow<Register>();
4430 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004431 if (shift == 1) {
4432 // This is just an addition.
4433 __ addl(low, low);
4434 __ adcl(high, high);
4435 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004436 // Shift by 32 is easy. High gets low, and low gets 0.
4437 codegen_->EmitParallelMoves(
4438 loc.ToLow(),
4439 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004440 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004441 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4442 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004443 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004444 } else if (shift > 32) {
4445 // Low part becomes 0. High part is low part << (shift-32).
4446 __ movl(high, low);
4447 __ shll(high, Immediate(shift - 32));
4448 __ xorl(low, low);
4449 } else {
4450 // Between 1 and 31.
4451 __ shld(high, low, Immediate(shift));
4452 __ shll(low, Immediate(shift));
4453 }
4454}
4455
Calin Juravle9aec02f2014-11-18 23:06:35 +00004456void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004457 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004458 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4459 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4460 __ testl(shifter, Immediate(32));
4461 __ j(kEqual, &done);
4462 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4463 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4464 __ Bind(&done);
4465}
4466
Mark P Mendell73945692015-04-29 14:56:17 +00004467void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4468 Register low = loc.AsRegisterPairLow<Register>();
4469 Register high = loc.AsRegisterPairHigh<Register>();
4470 if (shift == 32) {
4471 // Need to copy the sign.
4472 DCHECK_NE(low, high);
4473 __ movl(low, high);
4474 __ sarl(high, Immediate(31));
4475 } else if (shift > 32) {
4476 DCHECK_NE(low, high);
4477 // High part becomes sign. Low part is shifted by shift - 32.
4478 __ movl(low, high);
4479 __ sarl(high, Immediate(31));
4480 __ sarl(low, Immediate(shift - 32));
4481 } else {
4482 // Between 1 and 31.
4483 __ shrd(low, high, Immediate(shift));
4484 __ sarl(high, Immediate(shift));
4485 }
4486}
4487
Calin Juravle9aec02f2014-11-18 23:06:35 +00004488void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004489 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004490 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4491 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4492 __ testl(shifter, Immediate(32));
4493 __ j(kEqual, &done);
4494 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4495 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4496 __ Bind(&done);
4497}
4498
Mark P Mendell73945692015-04-29 14:56:17 +00004499void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4500 Register low = loc.AsRegisterPairLow<Register>();
4501 Register high = loc.AsRegisterPairHigh<Register>();
4502 if (shift == 32) {
4503 // Shift by 32 is easy. Low gets high, and high gets 0.
4504 codegen_->EmitParallelMoves(
4505 loc.ToHigh(),
4506 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004507 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004508 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4509 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004510 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004511 } else if (shift > 32) {
4512 // Low part is high >> (shift - 32). High part becomes 0.
4513 __ movl(low, high);
4514 __ shrl(low, Immediate(shift - 32));
4515 __ xorl(high, high);
4516 } else {
4517 // Between 1 and 31.
4518 __ shrd(low, high, Immediate(shift));
4519 __ shrl(high, Immediate(shift));
4520 }
4521}
4522
Calin Juravle9aec02f2014-11-18 23:06:35 +00004523void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004524 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004525 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4526 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4527 __ testl(shifter, Immediate(32));
4528 __ j(kEqual, &done);
4529 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4530 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4531 __ Bind(&done);
4532}
4533
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004534void LocationsBuilderX86::VisitRor(HRor* ror) {
4535 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004536 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004537
4538 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004539 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004540 // Add the temporary needed.
4541 locations->AddTemp(Location::RequiresRegister());
4542 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004543 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004544 locations->SetInAt(0, Location::RequiresRegister());
4545 // The shift count needs to be in CL (unless it is a constant).
4546 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4547 locations->SetOut(Location::SameAsFirstInput());
4548 break;
4549 default:
4550 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4551 UNREACHABLE();
4552 }
4553}
4554
4555void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4556 LocationSummary* locations = ror->GetLocations();
4557 Location first = locations->InAt(0);
4558 Location second = locations->InAt(1);
4559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004560 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004561 Register first_reg = first.AsRegister<Register>();
4562 if (second.IsRegister()) {
4563 Register second_reg = second.AsRegister<Register>();
4564 __ rorl(first_reg, second_reg);
4565 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004567 __ rorl(first_reg, imm);
4568 }
4569 return;
4570 }
4571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004572 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004573 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4574 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4575 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4576 if (second.IsRegister()) {
4577 Register second_reg = second.AsRegister<Register>();
4578 DCHECK_EQ(second_reg, ECX);
4579 __ movl(temp_reg, first_reg_hi);
4580 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4581 __ shrd(first_reg_lo, temp_reg, second_reg);
4582 __ movl(temp_reg, first_reg_hi);
4583 __ testl(second_reg, Immediate(32));
4584 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4585 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4586 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004588 if (shift_amt == 0) {
4589 // Already fine.
4590 return;
4591 }
4592 if (shift_amt == 32) {
4593 // Just swap.
4594 __ movl(temp_reg, first_reg_lo);
4595 __ movl(first_reg_lo, first_reg_hi);
4596 __ movl(first_reg_hi, temp_reg);
4597 return;
4598 }
4599
4600 Immediate imm(shift_amt);
4601 // Save the constents of the low value.
4602 __ movl(temp_reg, first_reg_lo);
4603
4604 // Shift right into low, feeding bits from high.
4605 __ shrd(first_reg_lo, first_reg_hi, imm);
4606
4607 // Shift right into high, feeding bits from the original low.
4608 __ shrd(first_reg_hi, temp_reg, imm);
4609
4610 // Swap if needed.
4611 if (shift_amt > 32) {
4612 __ movl(temp_reg, first_reg_lo);
4613 __ movl(first_reg_lo, first_reg_hi);
4614 __ movl(first_reg_hi, temp_reg);
4615 }
4616 }
4617}
4618
Calin Juravle9aec02f2014-11-18 23:06:35 +00004619void LocationsBuilderX86::VisitShl(HShl* shl) {
4620 HandleShift(shl);
4621}
4622
4623void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4624 HandleShift(shl);
4625}
4626
4627void LocationsBuilderX86::VisitShr(HShr* shr) {
4628 HandleShift(shr);
4629}
4630
4631void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4632 HandleShift(shr);
4633}
4634
4635void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4636 HandleShift(ushr);
4637}
4638
4639void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4640 HandleShift(ushr);
4641}
4642
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004643void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004644 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4645 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004646 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004647 InvokeRuntimeCallingConvention calling_convention;
4648 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004649}
4650
4651void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004652 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4653 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4654 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004655}
4656
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004657void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004658 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4659 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004660 locations->SetOut(Location::RegisterLocation(EAX));
4661 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004662 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4663 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004664}
4665
4666void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004667 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4668 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004669 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004670 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004671 DCHECK(!codegen_->IsLeafMethod());
4672}
4673
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004674void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004675 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004676 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004677 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4678 if (location.IsStackSlot()) {
4679 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4680 } else if (location.IsDoubleStackSlot()) {
4681 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004682 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004683 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004684}
4685
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004686void InstructionCodeGeneratorX86::VisitParameterValue(
4687 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4688}
4689
4690void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4691 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004692 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004693 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4694}
4695
4696void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004697}
4698
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004699void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4700 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004701 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004702 locations->SetInAt(0, Location::RequiresRegister());
4703 locations->SetOut(Location::RequiresRegister());
4704}
4705
4706void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4707 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004708 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004709 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004710 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004711 __ movl(locations->Out().AsRegister<Register>(),
4712 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004713 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004714 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004715 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004716 __ movl(locations->Out().AsRegister<Register>(),
4717 Address(locations->InAt(0).AsRegister<Register>(),
4718 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4719 // temp = temp->GetImtEntryAt(method_offset);
4720 __ movl(locations->Out().AsRegister<Register>(),
4721 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004722 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004723}
4724
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004725void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004726 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004727 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004728 locations->SetInAt(0, Location::RequiresRegister());
4729 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004730}
4731
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004732void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4733 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004734 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004735 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004736 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004737 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004738 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004739 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004740 break;
4741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004742 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004743 __ notl(out.AsRegisterPairLow<Register>());
4744 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004745 break;
4746
4747 default:
4748 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4749 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004750}
4751
David Brazdil66d126e2015-04-03 16:02:44 +01004752void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4753 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004754 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004755 locations->SetInAt(0, Location::RequiresRegister());
4756 locations->SetOut(Location::SameAsFirstInput());
4757}
4758
4759void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004760 LocationSummary* locations = bool_not->GetLocations();
4761 Location in = locations->InAt(0);
4762 Location out = locations->Out();
4763 DCHECK(in.Equals(out));
4764 __ xorl(out.AsRegister<Register>(), Immediate(1));
4765}
4766
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004767void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004768 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004769 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004770 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004771 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004772 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004774 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004775 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 case DataType::Type::kInt32:
4777 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004778 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004779 locations->SetInAt(1, Location::Any());
4780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4781 break;
4782 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 case DataType::Type::kFloat32:
4784 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004785 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004786 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4787 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4788 } else if (compare->InputAt(1)->IsConstant()) {
4789 locations->SetInAt(1, Location::RequiresFpuRegister());
4790 } else {
4791 locations->SetInAt(1, Location::Any());
4792 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004793 locations->SetOut(Location::RequiresRegister());
4794 break;
4795 }
4796 default:
4797 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4798 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004799}
4800
4801void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004802 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004803 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004804 Location left = locations->InAt(0);
4805 Location right = locations->InAt(1);
4806
Mark Mendell0c9497d2015-08-21 09:30:05 -04004807 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004808 Condition less_cond = kLess;
4809
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004810 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004811 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004812 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004813 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004814 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004815 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004816 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004817 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004818 break;
4819 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004821 Register left_low = left.AsRegisterPairLow<Register>();
4822 Register left_high = left.AsRegisterPairHigh<Register>();
4823 int32_t val_low = 0;
4824 int32_t val_high = 0;
4825 bool right_is_const = false;
4826
4827 if (right.IsConstant()) {
4828 DCHECK(right.GetConstant()->IsLongConstant());
4829 right_is_const = true;
4830 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4831 val_low = Low32Bits(val);
4832 val_high = High32Bits(val);
4833 }
4834
Calin Juravleddb7df22014-11-25 20:56:51 +00004835 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004836 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004837 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004838 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004839 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004840 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004841 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004842 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004843 __ j(kLess, &less); // Signed compare.
4844 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004845 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004846 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004847 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004848 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004849 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004850 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004851 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004852 }
Aart Bika19616e2016-02-01 18:57:58 -08004853 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004854 break;
4855 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004856 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004857 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004858 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004859 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004860 break;
4861 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004863 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004864 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004865 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004866 break;
4867 }
4868 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004869 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004870 }
Aart Bika19616e2016-02-01 18:57:58 -08004871
Calin Juravleddb7df22014-11-25 20:56:51 +00004872 __ movl(out, Immediate(0));
4873 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004874 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004875
4876 __ Bind(&greater);
4877 __ movl(out, Immediate(1));
4878 __ jmp(&done);
4879
4880 __ Bind(&less);
4881 __ movl(out, Immediate(-1));
4882
4883 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004884}
4885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004886void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004887 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004888 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004889 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004890 locations->SetInAt(i, Location::Any());
4891 }
4892 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004893}
4894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004895void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004896 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004897}
4898
Roland Levillain7c1559a2015-12-15 10:55:36 +00004899void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004900 /*
4901 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4902 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4903 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4904 */
4905 switch (kind) {
4906 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004907 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004908 break;
4909 }
4910 case MemBarrierKind::kAnyStore:
4911 case MemBarrierKind::kLoadAny:
4912 case MemBarrierKind::kStoreStore: {
4913 // nop
4914 break;
4915 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004916 case MemBarrierKind::kNTStoreStore:
4917 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004918 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004919 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004920 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921}
4922
Vladimir Markodc151b22015-10-15 18:02:30 +01004923HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4924 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004925 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004926 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004927}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004928
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004929Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4930 Register temp) {
4931 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004932 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004933 if (!invoke->GetLocations()->Intrinsified()) {
4934 return location.AsRegister<Register>();
4935 }
4936 // For intrinsics we allow any location, so it may be on the stack.
4937 if (!location.IsRegister()) {
4938 __ movl(temp, Address(ESP, location.GetStackIndex()));
4939 return temp;
4940 }
4941 // For register locations, check if the register was saved. If so, get it from the stack.
4942 // Note: There is a chance that the register was saved but not overwritten, so we could
4943 // save one load. However, since this is just an intrinsic slow path we prefer this
4944 // simple and more robust approach rather that trying to determine if that's the case.
4945 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004946 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4947 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4948 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4949 __ movl(temp, Address(ESP, stack_offset));
4950 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004951 }
4952 return location.AsRegister<Register>();
4953}
4954
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004955void CodeGeneratorX86::GenerateStaticOrDirectCall(
4956 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004957 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4958 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004959 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004960 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004961 uint32_t offset =
4962 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4963 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004964 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004965 }
Vladimir Marko58155012015-08-19 12:49:41 +00004966 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004967 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004968 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004969 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004970 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004971 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4972 temp.AsRegister<Register>());
4973 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004974 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004975 break;
4976 }
Vladimir Markob066d432018-01-03 13:14:37 +00004977 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4978 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4979 temp.AsRegister<Register>());
4980 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4981 RecordBootImageRelRoPatch(
4982 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004983 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004984 break;
4985 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004986 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004987 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4988 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004989 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004990 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004991 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004992 break;
4993 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004994 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4995 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4996 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004997 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4998 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4999 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005000 }
Vladimir Marko58155012015-08-19 12:49:41 +00005001 }
5002
5003 switch (invoke->GetCodePtrLocation()) {
5004 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5005 __ call(GetFrameEntryLabel());
5006 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005007 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5008 // (callee_method + offset_of_quick_compiled_code)()
5009 __ call(Address(callee_method.AsRegister<Register>(),
5010 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005011 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00005012 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005013 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005014 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04005015
5016 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005017}
5018
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005019void CodeGeneratorX86::GenerateVirtualCall(
5020 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005021 Register temp = temp_in.AsRegister<Register>();
5022 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5023 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005024
5025 // Use the calling convention instead of the location of the receiver, as
5026 // intrinsics may have put the receiver in a different register. In the intrinsics
5027 // slow path, the arguments have been moved to the right place, so here we are
5028 // guaranteed that the receiver is the first register of the calling convention.
5029 InvokeDexCallingConvention calling_convention;
5030 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005031 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005032 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005033 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005034 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005035 // Instead of simply (possibly) unpoisoning `temp` here, we should
5036 // emit a read barrier for the previous class reference load.
5037 // However this is not required in practice, as this is an
5038 // intermediate/temporary reference and because the current
5039 // concurrent copying collector keeps the from-space memory
5040 // intact/accessible until the end of the marking phase (the
5041 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005042 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005043
5044 MaybeGenerateInlineCacheCheck(invoke, temp);
5045
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005046 // temp = temp->GetMethodAt(method_offset);
5047 __ movl(temp, Address(temp, method_offset));
5048 // call temp->GetEntryPoint();
5049 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005050 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005051 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005052}
5053
Vladimir Marko6fd16062018-06-26 11:02:04 +01005054void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5055 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005056 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005057 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005058 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005059}
5060
Vladimir Markob066d432018-01-03 13:14:37 +00005061void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5062 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005063 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005064 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005065 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005066}
5067
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005068void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01005069 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005070 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005071 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005072 boot_image_method_patches_.emplace_back(
5073 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005074 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005075}
5076
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005077void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
5078 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5079 HX86ComputeBaseMethodAddress* method_address =
5080 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005081 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005082 method_bss_entry_patches_.emplace_back(
5083 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5084 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005085}
5086
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005087void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5088 HX86ComputeBaseMethodAddress* method_address =
5089 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5090 boot_image_type_patches_.emplace_back(
5091 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005092 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005093}
5094
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005095Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005096 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005097 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5098 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005099 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005100 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005101}
5102
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005103void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5104 HX86ComputeBaseMethodAddress* method_address =
5105 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5106 boot_image_string_patches_.emplace_back(
5107 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5108 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005109}
5110
Vladimir Markoaad75c62016-10-03 08:46:48 +00005111Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005112 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005113 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005114 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005115 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005116 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005117}
5118
Vladimir Markoeebb8212018-06-05 14:57:24 +01005119void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005120 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005121 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005122 if (GetCompilerOptions().IsBootImage()) {
5123 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5124 HX86ComputeBaseMethodAddress* method_address =
5125 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5126 DCHECK(method_address != nullptr);
5127 Register method_address_reg =
5128 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5129 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5130 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005131 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005132 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5133 HX86ComputeBaseMethodAddress* method_address =
5134 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5135 DCHECK(method_address != nullptr);
5136 Register method_address_reg =
5137 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5138 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005139 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005140 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005141 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005142 gc::Heap* heap = Runtime::Current()->GetHeap();
5143 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005144 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005145 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5146 }
5147}
5148
Vladimir Marko6fd16062018-06-26 11:02:04 +01005149void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5150 uint32_t boot_image_offset) {
5151 DCHECK(invoke->IsStatic());
5152 InvokeRuntimeCallingConvention calling_convention;
5153 Register argument = calling_convention.GetRegisterAt(0);
5154 if (GetCompilerOptions().IsBootImage()) {
5155 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5156 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5157 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5158 HX86ComputeBaseMethodAddress* method_address =
5159 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5160 DCHECK(method_address != nullptr);
5161 Register method_address_reg =
5162 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5163 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5164 MethodReference target_method = invoke->GetTargetMethod();
5165 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5166 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5167 __ Bind(&boot_image_type_patches_.back().label);
5168 } else {
5169 LoadBootImageAddress(argument, boot_image_offset, invoke);
5170 }
5171 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5172 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5173}
5174
Vladimir Markoaad75c62016-10-03 08:46:48 +00005175// The label points to the end of the "movl" or another instruction but the literal offset
5176// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5177constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5178
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005179template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005180inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005181 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005182 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005183 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005184 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005185 linker_patches->push_back(Factory(literal_offset,
5186 info.target_dex_file,
5187 GetMethodAddressOffset(info.method_address),
5188 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005189 }
5190}
5191
Vladimir Marko6fd16062018-06-26 11:02:04 +01005192template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5193linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5194 const DexFile* target_dex_file,
5195 uint32_t pc_insn_offset,
5196 uint32_t boot_image_offset) {
5197 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5198 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005199}
5200
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005201void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005202 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005203 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005204 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005205 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005206 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005207 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005208 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005209 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005210 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005211 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005212 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005213 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5214 boot_image_method_patches_, linker_patches);
5215 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5216 boot_image_type_patches_, linker_patches);
5217 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005218 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005219 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005220 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005221 DCHECK(boot_image_type_patches_.empty());
5222 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005223 }
5224 if (GetCompilerOptions().IsBootImage()) {
5225 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5226 boot_image_other_patches_, linker_patches);
5227 } else {
5228 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5229 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005230 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005231 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5232 method_bss_entry_patches_, linker_patches);
5233 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5234 type_bss_entry_patches_, linker_patches);
5235 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5236 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005237 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005238}
5239
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005240void CodeGeneratorX86::MarkGCCard(Register temp,
5241 Register card,
5242 Register object,
5243 Register value,
5244 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005245 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005246 if (value_can_be_null) {
5247 __ testl(value, value);
5248 __ j(kEqual, &is_null);
5249 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005250 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005251 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005252 // Calculate the offset (in the card table) of the card corresponding to
5253 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 __ movl(temp, object);
5255 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005256 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5257 // `object`'s card.
5258 //
5259 // Register `card` contains the address of the card table. Note that the card
5260 // table's base is biased during its creation so that it always starts at an
5261 // address whose least-significant byte is equal to `kCardDirty` (see
5262 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5263 // below writes the `kCardDirty` (byte) value into the `object`'s card
5264 // (located at `card + object >> kCardShift`).
5265 //
5266 // This dual use of the value in register `card` (1. to calculate the location
5267 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5268 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005269 __ movb(Address(temp, card, TIMES_1, 0),
5270 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005271 if (value_can_be_null) {
5272 __ Bind(&is_null);
5273 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274}
5275
Calin Juravle52c48962014-12-16 17:02:57 +00005276void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5277 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005278
5279 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005280 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005281 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005282 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5283 kEmitCompilerReadBarrier
5284 ? LocationSummary::kCallOnSlowPath
5285 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005286 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005287 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005288 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005289 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005292 locations->SetOut(Location::RequiresFpuRegister());
5293 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005294 // The output overlaps in case of long: we don't want the low move
5295 // to overwrite the object's location. Likewise, in the case of
5296 // an object field get with read barriers enabled, we do not want
5297 // the move to overwrite the object's location, as we need it to emit
5298 // the read barrier.
5299 locations->SetOut(
5300 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005301 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005302 Location::kOutputOverlap :
5303 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005304 }
Calin Juravle52c48962014-12-16 17:02:57 +00005305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005307 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 // So we use an XMM register as a temp to achieve atomicity (first
5309 // load the temp into the XMM and then copy the XMM into the
5310 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005311 locations->AddTemp(Location::RequiresFpuRegister());
5312 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005313}
5314
Calin Juravle52c48962014-12-16 17:02:57 +00005315void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5316 const FieldInfo& field_info) {
5317 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005318
Calin Juravle52c48962014-12-16 17:02:57 +00005319 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005320 Location base_loc = locations->InAt(0);
5321 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005322 Location out = locations->Out();
5323 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005324 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5325 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005326 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5327
Vladimir Marko61b92282017-10-11 13:23:17 +01005328 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005329 case DataType::Type::kBool:
5330 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005331 __ movzxb(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::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005336 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005337 break;
5338 }
5339
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005340 case DataType::Type::kUint16: {
5341 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005342 break;
5343 }
5344
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005345 case DataType::Type::kInt16: {
5346 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005347 break;
5348 }
5349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005351 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005352 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005354 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005355 // /* HeapReference<Object> */ out = *(base + offset)
5356 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005357 // Note that a potential implicit null check is handled in this
5358 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5359 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005360 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005361 if (is_volatile) {
5362 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5363 }
5364 } else {
5365 __ movl(out.AsRegister<Register>(), Address(base, offset));
5366 codegen_->MaybeRecordImplicitNullCheck(instruction);
5367 if (is_volatile) {
5368 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5369 }
5370 // If read barriers are enabled, emit read barriers other than
5371 // Baker's using a slow path (and also unpoison the loaded
5372 // reference, if heap poisoning is enabled).
5373 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5374 }
5375 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005376 }
5377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005378 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005379 if (is_volatile) {
5380 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5381 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005382 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005383 __ movd(out.AsRegisterPairLow<Register>(), temp);
5384 __ psrlq(temp, Immediate(32));
5385 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5386 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005387 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005388 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005390 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5391 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005392 break;
5393 }
5394
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005395 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005396 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005397 break;
5398 }
5399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005400 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005401 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005402 break;
5403 }
5404
Aart Bik66c158e2018-01-31 12:55:04 -08005405 case DataType::Type::kUint32:
5406 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005407 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005408 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005409 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005410 }
Calin Juravle52c48962014-12-16 17:02:57 +00005411
Vladimir Marko61b92282017-10-11 13:23:17 +01005412 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005413 // Potential implicit null checks, in the case of reference or
5414 // long fields, are handled in the previous switch statement.
5415 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005416 codegen_->MaybeRecordImplicitNullCheck(instruction);
5417 }
5418
Calin Juravle52c48962014-12-16 17:02:57 +00005419 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005420 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005421 // Memory barriers, in the case of references, are also handled
5422 // in the previous switch statement.
5423 } else {
5424 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5425 }
Roland Levillain4d027112015-07-01 15:41:14 +01005426 }
Calin Juravle52c48962014-12-16 17:02:57 +00005427}
5428
5429void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5430 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5431
5432 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005433 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005434 locations->SetInAt(0, Location::RequiresRegister());
5435 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005436 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005437 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005438
5439 // The register allocator does not support multiple
5440 // inputs that die at entry with one in a specific register.
5441 if (is_byte_type) {
5442 // Ensure the value is in a byte register.
5443 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005444 } else if (DataType::IsFloatingPointType(field_type)) {
5445 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005446 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5447 locations->SetInAt(1, Location::RequiresFpuRegister());
5448 } else {
5449 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5450 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005452 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005453 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005454
Calin Juravle52c48962014-12-16 17:02:57 +00005455 // 64bits value can be atomically written to an address with movsd and an XMM register.
5456 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5457 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5458 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5459 // isolated cases when we need this it isn't worth adding the extra complexity.
5460 locations->AddTemp(Location::RequiresFpuRegister());
5461 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005462 } else {
5463 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5464
5465 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5466 // Temporary registers for the write barrier.
5467 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5468 // Ensure the card is in a byte register.
5469 locations->AddTemp(Location::RegisterLocation(ECX));
5470 }
Calin Juravle52c48962014-12-16 17:02:57 +00005471 }
5472}
5473
5474void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005475 const FieldInfo& field_info,
5476 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005477 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5478
5479 LocationSummary* locations = instruction->GetLocations();
5480 Register base = locations->InAt(0).AsRegister<Register>();
5481 Location value = locations->InAt(1);
5482 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005483 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005484 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005485 bool needs_write_barrier =
5486 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005487
5488 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005489 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005490 }
5491
Mark Mendell81489372015-11-04 11:30:41 -05005492 bool maybe_record_implicit_null_check_done = false;
5493
Calin Juravle52c48962014-12-16 17:02:57 +00005494 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005495 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005496 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005498 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5499 break;
5500 }
5501
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005502 case DataType::Type::kUint16:
5503 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005504 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005505 __ movw(Address(base, offset),
5506 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005507 } else {
5508 __ movw(Address(base, offset), value.AsRegister<Register>());
5509 }
Calin Juravle52c48962014-12-16 17:02:57 +00005510 break;
5511 }
5512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005513 case DataType::Type::kInt32:
5514 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005515 if (kPoisonHeapReferences && needs_write_barrier) {
5516 // Note that in the case where `value` is a null reference,
5517 // we do not enter this block, as the reference does not
5518 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005520 Register temp = locations->GetTemp(0).AsRegister<Register>();
5521 __ movl(temp, value.AsRegister<Register>());
5522 __ PoisonHeapReference(temp);
5523 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005524 } else if (value.IsConstant()) {
5525 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5526 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005527 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005528 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005529 __ movl(Address(base, offset), value.AsRegister<Register>());
5530 }
Calin Juravle52c48962014-12-16 17:02:57 +00005531 break;
5532 }
5533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005535 if (is_volatile) {
5536 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5537 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5538 __ movd(temp1, value.AsRegisterPairLow<Register>());
5539 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5540 __ punpckldq(temp1, temp2);
5541 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005542 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005543 } else if (value.IsConstant()) {
5544 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5545 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5546 codegen_->MaybeRecordImplicitNullCheck(instruction);
5547 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005548 } else {
5549 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005550 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005551 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5552 }
Mark Mendell81489372015-11-04 11:30:41 -05005553 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005554 break;
5555 }
5556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005558 if (value.IsConstant()) {
5559 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5560 __ movl(Address(base, offset), Immediate(v));
5561 } else {
5562 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5563 }
Calin Juravle52c48962014-12-16 17:02:57 +00005564 break;
5565 }
5566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005568 if (value.IsConstant()) {
5569 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5570 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5571 codegen_->MaybeRecordImplicitNullCheck(instruction);
5572 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5573 maybe_record_implicit_null_check_done = true;
5574 } else {
5575 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5576 }
Calin Juravle52c48962014-12-16 17:02:57 +00005577 break;
5578 }
5579
Aart Bik66c158e2018-01-31 12:55:04 -08005580 case DataType::Type::kUint32:
5581 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005583 LOG(FATAL) << "Unreachable type " << field_type;
5584 UNREACHABLE();
5585 }
5586
Mark Mendell81489372015-11-04 11:30:41 -05005587 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005588 codegen_->MaybeRecordImplicitNullCheck(instruction);
5589 }
5590
Roland Levillain4d027112015-07-01 15:41:14 +01005591 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005592 Register temp = locations->GetTemp(0).AsRegister<Register>();
5593 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005594 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005595 }
5596
Calin Juravle52c48962014-12-16 17:02:57 +00005597 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005598 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005599 }
5600}
5601
5602void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5603 HandleFieldGet(instruction, instruction->GetFieldInfo());
5604}
5605
5606void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5607 HandleFieldGet(instruction, instruction->GetFieldInfo());
5608}
5609
5610void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5611 HandleFieldSet(instruction, instruction->GetFieldInfo());
5612}
5613
5614void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005615 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005616}
5617
5618void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5619 HandleFieldSet(instruction, instruction->GetFieldInfo());
5620}
5621
5622void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005623 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005624}
5625
5626void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5627 HandleFieldGet(instruction, instruction->GetFieldInfo());
5628}
5629
5630void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5631 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005632}
5633
Vladimir Marko552a1342017-10-31 10:56:47 +00005634void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5635 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5636}
5637
5638void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5639 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5640 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5641}
5642
Calin Juravlee460d1d2015-09-29 04:52:17 +01005643void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5644 HUnresolvedInstanceFieldGet* instruction) {
5645 FieldAccessCallingConventionX86 calling_convention;
5646 codegen_->CreateUnresolvedFieldLocationSummary(
5647 instruction, instruction->GetFieldType(), calling_convention);
5648}
5649
5650void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5651 HUnresolvedInstanceFieldGet* instruction) {
5652 FieldAccessCallingConventionX86 calling_convention;
5653 codegen_->GenerateUnresolvedFieldAccess(instruction,
5654 instruction->GetFieldType(),
5655 instruction->GetFieldIndex(),
5656 instruction->GetDexPc(),
5657 calling_convention);
5658}
5659
5660void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5661 HUnresolvedInstanceFieldSet* instruction) {
5662 FieldAccessCallingConventionX86 calling_convention;
5663 codegen_->CreateUnresolvedFieldLocationSummary(
5664 instruction, instruction->GetFieldType(), calling_convention);
5665}
5666
5667void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5668 HUnresolvedInstanceFieldSet* instruction) {
5669 FieldAccessCallingConventionX86 calling_convention;
5670 codegen_->GenerateUnresolvedFieldAccess(instruction,
5671 instruction->GetFieldType(),
5672 instruction->GetFieldIndex(),
5673 instruction->GetDexPc(),
5674 calling_convention);
5675}
5676
5677void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5678 HUnresolvedStaticFieldGet* instruction) {
5679 FieldAccessCallingConventionX86 calling_convention;
5680 codegen_->CreateUnresolvedFieldLocationSummary(
5681 instruction, instruction->GetFieldType(), calling_convention);
5682}
5683
5684void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5685 HUnresolvedStaticFieldGet* instruction) {
5686 FieldAccessCallingConventionX86 calling_convention;
5687 codegen_->GenerateUnresolvedFieldAccess(instruction,
5688 instruction->GetFieldType(),
5689 instruction->GetFieldIndex(),
5690 instruction->GetDexPc(),
5691 calling_convention);
5692}
5693
5694void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5695 HUnresolvedStaticFieldSet* instruction) {
5696 FieldAccessCallingConventionX86 calling_convention;
5697 codegen_->CreateUnresolvedFieldLocationSummary(
5698 instruction, instruction->GetFieldType(), calling_convention);
5699}
5700
5701void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5702 HUnresolvedStaticFieldSet* instruction) {
5703 FieldAccessCallingConventionX86 calling_convention;
5704 codegen_->GenerateUnresolvedFieldAccess(instruction,
5705 instruction->GetFieldType(),
5706 instruction->GetFieldIndex(),
5707 instruction->GetDexPc(),
5708 calling_convention);
5709}
5710
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005712 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5713 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5714 ? Location::RequiresRegister()
5715 : Location::Any();
5716 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005717}
5718
Calin Juravle2ae48182016-03-16 14:05:09 +00005719void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5720 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005721 return;
5722 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005723 LocationSummary* locations = instruction->GetLocations();
5724 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005725
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005726 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005727 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005728}
5729
Calin Juravle2ae48182016-03-16 14:05:09 +00005730void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005731 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005732 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733
5734 LocationSummary* locations = instruction->GetLocations();
5735 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005736
5737 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005738 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005739 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005740 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005741 } else {
5742 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005743 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005744 __ jmp(slow_path->GetEntryLabel());
5745 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 }
5747 __ j(kEqual, slow_path->GetEntryLabel());
5748}
5749
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005750void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005751 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005752}
5753
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005754void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005757 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005758 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5759 object_array_get_with_read_barrier
5760 ? LocationSummary::kCallOnSlowPath
5761 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005762 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005763 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005764 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005765 locations->SetInAt(0, Location::RequiresRegister());
5766 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005767 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005768 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5769 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770 // The output overlaps in case of long: we don't want the low move
5771 // to overwrite the array's location. Likewise, in the case of an
5772 // object array get with read barriers enabled, we do not want the
5773 // move to overwrite the array's location, as we need it to emit
5774 // the read barrier.
5775 locations->SetOut(
5776 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5778 ? Location::kOutputOverlap
5779 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005780 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005781}
5782
5783void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5784 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 Location obj_loc = locations->InAt(0);
5786 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005787 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005788 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005789 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005792 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005793 case DataType::Type::kBool:
5794 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005795 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005796 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005797 break;
5798 }
5799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005801 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005802 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005803 break;
5804 }
5805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005806 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005807 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005808 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5809 // Branch cases into compressed and uncompressed for each index's type.
5810 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5811 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005812 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005813 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005814 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5815 "Expecting 0=compressed, 1=uncompressed");
5816 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005817 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5818 __ jmp(&done);
5819 __ Bind(&not_compressed);
5820 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5821 __ Bind(&done);
5822 } else {
5823 // Common case for charAt of array of char or when string compression's
5824 // feature is turned off.
5825 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5826 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005827 break;
5828 }
5829
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005830 case DataType::Type::kInt16: {
5831 Register out = out_loc.AsRegister<Register>();
5832 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5833 break;
5834 }
5835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005836 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005837 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005838 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005839 break;
5840 }
5841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005842 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005843 static_assert(
5844 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5845 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005846 // /* HeapReference<Object> */ out =
5847 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5848 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005849 // Note that a potential implicit null check is handled in this
5850 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5851 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005852 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005853 } else {
5854 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005855 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5856 codegen_->MaybeRecordImplicitNullCheck(instruction);
5857 // If read barriers are enabled, emit read barriers other than
5858 // Baker's using a slow path (and also unpoison the loaded
5859 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005860 if (index.IsConstant()) {
5861 uint32_t offset =
5862 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005863 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5864 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005865 codegen_->MaybeGenerateReadBarrierSlow(
5866 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5867 }
5868 }
5869 break;
5870 }
5871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005873 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005874 __ movl(out_loc.AsRegisterPairLow<Register>(),
5875 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5876 codegen_->MaybeRecordImplicitNullCheck(instruction);
5877 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5878 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005879 break;
5880 }
5881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005883 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005884 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005885 break;
5886 }
5887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005889 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005890 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005891 break;
5892 }
5893
Aart Bik66c158e2018-01-31 12:55:04 -08005894 case DataType::Type::kUint32:
5895 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005896 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005897 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005898 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005899 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005902 // Potential implicit null checks, in the case of reference or
5903 // long arrays, are handled in the previous switch statement.
5904 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005905 codegen_->MaybeRecordImplicitNullCheck(instruction);
5906 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005907}
5908
5909void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005911
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005912 bool needs_write_barrier =
5913 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005914 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005915
Vladimir Markoca6fff82017-10-03 14:49:14 +01005916 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005917 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005918 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005919
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005920 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005921 // We need the inputs to be different than the output in case of long operation.
5922 // In case of a byte operation, the register allocator does not support multiple
5923 // inputs that die at entry with one in a specific register.
5924 locations->SetInAt(0, Location::RequiresRegister());
5925 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5926 if (is_byte_type) {
5927 // Ensure the value is in a byte register.
5928 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005929 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005930 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005931 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005932 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5933 }
5934 if (needs_write_barrier) {
5935 // Temporary registers for the write barrier.
5936 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5937 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005938 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005939 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005940}
5941
5942void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5943 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005944 Location array_loc = locations->InAt(0);
5945 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005947 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005949 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005950 bool needs_write_barrier =
5951 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005952
5953 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005955 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005957 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005958 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005959 if (value.IsRegister()) {
5960 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005961 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005962 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005963 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005964 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005965 break;
5966 }
5967
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005968 case DataType::Type::kUint16:
5969 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005970 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005971 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005972 if (value.IsRegister()) {
5973 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005974 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005975 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005976 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005977 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005978 break;
5979 }
5980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005982 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005983 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005985 if (!value.IsRegister()) {
5986 // Just setting null.
5987 DCHECK(instruction->InputAt(2)->IsNullConstant());
5988 DCHECK(value.IsConstant()) << value;
5989 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005990 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005991 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005992 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005993 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005995
5996 DCHECK(needs_write_barrier);
5997 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005998 Location temp_loc = locations->GetTemp(0);
5999 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006000
6001 bool can_value_be_null = instruction->GetValueCanBeNull();
6002 NearLabel do_store;
6003 if (can_value_be_null) {
6004 __ testl(register_value, register_value);
6005 __ j(kEqual, &do_store);
6006 }
6007
6008 SlowPathCode* slow_path = nullptr;
6009 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006010 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006011 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006012
6013 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6014 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6015 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006016
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006017 // Note that when Baker read barriers are enabled, the type
6018 // checks are performed without read barriers. This is fine,
6019 // even in the case where a class object is in the from-space
6020 // after the flip, as a comparison involving such a type would
6021 // not produce a false positive; it may of course produce a
6022 // false negative, in which case we would take the ArraySet
6023 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006024
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006025 // /* HeapReference<Class> */ temp = array->klass_
6026 __ movl(temp, Address(array, class_offset));
6027 codegen_->MaybeRecordImplicitNullCheck(instruction);
6028 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006029
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006030 // /* HeapReference<Class> */ temp = temp->component_type_
6031 __ movl(temp, Address(temp, component_offset));
6032 // If heap poisoning is enabled, no need to unpoison `temp`
6033 // nor the object reference in `register_value->klass`, as
6034 // we are comparing two poisoned references.
6035 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006036
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006037 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006038 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006039 __ j(kEqual, &do_put);
6040 // If heap poisoning is enabled, the `temp` reference has
6041 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006042 __ MaybeUnpoisonHeapReference(temp);
6043
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006044 // If heap poisoning is enabled, no need to unpoison the
6045 // heap reference loaded below, as it is only used for a
6046 // comparison with null.
6047 __ cmpl(Address(temp, super_offset), Immediate(0));
6048 __ j(kNotEqual, slow_path->GetEntryLabel());
6049 __ Bind(&do_put);
6050 } else {
6051 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006052 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006053 }
6054
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006055 Register card = locations->GetTemp(1).AsRegister<Register>();
6056 codegen_->MarkGCCard(
6057 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6058
6059 if (can_value_be_null) {
6060 DCHECK(do_store.IsLinked());
6061 __ Bind(&do_store);
6062 }
6063
6064 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006065 if (kPoisonHeapReferences) {
6066 __ movl(temp, register_value);
6067 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006068 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006069 }
6070
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006071 __ movl(address, source);
6072
6073 if (can_value_be_null || !needs_type_check) {
6074 codegen_->MaybeRecordImplicitNullCheck(instruction);
6075 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006076
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006077 if (slow_path != nullptr) {
6078 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006079 }
6080
6081 break;
6082 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006083
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006084 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006085 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006086 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006087 if (value.IsRegister()) {
6088 __ movl(address, value.AsRegister<Register>());
6089 } else {
6090 DCHECK(value.IsConstant()) << value;
6091 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6092 __ movl(address, Immediate(v));
6093 }
6094 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006095 break;
6096 }
6097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006100 if (value.IsRegisterPair()) {
6101 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6102 value.AsRegisterPairLow<Register>());
6103 codegen_->MaybeRecordImplicitNullCheck(instruction);
6104 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6105 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006106 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006107 DCHECK(value.IsConstant());
6108 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6109 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6110 Immediate(Low32Bits(val)));
6111 codegen_->MaybeRecordImplicitNullCheck(instruction);
6112 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6113 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006114 }
6115 break;
6116 }
6117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006118 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006119 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006120 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006121 if (value.IsFpuRegister()) {
6122 __ movss(address, value.AsFpuRegister<XmmRegister>());
6123 } else {
6124 DCHECK(value.IsConstant());
6125 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6126 __ movl(address, Immediate(v));
6127 }
6128 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006129 break;
6130 }
6131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006132 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006133 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006134 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006135 if (value.IsFpuRegister()) {
6136 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6137 } else {
6138 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006139 Address address_hi =
6140 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006141 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6142 __ movl(address, Immediate(Low32Bits(v)));
6143 codegen_->MaybeRecordImplicitNullCheck(instruction);
6144 __ movl(address_hi, Immediate(High32Bits(v)));
6145 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006146 break;
6147 }
6148
Aart Bik66c158e2018-01-31 12:55:04 -08006149 case DataType::Type::kUint32:
6150 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006152 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006153 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006154 }
6155}
6156
6157void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006158 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006159 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006160 if (!instruction->IsEmittedAtUseSite()) {
6161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6162 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006163}
6164
6165void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006166 if (instruction->IsEmittedAtUseSite()) {
6167 return;
6168 }
6169
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006170 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006171 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006172 Register obj = locations->InAt(0).AsRegister<Register>();
6173 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006175 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006176 // Mask out most significant bit in case the array is String's array of char.
6177 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006178 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006180}
6181
6182void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006183 RegisterSet caller_saves = RegisterSet::Empty();
6184 InvokeRuntimeCallingConvention calling_convention;
6185 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6186 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6187 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006188 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006189 HInstruction* length = instruction->InputAt(1);
6190 if (!length->IsEmittedAtUseSite()) {
6191 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6192 }
jessicahandojo4877b792016-09-08 19:49:13 -07006193 // Need register to see array's length.
6194 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6195 locations->AddTemp(Location::RequiresRegister());
6196 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006197}
6198
6199void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006200 const bool is_string_compressed_char_at =
6201 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006202 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006203 Location index_loc = locations->InAt(0);
6204 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006205 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006206 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006207
Mark Mendell99dbd682015-04-22 16:18:52 -04006208 if (length_loc.IsConstant()) {
6209 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6210 if (index_loc.IsConstant()) {
6211 // BCE will remove the bounds check if we are guarenteed to pass.
6212 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6213 if (index < 0 || index >= length) {
6214 codegen_->AddSlowPath(slow_path);
6215 __ jmp(slow_path->GetEntryLabel());
6216 } else {
6217 // Some optimization after BCE may have generated this, and we should not
6218 // generate a bounds check if it is a valid range.
6219 }
6220 return;
6221 }
6222
6223 // We have to reverse the jump condition because the length is the constant.
6224 Register index_reg = index_loc.AsRegister<Register>();
6225 __ cmpl(index_reg, Immediate(length));
6226 codegen_->AddSlowPath(slow_path);
6227 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006228 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006229 HInstruction* array_length = instruction->InputAt(1);
6230 if (array_length->IsEmittedAtUseSite()) {
6231 // Address the length field in the array.
6232 DCHECK(array_length->IsArrayLength());
6233 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6234 Location array_loc = array_length->GetLocations()->InAt(0);
6235 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006236 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006237 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6238 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006239 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6240 __ movl(length_reg, array_len);
6241 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006242 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006243 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006244 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006245 // Checking bounds for general case:
6246 // Array of char or string's array with feature compression off.
6247 if (index_loc.IsConstant()) {
6248 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6249 __ cmpl(array_len, Immediate(value));
6250 } else {
6251 __ cmpl(array_len, index_loc.AsRegister<Register>());
6252 }
6253 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006254 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006255 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006256 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006257 }
6258 codegen_->AddSlowPath(slow_path);
6259 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006260 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006261}
6262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006263void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006264 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006265}
6266
6267void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006268 if (instruction->GetNext()->IsSuspendCheck() &&
6269 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6270 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6271 // The back edge will generate the suspend check.
6272 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6273 }
6274
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006275 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6276}
6277
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006278void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006279 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6280 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006281 // In suspend check slow path, usually there are no caller-save registers at all.
6282 // If SIMD instructions are present, however, we force spilling all live SIMD
6283 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006284 locations->SetCustomSlowPathCallerSaves(
6285 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006286}
6287
6288void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006289 HBasicBlock* block = instruction->GetBlock();
6290 if (block->GetLoopInformation() != nullptr) {
6291 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6292 // The back edge will generate the suspend check.
6293 return;
6294 }
6295 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6296 // The goto will generate the suspend check.
6297 return;
6298 }
6299 GenerateSuspendCheck(instruction, nullptr);
6300}
6301
6302void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6303 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006304 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006305 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6306 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006307 slow_path =
6308 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006309 instruction->SetSlowPath(slow_path);
6310 codegen_->AddSlowPath(slow_path);
6311 if (successor != nullptr) {
6312 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006313 }
6314 } else {
6315 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6316 }
6317
Andreas Gampe542451c2016-07-26 09:02:02 -07006318 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006319 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006320 if (successor == nullptr) {
6321 __ j(kNotEqual, slow_path->GetEntryLabel());
6322 __ Bind(slow_path->GetReturnLabel());
6323 } else {
6324 __ j(kEqual, codegen_->GetLabelOf(successor));
6325 __ jmp(slow_path->GetEntryLabel());
6326 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006327}
6328
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006329X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6330 return codegen_->GetAssembler();
6331}
6332
Aart Bikcfe50bb2017-12-12 14:54:12 -08006333void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006334 ScratchRegisterScope ensure_scratch(
6335 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6336 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6337 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006338
Aart Bikcfe50bb2017-12-12 14:54:12 -08006339 // Now that temp register is available (possibly spilled), move blocks of memory.
6340 for (int i = 0; i < number_of_words; i++) {
6341 __ movl(temp_reg, Address(ESP, src + stack_offset));
6342 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6343 stack_offset += kX86WordSize;
6344 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006345}
6346
6347void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006348 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006349 Location source = move->GetSource();
6350 Location destination = move->GetDestination();
6351
6352 if (source.IsRegister()) {
6353 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006354 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006355 } else if (destination.IsFpuRegister()) {
6356 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006357 } else {
6358 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006359 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006360 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006361 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006363 // Create stack space for 2 elements.
6364 __ subl(ESP, Immediate(2 * elem_size));
6365 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6366 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6367 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6368 // And remove the temporary stack space we allocated.
6369 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006370 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006371 if (destination.IsRegister()) {
6372 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6373 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006374 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006375 } else if (destination.IsRegisterPair()) {
6376 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6377 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6378 __ psrlq(src_reg, Immediate(32));
6379 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006380 } else if (destination.IsStackSlot()) {
6381 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006382 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006383 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006384 } else {
6385 DCHECK(destination.IsSIMDStackSlot());
6386 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006387 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006388 } else if (source.IsStackSlot()) {
6389 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006390 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006391 } else if (destination.IsFpuRegister()) {
6392 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006393 } else {
6394 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006395 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006396 }
6397 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006398 if (destination.IsRegisterPair()) {
6399 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6400 __ movl(destination.AsRegisterPairHigh<Register>(),
6401 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6402 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006403 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6404 } else {
6405 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006406 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006407 }
Aart Bik5576f372017-03-23 16:17:37 -07006408 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006409 if (destination.IsFpuRegister()) {
6410 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6411 } else {
6412 DCHECK(destination.IsSIMDStackSlot());
6413 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6414 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006415 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006416 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006417 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006418 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006419 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006420 if (value == 0) {
6421 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6422 } else {
6423 __ movl(destination.AsRegister<Register>(), Immediate(value));
6424 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006425 } else {
6426 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006427 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006428 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006429 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006430 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006431 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006432 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006433 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006434 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6435 if (value == 0) {
6436 // Easy handling of 0.0.
6437 __ xorps(dest, dest);
6438 } else {
6439 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006440 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6441 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6442 __ movl(temp, Immediate(value));
6443 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006444 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006445 } else {
6446 DCHECK(destination.IsStackSlot()) << destination;
6447 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6448 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006449 } else if (constant->IsLongConstant()) {
6450 int64_t value = constant->AsLongConstant()->GetValue();
6451 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.IsDoubleStackSlot()) {
6456 __ movl(Address(ESP, destination.GetStackIndex()), low);
6457 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6458 } else {
6459 __ movl(destination.AsRegisterPairLow<Register>(), low);
6460 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6461 }
6462 } else {
6463 DCHECK(constant->IsDoubleConstant());
6464 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006465 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006466 int32_t low_value = Low32Bits(value);
6467 int32_t high_value = High32Bits(value);
6468 Immediate low(low_value);
6469 Immediate high(high_value);
6470 if (destination.IsFpuRegister()) {
6471 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6472 if (value == 0) {
6473 // Easy handling of 0.0.
6474 __ xorpd(dest, dest);
6475 } else {
6476 __ pushl(high);
6477 __ pushl(low);
6478 __ movsd(dest, Address(ESP, 0));
6479 __ addl(ESP, Immediate(8));
6480 }
6481 } else {
6482 DCHECK(destination.IsDoubleStackSlot()) << destination;
6483 __ movl(Address(ESP, destination.GetStackIndex()), low);
6484 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6485 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006486 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006487 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006488 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006489 }
6490}
6491
Mark Mendella5c19ce2015-04-01 12:51:05 -04006492void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006493 Register suggested_scratch = reg == EAX ? EBX : EAX;
6494 ScratchRegisterScope ensure_scratch(
6495 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6496
6497 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6498 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6499 __ movl(Address(ESP, mem + stack_offset), reg);
6500 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006501}
6502
Mark Mendell7c8d0092015-01-26 11:21:33 -05006503void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006504 ScratchRegisterScope ensure_scratch(
6505 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6506
6507 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6508 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6509 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6510 __ movss(Address(ESP, mem + stack_offset), reg);
6511 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006512}
6513
Aart Bikcfe50bb2017-12-12 14:54:12 -08006514void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6515 size_t extra_slot = 4 * kX86WordSize;
6516 __ subl(ESP, Immediate(extra_slot));
6517 __ movups(Address(ESP, 0), XmmRegister(reg));
6518 ExchangeMemory(0, mem + extra_slot, 4);
6519 __ movups(XmmRegister(reg), Address(ESP, 0));
6520 __ addl(ESP, Immediate(extra_slot));
6521}
6522
6523void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006524 ScratchRegisterScope ensure_scratch1(
6525 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006526
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006527 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6528 ScratchRegisterScope ensure_scratch2(
6529 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006530
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006531 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6532 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006533
6534 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6535 for (int i = 0; i < number_of_words; i++) {
6536 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6537 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6538 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6539 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6540 stack_offset += kX86WordSize;
6541 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006542}
6543
6544void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006545 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006546 Location source = move->GetSource();
6547 Location destination = move->GetDestination();
6548
6549 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006550 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6551 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6552 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6553 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6554 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006555 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006556 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006557 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006558 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006559 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006560 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006561 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6562 // Use XOR Swap algorithm to avoid a temporary.
6563 DCHECK_NE(source.reg(), destination.reg());
6564 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6565 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6566 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6567 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6568 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6569 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6570 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006571 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6572 // Take advantage of the 16 bytes in the XMM register.
6573 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6574 Address stack(ESP, destination.GetStackIndex());
6575 // Load the double into the high doubleword.
6576 __ movhpd(reg, stack);
6577
6578 // Store the low double into the destination.
6579 __ movsd(stack, reg);
6580
6581 // Move the high double to the low double.
6582 __ psrldq(reg, Immediate(8));
6583 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6584 // Take advantage of the 16 bytes in the XMM register.
6585 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6586 Address stack(ESP, source.GetStackIndex());
6587 // Load the double into the high doubleword.
6588 __ movhpd(reg, stack);
6589
6590 // Store the low double into the destination.
6591 __ movsd(stack, reg);
6592
6593 // Move the high double to the low double.
6594 __ psrldq(reg, Immediate(8));
6595 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006596 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6597 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6598 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6599 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6600 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6601 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6602 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006603 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006604 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006605 }
6606}
6607
6608void ParallelMoveResolverX86::SpillScratch(int reg) {
6609 __ pushl(static_cast<Register>(reg));
6610}
6611
6612void ParallelMoveResolverX86::RestoreScratch(int reg) {
6613 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006614}
6615
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006616HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6617 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006618 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006619 case HLoadClass::LoadKind::kInvalid:
6620 LOG(FATAL) << "UNREACHABLE";
6621 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006622 case HLoadClass::LoadKind::kReferrersClass:
6623 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006624 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006625 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006626 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006627 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006628 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006629 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006630 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006631 DCHECK(Runtime::Current()->UseJitCompilation());
6632 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006633 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006634 break;
6635 }
6636 return desired_class_load_kind;
6637}
6638
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006639void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006640 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006641 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006642 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006643 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006644 cls,
6645 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006646 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006647 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006648 return;
6649 }
Vladimir Marko41559982017-01-06 14:04:23 +00006650 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006651
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006652 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6653 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006654 ? LocationSummary::kCallOnSlowPath
6655 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006656 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006657 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006658 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006659 }
6660
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006661 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006662 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006663 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006664 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006665 locations->SetInAt(0, Location::RequiresRegister());
6666 }
6667 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006668 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6669 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6670 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006671 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006672 } else {
6673 // For non-Baker read barrier we have a temp-clobbering call.
6674 }
6675 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006676}
6677
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006678Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006679 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006680 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006681 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006682 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006683 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006684 PatchInfo<Label>* info = &jit_class_patches_.back();
6685 return &info->label;
6686}
6687
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006688// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6689// move.
6690void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006691 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006692 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006693 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006694 return;
6695 }
Vladimir Marko41559982017-01-06 14:04:23 +00006696 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006697
Vladimir Marko41559982017-01-06 14:04:23 +00006698 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699 Location out_loc = locations->Out();
6700 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006702 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006703 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6704 ? kWithoutReadBarrier
6705 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006706 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006707 case HLoadClass::LoadKind::kReferrersClass: {
6708 DCHECK(!cls->CanCallRuntime());
6709 DCHECK(!cls->MustGenerateClinitCheck());
6710 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6711 Register current_method = locations->InAt(0).AsRegister<Register>();
6712 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006713 cls,
6714 out_loc,
6715 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006716 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006717 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006718 break;
6719 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006720 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006721 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6722 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006723 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006724 Register method_address = locations->InAt(0).AsRegister<Register>();
6725 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006726 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006727 break;
6728 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006729 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006730 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6731 Register method_address = locations->InAt(0).AsRegister<Register>();
6732 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006733 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6734 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006735 break;
6736 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006737 case HLoadClass::LoadKind::kBssEntry: {
6738 Register method_address = locations->InAt(0).AsRegister<Register>();
6739 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6740 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6741 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006742 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006743 generate_null_check = true;
6744 break;
6745 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006746 case HLoadClass::LoadKind::kJitBootImageAddress: {
6747 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6748 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6749 DCHECK_NE(address, 0u);
6750 __ movl(out, Immediate(address));
6751 break;
6752 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006753 case HLoadClass::LoadKind::kJitTableAddress: {
6754 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6755 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006756 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006757 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006758 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006759 break;
6760 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006761 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006762 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006763 LOG(FATAL) << "UNREACHABLE";
6764 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006765 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006766
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006767 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6768 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006769 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006770 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006771
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006772 if (generate_null_check) {
6773 __ testl(out, out);
6774 __ j(kEqual, slow_path->GetEntryLabel());
6775 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006776
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006777 if (cls->MustGenerateClinitCheck()) {
6778 GenerateClassInitializationCheck(slow_path, out);
6779 } else {
6780 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006781 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006782 }
6783}
6784
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006785void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6786 InvokeRuntimeCallingConvention calling_convention;
6787 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6788 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6789}
6790
6791void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6792 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6793}
6794
Orion Hodson18259d72018-04-12 11:18:23 +01006795void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6796 InvokeRuntimeCallingConvention calling_convention;
6797 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6798 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6799}
6800
6801void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6802 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6803}
6804
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006805void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6806 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006807 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808 locations->SetInAt(0, Location::RequiresRegister());
6809 if (check->HasUses()) {
6810 locations->SetOut(Location::SameAsFirstInput());
6811 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006812 // Rely on the type initialization to save everything we need.
6813 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814}
6815
6816void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006817 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006818 SlowPathCode* slow_path =
6819 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006820 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006821 GenerateClassInitializationCheck(slow_path,
6822 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006823}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006825void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006826 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006827 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6828 const size_t status_byte_offset =
6829 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006830 constexpr uint32_t shifted_visibly_initialized_value =
6831 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006832
Vladimir Markobf121912019-06-04 13:49:05 +01006833 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006834 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006835 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006836}
6837
Vladimir Marko175e7862018-03-27 09:03:13 +00006838void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6839 Register temp) {
6840 uint32_t path_to_root = check->GetBitstringPathToRoot();
6841 uint32_t mask = check->GetBitstringMask();
6842 DCHECK(IsPowerOfTwo(mask + 1));
6843 size_t mask_bits = WhichPowerOf2(mask + 1);
6844
6845 if (mask_bits == 16u) {
6846 // Compare the bitstring in memory.
6847 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6848 } else {
6849 // /* uint32_t */ temp = temp->status_
6850 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6851 // Compare the bitstring bits using SUB.
6852 __ subl(temp, Immediate(path_to_root));
6853 // Shift out bits that do not contribute to the comparison.
6854 __ shll(temp, Immediate(32u - mask_bits));
6855 }
6856}
6857
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006858HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6859 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006860 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006861 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006862 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006863 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006864 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006865 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006866 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006867 case HLoadString::LoadKind::kJitTableAddress:
6868 DCHECK(Runtime::Current()->UseJitCompilation());
6869 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006870 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006871 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006872 }
6873 return desired_string_load_kind;
6874}
6875
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006876void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006877 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006878 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006879 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006880 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006881 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006882 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006883 locations->SetInAt(0, Location::RequiresRegister());
6884 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006885 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006886 locations->SetOut(Location::RegisterLocation(EAX));
6887 } else {
6888 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006889 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6890 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006891 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006892 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006893 } else {
6894 // For non-Baker read barrier we have a temp-clobbering call.
6895 }
6896 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006897 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006898}
6899
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006900Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006901 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006902 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006903 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006904 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006905 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006906 PatchInfo<Label>* info = &jit_string_patches_.back();
6907 return &info->label;
6908}
6909
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006910// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6911// move.
6912void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006913 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006914 Location out_loc = locations->Out();
6915 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006916
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006917 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006918 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006919 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6920 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006921 Register method_address = locations->InAt(0).AsRegister<Register>();
6922 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006923 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006924 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006925 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006926 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006927 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6928 Register method_address = locations->InAt(0).AsRegister<Register>();
6929 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006930 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6931 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006932 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006933 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006934 case HLoadString::LoadKind::kBssEntry: {
6935 Register method_address = locations->InAt(0).AsRegister<Register>();
6936 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6937 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006938 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006939 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006940 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006941 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006942 codegen_->AddSlowPath(slow_path);
6943 __ testl(out, out);
6944 __ j(kEqual, slow_path->GetEntryLabel());
6945 __ Bind(slow_path->GetExitLabel());
6946 return;
6947 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006948 case HLoadString::LoadKind::kJitBootImageAddress: {
6949 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6950 DCHECK_NE(address, 0u);
6951 __ movl(out, Immediate(address));
6952 return;
6953 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006954 case HLoadString::LoadKind::kJitTableAddress: {
6955 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6956 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006957 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006958 // /* GcRoot<mirror::String> */ out = *address
6959 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6960 return;
6961 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006962 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006963 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006964 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006965
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006966 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006967 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006968 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006969 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006970 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6971 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006972}
6973
David Brazdilcb1c0552015-08-04 16:22:25 +01006974static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006975 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006976}
6977
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006978void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6979 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006980 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006981 locations->SetOut(Location::RequiresRegister());
6982}
6983
6984void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006985 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6986}
6987
6988void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006989 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006990}
6991
6992void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6993 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006994}
6995
6996void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006997 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6998 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006999 InvokeRuntimeCallingConvention calling_convention;
7000 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7001}
7002
7003void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007004 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007005 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007006}
7007
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007008// Temp is used for read barrier.
7009static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7010 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007011 !kUseBakerReadBarrier &&
7012 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007013 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007014 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7015 return 1;
7016 }
7017 return 0;
7018}
7019
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007020// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7021// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007022// The other checks have one temp for loading the object's class.
7023static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007024 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007025 return 2;
7026 }
7027 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007028}
7029
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007030void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007031 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007032 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007033 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007034 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007035 case TypeCheckKind::kExactCheck:
7036 case TypeCheckKind::kAbstractClassCheck:
7037 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007038 case TypeCheckKind::kArrayObjectCheck: {
7039 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7040 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7041 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007042 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007043 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007044 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007045 case TypeCheckKind::kUnresolvedCheck:
7046 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007047 call_kind = LocationSummary::kCallOnSlowPath;
7048 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007049 case TypeCheckKind::kBitstringCheck:
7050 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007051 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007052
Vladimir Markoca6fff82017-10-03 14:49:14 +01007053 LocationSummary* locations =
7054 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007055 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007056 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007057 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007058 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007059 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7060 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7061 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7062 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7063 } else {
7064 locations->SetInAt(1, Location::Any());
7065 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007066 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7067 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007068 // When read barriers are enabled, we need a temporary register for some cases.
7069 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007070}
7071
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007072void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007073 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007074 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007075 Location obj_loc = locations->InAt(0);
7076 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007077 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007078 Location out_loc = locations->Out();
7079 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007080 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7081 DCHECK_LE(num_temps, 1u);
7082 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007083 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007084 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7085 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7086 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007087 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007088 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007089
7090 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007091 // Avoid null check if we know obj is not null.
7092 if (instruction->MustDoNullCheck()) {
7093 __ testl(obj, obj);
7094 __ j(kEqual, &zero);
7095 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007096
Roland Levillain7c1559a2015-12-15 10:55:36 +00007097 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007098 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007099 ReadBarrierOption read_barrier_option =
7100 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007101 // /* HeapReference<Class> */ out = obj->klass_
7102 GenerateReferenceLoadTwoRegisters(instruction,
7103 out_loc,
7104 obj_loc,
7105 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007106 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007107 if (cls.IsRegister()) {
7108 __ cmpl(out, cls.AsRegister<Register>());
7109 } else {
7110 DCHECK(cls.IsStackSlot()) << cls;
7111 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7112 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007113
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007114 // Classes must be equal for the instanceof to succeed.
7115 __ j(kNotEqual, &zero);
7116 __ movl(out, Immediate(1));
7117 __ jmp(&done);
7118 break;
7119 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007120
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007121 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007122 ReadBarrierOption read_barrier_option =
7123 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007124 // /* HeapReference<Class> */ out = obj->klass_
7125 GenerateReferenceLoadTwoRegisters(instruction,
7126 out_loc,
7127 obj_loc,
7128 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007129 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130 // If the class is abstract, we eagerly fetch the super class of the
7131 // object to avoid doing a comparison we know will fail.
7132 NearLabel loop;
7133 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007134 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007135 GenerateReferenceLoadOneRegister(instruction,
7136 out_loc,
7137 super_offset,
7138 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007139 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007140 __ testl(out, out);
7141 // If `out` is null, we use it for the result, and jump to `done`.
7142 __ j(kEqual, &done);
7143 if (cls.IsRegister()) {
7144 __ cmpl(out, cls.AsRegister<Register>());
7145 } else {
7146 DCHECK(cls.IsStackSlot()) << cls;
7147 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7148 }
7149 __ j(kNotEqual, &loop);
7150 __ movl(out, Immediate(1));
7151 if (zero.IsLinked()) {
7152 __ jmp(&done);
7153 }
7154 break;
7155 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007156
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007157 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007158 ReadBarrierOption read_barrier_option =
7159 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007160 // /* HeapReference<Class> */ out = obj->klass_
7161 GenerateReferenceLoadTwoRegisters(instruction,
7162 out_loc,
7163 obj_loc,
7164 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007165 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007166 // Walk over the class hierarchy to find a match.
7167 NearLabel loop, success;
7168 __ Bind(&loop);
7169 if (cls.IsRegister()) {
7170 __ cmpl(out, cls.AsRegister<Register>());
7171 } else {
7172 DCHECK(cls.IsStackSlot()) << cls;
7173 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7174 }
7175 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007176 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007177 GenerateReferenceLoadOneRegister(instruction,
7178 out_loc,
7179 super_offset,
7180 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007181 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007182 __ testl(out, out);
7183 __ j(kNotEqual, &loop);
7184 // If `out` is null, we use it for the result, and jump to `done`.
7185 __ jmp(&done);
7186 __ Bind(&success);
7187 __ movl(out, Immediate(1));
7188 if (zero.IsLinked()) {
7189 __ jmp(&done);
7190 }
7191 break;
7192 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007193
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007194 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007195 ReadBarrierOption read_barrier_option =
7196 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007197 // /* HeapReference<Class> */ out = obj->klass_
7198 GenerateReferenceLoadTwoRegisters(instruction,
7199 out_loc,
7200 obj_loc,
7201 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007202 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007203 // Do an exact check.
7204 NearLabel exact_check;
7205 if (cls.IsRegister()) {
7206 __ cmpl(out, cls.AsRegister<Register>());
7207 } else {
7208 DCHECK(cls.IsStackSlot()) << cls;
7209 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7210 }
7211 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007212 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007213 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007214 GenerateReferenceLoadOneRegister(instruction,
7215 out_loc,
7216 component_offset,
7217 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007218 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007219 __ testl(out, out);
7220 // If `out` is null, we use it for the result, and jump to `done`.
7221 __ j(kEqual, &done);
7222 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7223 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007224 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007225 __ movl(out, Immediate(1));
7226 __ jmp(&done);
7227 break;
7228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007230 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007231 // No read barrier since the slow path will retry upon failure.
7232 // /* HeapReference<Class> */ out = obj->klass_
7233 GenerateReferenceLoadTwoRegisters(instruction,
7234 out_loc,
7235 obj_loc,
7236 class_offset,
7237 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007238 if (cls.IsRegister()) {
7239 __ cmpl(out, cls.AsRegister<Register>());
7240 } else {
7241 DCHECK(cls.IsStackSlot()) << cls;
7242 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7243 }
7244 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007245 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007246 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007247 codegen_->AddSlowPath(slow_path);
7248 __ j(kNotEqual, slow_path->GetEntryLabel());
7249 __ movl(out, Immediate(1));
7250 if (zero.IsLinked()) {
7251 __ jmp(&done);
7252 }
7253 break;
7254 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007255
Calin Juravle98893e12015-10-02 21:05:03 +01007256 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007257 case TypeCheckKind::kInterfaceCheck: {
7258 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007259 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007260 // cases.
7261 //
7262 // We cannot directly call the InstanceofNonTrivial runtime
7263 // entry point without resorting to a type checking slow path
7264 // here (i.e. by calling InvokeRuntime directly), as it would
7265 // require to assign fixed registers for the inputs of this
7266 // HInstanceOf instruction (following the runtime calling
7267 // convention), which might be cluttered by the potential first
7268 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007269 //
7270 // TODO: Introduce a new runtime entry point taking the object
7271 // to test (instead of its class) as argument, and let it deal
7272 // with the read barrier issues. This will let us refactor this
7273 // case of the `switch` code as it was previously (with a direct
7274 // call to the runtime not using a type checking slow path).
7275 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007276 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007277 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007278 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007279 codegen_->AddSlowPath(slow_path);
7280 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007281 if (zero.IsLinked()) {
7282 __ jmp(&done);
7283 }
7284 break;
7285 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007286
7287 case TypeCheckKind::kBitstringCheck: {
7288 // /* HeapReference<Class> */ temp = obj->klass_
7289 GenerateReferenceLoadTwoRegisters(instruction,
7290 out_loc,
7291 obj_loc,
7292 class_offset,
7293 kWithoutReadBarrier);
7294
7295 GenerateBitstringTypeCheckCompare(instruction, out);
7296 __ j(kNotEqual, &zero);
7297 __ movl(out, Immediate(1));
7298 __ jmp(&done);
7299 break;
7300 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007301 }
7302
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007303 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007304 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007305 __ xorl(out, out);
7306 }
7307
7308 if (done.IsLinked()) {
7309 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007310 }
7311
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007312 if (slow_path != nullptr) {
7313 __ Bind(slow_path->GetExitLabel());
7314 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007315}
7316
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007317void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007318 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007319 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007320 LocationSummary* locations =
7321 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007322 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007323 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7324 // Require a register for the interface check since there is a loop that compares the class to
7325 // a memory address.
7326 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007327 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7328 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7329 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7330 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007331 } else {
7332 locations->SetInAt(1, Location::Any());
7333 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007334 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007335 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7336}
7337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007338void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007339 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007340 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007341 Location obj_loc = locations->InAt(0);
7342 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007343 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344 Location temp_loc = locations->GetTemp(0);
7345 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007346 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7347 DCHECK_GE(num_temps, 1u);
7348 DCHECK_LE(num_temps, 2u);
7349 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7350 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7351 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7352 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7353 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7354 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7355 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7356 const uint32_t object_array_data_offset =
7357 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007358
Vladimir Marko87584542017-12-12 17:47:52 +00007359 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007360 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007361 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7362 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007363 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007364
Roland Levillain0d5a2812015-11-13 10:07:31 +00007365 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007366 // Avoid null check if we know obj is not null.
7367 if (instruction->MustDoNullCheck()) {
7368 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007369 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007370 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007371
Roland Levillain0d5a2812015-11-13 10:07:31 +00007372 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007373 case TypeCheckKind::kExactCheck:
7374 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007375 // /* HeapReference<Class> */ temp = obj->klass_
7376 GenerateReferenceLoadTwoRegisters(instruction,
7377 temp_loc,
7378 obj_loc,
7379 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007380 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007381
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007382 if (cls.IsRegister()) {
7383 __ cmpl(temp, cls.AsRegister<Register>());
7384 } else {
7385 DCHECK(cls.IsStackSlot()) << cls;
7386 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7387 }
7388 // Jump to slow path for throwing the exception or doing a
7389 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007390 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007391 break;
7392 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007393
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007394 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007395 // /* HeapReference<Class> */ temp = obj->klass_
7396 GenerateReferenceLoadTwoRegisters(instruction,
7397 temp_loc,
7398 obj_loc,
7399 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007400 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007402 // If the class is abstract, we eagerly fetch the super class of the
7403 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007404 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007405 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007406 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007407 GenerateReferenceLoadOneRegister(instruction,
7408 temp_loc,
7409 super_offset,
7410 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007411 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007412
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007413 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7414 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007415 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007416 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007417
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007418 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007419 if (cls.IsRegister()) {
7420 __ cmpl(temp, cls.AsRegister<Register>());
7421 } else {
7422 DCHECK(cls.IsStackSlot()) << cls;
7423 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7424 }
7425 __ j(kNotEqual, &loop);
7426 break;
7427 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007428
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007429 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007430 // /* HeapReference<Class> */ temp = obj->klass_
7431 GenerateReferenceLoadTwoRegisters(instruction,
7432 temp_loc,
7433 obj_loc,
7434 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007435 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007437 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007438 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007439 __ Bind(&loop);
7440 if (cls.IsRegister()) {
7441 __ cmpl(temp, cls.AsRegister<Register>());
7442 } else {
7443 DCHECK(cls.IsStackSlot()) << cls;
7444 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7445 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007446 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007447
Roland Levillain0d5a2812015-11-13 10:07:31 +00007448 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007449 GenerateReferenceLoadOneRegister(instruction,
7450 temp_loc,
7451 super_offset,
7452 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007453 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007454
7455 // If the class reference currently in `temp` is not null, jump
7456 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007457 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007458 __ j(kNotZero, &loop);
7459 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007460 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007461 break;
7462 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007464 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007465 // /* HeapReference<Class> */ temp = obj->klass_
7466 GenerateReferenceLoadTwoRegisters(instruction,
7467 temp_loc,
7468 obj_loc,
7469 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007470 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007471
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007472 // Do an exact check.
7473 if (cls.IsRegister()) {
7474 __ cmpl(temp, cls.AsRegister<Register>());
7475 } else {
7476 DCHECK(cls.IsStackSlot()) << cls;
7477 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7478 }
7479 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007480
7481 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007482 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007483 GenerateReferenceLoadOneRegister(instruction,
7484 temp_loc,
7485 component_offset,
7486 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007487 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007488
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007489 // If the component type is null (i.e. the object not an array), jump to the slow path to
7490 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007491 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007492 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007493
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007494 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007495 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007496 break;
7497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007498
Calin Juravle98893e12015-10-02 21:05:03 +01007499 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007500 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007501 // We cannot directly call the CheckCast runtime entry point
7502 // without resorting to a type checking slow path here (i.e. by
7503 // calling InvokeRuntime directly), as it would require to
7504 // assign fixed registers for the inputs of this HInstanceOf
7505 // instruction (following the runtime calling convention), which
7506 // might be cluttered by the potential first read barrier
7507 // emission at the beginning of this method.
7508 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007509 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007510
7511 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007512 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7513 // We can not get false positives by doing this.
7514 // /* HeapReference<Class> */ temp = obj->klass_
7515 GenerateReferenceLoadTwoRegisters(instruction,
7516 temp_loc,
7517 obj_loc,
7518 class_offset,
7519 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007520
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007521 // /* HeapReference<Class> */ temp = temp->iftable_
7522 GenerateReferenceLoadTwoRegisters(instruction,
7523 temp_loc,
7524 temp_loc,
7525 iftable_offset,
7526 kWithoutReadBarrier);
7527 // Iftable is never null.
7528 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7529 // Maybe poison the `cls` for direct comparison with memory.
7530 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7531 // Loop through the iftable and check if any class matches.
7532 NearLabel start_loop;
7533 __ Bind(&start_loop);
7534 // Need to subtract first to handle the empty array case.
7535 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7536 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7537 // Go to next interface if the classes do not match.
7538 __ cmpl(cls.AsRegister<Register>(),
7539 CodeGeneratorX86::ArrayAddress(temp,
7540 maybe_temp2_loc,
7541 TIMES_4,
7542 object_array_data_offset));
7543 __ j(kNotEqual, &start_loop);
7544 // If `cls` was poisoned above, unpoison it.
7545 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007546 break;
7547 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007548
7549 case TypeCheckKind::kBitstringCheck: {
7550 // /* HeapReference<Class> */ temp = obj->klass_
7551 GenerateReferenceLoadTwoRegisters(instruction,
7552 temp_loc,
7553 obj_loc,
7554 class_offset,
7555 kWithoutReadBarrier);
7556
7557 GenerateBitstringTypeCheckCompare(instruction, temp);
7558 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7559 break;
7560 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007561 }
7562 __ Bind(&done);
7563
Roland Levillain0d5a2812015-11-13 10:07:31 +00007564 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007565}
7566
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007567void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007568 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7569 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007570 InvokeRuntimeCallingConvention calling_convention;
7571 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7572}
7573
7574void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007575 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7576 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007577 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007578 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007579 if (instruction->IsEnter()) {
7580 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7581 } else {
7582 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7583 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007584}
7585
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307586void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7587 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7588 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7589 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7590 locations->SetInAt(0, Location::RequiresRegister());
7591 locations->SetInAt(1, Location::RequiresRegister());
7592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7593}
7594
7595void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7596 LocationSummary* locations = instruction->GetLocations();
7597 Location first = locations->InAt(0);
7598 Location second = locations->InAt(1);
7599 Location dest = locations->Out();
7600 if (instruction->GetResultType() == DataType::Type::kInt32) {
7601 __ andn(dest.AsRegister<Register>(),
7602 first.AsRegister<Register>(),
7603 second.AsRegister<Register>());
7604 } else {
7605 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7606 __ andn(dest.AsRegisterPairLow<Register>(),
7607 first.AsRegisterPairLow<Register>(),
7608 second.AsRegisterPairLow<Register>());
7609 __ andn(dest.AsRegisterPairHigh<Register>(),
7610 first.AsRegisterPairHigh<Register>(),
7611 second.AsRegisterPairHigh<Register>());
7612 }
7613}
7614
7615void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7616 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7617 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7618 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7619 locations->SetInAt(0, Location::RequiresRegister());
7620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7621}
7622
7623void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7624 HX86MaskOrResetLeastSetBit* instruction) {
7625 LocationSummary* locations = instruction->GetLocations();
7626 Location src = locations->InAt(0);
7627 Location dest = locations->Out();
7628 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7629 switch (instruction->GetOpKind()) {
7630 case HInstruction::kAnd:
7631 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7632 break;
7633 case HInstruction::kXor:
7634 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7635 break;
7636 default:
7637 LOG(FATAL) << "Unreachable";
7638 }
7639}
7640
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007641void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7642void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7643void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7644
7645void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7646 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007647 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007648 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7649 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007650 locations->SetInAt(0, Location::RequiresRegister());
7651 locations->SetInAt(1, Location::Any());
7652 locations->SetOut(Location::SameAsFirstInput());
7653}
7654
7655void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7656 HandleBitwiseOperation(instruction);
7657}
7658
7659void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7660 HandleBitwiseOperation(instruction);
7661}
7662
7663void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7664 HandleBitwiseOperation(instruction);
7665}
7666
7667void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7668 LocationSummary* locations = instruction->GetLocations();
7669 Location first = locations->InAt(0);
7670 Location second = locations->InAt(1);
7671 DCHECK(first.Equals(locations->Out()));
7672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007673 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007674 if (second.IsRegister()) {
7675 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007676 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007677 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007678 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007679 } else {
7680 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007681 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007682 }
7683 } else if (second.IsConstant()) {
7684 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007685 __ andl(first.AsRegister<Register>(),
7686 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007687 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007688 __ orl(first.AsRegister<Register>(),
7689 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007690 } else {
7691 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007692 __ xorl(first.AsRegister<Register>(),
7693 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007694 }
7695 } else {
7696 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007697 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007698 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007699 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007700 } else {
7701 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007702 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007703 }
7704 }
7705 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007706 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007707 if (second.IsRegisterPair()) {
7708 if (instruction->IsAnd()) {
7709 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7710 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7711 } else if (instruction->IsOr()) {
7712 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7713 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7714 } else {
7715 DCHECK(instruction->IsXor());
7716 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7717 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7718 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007719 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007720 if (instruction->IsAnd()) {
7721 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7722 __ andl(first.AsRegisterPairHigh<Register>(),
7723 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7724 } else if (instruction->IsOr()) {
7725 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7726 __ orl(first.AsRegisterPairHigh<Register>(),
7727 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7728 } else {
7729 DCHECK(instruction->IsXor());
7730 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7731 __ xorl(first.AsRegisterPairHigh<Register>(),
7732 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7733 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007734 } else {
7735 DCHECK(second.IsConstant()) << second;
7736 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007737 int32_t low_value = Low32Bits(value);
7738 int32_t high_value = High32Bits(value);
7739 Immediate low(low_value);
7740 Immediate high(high_value);
7741 Register first_low = first.AsRegisterPairLow<Register>();
7742 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007743 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007744 if (low_value == 0) {
7745 __ xorl(first_low, first_low);
7746 } else if (low_value != -1) {
7747 __ andl(first_low, low);
7748 }
7749 if (high_value == 0) {
7750 __ xorl(first_high, first_high);
7751 } else if (high_value != -1) {
7752 __ andl(first_high, high);
7753 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007754 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007755 if (low_value != 0) {
7756 __ orl(first_low, low);
7757 }
7758 if (high_value != 0) {
7759 __ orl(first_high, high);
7760 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007761 } else {
7762 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007763 if (low_value != 0) {
7764 __ xorl(first_low, low);
7765 }
7766 if (high_value != 0) {
7767 __ xorl(first_high, high);
7768 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007769 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007770 }
7771 }
7772}
7773
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007774void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7775 HInstruction* instruction,
7776 Location out,
7777 uint32_t offset,
7778 Location maybe_temp,
7779 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007780 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007781 if (read_barrier_option == kWithReadBarrier) {
7782 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007783 if (kUseBakerReadBarrier) {
7784 // Load with fast path based Baker's read barrier.
7785 // /* HeapReference<Object> */ out = *(out + offset)
7786 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007787 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007788 } else {
7789 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007790 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007791 // in the following move operation, as we will need it for the
7792 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007793 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007794 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007795 // /* HeapReference<Object> */ out = *(out + offset)
7796 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007797 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007798 }
7799 } else {
7800 // Plain load with no read barrier.
7801 // /* HeapReference<Object> */ out = *(out + offset)
7802 __ movl(out_reg, Address(out_reg, offset));
7803 __ MaybeUnpoisonHeapReference(out_reg);
7804 }
7805}
7806
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007807void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7808 HInstruction* instruction,
7809 Location out,
7810 Location obj,
7811 uint32_t offset,
7812 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007813 Register out_reg = out.AsRegister<Register>();
7814 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007815 if (read_barrier_option == kWithReadBarrier) {
7816 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007817 if (kUseBakerReadBarrier) {
7818 // Load with fast path based Baker's read barrier.
7819 // /* HeapReference<Object> */ out = *(obj + offset)
7820 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007821 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007822 } else {
7823 // Load with slow path based read barrier.
7824 // /* HeapReference<Object> */ out = *(obj + offset)
7825 __ movl(out_reg, Address(obj_reg, offset));
7826 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7827 }
7828 } else {
7829 // Plain load with no read barrier.
7830 // /* HeapReference<Object> */ out = *(obj + offset)
7831 __ movl(out_reg, Address(obj_reg, offset));
7832 __ MaybeUnpoisonHeapReference(out_reg);
7833 }
7834}
7835
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007836void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7837 HInstruction* instruction,
7838 Location root,
7839 const Address& address,
7840 Label* fixup_label,
7841 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007842 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007843 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007844 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007845 if (kUseBakerReadBarrier) {
7846 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7847 // Baker's read barrier are used:
7848 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007849 // root = obj.field;
7850 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7851 // if (temp != null) {
7852 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007853 // }
7854
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007855 // /* GcRoot<mirror::Object> */ root = *address
7856 __ movl(root_reg, address);
7857 if (fixup_label != nullptr) {
7858 __ Bind(fixup_label);
7859 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007860 static_assert(
7861 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7862 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7863 "have different sizes.");
7864 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7865 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7866 "have different sizes.");
7867
Vladimir Marko953437b2016-08-24 08:30:46 +00007868 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007869 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007870 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007871 codegen_->AddSlowPath(slow_path);
7872
Roland Levillaind966ce72017-02-09 16:20:14 +00007873 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7874 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007875 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007876 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7877 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007878 __ j(kNotEqual, slow_path->GetEntryLabel());
7879 __ Bind(slow_path->GetExitLabel());
7880 } else {
7881 // GC root loaded through a slow path for read barriers other
7882 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007883 // /* GcRoot<mirror::Object>* */ root = address
7884 __ leal(root_reg, address);
7885 if (fixup_label != nullptr) {
7886 __ Bind(fixup_label);
7887 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007888 // /* mirror::Object* */ root = root->Read()
7889 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7890 }
7891 } else {
7892 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007893 // /* GcRoot<mirror::Object> */ root = *address
7894 __ movl(root_reg, address);
7895 if (fixup_label != nullptr) {
7896 __ Bind(fixup_label);
7897 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007898 // Note that GC roots are not affected by heap poisoning, thus we
7899 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007900 }
7901}
7902
7903void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7904 Location ref,
7905 Register obj,
7906 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007907 bool needs_null_check) {
7908 DCHECK(kEmitCompilerReadBarrier);
7909 DCHECK(kUseBakerReadBarrier);
7910
7911 // /* HeapReference<Object> */ ref = *(obj + offset)
7912 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007913 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007914}
7915
7916void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7917 Location ref,
7918 Register obj,
7919 uint32_t data_offset,
7920 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007921 bool needs_null_check) {
7922 DCHECK(kEmitCompilerReadBarrier);
7923 DCHECK(kUseBakerReadBarrier);
7924
Roland Levillain3d312422016-06-23 13:53:42 +01007925 static_assert(
7926 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7927 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007928 // /* HeapReference<Object> */ ref =
7929 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007930 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007931 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007932}
7933
7934void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7935 Location ref,
7936 Register obj,
7937 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007938 bool needs_null_check,
7939 bool always_update_field,
7940 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007941 DCHECK(kEmitCompilerReadBarrier);
7942 DCHECK(kUseBakerReadBarrier);
7943
7944 // In slow path based read barriers, the read barrier call is
7945 // inserted after the original load. However, in fast path based
7946 // Baker's read barriers, we need to perform the load of
7947 // mirror::Object::monitor_ *before* the original reference load.
7948 // This load-load ordering is required by the read barrier.
7949 // The fast path/slow path (for Baker's algorithm) should look like:
7950 //
7951 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7952 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7953 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007954 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007955 // if (is_gray) {
7956 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7957 // }
7958 //
7959 // Note: the original implementation in ReadBarrier::Barrier is
7960 // slightly more complex as:
7961 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007962 // the high-bits of rb_state, which are expected to be all zeroes
7963 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7964 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007965 // - it performs additional checks that we do not do here for
7966 // performance reasons.
7967
7968 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007969 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7970
Vladimir Marko953437b2016-08-24 08:30:46 +00007971 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007972 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007973 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007974 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7975 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7976 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7977
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007978 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007979 // ref = ReadBarrier::Mark(ref);
7980 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7981 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007982 if (needs_null_check) {
7983 MaybeRecordImplicitNullCheck(instruction);
7984 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007985
7986 // Load fence to prevent load-load reordering.
7987 // Note that this is a no-op, thanks to the x86 memory model.
7988 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7989
7990 // The actual reference load.
7991 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007992 __ movl(ref_reg, src); // Flags are unaffected.
7993
7994 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7995 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007996 SlowPathCode* slow_path;
7997 if (always_update_field) {
7998 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007999 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008000 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008001 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008002 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008003 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008004 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008005 AddSlowPath(slow_path);
8006
8007 // We have done the "if" of the gray bit check above, now branch based on the flags.
8008 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008009
8010 // Object* ref = ref_addr->AsMirrorPtr()
8011 __ MaybeUnpoisonHeapReference(ref_reg);
8012
Roland Levillain7c1559a2015-12-15 10:55:36 +00008013 __ Bind(slow_path->GetExitLabel());
8014}
8015
8016void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8017 Location out,
8018 Location ref,
8019 Location obj,
8020 uint32_t offset,
8021 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008022 DCHECK(kEmitCompilerReadBarrier);
8023
Roland Levillain7c1559a2015-12-15 10:55:36 +00008024 // Insert a slow path based read barrier *after* the reference load.
8025 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008026 // If heap poisoning is enabled, the unpoisoning of the loaded
8027 // reference will be carried out by the runtime within the slow
8028 // path.
8029 //
8030 // Note that `ref` currently does not get unpoisoned (when heap
8031 // poisoning is enabled), which is alright as the `ref` argument is
8032 // not used by the artReadBarrierSlow entry point.
8033 //
8034 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008035 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008036 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8037 AddSlowPath(slow_path);
8038
Roland Levillain0d5a2812015-11-13 10:07:31 +00008039 __ jmp(slow_path->GetEntryLabel());
8040 __ Bind(slow_path->GetExitLabel());
8041}
8042
Roland Levillain7c1559a2015-12-15 10:55:36 +00008043void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8044 Location out,
8045 Location ref,
8046 Location obj,
8047 uint32_t offset,
8048 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008049 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008050 // Baker's read barriers shall be handled by the fast path
8051 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8052 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008053 // If heap poisoning is enabled, unpoisoning will be taken care of
8054 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008055 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008056 } else if (kPoisonHeapReferences) {
8057 __ UnpoisonHeapReference(out.AsRegister<Register>());
8058 }
8059}
8060
Roland Levillain7c1559a2015-12-15 10:55:36 +00008061void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8062 Location out,
8063 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008064 DCHECK(kEmitCompilerReadBarrier);
8065
Roland Levillain7c1559a2015-12-15 10:55:36 +00008066 // Insert a slow path based read barrier *after* the GC root load.
8067 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008068 // Note that GC roots are not affected by heap poisoning, so we do
8069 // not need to do anything special for this here.
8070 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008071 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008072 AddSlowPath(slow_path);
8073
Roland Levillain0d5a2812015-11-13 10:07:31 +00008074 __ jmp(slow_path->GetEntryLabel());
8075 __ Bind(slow_path->GetExitLabel());
8076}
8077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008078void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008079 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008080 LOG(FATAL) << "Unreachable";
8081}
8082
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008083void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008084 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008085 LOG(FATAL) << "Unreachable";
8086}
8087
Mark Mendellfe57faa2015-09-18 09:26:15 -04008088// Simple implementation of packed switch - generate cascaded compare/jumps.
8089void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8090 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008091 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008092 locations->SetInAt(0, Location::RequiresRegister());
8093}
8094
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008095void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8096 int32_t lower_bound,
8097 uint32_t num_entries,
8098 HBasicBlock* switch_block,
8099 HBasicBlock* default_block) {
8100 // Figure out the correct compare values and jump conditions.
8101 // Handle the first compare/branch as a special case because it might
8102 // jump to the default case.
8103 DCHECK_GT(num_entries, 2u);
8104 Condition first_condition;
8105 uint32_t index;
8106 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8107 if (lower_bound != 0) {
8108 first_condition = kLess;
8109 __ cmpl(value_reg, Immediate(lower_bound));
8110 __ j(first_condition, codegen_->GetLabelOf(default_block));
8111 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008112
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008113 index = 1;
8114 } else {
8115 // Handle all the compare/jumps below.
8116 first_condition = kBelow;
8117 index = 0;
8118 }
8119
8120 // Handle the rest of the compare/jumps.
8121 for (; index + 1 < num_entries; index += 2) {
8122 int32_t compare_to_value = lower_bound + index + 1;
8123 __ cmpl(value_reg, Immediate(compare_to_value));
8124 // Jump to successors[index] if value < case_value[index].
8125 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8126 // Jump to successors[index + 1] if value == case_value[index + 1].
8127 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8128 }
8129
8130 if (index != num_entries) {
8131 // There are an odd number of entries. Handle the last one.
8132 DCHECK_EQ(index + 1, num_entries);
8133 __ cmpl(value_reg, Immediate(lower_bound + index));
8134 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008135 }
8136
8137 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008138 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8139 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008140 }
8141}
8142
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008143void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8144 int32_t lower_bound = switch_instr->GetStartValue();
8145 uint32_t num_entries = switch_instr->GetNumEntries();
8146 LocationSummary* locations = switch_instr->GetLocations();
8147 Register value_reg = locations->InAt(0).AsRegister<Register>();
8148
8149 GenPackedSwitchWithCompares(value_reg,
8150 lower_bound,
8151 num_entries,
8152 switch_instr->GetBlock(),
8153 switch_instr->GetDefaultBlock());
8154}
8155
Mark Mendell805b3b52015-09-18 14:10:29 -04008156void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8157 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008158 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008159 locations->SetInAt(0, Location::RequiresRegister());
8160
8161 // Constant area pointer.
8162 locations->SetInAt(1, Location::RequiresRegister());
8163
8164 // And the temporary we need.
8165 locations->AddTemp(Location::RequiresRegister());
8166}
8167
8168void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8169 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008170 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008171 LocationSummary* locations = switch_instr->GetLocations();
8172 Register value_reg = locations->InAt(0).AsRegister<Register>();
8173 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8174
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008175 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8176 GenPackedSwitchWithCompares(value_reg,
8177 lower_bound,
8178 num_entries,
8179 switch_instr->GetBlock(),
8180 default_block);
8181 return;
8182 }
8183
Mark Mendell805b3b52015-09-18 14:10:29 -04008184 // Optimizing has a jump area.
8185 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8186 Register constant_area = locations->InAt(1).AsRegister<Register>();
8187
8188 // Remove the bias, if needed.
8189 if (lower_bound != 0) {
8190 __ leal(temp_reg, Address(value_reg, -lower_bound));
8191 value_reg = temp_reg;
8192 }
8193
8194 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008195 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008196 __ cmpl(value_reg, Immediate(num_entries - 1));
8197 __ j(kAbove, codegen_->GetLabelOf(default_block));
8198
8199 // We are in the range of the table.
8200 // Load (target-constant_area) from the jump table, indexing by the value.
8201 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8202
8203 // Compute the actual target address by adding in constant_area.
8204 __ addl(temp_reg, constant_area);
8205
8206 // And jump.
8207 __ jmp(temp_reg);
8208}
8209
Mark Mendell0616ae02015-04-17 12:49:27 -04008210void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8211 HX86ComputeBaseMethodAddress* insn) {
8212 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008213 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008214 locations->SetOut(Location::RequiresRegister());
8215}
8216
8217void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8218 HX86ComputeBaseMethodAddress* insn) {
8219 LocationSummary* locations = insn->GetLocations();
8220 Register reg = locations->Out().AsRegister<Register>();
8221
8222 // Generate call to next instruction.
8223 Label next_instruction;
8224 __ call(&next_instruction);
8225 __ Bind(&next_instruction);
8226
8227 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008228 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008229
8230 // Grab the return address off the stack.
8231 __ popl(reg);
8232}
8233
8234void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8235 HX86LoadFromConstantTable* insn) {
8236 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008237 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008238
8239 locations->SetInAt(0, Location::RequiresRegister());
8240 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8241
8242 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008243 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008244 return;
8245 }
8246
8247 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008248 case DataType::Type::kFloat32:
8249 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008250 locations->SetOut(Location::RequiresFpuRegister());
8251 break;
8252
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008253 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008254 locations->SetOut(Location::RequiresRegister());
8255 break;
8256
8257 default:
8258 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8259 }
8260}
8261
8262void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008263 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008264 return;
8265 }
8266
8267 LocationSummary* locations = insn->GetLocations();
8268 Location out = locations->Out();
8269 Register const_area = locations->InAt(0).AsRegister<Register>();
8270 HConstant *value = insn->GetConstant();
8271
8272 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008273 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008274 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008275 codegen_->LiteralFloatAddress(
8276 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008277 break;
8278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008279 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008280 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008281 codegen_->LiteralDoubleAddress(
8282 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008283 break;
8284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008285 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008286 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008287 codegen_->LiteralInt32Address(
8288 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008289 break;
8290
8291 default:
8292 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8293 }
8294}
8295
Mark Mendell0616ae02015-04-17 12:49:27 -04008296/**
8297 * Class to handle late fixup of offsets into constant area.
8298 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008299class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008300 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008301 RIPFixup(CodeGeneratorX86& codegen,
8302 HX86ComputeBaseMethodAddress* base_method_address,
8303 size_t offset)
8304 : codegen_(&codegen),
8305 base_method_address_(base_method_address),
8306 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008307
8308 protected:
8309 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8310
8311 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008312 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008313
8314 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008315 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008316 // Patch the correct offset for the instruction. The place to patch is the
8317 // last 4 bytes of the instruction.
8318 // The value to patch is the distance from the offset in the constant area
8319 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008320 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008321 int32_t relative_position =
8322 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008323
8324 // Patch in the right value.
8325 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8326 }
8327
Mark Mendell0616ae02015-04-17 12:49:27 -04008328 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008329 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008330};
8331
Mark Mendell805b3b52015-09-18 14:10:29 -04008332/**
8333 * Class to handle late fixup of offsets to a jump table that will be created in the
8334 * constant area.
8335 */
8336class JumpTableRIPFixup : public RIPFixup {
8337 public:
8338 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008339 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8340 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008341
8342 void CreateJumpTable() {
8343 X86Assembler* assembler = codegen_->GetAssembler();
8344
8345 // Ensure that the reference to the jump table has the correct offset.
8346 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8347 SetOffset(offset_in_constant_table);
8348
8349 // The label values in the jump table are computed relative to the
8350 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008351 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008352
8353 // Populate the jump table with the correct values for the jump table.
8354 int32_t num_entries = switch_instr_->GetNumEntries();
8355 HBasicBlock* block = switch_instr_->GetBlock();
8356 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8357 // The value that we want is the target offset - the position of the table.
8358 for (int32_t i = 0; i < num_entries; i++) {
8359 HBasicBlock* b = successors[i];
8360 Label* l = codegen_->GetLabelOf(b);
8361 DCHECK(l->IsBound());
8362 int32_t offset_to_block = l->Position() - relative_offset;
8363 assembler->AppendInt32(offset_to_block);
8364 }
8365 }
8366
8367 private:
8368 const HX86PackedSwitch* switch_instr_;
8369};
8370
8371void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8372 // Generate the constant area if needed.
8373 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308374
Mark Mendell805b3b52015-09-18 14:10:29 -04008375 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8376 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8377 // byte values.
8378 assembler->Align(4, 0);
8379 constant_area_start_ = assembler->CodeSize();
8380
8381 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008382 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008383 jump_table->CreateJumpTable();
8384 }
8385
8386 // And now add the constant area to the generated code.
8387 assembler->AddConstantArea();
8388 }
8389
8390 // And finish up.
8391 CodeGenerator::Finalize(allocator);
8392}
8393
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008394Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8395 HX86ComputeBaseMethodAddress* method_base,
8396 Register reg) {
8397 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008398 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008399 return Address(reg, kDummy32BitOffset, fixup);
8400}
8401
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008402Address CodeGeneratorX86::LiteralFloatAddress(float v,
8403 HX86ComputeBaseMethodAddress* method_base,
8404 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008405 AssemblerFixup* fixup =
8406 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008407 return Address(reg, kDummy32BitOffset, fixup);
8408}
8409
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008410Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8411 HX86ComputeBaseMethodAddress* method_base,
8412 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008413 AssemblerFixup* fixup =
8414 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008415 return Address(reg, kDummy32BitOffset, fixup);
8416}
8417
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008418Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8419 HX86ComputeBaseMethodAddress* method_base,
8420 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008421 AssemblerFixup* fixup =
8422 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008423 return Address(reg, kDummy32BitOffset, fixup);
8424}
8425
Aart Bika19616e2016-02-01 18:57:58 -08008426void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8427 if (value == 0) {
8428 __ xorl(dest, dest);
8429 } else {
8430 __ movl(dest, Immediate(value));
8431 }
8432}
8433
8434void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8435 if (value == 0) {
8436 __ testl(dest, dest);
8437 } else {
8438 __ cmpl(dest, Immediate(value));
8439 }
8440}
8441
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008442void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8443 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008444 GenerateIntCompare(lhs_reg, rhs);
8445}
8446
8447void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008448 if (rhs.IsConstant()) {
8449 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008450 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008451 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008452 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008453 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008454 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008455 }
8456}
8457
8458Address CodeGeneratorX86::ArrayAddress(Register obj,
8459 Location index,
8460 ScaleFactor scale,
8461 uint32_t data_offset) {
8462 return index.IsConstant() ?
8463 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8464 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8465}
8466
Mark Mendell805b3b52015-09-18 14:10:29 -04008467Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8468 Register reg,
8469 Register value) {
8470 // Create a fixup to be used to create and address the jump table.
8471 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008472 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008473
8474 // We have to populate the jump tables.
8475 fixups_to_jump_tables_.push_back(table_fixup);
8476
8477 // We want a scaled address, as we are extracting the correct offset from the table.
8478 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8479}
8480
Andreas Gampe85b62f22015-09-09 13:15:38 -07008481// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008482void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008483 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008484 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008485 return;
8486 }
8487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008488 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008489
8490 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8491 if (target.Equals(return_loc)) {
8492 return;
8493 }
8494
8495 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8496 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008497 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008498 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008499 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8500 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008501 GetMoveResolver()->EmitNativeCode(&parallel_move);
8502 } else {
8503 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008504 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008505 parallel_move.AddMove(return_loc, target, type, nullptr);
8506 GetMoveResolver()->EmitNativeCode(&parallel_move);
8507 }
8508}
8509
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008510void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8511 const uint8_t* roots_data,
8512 const PatchInfo<Label>& info,
8513 uint64_t index_in_table) const {
8514 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8515 uintptr_t address =
8516 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008517 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008518 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8519 dchecked_integral_cast<uint32_t>(address);
8520}
8521
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008522void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8523 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008524 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008525 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008526 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008527 }
8528
8529 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008530 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008531 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008532 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008533 }
8534}
8535
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008536void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8537 ATTRIBUTE_UNUSED) {
8538 LOG(FATAL) << "Unreachable";
8539}
8540
8541void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8542 ATTRIBUTE_UNUSED) {
8543 LOG(FATAL) << "Unreachable";
8544}
8545
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308546bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8547 return codegen_->GetInstructionSetFeatures().HasAVX();
8548}
8549bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8550 return codegen_->GetInstructionSetFeatures().HasAVX2();
8551}
8552bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8553 return codegen_->GetInstructionSetFeatures().HasAVX();
8554}
8555bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8556 return codegen_->GetInstructionSetFeatures().HasAVX2();
8557}
8558
Roland Levillain4d027112015-07-01 15:41:14 +01008559#undef __
8560
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008561} // namespace x86
8562} // namespace art