blob: ac36ce3f1a222027ba5b800ded9f1ef457fdf365 [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 Geoffrayd4dd2552014-02-28 10:23:58 +00001075void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001077 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001078 bool skip_overflow_check =
1079 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001080 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001081
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001082 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07001083 NearLabel overflow;
1084 __ cmpw(Address(kMethodRegisterArgument,
1085 ArtMethod::HotnessCountOffset().Int32Value()),
1086 Immediate(ArtMethod::MaxCounter()));
1087 __ j(kEqual, &overflow);
1088 __ addw(Address(kMethodRegisterArgument,
1089 ArtMethod::HotnessCountOffset().Int32Value()),
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001090 Immediate(1));
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07001091 __ Bind(&overflow);
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001092 }
1093
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001094 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001095 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1096 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001097 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001098 }
1099
Mark Mendell5f874182015-03-04 15:42:45 -05001100 if (HasEmptyFrame()) {
1101 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001102 }
Mark Mendell5f874182015-03-04 15:42:45 -05001103
1104 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1105 Register reg = kCoreCalleeSaves[i];
1106 if (allocated_registers_.ContainsCoreRegister(reg)) {
1107 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001108 __ cfi().AdjustCFAOffset(kX86WordSize);
1109 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001110 }
1111 }
1112
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001113 int adjust = GetFrameSize() - FrameEntrySpillSize();
1114 __ subl(ESP, Immediate(adjust));
1115 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001116 // Save the current method if we need it. Note that we do not
1117 // do this in HCurrentMethod, as the instruction might have been removed
1118 // in the SSA graph.
1119 if (RequiresCurrentMethod()) {
1120 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1121 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001122
1123 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1124 // Initialize should_deoptimize flag to 0.
1125 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1126 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
1129void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001130 __ cfi().RememberState();
1131 if (!HasEmptyFrame()) {
1132 int adjust = GetFrameSize() - FrameEntrySpillSize();
1133 __ addl(ESP, Immediate(adjust));
1134 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001135
David Srbeckyc34dc932015-04-12 09:27:43 +01001136 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1137 Register reg = kCoreCalleeSaves[i];
1138 if (allocated_registers_.ContainsCoreRegister(reg)) {
1139 __ popl(reg);
1140 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1141 __ cfi().Restore(DWARFReg(reg));
1142 }
Mark Mendell5f874182015-03-04 15:42:45 -05001143 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001144 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001145 __ ret();
1146 __ cfi().RestoreState();
1147 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001148}
1149
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001150void CodeGeneratorX86::Bind(HBasicBlock* block) {
1151 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001152}
1153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001154Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001155 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001156 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001157 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001158 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001159 case DataType::Type::kInt8:
1160 case DataType::Type::kUint16:
1161 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001162 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001163 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001164 return Location::RegisterLocation(EAX);
1165
Aart Bik66c158e2018-01-31 12:55:04 -08001166 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001168 return Location::RegisterPairLocation(EAX, EDX);
1169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001170 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001171 return Location::NoLocation();
1172
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001173 case DataType::Type::kFloat64:
1174 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001175 return Location::FpuRegisterLocation(XMM0);
1176 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001177
1178 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001179}
1180
1181Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1182 return Location::RegisterLocation(kMethodRegisterArgument);
1183}
1184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001185Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001187 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001188 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001189 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001190 case DataType::Type::kInt8:
1191 case DataType::Type::kUint16:
1192 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001193 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001194 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001195 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001196 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001197 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001198 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001199 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001200 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001201 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001203 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001204 uint32_t index = gp_index_;
1205 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001206 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001207 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001208 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1209 calling_convention.GetRegisterPairAt(index));
1210 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001212 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1213 }
1214 }
1215
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001217 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001218 stack_index_++;
1219 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1220 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1221 } else {
1222 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1223 }
1224 }
1225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001226 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001227 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001228 stack_index_ += 2;
1229 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1230 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1231 } else {
1232 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001233 }
1234 }
1235
Aart Bik66c158e2018-01-31 12:55:04 -08001236 case DataType::Type::kUint32:
1237 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001238 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001239 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001240 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001241 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001242 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001243}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001244
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245void CodeGeneratorX86::Move32(Location destination, Location source) {
1246 if (source.Equals(destination)) {
1247 return;
1248 }
1249 if (destination.IsRegister()) {
1250 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001253 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001254 } else {
1255 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001256 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001258 } else if (destination.IsFpuRegister()) {
1259 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001260 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001261 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001262 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001263 } else {
1264 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001266 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001268 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001270 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001271 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001272 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001273 } else if (source.IsConstant()) {
1274 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001275 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001276 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 } else {
1278 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001279 __ pushl(Address(ESP, source.GetStackIndex()));
1280 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 }
1282 }
1283}
1284
1285void CodeGeneratorX86::Move64(Location destination, Location source) {
1286 if (source.Equals(destination)) {
1287 return;
1288 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001289 if (destination.IsRegisterPair()) {
1290 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001291 EmitParallelMoves(
1292 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1293 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001294 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001295 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001296 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001297 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001298 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001299 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1300 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1301 __ psrlq(src_reg, Immediate(32));
1302 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001304 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001305 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001306 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1307 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1309 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001310 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001311 if (source.IsFpuRegister()) {
1312 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1313 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001315 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001316 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001317 // Create stack space for 2 elements.
1318 __ subl(ESP, Immediate(2 * elem_size));
1319 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1320 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1321 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1322 // And remove the temporary stack space we allocated.
1323 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001324 } else {
1325 LOG(FATAL) << "Unimplemented";
1326 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001328 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001329 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001330 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001331 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001332 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001333 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001334 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001335 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001336 } else if (source.IsConstant()) {
1337 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001338 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1339 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001340 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001341 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1342 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001343 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001344 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001345 EmitParallelMoves(
1346 Location::StackSlot(source.GetStackIndex()),
1347 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001349 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001350 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001351 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 }
1353 }
1354}
1355
Calin Juravle175dc732015-08-25 15:42:32 +01001356void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1357 DCHECK(location.IsRegister());
1358 __ movl(location.AsRegister<Register>(), Immediate(value));
1359}
1360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001361void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001362 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001363 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1364 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1365 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001366 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001367 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001368 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001369 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001370}
1371
1372void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1373 if (location.IsRegister()) {
1374 locations->AddTemp(location);
1375 } else if (location.IsRegisterPair()) {
1376 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1377 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1378 } else {
1379 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1380 }
1381}
1382
David Brazdilfc6a86a2015-06-26 10:33:45 +00001383void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001384 if (successor->IsExitBlock()) {
1385 DCHECK(got->GetPrevious()->AlwaysThrows());
1386 return; // no code needed
1387 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001388
1389 HBasicBlock* block = got->GetBlock();
1390 HInstruction* previous = got->GetPrevious();
1391
1392 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001393 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001394 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1395 __ pushl(EAX);
1396 __ movl(EAX, Address(ESP, kX86WordSize));
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07001397 NearLabel overflow;
1398 __ cmpw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()),
1399 Immediate(ArtMethod::MaxCounter()));
1400 __ j(kEqual, &overflow);
1401 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()),
1402 Immediate(1));
1403 __ Bind(&overflow);
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001404 __ popl(EAX);
1405 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001406 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1407 return;
1408 }
1409
1410 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1411 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1412 }
1413 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001414 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001415 }
1416}
1417
David Brazdilfc6a86a2015-06-26 10:33:45 +00001418void LocationsBuilderX86::VisitGoto(HGoto* got) {
1419 got->SetLocations(nullptr);
1420}
1421
1422void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1423 HandleGoto(got, got->GetSuccessor());
1424}
1425
1426void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1427 try_boundary->SetLocations(nullptr);
1428}
1429
1430void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1431 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1432 if (!successor->IsExitBlock()) {
1433 HandleGoto(try_boundary, successor);
1434 }
1435}
1436
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001437void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001438 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001439}
1440
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001441void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001442}
1443
Mark Mendell152408f2015-12-31 12:28:50 -05001444template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001445void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001446 LabelType* true_label,
1447 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 if (cond->IsFPConditionTrueIfNaN()) {
1449 __ j(kUnordered, true_label);
1450 } else if (cond->IsFPConditionFalseIfNaN()) {
1451 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001452 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001453 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001454}
1455
Mark Mendell152408f2015-12-31 12:28:50 -05001456template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001457void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001458 LabelType* true_label,
1459 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001460 LocationSummary* locations = cond->GetLocations();
1461 Location left = locations->InAt(0);
1462 Location right = locations->InAt(1);
1463 IfCondition if_cond = cond->GetCondition();
1464
Mark Mendellc4701932015-04-10 13:18:51 -04001465 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001466 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001467 IfCondition true_high_cond = if_cond;
1468 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001469 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001470
1471 // Set the conditions for the test, remembering that == needs to be
1472 // decided using the low words.
1473 switch (if_cond) {
1474 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001475 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001476 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001477 break;
1478 case kCondLT:
1479 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001480 break;
1481 case kCondLE:
1482 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001483 break;
1484 case kCondGT:
1485 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001486 break;
1487 case kCondGE:
1488 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001489 break;
Aart Bike9f37602015-10-09 11:15:55 -07001490 case kCondB:
1491 false_high_cond = kCondA;
1492 break;
1493 case kCondBE:
1494 true_high_cond = kCondB;
1495 break;
1496 case kCondA:
1497 false_high_cond = kCondB;
1498 break;
1499 case kCondAE:
1500 true_high_cond = kCondA;
1501 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001502 }
1503
1504 if (right.IsConstant()) {
1505 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001506 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001507 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001508
Aart Bika19616e2016-02-01 18:57:58 -08001509 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001510 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001511 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001512 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001513 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001514 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001515 __ j(X86Condition(true_high_cond), true_label);
1516 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001517 }
1518 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001519 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001520 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001521 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001522 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001523
1524 __ cmpl(left_high, right_high);
1525 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001526 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001527 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001528 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001529 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001530 __ j(X86Condition(true_high_cond), true_label);
1531 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001532 }
1533 // Must be equal high, so compare the lows.
1534 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001535 } else {
1536 DCHECK(right.IsDoubleStackSlot());
1537 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1538 if (if_cond == kCondNE) {
1539 __ j(X86Condition(true_high_cond), true_label);
1540 } else if (if_cond == kCondEQ) {
1541 __ j(X86Condition(false_high_cond), false_label);
1542 } else {
1543 __ j(X86Condition(true_high_cond), true_label);
1544 __ j(X86Condition(false_high_cond), false_label);
1545 }
1546 // Must be equal high, so compare the lows.
1547 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001548 }
1549 // The last comparison might be unsigned.
1550 __ j(final_condition, true_label);
1551}
1552
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001553void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1554 Location rhs,
1555 HInstruction* insn,
1556 bool is_double) {
1557 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1558 if (is_double) {
1559 if (rhs.IsFpuRegister()) {
1560 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1561 } else if (const_area != nullptr) {
1562 DCHECK(const_area->IsEmittedAtUseSite());
1563 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1564 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001565 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1566 const_area->GetBaseMethodAddress(),
1567 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001568 } else {
1569 DCHECK(rhs.IsDoubleStackSlot());
1570 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1571 }
1572 } else {
1573 if (rhs.IsFpuRegister()) {
1574 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1575 } else if (const_area != nullptr) {
1576 DCHECK(const_area->IsEmittedAtUseSite());
1577 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1578 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001579 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1580 const_area->GetBaseMethodAddress(),
1581 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001582 } else {
1583 DCHECK(rhs.IsStackSlot());
1584 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1585 }
1586 }
1587}
1588
Mark Mendell152408f2015-12-31 12:28:50 -05001589template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001590void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001591 LabelType* true_target_in,
1592 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001593 // Generated branching requires both targets to be explicit. If either of the
1594 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001595 LabelType fallthrough_target;
1596 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1597 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001598
Mark Mendellc4701932015-04-10 13:18:51 -04001599 LocationSummary* locations = condition->GetLocations();
1600 Location left = locations->InAt(0);
1601 Location right = locations->InAt(1);
1602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001603 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001604 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001605 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001606 GenerateLongComparesAndJumps(condition, true_target, false_target);
1607 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001608 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001609 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001610 GenerateFPJumps(condition, true_target, false_target);
1611 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001613 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001614 GenerateFPJumps(condition, true_target, false_target);
1615 break;
1616 default:
1617 LOG(FATAL) << "Unexpected compare type " << type;
1618 }
1619
David Brazdil0debae72015-11-12 18:37:00 +00001620 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001621 __ jmp(false_target);
1622 }
David Brazdil0debae72015-11-12 18:37:00 +00001623
1624 if (fallthrough_target.IsLinked()) {
1625 __ Bind(&fallthrough_target);
1626 }
Mark Mendellc4701932015-04-10 13:18:51 -04001627}
1628
David Brazdil0debae72015-11-12 18:37:00 +00001629static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1630 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1631 // are set only strictly before `branch`. We can't use the eflags on long/FP
1632 // conditions if they are materialized due to the complex branching.
1633 return cond->IsCondition() &&
1634 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1636 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001637}
1638
Mark Mendell152408f2015-12-31 12:28:50 -05001639template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001640void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001641 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001642 LabelType* true_target,
1643 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001644 HInstruction* cond = instruction->InputAt(condition_input_index);
1645
1646 if (true_target == nullptr && false_target == nullptr) {
1647 // Nothing to do. The code always falls through.
1648 return;
1649 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001650 // Constant condition, statically compared against "true" (integer value 1).
1651 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001652 if (true_target != nullptr) {
1653 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001654 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001655 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001656 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001657 if (false_target != nullptr) {
1658 __ jmp(false_target);
1659 }
1660 }
1661 return;
1662 }
1663
1664 // The following code generates these patterns:
1665 // (1) true_target == nullptr && false_target != nullptr
1666 // - opposite condition true => branch to false_target
1667 // (2) true_target != nullptr && false_target == nullptr
1668 // - condition true => branch to true_target
1669 // (3) true_target != nullptr && false_target != nullptr
1670 // - condition true => branch to true_target
1671 // - branch to false_target
1672 if (IsBooleanValueOrMaterializedCondition(cond)) {
1673 if (AreEflagsSetFrom(cond, instruction)) {
1674 if (true_target == nullptr) {
1675 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1676 } else {
1677 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1678 }
1679 } else {
1680 // Materialized condition, compare against 0.
1681 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1682 if (lhs.IsRegister()) {
1683 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1684 } else {
1685 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1686 }
1687 if (true_target == nullptr) {
1688 __ j(kEqual, false_target);
1689 } else {
1690 __ j(kNotEqual, true_target);
1691 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001692 }
1693 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001694 // Condition has not been materialized, use its inputs as the comparison and
1695 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001696 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001697
1698 // If this is a long or FP comparison that has been folded into
1699 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 DataType::Type type = condition->InputAt(0)->GetType();
1701 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001702 GenerateCompareTestAndBranch(condition, true_target, false_target);
1703 return;
1704 }
1705
1706 Location lhs = condition->GetLocations()->InAt(0);
1707 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001708 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001709 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001710 if (true_target == nullptr) {
1711 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1712 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001713 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001714 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001715 }
David Brazdil0debae72015-11-12 18:37:00 +00001716
1717 // If neither branch falls through (case 3), the conditional branch to `true_target`
1718 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1719 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001720 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001721 }
1722}
1723
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001724void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001726 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001727 locations->SetInAt(0, Location::Any());
1728 }
1729}
1730
1731void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001732 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1733 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1734 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1735 nullptr : codegen_->GetLabelOf(true_successor);
1736 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1737 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001738 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001739}
1740
1741void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001742 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001743 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001744 InvokeRuntimeCallingConvention calling_convention;
1745 RegisterSet caller_saves = RegisterSet::Empty();
1746 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1747 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001748 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001749 locations->SetInAt(0, Location::Any());
1750 }
1751}
1752
1753void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001754 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001755 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001756 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001757 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001758 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001759}
1760
Mingyao Yang063fc772016-08-02 11:02:54 -07001761void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001762 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001763 LocationSummary(flag, LocationSummary::kNoCall);
1764 locations->SetOut(Location::RequiresRegister());
1765}
1766
1767void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1768 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1769 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1770}
1771
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772static bool SelectCanUseCMOV(HSelect* select) {
1773 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001775 return false;
1776 }
1777
1778 // A FP condition doesn't generate the single CC that we need.
1779 // In 32 bit mode, a long condition doesn't generate a single CC either.
1780 HInstruction* condition = select->GetCondition();
1781 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 DataType::Type compare_type = condition->InputAt(0)->GetType();
1783 if (compare_type == DataType::Type::kInt64 ||
1784 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001785 return false;
1786 }
1787 }
1788
1789 // We can generate a CMOV for this Select.
1790 return true;
1791}
1792
David Brazdil74eb1b22015-12-14 11:44:01 +00001793void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001794 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001795 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001796 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001797 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001798 } else {
1799 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001800 if (SelectCanUseCMOV(select)) {
1801 if (select->InputAt(1)->IsConstant()) {
1802 // Cmov can't handle a constant value.
1803 locations->SetInAt(1, Location::RequiresRegister());
1804 } else {
1805 locations->SetInAt(1, Location::Any());
1806 }
1807 } else {
1808 locations->SetInAt(1, Location::Any());
1809 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001810 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001811 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1812 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001813 }
1814 locations->SetOut(Location::SameAsFirstInput());
1815}
1816
1817void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1818 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001819 DCHECK(locations->InAt(0).Equals(locations->Out()));
1820 if (SelectCanUseCMOV(select)) {
1821 // If both the condition and the source types are integer, we can generate
1822 // a CMOV to implement Select.
1823
1824 HInstruction* select_condition = select->GetCondition();
1825 Condition cond = kNotEqual;
1826
1827 // Figure out how to test the 'condition'.
1828 if (select_condition->IsCondition()) {
1829 HCondition* condition = select_condition->AsCondition();
1830 if (!condition->IsEmittedAtUseSite()) {
1831 // This was a previously materialized condition.
1832 // Can we use the existing condition code?
1833 if (AreEflagsSetFrom(condition, select)) {
1834 // Materialization was the previous instruction. Condition codes are right.
1835 cond = X86Condition(condition->GetCondition());
1836 } else {
1837 // No, we have to recreate the condition code.
1838 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1839 __ testl(cond_reg, cond_reg);
1840 }
1841 } else {
1842 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001843 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1844 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001845 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001846 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001847 cond = X86Condition(condition->GetCondition());
1848 }
1849 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001850 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001851 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1852 __ testl(cond_reg, cond_reg);
1853 }
1854
1855 // If the condition is true, overwrite the output, which already contains false.
1856 Location false_loc = locations->InAt(0);
1857 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001859 // 64 bit conditional move.
1860 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1861 Register false_low = false_loc.AsRegisterPairLow<Register>();
1862 if (true_loc.IsRegisterPair()) {
1863 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1864 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1865 } else {
1866 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1867 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1868 }
1869 } else {
1870 // 32 bit conditional move.
1871 Register false_reg = false_loc.AsRegister<Register>();
1872 if (true_loc.IsRegister()) {
1873 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1874 } else {
1875 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1876 }
1877 }
1878 } else {
1879 NearLabel false_target;
1880 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001881 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001882 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1883 __ Bind(&false_target);
1884 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001885}
1886
David Srbecky0cf44932015-12-09 14:09:59 +00001887void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001888 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001889}
1890
David Srbeckyd28f4a02016-03-14 17:14:24 +00001891void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1892 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001893}
1894
1895void CodeGeneratorX86::GenerateNop() {
1896 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001897}
1898
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001899void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001901 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001902 // Handle the long/FP comparisons made in instruction simplification.
1903 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001904 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001905 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001906 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001907 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001908 locations->SetOut(Location::RequiresRegister());
1909 }
1910 break;
1911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001912 case DataType::Type::kFloat32:
1913 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001914 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001915 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1916 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1917 } else if (cond->InputAt(1)->IsConstant()) {
1918 locations->SetInAt(1, Location::RequiresFpuRegister());
1919 } else {
1920 locations->SetInAt(1, Location::Any());
1921 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001922 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001923 locations->SetOut(Location::RequiresRegister());
1924 }
1925 break;
1926 }
1927 default:
1928 locations->SetInAt(0, Location::RequiresRegister());
1929 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001930 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001931 // We need a byte register.
1932 locations->SetOut(Location::RegisterLocation(ECX));
1933 }
1934 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001935 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001936}
1937
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001938void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001939 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001940 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001941 }
Mark Mendellc4701932015-04-10 13:18:51 -04001942
1943 LocationSummary* locations = cond->GetLocations();
1944 Location lhs = locations->InAt(0);
1945 Location rhs = locations->InAt(1);
1946 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001947 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001948
1949 switch (cond->InputAt(0)->GetType()) {
1950 default: {
1951 // Integer case.
1952
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001953 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001954 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001955 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001956 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001957 return;
1958 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001959 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001960 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1961 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001962 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001963 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001964 GenerateFPJumps(cond, &true_label, &false_label);
1965 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001966 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001967 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001968 GenerateFPJumps(cond, &true_label, &false_label);
1969 break;
1970 }
1971
1972 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001973 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001974
Roland Levillain4fa13f62015-07-06 18:11:54 +01001975 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001976 __ Bind(&false_label);
1977 __ xorl(reg, reg);
1978 __ jmp(&done_label);
1979
Roland Levillain4fa13f62015-07-06 18:11:54 +01001980 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001981 __ Bind(&true_label);
1982 __ movl(reg, Immediate(1));
1983 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002004}
2005
2006void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002008}
2009
2010void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002012}
2013
2014void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002016}
2017
2018void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002020}
2021
2022void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002024}
2025
2026void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002028}
2029
2030void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002032}
2033
Aart Bike9f37602015-10-09 11:15:55 -07002034void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
2038void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002040}
2041
2042void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002044}
2045
2046void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002048}
2049
2050void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002052}
2053
2054void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002056}
2057
2058void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002060}
2061
2062void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002064}
2065
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002066void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002067 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002068 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002069 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002070}
2071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002072void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002073 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002074}
2075
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002076void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2077 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002078 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002079 locations->SetOut(Location::ConstantLocation(constant));
2080}
2081
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002082void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002083 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002084}
2085
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002086void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002087 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002088 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002089 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002090}
2091
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002092void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002093 // Will be generated at use site.
2094}
2095
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002096void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2097 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002098 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002099 locations->SetOut(Location::ConstantLocation(constant));
2100}
2101
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002102void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002103 // Will be generated at use site.
2104}
2105
2106void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002109 locations->SetOut(Location::ConstantLocation(constant));
2110}
2111
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002112void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002113 // Will be generated at use site.
2114}
2115
Igor Murashkind01745e2017-04-05 16:40:31 -07002116void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2117 constructor_fence->SetLocations(nullptr);
2118}
2119
2120void InstructionCodeGeneratorX86::VisitConstructorFence(
2121 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2122 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2123}
2124
Calin Juravle27df7582015-04-17 19:12:31 +01002125void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2126 memory_barrier->SetLocations(nullptr);
2127}
2128
2129void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002130 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002131}
2132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002134 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135}
2136
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002137void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002138 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002139}
2140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002143 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002144 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002145 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002147 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 case DataType::Type::kInt8:
2149 case DataType::Type::kUint16:
2150 case DataType::Type::kInt16:
2151 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002152 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002153 break;
2154
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002155 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002156 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002157 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002158 break;
2159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 case DataType::Type::kFloat32:
2161 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002162 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002163 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002164 break;
2165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002166 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002167 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002169}
2170
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002171void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002172 if (kIsDebugBuild) {
2173 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002174 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002175 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002176 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 case DataType::Type::kInt8:
2178 case DataType::Type::kUint16:
2179 case DataType::Type::kInt16:
2180 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002181 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002182 break;
2183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002185 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2186 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002187 break;
2188
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 case DataType::Type::kFloat32:
2190 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002191 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002192 break;
2193
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002194 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002195 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002196 }
2197 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002198 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002199}
2200
Calin Juravle175dc732015-08-25 15:42:32 +01002201void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2202 // The trampoline uses the same calling convention as dex calling conventions,
2203 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2204 // the method_idx.
2205 HandleInvoke(invoke);
2206}
2207
2208void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2209 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2210}
2211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002212void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002213 // Explicit clinit checks triggered by static invokes must have been pruned by
2214 // art::PrepareForRegisterAllocation.
2215 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002216
Mark Mendellfb8d2792015-03-31 22:16:59 -04002217 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002218 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002219 if (invoke->GetLocations()->CanCall() &&
2220 invoke->HasPcRelativeMethodLoadKind() &&
2221 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002222 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002223 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002224 return;
2225 }
2226
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002227 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002228
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002229 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002230 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002231 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002232 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002233}
2234
Mark Mendell09ed1a32015-03-25 08:30:06 -04002235static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2236 if (invoke->GetLocations()->Intrinsified()) {
2237 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2238 intrinsic.Dispatch(invoke);
2239 return true;
2240 }
2241 return false;
2242}
2243
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002244void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002245 // Explicit clinit checks triggered by static invokes must have been pruned by
2246 // art::PrepareForRegisterAllocation.
2247 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002248
Mark Mendell09ed1a32015-03-25 08:30:06 -04002249 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2250 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002251 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002252
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002253 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002254 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002255 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002256}
2257
2258void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002259 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2260 if (intrinsic.TryDispatch(invoke)) {
2261 return;
2262 }
2263
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002264 HandleInvoke(invoke);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002265 if (codegen_->GetCompilerOptions().IsBaseline() && !Runtime::Current()->IsAotCompiler()) {
2266 // Add one temporary for inline cache update.
2267 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2268 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002269}
2270
2271void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002272 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002273 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002274}
2275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002277 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2278 return;
2279 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002280
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002281 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002282 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002283}
2284
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002285void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002286 // This call to HandleInvoke allocates a temporary (core) register
2287 // which is also used to transfer the hidden argument from FP to
2288 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002289 HandleInvoke(invoke);
2290 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002291 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002292
2293 if (codegen_->GetCompilerOptions().IsBaseline() && !Runtime::Current()->IsAotCompiler()) {
2294 // Add one temporary for inline cache update.
2295 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2296 }
2297}
2298
2299void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2300 DCHECK_EQ(EAX, klass);
2301 if (GetCompilerOptions().IsBaseline() && !Runtime::Current()->IsAotCompiler()) {
2302 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2303 ScopedObjectAccess soa(Thread::Current());
2304 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
2305 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2306 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2307 if (kIsDebugBuild) {
2308 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2309 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2310 }
2311 Register temp = EBP;
2312 NearLabel done;
2313 __ movl(temp, Immediate(address));
2314 // Fast path for a monomorphic cache.
2315 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2316 __ j(kEqual, &done);
2317 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2318 __ Bind(&done);
2319 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002320}
2321
2322void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2323 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002324 LocationSummary* locations = invoke->GetLocations();
2325 Register temp = locations->GetTemp(0).AsRegister<Register>();
2326 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002327 Location receiver = locations->InAt(0);
2328 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2329
Roland Levillain0d5a2812015-11-13 10:07:31 +00002330 // Set the hidden argument. This is safe to do this here, as XMM7
2331 // won't be modified thereafter, before the `call` instruction.
2332 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002333 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002334 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002335
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002336 if (receiver.IsStackSlot()) {
2337 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002338 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002339 __ movl(temp, Address(temp, class_offset));
2340 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002341 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002343 }
Roland Levillain4d027112015-07-01 15:41:14 +01002344 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002345 // Instead of simply (possibly) unpoisoning `temp` here, we should
2346 // emit a read barrier for the previous class reference load.
2347 // However this is not required in practice, as this is an
2348 // intermediate/temporary reference and because the current
2349 // concurrent copying collector keeps the from-space memory
2350 // intact/accessible until the end of the marking phase (the
2351 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002352 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002353
2354 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2355
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002356 // temp = temp->GetAddressOfIMT()
2357 __ movl(temp,
2358 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002359 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002360 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002361 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002362 __ movl(temp, Address(temp, method_offset));
2363 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002364 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002365 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002366
2367 DCHECK(!codegen_->IsLeafMethod());
2368 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2369}
2370
Orion Hodsonac141392017-01-13 11:53:47 +00002371void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2372 HandleInvoke(invoke);
2373}
2374
2375void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2376 codegen_->GenerateInvokePolymorphicCall(invoke);
2377}
2378
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002379void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2380 HandleInvoke(invoke);
2381}
2382
2383void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2384 codegen_->GenerateInvokeCustomCall(invoke);
2385}
2386
Roland Levillain88cb1752014-10-20 16:36:47 +01002387void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2388 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002389 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002390 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 case DataType::Type::kInt32:
2392 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002393 locations->SetInAt(0, Location::RequiresRegister());
2394 locations->SetOut(Location::SameAsFirstInput());
2395 break;
2396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002398 locations->SetInAt(0, Location::RequiresFpuRegister());
2399 locations->SetOut(Location::SameAsFirstInput());
2400 locations->AddTemp(Location::RequiresRegister());
2401 locations->AddTemp(Location::RequiresFpuRegister());
2402 break;
2403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002405 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002406 locations->SetOut(Location::SameAsFirstInput());
2407 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002408 break;
2409
2410 default:
2411 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2412 }
2413}
2414
2415void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2416 LocationSummary* locations = neg->GetLocations();
2417 Location out = locations->Out();
2418 Location in = locations->InAt(0);
2419 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002421 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002422 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002423 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002424 break;
2425
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002427 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002428 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002429 __ negl(out.AsRegisterPairLow<Register>());
2430 // Negation is similar to subtraction from zero. The least
2431 // significant byte triggers a borrow when it is different from
2432 // zero; to take it into account, add 1 to the most significant
2433 // byte if the carry flag (CF) is set to 1 after the first NEGL
2434 // operation.
2435 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2436 __ negl(out.AsRegisterPairHigh<Register>());
2437 break;
2438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002440 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 Register constant = locations->GetTemp(0).AsRegister<Register>();
2442 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002443 // Implement float negation with an exclusive or with value
2444 // 0x80000000 (mask for bit 31, representing the sign of a
2445 // single-precision floating-point number).
2446 __ movl(constant, Immediate(INT32_C(0x80000000)));
2447 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002449 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002450 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002453 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002455 // Implement double negation with an exclusive or with value
2456 // 0x8000000000000000 (mask for bit 63, representing the sign of
2457 // a double-precision floating-point number).
2458 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002460 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002461 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002462
2463 default:
2464 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2465 }
2466}
2467
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002468void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2469 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002470 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002472 locations->SetInAt(0, Location::RequiresFpuRegister());
2473 locations->SetInAt(1, Location::RequiresRegister());
2474 locations->SetOut(Location::SameAsFirstInput());
2475 locations->AddTemp(Location::RequiresFpuRegister());
2476}
2477
2478void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2479 LocationSummary* locations = neg->GetLocations();
2480 Location out = locations->Out();
2481 DCHECK(locations->InAt(0).Equals(out));
2482
2483 Register constant_area = locations->InAt(1).AsRegister<Register>();
2484 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002486 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2487 neg->GetBaseMethodAddress(),
2488 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002489 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2490 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002491 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2492 neg->GetBaseMethodAddress(),
2493 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002494 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2495 }
2496}
2497
Roland Levillaindff1f282014-11-05 14:15:05 +00002498void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002499 DataType::Type result_type = conversion->GetResultType();
2500 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002501 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2502 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002503
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002504 // The float-to-long and double-to-long type conversions rely on a
2505 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002506 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2508 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002509 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002510 : LocationSummary::kNoCall;
2511 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002512 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002513
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002515 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002517 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002518 case DataType::Type::kUint8:
2519 case DataType::Type::kInt8:
2520 case DataType::Type::kUint16:
2521 case DataType::Type::kInt16:
2522 case DataType::Type::kInt32:
2523 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2524 // Make the output overlap to please the register allocator. This greatly simplifies
2525 // the validation of the linear scan implementation
2526 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2527 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002529 HInstruction* input = conversion->InputAt(0);
2530 Location input_location = input->IsConstant()
2531 ? Location::ConstantLocation(input->AsConstant())
2532 : Location::RegisterPairLocation(EAX, EDX);
2533 locations->SetInAt(0, input_location);
2534 // Make the output overlap to please the register allocator. This greatly simplifies
2535 // the validation of the linear scan implementation
2536 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2537 break;
2538 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002539
2540 default:
2541 LOG(FATAL) << "Unexpected type conversion from " << input_type
2542 << " to " << result_type;
2543 }
2544 break;
2545
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002546 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002548 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2549 locations->SetInAt(0, Location::Any());
2550 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002551 break;
2552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002554 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002556 locations->SetInAt(0, Location::Any());
2557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2558 break;
2559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002561 locations->SetInAt(0, Location::RequiresFpuRegister());
2562 locations->SetOut(Location::RequiresRegister());
2563 locations->AddTemp(Location::RequiresFpuRegister());
2564 break;
2565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002567 locations->SetInAt(0, Location::RequiresFpuRegister());
2568 locations->SetOut(Location::RequiresRegister());
2569 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002570 break;
2571
2572 default:
2573 LOG(FATAL) << "Unexpected type conversion from " << input_type
2574 << " to " << result_type;
2575 }
2576 break;
2577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002579 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002581 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002583 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002584 case DataType::Type::kInt16:
2585 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002586 locations->SetInAt(0, Location::RegisterLocation(EAX));
2587 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2588 break;
2589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 case DataType::Type::kFloat32:
2591 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002592 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002593 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2594 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2595
Vladimir Marko949c91f2015-01-27 10:48:44 +00002596 // The runtime helper puts the result in EAX, EDX.
2597 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002598 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002599 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002600
2601 default:
2602 LOG(FATAL) << "Unexpected type conversion from " << input_type
2603 << " to " << result_type;
2604 }
2605 break;
2606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002608 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002610 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002612 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 case DataType::Type::kInt16:
2614 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002615 locations->SetInAt(0, Location::RequiresRegister());
2616 locations->SetOut(Location::RequiresFpuRegister());
2617 break;
2618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002619 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002620 locations->SetInAt(0, Location::Any());
2621 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002622 break;
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002625 locations->SetInAt(0, Location::RequiresFpuRegister());
2626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002627 break;
2628
2629 default:
2630 LOG(FATAL) << "Unexpected type conversion from " << input_type
2631 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002632 }
Roland Levillaincff13742014-11-17 14:32:17 +00002633 break;
2634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002636 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002638 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002640 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002641 case DataType::Type::kInt16:
2642 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002643 locations->SetInAt(0, Location::RequiresRegister());
2644 locations->SetOut(Location::RequiresFpuRegister());
2645 break;
2646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002648 locations->SetInAt(0, Location::Any());
2649 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002650 break;
2651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002653 locations->SetInAt(0, Location::RequiresFpuRegister());
2654 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002655 break;
2656
2657 default:
2658 LOG(FATAL) << "Unexpected type conversion from " << input_type
2659 << " to " << result_type;
2660 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002661 break;
2662
2663 default:
2664 LOG(FATAL) << "Unexpected type conversion from " << input_type
2665 << " to " << result_type;
2666 }
2667}
2668
2669void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2670 LocationSummary* locations = conversion->GetLocations();
2671 Location out = locations->Out();
2672 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 DataType::Type result_type = conversion->GetResultType();
2674 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002675 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2676 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002677 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002678 case DataType::Type::kUint8:
2679 switch (input_type) {
2680 case DataType::Type::kInt8:
2681 case DataType::Type::kUint16:
2682 case DataType::Type::kInt16:
2683 case DataType::Type::kInt32:
2684 if (in.IsRegister()) {
2685 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2686 } else {
2687 DCHECK(in.GetConstant()->IsIntConstant());
2688 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2689 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2690 }
2691 break;
2692 case DataType::Type::kInt64:
2693 if (in.IsRegisterPair()) {
2694 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2695 } else {
2696 DCHECK(in.GetConstant()->IsLongConstant());
2697 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2698 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2699 }
2700 break;
2701
2702 default:
2703 LOG(FATAL) << "Unexpected type conversion from " << input_type
2704 << " to " << result_type;
2705 }
2706 break;
2707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002709 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002710 case DataType::Type::kUint8:
2711 case DataType::Type::kUint16:
2712 case DataType::Type::kInt16:
2713 case DataType::Type::kInt32:
2714 if (in.IsRegister()) {
2715 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2716 } else {
2717 DCHECK(in.GetConstant()->IsIntConstant());
2718 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2719 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2720 }
2721 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002723 if (in.IsRegisterPair()) {
2724 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2725 } else {
2726 DCHECK(in.GetConstant()->IsLongConstant());
2727 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2728 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2729 }
2730 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002731
2732 default:
2733 LOG(FATAL) << "Unexpected type conversion from " << input_type
2734 << " to " << result_type;
2735 }
2736 break;
2737
2738 case DataType::Type::kUint16:
2739 switch (input_type) {
2740 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 case DataType::Type::kInt16:
2742 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002743 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002744 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2745 } else if (in.IsStackSlot()) {
2746 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002747 } else {
2748 DCHECK(in.GetConstant()->IsIntConstant());
2749 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002750 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2751 }
2752 break;
2753 case DataType::Type::kInt64:
2754 if (in.IsRegisterPair()) {
2755 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2756 } else if (in.IsDoubleStackSlot()) {
2757 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2758 } else {
2759 DCHECK(in.GetConstant()->IsLongConstant());
2760 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2761 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002762 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002763 break;
2764
2765 default:
2766 LOG(FATAL) << "Unexpected type conversion from " << input_type
2767 << " to " << result_type;
2768 }
2769 break;
2770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002772 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002774 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002775 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002777 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002779 } else {
2780 DCHECK(in.GetConstant()->IsIntConstant());
2781 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002783 }
2784 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002785 case DataType::Type::kInt64:
2786 if (in.IsRegisterPair()) {
2787 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2788 } else if (in.IsDoubleStackSlot()) {
2789 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2790 } else {
2791 DCHECK(in.GetConstant()->IsLongConstant());
2792 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2793 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2794 }
2795 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002796
2797 default:
2798 LOG(FATAL) << "Unexpected type conversion from " << input_type
2799 << " to " << result_type;
2800 }
2801 break;
2802
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002804 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002806 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002808 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002810 } else {
2811 DCHECK(in.IsConstant());
2812 DCHECK(in.GetConstant()->IsLongConstant());
2813 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002815 }
2816 break;
2817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002819 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2820 Register output = out.AsRegister<Register>();
2821 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002822 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002823
2824 __ movl(output, Immediate(kPrimIntMax));
2825 // temp = int-to-float(output)
2826 __ cvtsi2ss(temp, output);
2827 // if input >= temp goto done
2828 __ comiss(input, temp);
2829 __ j(kAboveEqual, &done);
2830 // if input == NaN goto nan
2831 __ j(kUnordered, &nan);
2832 // output = float-to-int-truncate(input)
2833 __ cvttss2si(output, input);
2834 __ jmp(&done);
2835 __ Bind(&nan);
2836 // output = 0
2837 __ xorl(output, output);
2838 __ Bind(&done);
2839 break;
2840 }
2841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002843 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2844 Register output = out.AsRegister<Register>();
2845 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002846 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002847
2848 __ movl(output, Immediate(kPrimIntMax));
2849 // temp = int-to-double(output)
2850 __ cvtsi2sd(temp, output);
2851 // if input >= temp goto done
2852 __ comisd(input, temp);
2853 __ j(kAboveEqual, &done);
2854 // if input == NaN goto nan
2855 __ j(kUnordered, &nan);
2856 // output = double-to-int-truncate(input)
2857 __ cvttsd2si(output, input);
2858 __ jmp(&done);
2859 __ Bind(&nan);
2860 // output = 0
2861 __ xorl(output, output);
2862 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002863 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002864 }
Roland Levillain946e1432014-11-11 17:35:19 +00002865
2866 default:
2867 LOG(FATAL) << "Unexpected type conversion from " << input_type
2868 << " to " << result_type;
2869 }
2870 break;
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002873 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002875 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002877 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 case DataType::Type::kInt16:
2879 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002880 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2881 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002883 __ cdq();
2884 break;
2885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002886 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002887 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002888 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002889 break;
2890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002892 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002893 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002894 break;
2895
2896 default:
2897 LOG(FATAL) << "Unexpected type conversion from " << input_type
2898 << " to " << result_type;
2899 }
2900 break;
2901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002903 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002905 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002907 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kInt16:
2909 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002910 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002911 break;
2912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002914 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002915
Roland Levillain232ade02015-04-20 15:14:36 +01002916 // Create stack space for the call to
2917 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2918 // TODO: enhance register allocator to ask for stack temporaries.
2919 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002921 __ subl(ESP, Immediate(adjustment));
2922 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002923
Roland Levillain232ade02015-04-20 15:14:36 +01002924 // Load the value to the FP stack, using temporaries if needed.
2925 PushOntoFPStack(in, 0, adjustment, false, true);
2926
2927 if (out.IsStackSlot()) {
2928 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2929 } else {
2930 __ fstps(Address(ESP, 0));
2931 Location stack_temp = Location::StackSlot(0);
2932 codegen_->Move32(out, stack_temp);
2933 }
2934
2935 // Remove the temporary stack space we allocated.
2936 if (adjustment != 0) {
2937 __ addl(ESP, Immediate(adjustment));
2938 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002939 break;
2940 }
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002943 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002944 break;
2945
2946 default:
2947 LOG(FATAL) << "Unexpected type conversion from " << input_type
2948 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002949 }
Roland Levillaincff13742014-11-17 14:32:17 +00002950 break;
2951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002953 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002955 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002957 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 case DataType::Type::kInt16:
2959 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002961 break;
2962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002964 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002965
Roland Levillain232ade02015-04-20 15:14:36 +01002966 // Create stack space for the call to
2967 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2968 // TODO: enhance register allocator to ask for stack temporaries.
2969 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002971 __ subl(ESP, Immediate(adjustment));
2972 }
2973
2974 // Load the value to the FP stack, using temporaries if needed.
2975 PushOntoFPStack(in, 0, adjustment, false, true);
2976
2977 if (out.IsDoubleStackSlot()) {
2978 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2979 } else {
2980 __ fstpl(Address(ESP, 0));
2981 Location stack_temp = Location::DoubleStackSlot(0);
2982 codegen_->Move64(out, stack_temp);
2983 }
2984
2985 // Remove the temporary stack space we allocated.
2986 if (adjustment != 0) {
2987 __ addl(ESP, Immediate(adjustment));
2988 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002989 break;
2990 }
2991
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002993 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002994 break;
2995
2996 default:
2997 LOG(FATAL) << "Unexpected type conversion from " << input_type
2998 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002999 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003000 break;
3001
3002 default:
3003 LOG(FATAL) << "Unexpected type conversion from " << input_type
3004 << " to " << result_type;
3005 }
3006}
3007
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003008void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003009 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003010 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003011 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003013 locations->SetInAt(0, Location::RequiresRegister());
3014 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3015 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3016 break;
3017 }
3018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 locations->SetInAt(0, Location::RequiresRegister());
3021 locations->SetInAt(1, Location::Any());
3022 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 break;
3024 }
3025
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 case DataType::Type::kFloat32:
3027 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003028 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003029 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3030 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003031 } else if (add->InputAt(1)->IsConstant()) {
3032 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003033 } else {
3034 locations->SetInAt(1, Location::Any());
3035 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003036 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003037 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003038 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003040 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003041 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003042 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003043 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003044}
3045
3046void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3047 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003048 Location first = locations->InAt(0);
3049 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003050 Location out = locations->Out();
3051
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003052 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003054 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003055 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3056 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003057 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3058 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003059 } else {
3060 __ leal(out.AsRegister<Register>(), Address(
3061 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3062 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003063 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003064 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3065 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3066 __ addl(out.AsRegister<Register>(), Immediate(value));
3067 } else {
3068 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3069 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003070 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003071 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003073 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003074 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003075 }
3076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003078 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003079 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3080 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003081 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003082 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3083 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003084 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003085 } else {
3086 DCHECK(second.IsConstant()) << second;
3087 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3088 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3089 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003091 break;
3092 }
3093
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003095 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003096 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3098 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003100 __ addss(first.AsFpuRegister<XmmRegister>(),
3101 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003102 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3103 const_area->GetBaseMethodAddress(),
3104 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003105 } else {
3106 DCHECK(second.IsStackSlot());
3107 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003110 }
3111
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003113 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003115 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3116 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003117 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003118 __ addsd(first.AsFpuRegister<XmmRegister>(),
3119 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003120 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3121 const_area->GetBaseMethodAddress(),
3122 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003123 } else {
3124 DCHECK(second.IsDoubleStackSlot());
3125 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003126 }
3127 break;
3128 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003129
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003130 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003131 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003132 }
3133}
3134
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003135void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003136 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003137 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003138 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kInt32:
3140 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003141 locations->SetInAt(0, Location::RequiresRegister());
3142 locations->SetInAt(1, Location::Any());
3143 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003144 break;
3145 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kFloat32:
3147 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003148 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003149 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3150 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003151 } else if (sub->InputAt(1)->IsConstant()) {
3152 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003153 } else {
3154 locations->SetInAt(1, Location::Any());
3155 }
Calin Juravle11351682014-10-23 15:38:15 +01003156 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003157 break;
Calin Juravle11351682014-10-23 15:38:15 +01003158 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003159
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003160 default:
Calin Juravle11351682014-10-23 15:38:15 +01003161 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003163}
3164
3165void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3166 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003167 Location first = locations->InAt(0);
3168 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003169 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003170 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003172 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003173 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003174 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003175 __ subl(first.AsRegister<Register>(),
3176 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003177 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003179 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003180 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003181 }
3182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003184 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003185 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3186 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003187 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003188 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003189 __ sbbl(first.AsRegisterPairHigh<Register>(),
3190 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003191 } else {
3192 DCHECK(second.IsConstant()) << second;
3193 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3194 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3195 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003196 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003197 break;
3198 }
3199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003200 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003201 if (second.IsFpuRegister()) {
3202 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3203 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3204 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003205 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003206 __ subss(first.AsFpuRegister<XmmRegister>(),
3207 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003208 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3209 const_area->GetBaseMethodAddress(),
3210 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003211 } else {
3212 DCHECK(second.IsStackSlot());
3213 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3214 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003215 break;
Calin Juravle11351682014-10-23 15:38:15 +01003216 }
3217
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003218 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003219 if (second.IsFpuRegister()) {
3220 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3221 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3222 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003223 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003224 __ subsd(first.AsFpuRegister<XmmRegister>(),
3225 codegen_->LiteralDoubleAddress(
3226 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003227 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003228 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3229 } else {
3230 DCHECK(second.IsDoubleStackSlot());
3231 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3232 }
Calin Juravle11351682014-10-23 15:38:15 +01003233 break;
3234 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003235
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003236 default:
Calin Juravle11351682014-10-23 15:38:15 +01003237 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003238 }
3239}
3240
Calin Juravle34bacdf2014-10-07 20:23:36 +01003241void LocationsBuilderX86::VisitMul(HMul* mul) {
3242 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003243 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003244 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003245 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246 locations->SetInAt(0, Location::RequiresRegister());
3247 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003248 if (mul->InputAt(1)->IsIntConstant()) {
3249 // Can use 3 operand multiply.
3250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3251 } else {
3252 locations->SetOut(Location::SameAsFirstInput());
3253 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003254 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003255 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003256 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257 locations->SetInAt(1, Location::Any());
3258 locations->SetOut(Location::SameAsFirstInput());
3259 // Needed for imul on 32bits with 64bits output.
3260 locations->AddTemp(Location::RegisterLocation(EAX));
3261 locations->AddTemp(Location::RegisterLocation(EDX));
3262 break;
3263 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003264 case DataType::Type::kFloat32:
3265 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003266 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003267 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3268 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003269 } else if (mul->InputAt(1)->IsConstant()) {
3270 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003271 } else {
3272 locations->SetInAt(1, Location::Any());
3273 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003274 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003275 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003276 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003277
3278 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003279 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003280 }
3281}
3282
3283void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3284 LocationSummary* locations = mul->GetLocations();
3285 Location first = locations->InAt(0);
3286 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003287 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003288
3289 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003291 // The constant may have ended up in a register, so test explicitly to avoid
3292 // problems where the output may not be the same as the first operand.
3293 if (mul->InputAt(1)->IsIntConstant()) {
3294 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3295 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3296 } else if (second.IsRegister()) {
3297 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003298 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003299 } else {
3300 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003301 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 }
3304 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003306 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003307 Register in1_hi = first.AsRegisterPairHigh<Register>();
3308 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003309 Register eax = locations->GetTemp(0).AsRegister<Register>();
3310 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311
3312 DCHECK_EQ(EAX, eax);
3313 DCHECK_EQ(EDX, edx);
3314
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003315 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003316 // output: in1
3317 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3318 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3319 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003320 if (second.IsConstant()) {
3321 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003322
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003323 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3324 int32_t low_value = Low32Bits(value);
3325 int32_t high_value = High32Bits(value);
3326 Immediate low(low_value);
3327 Immediate high(high_value);
3328
3329 __ movl(eax, high);
3330 // eax <- in1.lo * in2.hi
3331 __ imull(eax, in1_lo);
3332 // in1.hi <- in1.hi * in2.lo
3333 __ imull(in1_hi, low);
3334 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3335 __ addl(in1_hi, eax);
3336 // move in2_lo to eax to prepare for double precision
3337 __ movl(eax, low);
3338 // edx:eax <- in1.lo * in2.lo
3339 __ mull(in1_lo);
3340 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3341 __ addl(in1_hi, edx);
3342 // in1.lo <- (in1.lo * in2.lo)[31:0];
3343 __ movl(in1_lo, eax);
3344 } else if (second.IsRegisterPair()) {
3345 Register in2_hi = second.AsRegisterPairHigh<Register>();
3346 Register in2_lo = second.AsRegisterPairLow<Register>();
3347
3348 __ movl(eax, in2_hi);
3349 // eax <- in1.lo * in2.hi
3350 __ imull(eax, in1_lo);
3351 // in1.hi <- in1.hi * in2.lo
3352 __ imull(in1_hi, in2_lo);
3353 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3354 __ addl(in1_hi, eax);
3355 // move in1_lo to eax to prepare for double precision
3356 __ movl(eax, in1_lo);
3357 // edx:eax <- in1.lo * in2.lo
3358 __ mull(in2_lo);
3359 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3360 __ addl(in1_hi, edx);
3361 // in1.lo <- (in1.lo * in2.lo)[31:0];
3362 __ movl(in1_lo, eax);
3363 } else {
3364 DCHECK(second.IsDoubleStackSlot()) << second;
3365 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3366 Address in2_lo(ESP, second.GetStackIndex());
3367
3368 __ movl(eax, in2_hi);
3369 // eax <- in1.lo * in2.hi
3370 __ imull(eax, in1_lo);
3371 // in1.hi <- in1.hi * in2.lo
3372 __ imull(in1_hi, in2_lo);
3373 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3374 __ addl(in1_hi, eax);
3375 // move in1_lo to eax to prepare for double precision
3376 __ movl(eax, in1_lo);
3377 // edx:eax <- in1.lo * in2.lo
3378 __ mull(in2_lo);
3379 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3380 __ addl(in1_hi, edx);
3381 // in1.lo <- (in1.lo * in2.lo)[31:0];
3382 __ movl(in1_lo, eax);
3383 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003384
3385 break;
3386 }
3387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003389 DCHECK(first.Equals(locations->Out()));
3390 if (second.IsFpuRegister()) {
3391 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3392 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3393 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003394 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003395 __ mulss(first.AsFpuRegister<XmmRegister>(),
3396 codegen_->LiteralFloatAddress(
3397 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003398 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003399 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3400 } else {
3401 DCHECK(second.IsStackSlot());
3402 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3403 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003404 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003405 }
3406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003407 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003408 DCHECK(first.Equals(locations->Out()));
3409 if (second.IsFpuRegister()) {
3410 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3411 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3412 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003413 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003414 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3415 codegen_->LiteralDoubleAddress(
3416 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003417 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003418 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3419 } else {
3420 DCHECK(second.IsDoubleStackSlot());
3421 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3422 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003423 break;
3424 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003425
3426 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003427 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003428 }
3429}
3430
Roland Levillain232ade02015-04-20 15:14:36 +01003431void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3432 uint32_t temp_offset,
3433 uint32_t stack_adjustment,
3434 bool is_fp,
3435 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003436 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003437 DCHECK(!is_wide);
3438 if (is_fp) {
3439 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3440 } else {
3441 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3442 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003443 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003444 DCHECK(is_wide);
3445 if (is_fp) {
3446 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3447 } else {
3448 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3449 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003450 } else {
3451 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003452 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003453 Location stack_temp = Location::StackSlot(temp_offset);
3454 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003455 if (is_fp) {
3456 __ flds(Address(ESP, temp_offset));
3457 } else {
3458 __ filds(Address(ESP, temp_offset));
3459 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003460 } else {
3461 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3462 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003463 if (is_fp) {
3464 __ fldl(Address(ESP, temp_offset));
3465 } else {
3466 __ fildl(Address(ESP, temp_offset));
3467 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003468 }
3469 }
3470}
3471
3472void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003473 DataType::Type type = rem->GetResultType();
3474 bool is_float = type == DataType::Type::kFloat32;
3475 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003476 LocationSummary* locations = rem->GetLocations();
3477 Location first = locations->InAt(0);
3478 Location second = locations->InAt(1);
3479 Location out = locations->Out();
3480
3481 // Create stack space for 2 elements.
3482 // TODO: enhance register allocator to ask for stack temporaries.
3483 __ subl(ESP, Immediate(2 * elem_size));
3484
3485 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003486 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003487 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3488 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003489
3490 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003491 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003492 __ Bind(&retry);
3493 __ fprem();
3494
3495 // Move FP status to AX.
3496 __ fstsw();
3497
3498 // And see if the argument reduction is complete. This is signaled by the
3499 // C2 FPU flag bit set to 0.
3500 __ andl(EAX, Immediate(kC2ConditionMask));
3501 __ j(kNotEqual, &retry);
3502
3503 // We have settled on the final value. Retrieve it into an XMM register.
3504 // Store FP top of stack to real stack.
3505 if (is_float) {
3506 __ fsts(Address(ESP, 0));
3507 } else {
3508 __ fstl(Address(ESP, 0));
3509 }
3510
3511 // Pop the 2 items from the FP stack.
3512 __ fucompp();
3513
3514 // Load the value from the stack into an XMM register.
3515 DCHECK(out.IsFpuRegister()) << out;
3516 if (is_float) {
3517 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3518 } else {
3519 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3520 }
3521
3522 // And remove the temporary stack space we allocated.
3523 __ addl(ESP, Immediate(2 * elem_size));
3524}
3525
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003526
3527void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3528 DCHECK(instruction->IsDiv() || instruction->IsRem());
3529
3530 LocationSummary* locations = instruction->GetLocations();
3531 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003532 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533
3534 Register out_register = locations->Out().AsRegister<Register>();
3535 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003536 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537
3538 DCHECK(imm == 1 || imm == -1);
3539
3540 if (instruction->IsRem()) {
3541 __ xorl(out_register, out_register);
3542 } else {
3543 __ movl(out_register, input_register);
3544 if (imm == -1) {
3545 __ negl(out_register);
3546 }
3547 }
3548}
3549
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303550void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3551 LocationSummary* locations = instruction->GetLocations();
3552 Location second = locations->InAt(1);
3553
3554 Register out = locations->Out().AsRegister<Register>();
3555 Register numerator = locations->InAt(0).AsRegister<Register>();
3556
3557 int32_t imm = Int64FromConstant(second.GetConstant());
3558 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3559 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3560
3561 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3562 NearLabel done;
3563 __ movl(out, numerator);
3564 __ andl(out, Immediate(abs_imm-1));
3565 __ j(Condition::kZero, &done);
3566 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3567 __ testl(numerator, numerator);
3568 __ cmovl(Condition::kLess, out, tmp);
3569 __ Bind(&done);
3570}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003571
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003572void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573 LocationSummary* locations = instruction->GetLocations();
3574
3575 Register out_register = locations->Out().AsRegister<Register>();
3576 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003577 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003578 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3579 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581 Register num = locations->GetTemp(0).AsRegister<Register>();
3582
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003583 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584 __ testl(input_register, input_register);
3585 __ cmovl(kGreaterEqual, num, input_register);
3586 int shift = CTZ(imm);
3587 __ sarl(num, Immediate(shift));
3588
3589 if (imm < 0) {
3590 __ negl(num);
3591 }
3592
3593 __ movl(out_register, num);
3594}
3595
3596void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3597 DCHECK(instruction->IsDiv() || instruction->IsRem());
3598
3599 LocationSummary* locations = instruction->GetLocations();
3600 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3601
3602 Register eax = locations->InAt(0).AsRegister<Register>();
3603 Register out = locations->Out().AsRegister<Register>();
3604 Register num;
3605 Register edx;
3606
3607 if (instruction->IsDiv()) {
3608 edx = locations->GetTemp(0).AsRegister<Register>();
3609 num = locations->GetTemp(1).AsRegister<Register>();
3610 } else {
3611 edx = locations->Out().AsRegister<Register>();
3612 num = locations->GetTemp(0).AsRegister<Register>();
3613 }
3614
3615 DCHECK_EQ(EAX, eax);
3616 DCHECK_EQ(EDX, edx);
3617 if (instruction->IsDiv()) {
3618 DCHECK_EQ(EAX, out);
3619 } else {
3620 DCHECK_EQ(EDX, out);
3621 }
3622
3623 int64_t magic;
3624 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003625 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003626
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003627 // Save the numerator.
3628 __ movl(num, eax);
3629
3630 // EAX = magic
3631 __ movl(eax, Immediate(magic));
3632
3633 // EDX:EAX = magic * numerator
3634 __ imull(num);
3635
3636 if (imm > 0 && magic < 0) {
3637 // EDX += num
3638 __ addl(edx, num);
3639 } else if (imm < 0 && magic > 0) {
3640 __ subl(edx, num);
3641 }
3642
3643 // Shift if needed.
3644 if (shift != 0) {
3645 __ sarl(edx, Immediate(shift));
3646 }
3647
3648 // EDX += 1 if EDX < 0
3649 __ movl(eax, edx);
3650 __ shrl(edx, Immediate(31));
3651 __ addl(edx, eax);
3652
3653 if (instruction->IsRem()) {
3654 __ movl(eax, num);
3655 __ imull(edx, Immediate(imm));
3656 __ subl(eax, edx);
3657 __ movl(edx, eax);
3658 } else {
3659 __ movl(eax, edx);
3660 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003661}
3662
Calin Juravlebacfec32014-11-14 15:54:36 +00003663void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3664 DCHECK(instruction->IsDiv() || instruction->IsRem());
3665
3666 LocationSummary* locations = instruction->GetLocations();
3667 Location out = locations->Out();
3668 Location first = locations->InAt(0);
3669 Location second = locations->InAt(1);
3670 bool is_div = instruction->IsDiv();
3671
3672 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003674 DCHECK_EQ(EAX, first.AsRegister<Register>());
3675 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003676
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003677 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003678 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003679
3680 if (imm == 0) {
3681 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3682 } else if (imm == 1 || imm == -1) {
3683 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303684 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3685 if (is_div) {
3686 DivByPowerOfTwo(instruction->AsDiv());
3687 } else {
3688 RemByPowerOfTwo(instruction->AsRem());
3689 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003690 } else {
3691 DCHECK(imm <= -2 || imm >= 2);
3692 GenerateDivRemWithAnyConstant(instruction);
3693 }
3694 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003695 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003696 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003697 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003698
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003699 Register second_reg = second.AsRegister<Register>();
3700 // 0x80000000/-1 triggers an arithmetic exception!
3701 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3702 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003703
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003704 __ cmpl(second_reg, Immediate(-1));
3705 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003706
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003707 // edx:eax <- sign-extended of eax
3708 __ cdq();
3709 // eax = quotient, edx = remainder
3710 __ idivl(second_reg);
3711 __ Bind(slow_path->GetExitLabel());
3712 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003713 break;
3714 }
3715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003716 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003717 InvokeRuntimeCallingConvention calling_convention;
3718 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3719 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3720 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3721 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3722 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3723 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3724
3725 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003726 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003727 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003728 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003729 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003730 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003731 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003732 break;
3733 }
3734
3735 default:
3736 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3737 }
3738}
3739
Calin Juravle7c4954d2014-10-28 16:57:40 +00003740void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003741 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003742 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003743 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003744 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003745
Calin Juravle7c4954d2014-10-28 16:57:40 +00003746 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003748 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003749 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003750 locations->SetOut(Location::SameAsFirstInput());
3751 // Intel uses edx:eax as the dividend.
3752 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003753 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3754 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3755 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003756 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003757 locations->AddTemp(Location::RequiresRegister());
3758 }
Calin Juravled0d48522014-11-04 16:40:20 +00003759 break;
3760 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003762 InvokeRuntimeCallingConvention calling_convention;
3763 locations->SetInAt(0, Location::RegisterPairLocation(
3764 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3765 locations->SetInAt(1, Location::RegisterPairLocation(
3766 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3767 // Runtime helper puts the result in EAX, EDX.
3768 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003769 break;
3770 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kFloat32:
3772 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003773 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003774 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3775 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003776 } else if (div->InputAt(1)->IsConstant()) {
3777 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003778 } else {
3779 locations->SetInAt(1, Location::Any());
3780 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003781 locations->SetOut(Location::SameAsFirstInput());
3782 break;
3783 }
3784
3785 default:
3786 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3787 }
3788}
3789
3790void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3791 LocationSummary* locations = div->GetLocations();
3792 Location first = locations->InAt(0);
3793 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003794
3795 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 case DataType::Type::kInt32:
3797 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003799 break;
3800 }
3801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003803 if (second.IsFpuRegister()) {
3804 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3805 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3806 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003807 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003808 __ divss(first.AsFpuRegister<XmmRegister>(),
3809 codegen_->LiteralFloatAddress(
3810 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003811 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003812 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3813 } else {
3814 DCHECK(second.IsStackSlot());
3815 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3816 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003817 break;
3818 }
3819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003820 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003821 if (second.IsFpuRegister()) {
3822 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3823 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3824 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003825 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003826 __ divsd(first.AsFpuRegister<XmmRegister>(),
3827 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003828 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3829 const_area->GetBaseMethodAddress(),
3830 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003831 } else {
3832 DCHECK(second.IsDoubleStackSlot());
3833 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3834 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003835 break;
3836 }
3837
3838 default:
3839 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3840 }
3841}
3842
Calin Juravlebacfec32014-11-14 15:54:36 +00003843void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003844 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003846 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003847 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003848 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003849 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003850
Calin Juravled2ec87d2014-12-08 14:24:46 +00003851 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003853 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003854 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003855 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003856 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3857 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3858 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003859 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003860 locations->AddTemp(Location::RequiresRegister());
3861 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003862 break;
3863 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003865 InvokeRuntimeCallingConvention calling_convention;
3866 locations->SetInAt(0, Location::RegisterPairLocation(
3867 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3868 locations->SetInAt(1, Location::RegisterPairLocation(
3869 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3870 // Runtime helper puts the result in EAX, EDX.
3871 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3872 break;
3873 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kFloat64:
3875 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003876 locations->SetInAt(0, Location::Any());
3877 locations->SetInAt(1, Location::Any());
3878 locations->SetOut(Location::RequiresFpuRegister());
3879 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003880 break;
3881 }
3882
3883 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003884 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003885 }
3886}
3887
3888void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003890 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt32:
3892 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003893 GenerateDivRemIntegral(rem);
3894 break;
3895 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kFloat32:
3897 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003898 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003899 break;
3900 }
3901 default:
3902 LOG(FATAL) << "Unexpected rem type " << type;
3903 }
3904}
3905
Aart Bik1f8d51b2018-02-15 10:42:37 -08003906static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3907 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3908 switch (minmax->GetResultType()) {
3909 case DataType::Type::kInt32:
3910 locations->SetInAt(0, Location::RequiresRegister());
3911 locations->SetInAt(1, Location::RequiresRegister());
3912 locations->SetOut(Location::SameAsFirstInput());
3913 break;
3914 case DataType::Type::kInt64:
3915 locations->SetInAt(0, Location::RequiresRegister());
3916 locations->SetInAt(1, Location::RequiresRegister());
3917 locations->SetOut(Location::SameAsFirstInput());
3918 // Register to use to perform a long subtract to set cc.
3919 locations->AddTemp(Location::RequiresRegister());
3920 break;
3921 case DataType::Type::kFloat32:
3922 locations->SetInAt(0, Location::RequiresFpuRegister());
3923 locations->SetInAt(1, Location::RequiresFpuRegister());
3924 locations->SetOut(Location::SameAsFirstInput());
3925 locations->AddTemp(Location::RequiresRegister());
3926 break;
3927 case DataType::Type::kFloat64:
3928 locations->SetInAt(0, Location::RequiresFpuRegister());
3929 locations->SetInAt(1, Location::RequiresFpuRegister());
3930 locations->SetOut(Location::SameAsFirstInput());
3931 break;
3932 default:
3933 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3934 }
3935}
3936
Aart Bik351df3e2018-03-07 11:54:57 -08003937void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3938 bool is_min,
3939 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003940 Location op1_loc = locations->InAt(0);
3941 Location op2_loc = locations->InAt(1);
3942
3943 // Shortcut for same input locations.
3944 if (op1_loc.Equals(op2_loc)) {
3945 // Can return immediately, as op1_loc == out_loc.
3946 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3947 // a copy here.
3948 DCHECK(locations->Out().Equals(op1_loc));
3949 return;
3950 }
3951
3952 if (type == DataType::Type::kInt64) {
3953 // Need to perform a subtract to get the sign right.
3954 // op1 is already in the same location as the output.
3955 Location output = locations->Out();
3956 Register output_lo = output.AsRegisterPairLow<Register>();
3957 Register output_hi = output.AsRegisterPairHigh<Register>();
3958
3959 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3960 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3961
3962 // The comparison is performed by subtracting the second operand from
3963 // the first operand and then setting the status flags in the same
3964 // manner as the SUB instruction."
3965 __ cmpl(output_lo, op2_lo);
3966
3967 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3968 Register temp = locations->GetTemp(0).AsRegister<Register>();
3969 __ movl(temp, output_hi);
3970 __ sbbl(temp, op2_hi);
3971
3972 // Now the condition code is correct.
3973 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3974 __ cmovl(cond, output_lo, op2_lo);
3975 __ cmovl(cond, output_hi, op2_hi);
3976 } else {
3977 DCHECK_EQ(type, DataType::Type::kInt32);
3978 Register out = locations->Out().AsRegister<Register>();
3979 Register op2 = op2_loc.AsRegister<Register>();
3980
3981 // (out := op1)
3982 // out <=? op2
3983 // if out is min jmp done
3984 // out := op2
3985 // done:
3986
3987 __ cmpl(out, op2);
3988 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3989 __ cmovl(cond, out, op2);
3990 }
3991}
3992
3993void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3994 bool is_min,
3995 DataType::Type type) {
3996 Location op1_loc = locations->InAt(0);
3997 Location op2_loc = locations->InAt(1);
3998 Location out_loc = locations->Out();
3999 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4000
4001 // Shortcut for same input locations.
4002 if (op1_loc.Equals(op2_loc)) {
4003 DCHECK(out_loc.Equals(op1_loc));
4004 return;
4005 }
4006
4007 // (out := op1)
4008 // out <=? op2
4009 // if Nan jmp Nan_label
4010 // if out is min jmp done
4011 // if op2 is min jmp op2_label
4012 // handle -0/+0
4013 // jmp done
4014 // Nan_label:
4015 // out := NaN
4016 // op2_label:
4017 // out := op2
4018 // done:
4019 //
4020 // This removes one jmp, but needs to copy one input (op1) to out.
4021 //
4022 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4023
4024 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4025
4026 NearLabel nan, done, op2_label;
4027 if (type == DataType::Type::kFloat64) {
4028 __ ucomisd(out, op2);
4029 } else {
4030 DCHECK_EQ(type, DataType::Type::kFloat32);
4031 __ ucomiss(out, op2);
4032 }
4033
4034 __ j(Condition::kParityEven, &nan);
4035
4036 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4037 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4038
4039 // Handle 0.0/-0.0.
4040 if (is_min) {
4041 if (type == DataType::Type::kFloat64) {
4042 __ orpd(out, op2);
4043 } else {
4044 __ orps(out, op2);
4045 }
4046 } else {
4047 if (type == DataType::Type::kFloat64) {
4048 __ andpd(out, op2);
4049 } else {
4050 __ andps(out, op2);
4051 }
4052 }
4053 __ jmp(&done);
4054
4055 // NaN handling.
4056 __ Bind(&nan);
4057 if (type == DataType::Type::kFloat64) {
4058 // TODO: Use a constant from the constant table (requires extra input).
4059 __ LoadLongConstant(out, kDoubleNaN);
4060 } else {
4061 Register constant = locations->GetTemp(0).AsRegister<Register>();
4062 __ movl(constant, Immediate(kFloatNaN));
4063 __ movd(out, constant);
4064 }
4065 __ jmp(&done);
4066
4067 // out := op2;
4068 __ Bind(&op2_label);
4069 if (type == DataType::Type::kFloat64) {
4070 __ movsd(out, op2);
4071 } else {
4072 __ movss(out, op2);
4073 }
4074
4075 // Done.
4076 __ Bind(&done);
4077}
4078
Aart Bik351df3e2018-03-07 11:54:57 -08004079void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4080 DataType::Type type = minmax->GetResultType();
4081 switch (type) {
4082 case DataType::Type::kInt32:
4083 case DataType::Type::kInt64:
4084 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4085 break;
4086 case DataType::Type::kFloat32:
4087 case DataType::Type::kFloat64:
4088 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4089 break;
4090 default:
4091 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4092 }
4093}
4094
Aart Bik1f8d51b2018-02-15 10:42:37 -08004095void LocationsBuilderX86::VisitMin(HMin* min) {
4096 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4097}
4098
4099void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004100 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004101}
4102
4103void LocationsBuilderX86::VisitMax(HMax* max) {
4104 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4105}
4106
4107void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004108 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004109}
4110
Aart Bik3dad3412018-02-28 12:01:46 -08004111void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4112 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4113 switch (abs->GetResultType()) {
4114 case DataType::Type::kInt32:
4115 locations->SetInAt(0, Location::RegisterLocation(EAX));
4116 locations->SetOut(Location::SameAsFirstInput());
4117 locations->AddTemp(Location::RegisterLocation(EDX));
4118 break;
4119 case DataType::Type::kInt64:
4120 locations->SetInAt(0, Location::RequiresRegister());
4121 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4122 locations->AddTemp(Location::RequiresRegister());
4123 break;
4124 case DataType::Type::kFloat32:
4125 locations->SetInAt(0, Location::RequiresFpuRegister());
4126 locations->SetOut(Location::SameAsFirstInput());
4127 locations->AddTemp(Location::RequiresFpuRegister());
4128 locations->AddTemp(Location::RequiresRegister());
4129 break;
4130 case DataType::Type::kFloat64:
4131 locations->SetInAt(0, Location::RequiresFpuRegister());
4132 locations->SetOut(Location::SameAsFirstInput());
4133 locations->AddTemp(Location::RequiresFpuRegister());
4134 break;
4135 default:
4136 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4137 }
4138}
4139
4140void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4141 LocationSummary* locations = abs->GetLocations();
4142 switch (abs->GetResultType()) {
4143 case DataType::Type::kInt32: {
4144 Register out = locations->Out().AsRegister<Register>();
4145 DCHECK_EQ(out, EAX);
4146 Register temp = locations->GetTemp(0).AsRegister<Register>();
4147 DCHECK_EQ(temp, EDX);
4148 // Sign extend EAX into EDX.
4149 __ cdq();
4150 // XOR EAX with sign.
4151 __ xorl(EAX, EDX);
4152 // Subtract out sign to correct.
4153 __ subl(EAX, EDX);
4154 // The result is in EAX.
4155 break;
4156 }
4157 case DataType::Type::kInt64: {
4158 Location input = locations->InAt(0);
4159 Register input_lo = input.AsRegisterPairLow<Register>();
4160 Register input_hi = input.AsRegisterPairHigh<Register>();
4161 Location output = locations->Out();
4162 Register output_lo = output.AsRegisterPairLow<Register>();
4163 Register output_hi = output.AsRegisterPairHigh<Register>();
4164 Register temp = locations->GetTemp(0).AsRegister<Register>();
4165 // Compute the sign into the temporary.
4166 __ movl(temp, input_hi);
4167 __ sarl(temp, Immediate(31));
4168 // Store the sign into the output.
4169 __ movl(output_lo, temp);
4170 __ movl(output_hi, temp);
4171 // XOR the input to the output.
4172 __ xorl(output_lo, input_lo);
4173 __ xorl(output_hi, input_hi);
4174 // Subtract the sign.
4175 __ subl(output_lo, temp);
4176 __ sbbl(output_hi, temp);
4177 break;
4178 }
4179 case DataType::Type::kFloat32: {
4180 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4181 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4182 Register constant = locations->GetTemp(1).AsRegister<Register>();
4183 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4184 __ movd(temp, constant);
4185 __ andps(out, temp);
4186 break;
4187 }
4188 case DataType::Type::kFloat64: {
4189 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4190 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4191 // TODO: Use a constant from the constant table (requires extra input).
4192 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4193 __ andpd(out, temp);
4194 break;
4195 }
4196 default:
4197 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4198 }
4199}
4200
Calin Juravled0d48522014-11-04 16:40:20 +00004201void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004202 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004203 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004204 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004205 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004206 case DataType::Type::kInt8:
4207 case DataType::Type::kUint16:
4208 case DataType::Type::kInt16:
4209 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004210 locations->SetInAt(0, Location::Any());
4211 break;
4212 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004214 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4215 if (!instruction->IsConstant()) {
4216 locations->AddTemp(Location::RequiresRegister());
4217 }
4218 break;
4219 }
4220 default:
4221 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4222 }
Calin Juravled0d48522014-11-04 16:40:20 +00004223}
4224
4225void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004226 SlowPathCode* slow_path =
4227 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004228 codegen_->AddSlowPath(slow_path);
4229
4230 LocationSummary* locations = instruction->GetLocations();
4231 Location value = locations->InAt(0);
4232
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004233 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004234 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004235 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 case DataType::Type::kInt8:
4237 case DataType::Type::kUint16:
4238 case DataType::Type::kInt16:
4239 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004240 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004241 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004242 __ j(kEqual, slow_path->GetEntryLabel());
4243 } else if (value.IsStackSlot()) {
4244 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4245 __ j(kEqual, slow_path->GetEntryLabel());
4246 } else {
4247 DCHECK(value.IsConstant()) << value;
4248 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004249 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004250 }
4251 }
4252 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004253 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004254 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004255 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004256 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004257 __ movl(temp, value.AsRegisterPairLow<Register>());
4258 __ orl(temp, value.AsRegisterPairHigh<Register>());
4259 __ j(kEqual, slow_path->GetEntryLabel());
4260 } else {
4261 DCHECK(value.IsConstant()) << value;
4262 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4263 __ jmp(slow_path->GetEntryLabel());
4264 }
4265 }
4266 break;
4267 }
4268 default:
4269 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004270 }
Calin Juravled0d48522014-11-04 16:40:20 +00004271}
4272
Calin Juravle9aec02f2014-11-18 23:06:35 +00004273void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4274 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4275
4276 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004277 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004278
4279 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004280 case DataType::Type::kInt32:
4281 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004282 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004283 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004284 // The shift count needs to be in CL or a constant.
4285 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004286 locations->SetOut(Location::SameAsFirstInput());
4287 break;
4288 }
4289 default:
4290 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4291 }
4292}
4293
4294void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4295 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4296
4297 LocationSummary* locations = op->GetLocations();
4298 Location first = locations->InAt(0);
4299 Location second = locations->InAt(1);
4300 DCHECK(first.Equals(locations->Out()));
4301
4302 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004304 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004305 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004306 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004307 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004308 DCHECK_EQ(ECX, second_reg);
4309 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004310 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004311 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004312 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004313 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004314 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004315 }
4316 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004317 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004318 if (shift == 0) {
4319 return;
4320 }
4321 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004322 if (op->IsShl()) {
4323 __ shll(first_reg, imm);
4324 } else if (op->IsShr()) {
4325 __ sarl(first_reg, imm);
4326 } else {
4327 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004328 }
4329 }
4330 break;
4331 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004332 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004333 if (second.IsRegister()) {
4334 Register second_reg = second.AsRegister<Register>();
4335 DCHECK_EQ(ECX, second_reg);
4336 if (op->IsShl()) {
4337 GenerateShlLong(first, second_reg);
4338 } else if (op->IsShr()) {
4339 GenerateShrLong(first, second_reg);
4340 } else {
4341 GenerateUShrLong(first, second_reg);
4342 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004343 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004344 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004345 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004346 // Nothing to do if the shift is 0, as the input is already the output.
4347 if (shift != 0) {
4348 if (op->IsShl()) {
4349 GenerateShlLong(first, shift);
4350 } else if (op->IsShr()) {
4351 GenerateShrLong(first, shift);
4352 } else {
4353 GenerateUShrLong(first, shift);
4354 }
4355 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004356 }
4357 break;
4358 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004359 default:
4360 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4361 }
4362}
4363
Mark P Mendell73945692015-04-29 14:56:17 +00004364void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4365 Register low = loc.AsRegisterPairLow<Register>();
4366 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004367 if (shift == 1) {
4368 // This is just an addition.
4369 __ addl(low, low);
4370 __ adcl(high, high);
4371 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004372 // Shift by 32 is easy. High gets low, and low gets 0.
4373 codegen_->EmitParallelMoves(
4374 loc.ToLow(),
4375 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004376 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004377 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4378 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004379 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004380 } else if (shift > 32) {
4381 // Low part becomes 0. High part is low part << (shift-32).
4382 __ movl(high, low);
4383 __ shll(high, Immediate(shift - 32));
4384 __ xorl(low, low);
4385 } else {
4386 // Between 1 and 31.
4387 __ shld(high, low, Immediate(shift));
4388 __ shll(low, Immediate(shift));
4389 }
4390}
4391
Calin Juravle9aec02f2014-11-18 23:06:35 +00004392void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004393 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004394 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4395 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4396 __ testl(shifter, Immediate(32));
4397 __ j(kEqual, &done);
4398 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4399 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4400 __ Bind(&done);
4401}
4402
Mark P Mendell73945692015-04-29 14:56:17 +00004403void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4404 Register low = loc.AsRegisterPairLow<Register>();
4405 Register high = loc.AsRegisterPairHigh<Register>();
4406 if (shift == 32) {
4407 // Need to copy the sign.
4408 DCHECK_NE(low, high);
4409 __ movl(low, high);
4410 __ sarl(high, Immediate(31));
4411 } else if (shift > 32) {
4412 DCHECK_NE(low, high);
4413 // High part becomes sign. Low part is shifted by shift - 32.
4414 __ movl(low, high);
4415 __ sarl(high, Immediate(31));
4416 __ sarl(low, Immediate(shift - 32));
4417 } else {
4418 // Between 1 and 31.
4419 __ shrd(low, high, Immediate(shift));
4420 __ sarl(high, Immediate(shift));
4421 }
4422}
4423
Calin Juravle9aec02f2014-11-18 23:06:35 +00004424void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004425 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004426 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4427 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4428 __ testl(shifter, Immediate(32));
4429 __ j(kEqual, &done);
4430 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4431 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4432 __ Bind(&done);
4433}
4434
Mark P Mendell73945692015-04-29 14:56:17 +00004435void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4436 Register low = loc.AsRegisterPairLow<Register>();
4437 Register high = loc.AsRegisterPairHigh<Register>();
4438 if (shift == 32) {
4439 // Shift by 32 is easy. Low gets high, and high gets 0.
4440 codegen_->EmitParallelMoves(
4441 loc.ToHigh(),
4442 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004443 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004444 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4445 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004446 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004447 } else if (shift > 32) {
4448 // Low part is high >> (shift - 32). High part becomes 0.
4449 __ movl(low, high);
4450 __ shrl(low, Immediate(shift - 32));
4451 __ xorl(high, high);
4452 } else {
4453 // Between 1 and 31.
4454 __ shrd(low, high, Immediate(shift));
4455 __ shrl(high, Immediate(shift));
4456 }
4457}
4458
Calin Juravle9aec02f2014-11-18 23:06:35 +00004459void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004460 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004461 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4462 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4463 __ testl(shifter, Immediate(32));
4464 __ j(kEqual, &done);
4465 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4466 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4467 __ Bind(&done);
4468}
4469
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004470void LocationsBuilderX86::VisitRor(HRor* ror) {
4471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004472 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004473
4474 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004475 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004476 // Add the temporary needed.
4477 locations->AddTemp(Location::RequiresRegister());
4478 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004479 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004480 locations->SetInAt(0, Location::RequiresRegister());
4481 // The shift count needs to be in CL (unless it is a constant).
4482 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4483 locations->SetOut(Location::SameAsFirstInput());
4484 break;
4485 default:
4486 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4487 UNREACHABLE();
4488 }
4489}
4490
4491void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4492 LocationSummary* locations = ror->GetLocations();
4493 Location first = locations->InAt(0);
4494 Location second = locations->InAt(1);
4495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004496 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004497 Register first_reg = first.AsRegister<Register>();
4498 if (second.IsRegister()) {
4499 Register second_reg = second.AsRegister<Register>();
4500 __ rorl(first_reg, second_reg);
4501 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004503 __ rorl(first_reg, imm);
4504 }
4505 return;
4506 }
4507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004508 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004509 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4510 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4511 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4512 if (second.IsRegister()) {
4513 Register second_reg = second.AsRegister<Register>();
4514 DCHECK_EQ(second_reg, ECX);
4515 __ movl(temp_reg, first_reg_hi);
4516 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4517 __ shrd(first_reg_lo, temp_reg, second_reg);
4518 __ movl(temp_reg, first_reg_hi);
4519 __ testl(second_reg, Immediate(32));
4520 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4521 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4522 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004523 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004524 if (shift_amt == 0) {
4525 // Already fine.
4526 return;
4527 }
4528 if (shift_amt == 32) {
4529 // Just swap.
4530 __ movl(temp_reg, first_reg_lo);
4531 __ movl(first_reg_lo, first_reg_hi);
4532 __ movl(first_reg_hi, temp_reg);
4533 return;
4534 }
4535
4536 Immediate imm(shift_amt);
4537 // Save the constents of the low value.
4538 __ movl(temp_reg, first_reg_lo);
4539
4540 // Shift right into low, feeding bits from high.
4541 __ shrd(first_reg_lo, first_reg_hi, imm);
4542
4543 // Shift right into high, feeding bits from the original low.
4544 __ shrd(first_reg_hi, temp_reg, imm);
4545
4546 // Swap if needed.
4547 if (shift_amt > 32) {
4548 __ movl(temp_reg, first_reg_lo);
4549 __ movl(first_reg_lo, first_reg_hi);
4550 __ movl(first_reg_hi, temp_reg);
4551 }
4552 }
4553}
4554
Calin Juravle9aec02f2014-11-18 23:06:35 +00004555void LocationsBuilderX86::VisitShl(HShl* shl) {
4556 HandleShift(shl);
4557}
4558
4559void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4560 HandleShift(shl);
4561}
4562
4563void LocationsBuilderX86::VisitShr(HShr* shr) {
4564 HandleShift(shr);
4565}
4566
4567void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4568 HandleShift(shr);
4569}
4570
4571void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4572 HandleShift(ushr);
4573}
4574
4575void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4576 HandleShift(ushr);
4577}
4578
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004579void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004580 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4581 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004582 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004583 InvokeRuntimeCallingConvention calling_convention;
4584 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004585}
4586
4587void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004588 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4589 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4590 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004591}
4592
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004593void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004594 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4595 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004596 locations->SetOut(Location::RegisterLocation(EAX));
4597 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004598 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4599 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004600}
4601
4602void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004603 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4604 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004605 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004606 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004607 DCHECK(!codegen_->IsLeafMethod());
4608}
4609
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004610void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004611 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004612 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004613 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4614 if (location.IsStackSlot()) {
4615 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4616 } else if (location.IsDoubleStackSlot()) {
4617 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004618 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004619 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004620}
4621
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004622void InstructionCodeGeneratorX86::VisitParameterValue(
4623 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4624}
4625
4626void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4627 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004628 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004629 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4630}
4631
4632void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004633}
4634
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004635void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4636 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004637 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004638 locations->SetInAt(0, Location::RequiresRegister());
4639 locations->SetOut(Location::RequiresRegister());
4640}
4641
4642void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4643 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004644 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004645 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004646 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004647 __ movl(locations->Out().AsRegister<Register>(),
4648 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004649 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004650 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004651 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004652 __ movl(locations->Out().AsRegister<Register>(),
4653 Address(locations->InAt(0).AsRegister<Register>(),
4654 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4655 // temp = temp->GetImtEntryAt(method_offset);
4656 __ movl(locations->Out().AsRegister<Register>(),
4657 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004658 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004659}
4660
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004661void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004662 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004663 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004664 locations->SetInAt(0, Location::RequiresRegister());
4665 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004666}
4667
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004668void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4669 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004670 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004671 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004672 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004673 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004675 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004676 break;
4677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004678 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004679 __ notl(out.AsRegisterPairLow<Register>());
4680 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004681 break;
4682
4683 default:
4684 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4685 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004686}
4687
David Brazdil66d126e2015-04-03 16:02:44 +01004688void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4689 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004690 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004691 locations->SetInAt(0, Location::RequiresRegister());
4692 locations->SetOut(Location::SameAsFirstInput());
4693}
4694
4695void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004696 LocationSummary* locations = bool_not->GetLocations();
4697 Location in = locations->InAt(0);
4698 Location out = locations->Out();
4699 DCHECK(in.Equals(out));
4700 __ xorl(out.AsRegister<Register>(), Immediate(1));
4701}
4702
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004703void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004704 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004705 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004706 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004707 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004708 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004709 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004710 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004711 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 case DataType::Type::kInt32:
4713 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004714 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004715 locations->SetInAt(1, Location::Any());
4716 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4717 break;
4718 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 case DataType::Type::kFloat32:
4720 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004721 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004722 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4723 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4724 } else if (compare->InputAt(1)->IsConstant()) {
4725 locations->SetInAt(1, Location::RequiresFpuRegister());
4726 } else {
4727 locations->SetInAt(1, Location::Any());
4728 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004729 locations->SetOut(Location::RequiresRegister());
4730 break;
4731 }
4732 default:
4733 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4734 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004735}
4736
4737void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004738 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004739 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004740 Location left = locations->InAt(0);
4741 Location right = locations->InAt(1);
4742
Mark Mendell0c9497d2015-08-21 09:30:05 -04004743 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004744 Condition less_cond = kLess;
4745
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004746 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004748 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004749 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004750 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004751 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004752 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004753 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004754 break;
4755 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004756 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004757 Register left_low = left.AsRegisterPairLow<Register>();
4758 Register left_high = left.AsRegisterPairHigh<Register>();
4759 int32_t val_low = 0;
4760 int32_t val_high = 0;
4761 bool right_is_const = false;
4762
4763 if (right.IsConstant()) {
4764 DCHECK(right.GetConstant()->IsLongConstant());
4765 right_is_const = true;
4766 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4767 val_low = Low32Bits(val);
4768 val_high = High32Bits(val);
4769 }
4770
Calin Juravleddb7df22014-11-25 20:56:51 +00004771 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004772 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004773 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004774 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004775 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004776 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004777 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004778 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004779 __ j(kLess, &less); // Signed compare.
4780 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004781 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004782 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004783 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004784 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004785 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004786 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004787 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004788 }
Aart Bika19616e2016-02-01 18:57:58 -08004789 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004790 break;
4791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004793 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004794 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004795 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004796 break;
4797 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004799 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004800 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004801 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004802 break;
4803 }
4804 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004805 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004806 }
Aart Bika19616e2016-02-01 18:57:58 -08004807
Calin Juravleddb7df22014-11-25 20:56:51 +00004808 __ movl(out, Immediate(0));
4809 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004810 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004811
4812 __ Bind(&greater);
4813 __ movl(out, Immediate(1));
4814 __ jmp(&done);
4815
4816 __ Bind(&less);
4817 __ movl(out, Immediate(-1));
4818
4819 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004820}
4821
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004822void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004823 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004824 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004825 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004826 locations->SetInAt(i, Location::Any());
4827 }
4828 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004829}
4830
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004831void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004832 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004833}
4834
Roland Levillain7c1559a2015-12-15 10:55:36 +00004835void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004836 /*
4837 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4838 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4839 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4840 */
4841 switch (kind) {
4842 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004843 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004844 break;
4845 }
4846 case MemBarrierKind::kAnyStore:
4847 case MemBarrierKind::kLoadAny:
4848 case MemBarrierKind::kStoreStore: {
4849 // nop
4850 break;
4851 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004852 case MemBarrierKind::kNTStoreStore:
4853 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004854 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004855 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004856 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857}
4858
Vladimir Markodc151b22015-10-15 18:02:30 +01004859HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4860 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004861 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004862 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004863}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004864
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004865Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4866 Register temp) {
4867 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004868 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004869 if (!invoke->GetLocations()->Intrinsified()) {
4870 return location.AsRegister<Register>();
4871 }
4872 // For intrinsics we allow any location, so it may be on the stack.
4873 if (!location.IsRegister()) {
4874 __ movl(temp, Address(ESP, location.GetStackIndex()));
4875 return temp;
4876 }
4877 // For register locations, check if the register was saved. If so, get it from the stack.
4878 // Note: There is a chance that the register was saved but not overwritten, so we could
4879 // save one load. However, since this is just an intrinsic slow path we prefer this
4880 // simple and more robust approach rather that trying to determine if that's the case.
4881 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004882 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4883 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4884 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4885 __ movl(temp, Address(ESP, stack_offset));
4886 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004887 }
4888 return location.AsRegister<Register>();
4889}
4890
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004891void CodeGeneratorX86::GenerateStaticOrDirectCall(
4892 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004893 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4894 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004895 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004896 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004897 uint32_t offset =
4898 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4899 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004900 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004901 }
Vladimir Marko58155012015-08-19 12:49:41 +00004902 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004903 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004904 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004905 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004906 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004907 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4908 temp.AsRegister<Register>());
4909 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004910 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004911 break;
4912 }
Vladimir Markob066d432018-01-03 13:14:37 +00004913 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4914 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4915 temp.AsRegister<Register>());
4916 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4917 RecordBootImageRelRoPatch(
4918 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004919 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004920 break;
4921 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004922 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004923 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4924 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004925 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004926 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004927 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004928 break;
4929 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004930 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4931 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4932 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004933 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4934 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4935 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004936 }
Vladimir Marko58155012015-08-19 12:49:41 +00004937 }
4938
4939 switch (invoke->GetCodePtrLocation()) {
4940 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4941 __ call(GetFrameEntryLabel());
4942 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004943 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4944 // (callee_method + offset_of_quick_compiled_code)()
4945 __ call(Address(callee_method.AsRegister<Register>(),
4946 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004947 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004948 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004949 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004950 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004951
4952 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004953}
4954
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004955void CodeGeneratorX86::GenerateVirtualCall(
4956 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004957 Register temp = temp_in.AsRegister<Register>();
4958 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4959 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004960
4961 // Use the calling convention instead of the location of the receiver, as
4962 // intrinsics may have put the receiver in a different register. In the intrinsics
4963 // slow path, the arguments have been moved to the right place, so here we are
4964 // guaranteed that the receiver is the first register of the calling convention.
4965 InvokeDexCallingConvention calling_convention;
4966 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004967 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004968 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004969 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004970 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004971 // Instead of simply (possibly) unpoisoning `temp` here, we should
4972 // emit a read barrier for the previous class reference load.
4973 // However this is not required in practice, as this is an
4974 // intermediate/temporary reference and because the current
4975 // concurrent copying collector keeps the from-space memory
4976 // intact/accessible until the end of the marking phase (the
4977 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004978 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004979
4980 MaybeGenerateInlineCacheCheck(invoke, temp);
4981
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004982 // temp = temp->GetMethodAt(method_offset);
4983 __ movl(temp, Address(temp, method_offset));
4984 // call temp->GetEntryPoint();
4985 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004986 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004987 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004988}
4989
Vladimir Marko6fd16062018-06-26 11:02:04 +01004990void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
4991 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004992 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08004993 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01004994 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004995}
4996
Vladimir Markob066d432018-01-03 13:14:37 +00004997void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4998 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004999 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005000 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005001 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005002}
5003
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005004void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01005005 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005006 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005007 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005008 boot_image_method_patches_.emplace_back(
5009 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005010 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005011}
5012
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005013void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
5014 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5015 HX86ComputeBaseMethodAddress* method_address =
5016 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005017 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005018 method_bss_entry_patches_.emplace_back(
5019 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5020 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005021}
5022
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005023void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5024 HX86ComputeBaseMethodAddress* method_address =
5025 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5026 boot_image_type_patches_.emplace_back(
5027 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005028 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005029}
5030
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005031Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005032 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005033 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5034 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005035 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005036 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005037}
5038
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005039void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5040 HX86ComputeBaseMethodAddress* method_address =
5041 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5042 boot_image_string_patches_.emplace_back(
5043 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5044 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005045}
5046
Vladimir Markoaad75c62016-10-03 08:46:48 +00005047Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005048 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005049 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005050 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005051 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005052 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005053}
5054
Vladimir Markoeebb8212018-06-05 14:57:24 +01005055void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005056 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005057 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005058 if (GetCompilerOptions().IsBootImage()) {
5059 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5060 HX86ComputeBaseMethodAddress* method_address =
5061 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5062 DCHECK(method_address != nullptr);
5063 Register method_address_reg =
5064 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5065 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5066 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005067 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005068 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5069 HX86ComputeBaseMethodAddress* method_address =
5070 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5071 DCHECK(method_address != nullptr);
5072 Register method_address_reg =
5073 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5074 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005075 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005076 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005077 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005078 gc::Heap* heap = Runtime::Current()->GetHeap();
5079 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005080 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005081 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5082 }
5083}
5084
Vladimir Marko6fd16062018-06-26 11:02:04 +01005085void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5086 uint32_t boot_image_offset) {
5087 DCHECK(invoke->IsStatic());
5088 InvokeRuntimeCallingConvention calling_convention;
5089 Register argument = calling_convention.GetRegisterAt(0);
5090 if (GetCompilerOptions().IsBootImage()) {
5091 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5092 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5093 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5094 HX86ComputeBaseMethodAddress* method_address =
5095 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5096 DCHECK(method_address != nullptr);
5097 Register method_address_reg =
5098 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5099 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5100 MethodReference target_method = invoke->GetTargetMethod();
5101 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5102 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5103 __ Bind(&boot_image_type_patches_.back().label);
5104 } else {
5105 LoadBootImageAddress(argument, boot_image_offset, invoke);
5106 }
5107 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5108 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5109}
5110
Vladimir Markoaad75c62016-10-03 08:46:48 +00005111// The label points to the end of the "movl" or another instruction but the literal offset
5112// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5113constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5114
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005115template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005116inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005117 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005118 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005119 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005120 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005121 linker_patches->push_back(Factory(literal_offset,
5122 info.target_dex_file,
5123 GetMethodAddressOffset(info.method_address),
5124 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005125 }
5126}
5127
Vladimir Marko6fd16062018-06-26 11:02:04 +01005128template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5129linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5130 const DexFile* target_dex_file,
5131 uint32_t pc_insn_offset,
5132 uint32_t boot_image_offset) {
5133 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5134 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005135}
5136
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005137void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005138 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005139 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005140 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005141 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005142 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005143 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005144 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005145 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005146 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005147 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005148 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005149 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5150 boot_image_method_patches_, linker_patches);
5151 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5152 boot_image_type_patches_, linker_patches);
5153 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005154 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005155 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005156 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005157 DCHECK(boot_image_type_patches_.empty());
5158 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005159 }
5160 if (GetCompilerOptions().IsBootImage()) {
5161 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5162 boot_image_other_patches_, linker_patches);
5163 } else {
5164 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5165 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005166 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005167 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5168 method_bss_entry_patches_, linker_patches);
5169 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5170 type_bss_entry_patches_, linker_patches);
5171 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5172 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005173 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005174}
5175
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005176void CodeGeneratorX86::MarkGCCard(Register temp,
5177 Register card,
5178 Register object,
5179 Register value,
5180 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005181 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005182 if (value_can_be_null) {
5183 __ testl(value, value);
5184 __ j(kEqual, &is_null);
5185 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005186 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005187 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005188 // Calculate the offset (in the card table) of the card corresponding to
5189 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 __ movl(temp, object);
5191 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005192 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5193 // `object`'s card.
5194 //
5195 // Register `card` contains the address of the card table. Note that the card
5196 // table's base is biased during its creation so that it always starts at an
5197 // address whose least-significant byte is equal to `kCardDirty` (see
5198 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5199 // below writes the `kCardDirty` (byte) value into the `object`'s card
5200 // (located at `card + object >> kCardShift`).
5201 //
5202 // This dual use of the value in register `card` (1. to calculate the location
5203 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5204 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005205 __ movb(Address(temp, card, TIMES_1, 0),
5206 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005207 if (value_can_be_null) {
5208 __ Bind(&is_null);
5209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210}
5211
Calin Juravle52c48962014-12-16 17:02:57 +00005212void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5213 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214
5215 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005216 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005218 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5219 kEmitCompilerReadBarrier
5220 ? LocationSummary::kCallOnSlowPath
5221 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005222 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005223 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005224 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005225 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005227 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005228 locations->SetOut(Location::RequiresFpuRegister());
5229 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005230 // The output overlaps in case of long: we don't want the low move
5231 // to overwrite the object's location. Likewise, in the case of
5232 // an object field get with read barriers enabled, we do not want
5233 // the move to overwrite the object's location, as we need it to emit
5234 // the read barrier.
5235 locations->SetOut(
5236 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005238 Location::kOutputOverlap :
5239 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005240 }
Calin Juravle52c48962014-12-16 17:02:57 +00005241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005243 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005244 // So we use an XMM register as a temp to achieve atomicity (first
5245 // load the temp into the XMM and then copy the XMM into the
5246 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005247 locations->AddTemp(Location::RequiresFpuRegister());
5248 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005249}
5250
Calin Juravle52c48962014-12-16 17:02:57 +00005251void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5252 const FieldInfo& field_info) {
5253 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005254
Calin Juravle52c48962014-12-16 17:02:57 +00005255 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005256 Location base_loc = locations->InAt(0);
5257 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005258 Location out = locations->Out();
5259 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005260 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5261 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005262 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5263
Vladimir Marko61b92282017-10-11 13:23:17 +01005264 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005265 case DataType::Type::kBool:
5266 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005267 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005268 break;
5269 }
5270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005271 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005272 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005273 break;
5274 }
5275
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005276 case DataType::Type::kUint16: {
5277 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005278 break;
5279 }
5280
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005281 case DataType::Type::kInt16: {
5282 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005283 break;
5284 }
5285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005287 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005288 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005290 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 // /* HeapReference<Object> */ out = *(base + offset)
5292 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005293 // Note that a potential implicit null check is handled in this
5294 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5295 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005296 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 if (is_volatile) {
5298 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5299 }
5300 } else {
5301 __ movl(out.AsRegister<Register>(), Address(base, offset));
5302 codegen_->MaybeRecordImplicitNullCheck(instruction);
5303 if (is_volatile) {
5304 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5305 }
5306 // If read barriers are enabled, emit read barriers other than
5307 // Baker's using a slow path (and also unpoison the loaded
5308 // reference, if heap poisoning is enabled).
5309 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5310 }
5311 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005312 }
5313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005314 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005315 if (is_volatile) {
5316 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5317 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005319 __ movd(out.AsRegisterPairLow<Register>(), temp);
5320 __ psrlq(temp, Immediate(32));
5321 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5322 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005323 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005324 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005326 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5327 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005328 break;
5329 }
5330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005331 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005332 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005333 break;
5334 }
5335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005336 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005337 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005338 break;
5339 }
5340
Aart Bik66c158e2018-01-31 12:55:04 -08005341 case DataType::Type::kUint32:
5342 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005343 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005344 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005345 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005346 }
Calin Juravle52c48962014-12-16 17:02:57 +00005347
Vladimir Marko61b92282017-10-11 13:23:17 +01005348 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005349 // Potential implicit null checks, in the case of reference or
5350 // long fields, are handled in the previous switch statement.
5351 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 codegen_->MaybeRecordImplicitNullCheck(instruction);
5353 }
5354
Calin Juravle52c48962014-12-16 17:02:57 +00005355 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005356 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005357 // Memory barriers, in the case of references, are also handled
5358 // in the previous switch statement.
5359 } else {
5360 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5361 }
Roland Levillain4d027112015-07-01 15:41:14 +01005362 }
Calin Juravle52c48962014-12-16 17:02:57 +00005363}
5364
5365void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5366 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5367
5368 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005369 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005370 locations->SetInAt(0, Location::RequiresRegister());
5371 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005372 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005373 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005374
5375 // The register allocator does not support multiple
5376 // inputs that die at entry with one in a specific register.
5377 if (is_byte_type) {
5378 // Ensure the value is in a byte register.
5379 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005380 } else if (DataType::IsFloatingPointType(field_type)) {
5381 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005382 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5383 locations->SetInAt(1, Location::RequiresFpuRegister());
5384 } else {
5385 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5386 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005387 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005388 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005389 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005390
Calin Juravle52c48962014-12-16 17:02:57 +00005391 // 64bits value can be atomically written to an address with movsd and an XMM register.
5392 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5393 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5394 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5395 // isolated cases when we need this it isn't worth adding the extra complexity.
5396 locations->AddTemp(Location::RequiresFpuRegister());
5397 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005398 } else {
5399 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5400
5401 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5402 // Temporary registers for the write barrier.
5403 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5404 // Ensure the card is in a byte register.
5405 locations->AddTemp(Location::RegisterLocation(ECX));
5406 }
Calin Juravle52c48962014-12-16 17:02:57 +00005407 }
5408}
5409
5410void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005411 const FieldInfo& field_info,
5412 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005413 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5414
5415 LocationSummary* locations = instruction->GetLocations();
5416 Register base = locations->InAt(0).AsRegister<Register>();
5417 Location value = locations->InAt(1);
5418 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005419 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005420 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005421 bool needs_write_barrier =
5422 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005423
5424 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005425 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005426 }
5427
Mark Mendell81489372015-11-04 11:30:41 -05005428 bool maybe_record_implicit_null_check_done = false;
5429
Calin Juravle52c48962014-12-16 17:02:57 +00005430 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005431 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005432 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005433 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005434 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5435 break;
5436 }
5437
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005438 case DataType::Type::kUint16:
5439 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005440 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005441 __ movw(Address(base, offset),
5442 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005443 } else {
5444 __ movw(Address(base, offset), value.AsRegister<Register>());
5445 }
Calin Juravle52c48962014-12-16 17:02:57 +00005446 break;
5447 }
5448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005449 case DataType::Type::kInt32:
5450 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005451 if (kPoisonHeapReferences && needs_write_barrier) {
5452 // Note that in the case where `value` is a null reference,
5453 // we do not enter this block, as the reference does not
5454 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005455 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005456 Register temp = locations->GetTemp(0).AsRegister<Register>();
5457 __ movl(temp, value.AsRegister<Register>());
5458 __ PoisonHeapReference(temp);
5459 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005460 } else if (value.IsConstant()) {
5461 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5462 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005463 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005464 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005465 __ movl(Address(base, offset), value.AsRegister<Register>());
5466 }
Calin Juravle52c48962014-12-16 17:02:57 +00005467 break;
5468 }
5469
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005470 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005471 if (is_volatile) {
5472 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5473 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5474 __ movd(temp1, value.AsRegisterPairLow<Register>());
5475 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5476 __ punpckldq(temp1, temp2);
5477 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005478 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005479 } else if (value.IsConstant()) {
5480 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5481 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5482 codegen_->MaybeRecordImplicitNullCheck(instruction);
5483 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005484 } else {
5485 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005486 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005487 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5488 }
Mark Mendell81489372015-11-04 11:30:41 -05005489 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005490 break;
5491 }
5492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005493 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005494 if (value.IsConstant()) {
5495 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5496 __ movl(Address(base, offset), Immediate(v));
5497 } else {
5498 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5499 }
Calin Juravle52c48962014-12-16 17:02:57 +00005500 break;
5501 }
5502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005504 if (value.IsConstant()) {
5505 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5506 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5507 codegen_->MaybeRecordImplicitNullCheck(instruction);
5508 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5509 maybe_record_implicit_null_check_done = true;
5510 } else {
5511 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5512 }
Calin Juravle52c48962014-12-16 17:02:57 +00005513 break;
5514 }
5515
Aart Bik66c158e2018-01-31 12:55:04 -08005516 case DataType::Type::kUint32:
5517 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005518 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005519 LOG(FATAL) << "Unreachable type " << field_type;
5520 UNREACHABLE();
5521 }
5522
Mark Mendell81489372015-11-04 11:30:41 -05005523 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005524 codegen_->MaybeRecordImplicitNullCheck(instruction);
5525 }
5526
Roland Levillain4d027112015-07-01 15:41:14 +01005527 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005528 Register temp = locations->GetTemp(0).AsRegister<Register>();
5529 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005530 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005531 }
5532
Calin Juravle52c48962014-12-16 17:02:57 +00005533 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005534 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005535 }
5536}
5537
5538void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5539 HandleFieldGet(instruction, instruction->GetFieldInfo());
5540}
5541
5542void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5543 HandleFieldGet(instruction, instruction->GetFieldInfo());
5544}
5545
5546void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5547 HandleFieldSet(instruction, instruction->GetFieldInfo());
5548}
5549
5550void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005551 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005552}
5553
5554void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5555 HandleFieldSet(instruction, instruction->GetFieldInfo());
5556}
5557
5558void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005559 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005560}
5561
5562void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5563 HandleFieldGet(instruction, instruction->GetFieldInfo());
5564}
5565
5566void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5567 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005568}
5569
Vladimir Marko552a1342017-10-31 10:56:47 +00005570void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5571 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5572}
5573
5574void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5575 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5576 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5577}
5578
Calin Juravlee460d1d2015-09-29 04:52:17 +01005579void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5580 HUnresolvedInstanceFieldGet* instruction) {
5581 FieldAccessCallingConventionX86 calling_convention;
5582 codegen_->CreateUnresolvedFieldLocationSummary(
5583 instruction, instruction->GetFieldType(), calling_convention);
5584}
5585
5586void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5587 HUnresolvedInstanceFieldGet* instruction) {
5588 FieldAccessCallingConventionX86 calling_convention;
5589 codegen_->GenerateUnresolvedFieldAccess(instruction,
5590 instruction->GetFieldType(),
5591 instruction->GetFieldIndex(),
5592 instruction->GetDexPc(),
5593 calling_convention);
5594}
5595
5596void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5597 HUnresolvedInstanceFieldSet* instruction) {
5598 FieldAccessCallingConventionX86 calling_convention;
5599 codegen_->CreateUnresolvedFieldLocationSummary(
5600 instruction, instruction->GetFieldType(), calling_convention);
5601}
5602
5603void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5604 HUnresolvedInstanceFieldSet* instruction) {
5605 FieldAccessCallingConventionX86 calling_convention;
5606 codegen_->GenerateUnresolvedFieldAccess(instruction,
5607 instruction->GetFieldType(),
5608 instruction->GetFieldIndex(),
5609 instruction->GetDexPc(),
5610 calling_convention);
5611}
5612
5613void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5614 HUnresolvedStaticFieldGet* instruction) {
5615 FieldAccessCallingConventionX86 calling_convention;
5616 codegen_->CreateUnresolvedFieldLocationSummary(
5617 instruction, instruction->GetFieldType(), calling_convention);
5618}
5619
5620void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5621 HUnresolvedStaticFieldGet* instruction) {
5622 FieldAccessCallingConventionX86 calling_convention;
5623 codegen_->GenerateUnresolvedFieldAccess(instruction,
5624 instruction->GetFieldType(),
5625 instruction->GetFieldIndex(),
5626 instruction->GetDexPc(),
5627 calling_convention);
5628}
5629
5630void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5631 HUnresolvedStaticFieldSet* instruction) {
5632 FieldAccessCallingConventionX86 calling_convention;
5633 codegen_->CreateUnresolvedFieldLocationSummary(
5634 instruction, instruction->GetFieldType(), calling_convention);
5635}
5636
5637void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5638 HUnresolvedStaticFieldSet* instruction) {
5639 FieldAccessCallingConventionX86 calling_convention;
5640 codegen_->GenerateUnresolvedFieldAccess(instruction,
5641 instruction->GetFieldType(),
5642 instruction->GetFieldIndex(),
5643 instruction->GetDexPc(),
5644 calling_convention);
5645}
5646
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005647void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005648 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5649 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5650 ? Location::RequiresRegister()
5651 : Location::Any();
5652 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653}
5654
Calin Juravle2ae48182016-03-16 14:05:09 +00005655void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5656 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005657 return;
5658 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005659 LocationSummary* locations = instruction->GetLocations();
5660 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005661
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005662 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005663 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005664}
5665
Calin Juravle2ae48182016-03-16 14:05:09 +00005666void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005667 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005668 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005669
5670 LocationSummary* locations = instruction->GetLocations();
5671 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005672
5673 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005674 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005675 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005676 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005677 } else {
5678 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005679 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005680 __ jmp(slow_path->GetEntryLabel());
5681 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005682 }
5683 __ j(kEqual, slow_path->GetEntryLabel());
5684}
5685
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005686void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005687 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005688}
5689
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005690void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005691 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005692 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005693 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005694 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5695 object_array_get_with_read_barrier
5696 ? LocationSummary::kCallOnSlowPath
5697 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005698 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005699 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005700 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005701 locations->SetInAt(0, Location::RequiresRegister());
5702 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005703 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005704 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5705 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 // The output overlaps in case of long: we don't want the low move
5707 // to overwrite the array's location. Likewise, in the case of an
5708 // object array get with read barriers enabled, we do not want the
5709 // move to overwrite the array's location, as we need it to emit
5710 // the read barrier.
5711 locations->SetOut(
5712 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005713 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5714 ? Location::kOutputOverlap
5715 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005716 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005717}
5718
5719void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5720 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005721 Location obj_loc = locations->InAt(0);
5722 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005723 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005724 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005725 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005727 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005728 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005729 case DataType::Type::kBool:
5730 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005731 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005732 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005733 break;
5734 }
5735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005736 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005737 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005738 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005739 break;
5740 }
5741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005743 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005744 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5745 // Branch cases into compressed and uncompressed for each index's type.
5746 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5747 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005748 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005749 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005750 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5751 "Expecting 0=compressed, 1=uncompressed");
5752 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005753 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5754 __ jmp(&done);
5755 __ Bind(&not_compressed);
5756 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5757 __ Bind(&done);
5758 } else {
5759 // Common case for charAt of array of char or when string compression's
5760 // feature is turned off.
5761 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5762 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005763 break;
5764 }
5765
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005766 case DataType::Type::kInt16: {
5767 Register out = out_loc.AsRegister<Register>();
5768 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5769 break;
5770 }
5771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005773 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005774 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005775 break;
5776 }
5777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005779 static_assert(
5780 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5781 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005782 // /* HeapReference<Object> */ out =
5783 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5784 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005785 // Note that a potential implicit null check is handled in this
5786 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5787 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005788 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005789 } else {
5790 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005791 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5792 codegen_->MaybeRecordImplicitNullCheck(instruction);
5793 // If read barriers are enabled, emit read barriers other than
5794 // Baker's using a slow path (and also unpoison the loaded
5795 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005796 if (index.IsConstant()) {
5797 uint32_t offset =
5798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005799 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5800 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005801 codegen_->MaybeGenerateReadBarrierSlow(
5802 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5803 }
5804 }
5805 break;
5806 }
5807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005809 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005810 __ movl(out_loc.AsRegisterPairLow<Register>(),
5811 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5812 codegen_->MaybeRecordImplicitNullCheck(instruction);
5813 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5814 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005815 break;
5816 }
5817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005818 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005819 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005820 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005821 break;
5822 }
5823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005824 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005825 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005826 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005827 break;
5828 }
5829
Aart Bik66c158e2018-01-31 12:55:04 -08005830 case DataType::Type::kUint32:
5831 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005833 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005834 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005835 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005837 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005838 // Potential implicit null checks, in the case of reference or
5839 // long arrays, are handled in the previous switch statement.
5840 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005841 codegen_->MaybeRecordImplicitNullCheck(instruction);
5842 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005843}
5844
5845void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005846 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005848 bool needs_write_barrier =
5849 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005850 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005851
Vladimir Markoca6fff82017-10-03 14:49:14 +01005852 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005853 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005854 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005855
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005856 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005857 // We need the inputs to be different than the output in case of long operation.
5858 // In case of a byte operation, the register allocator does not support multiple
5859 // inputs that die at entry with one in a specific register.
5860 locations->SetInAt(0, Location::RequiresRegister());
5861 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5862 if (is_byte_type) {
5863 // Ensure the value is in a byte register.
5864 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005865 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005866 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005867 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005868 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5869 }
5870 if (needs_write_barrier) {
5871 // Temporary registers for the write barrier.
5872 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5873 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005874 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005875 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005876}
5877
5878void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5879 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 Location array_loc = locations->InAt(0);
5881 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005882 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005883 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005885 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005886 bool needs_write_barrier =
5887 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005888
5889 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005891 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005892 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005893 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005894 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005895 if (value.IsRegister()) {
5896 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005897 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005898 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005899 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005900 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005901 break;
5902 }
5903
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005904 case DataType::Type::kUint16:
5905 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005906 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005907 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005908 if (value.IsRegister()) {
5909 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005910 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005911 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005912 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005913 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005914 break;
5915 }
5916
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005918 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005919 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005920
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005921 if (!value.IsRegister()) {
5922 // Just setting null.
5923 DCHECK(instruction->InputAt(2)->IsNullConstant());
5924 DCHECK(value.IsConstant()) << value;
5925 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005926 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005927 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005928 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005929 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005930 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005931
5932 DCHECK(needs_write_barrier);
5933 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005934 Location temp_loc = locations->GetTemp(0);
5935 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005936
5937 bool can_value_be_null = instruction->GetValueCanBeNull();
5938 NearLabel do_store;
5939 if (can_value_be_null) {
5940 __ testl(register_value, register_value);
5941 __ j(kEqual, &do_store);
5942 }
5943
5944 SlowPathCode* slow_path = nullptr;
5945 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005946 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005947 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005948
5949 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5950 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5951 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005952
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005953 // Note that when Baker read barriers are enabled, the type
5954 // checks are performed without read barriers. This is fine,
5955 // even in the case where a class object is in the from-space
5956 // after the flip, as a comparison involving such a type would
5957 // not produce a false positive; it may of course produce a
5958 // false negative, in which case we would take the ArraySet
5959 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005960
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005961 // /* HeapReference<Class> */ temp = array->klass_
5962 __ movl(temp, Address(array, class_offset));
5963 codegen_->MaybeRecordImplicitNullCheck(instruction);
5964 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005965
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005966 // /* HeapReference<Class> */ temp = temp->component_type_
5967 __ movl(temp, Address(temp, component_offset));
5968 // If heap poisoning is enabled, no need to unpoison `temp`
5969 // nor the object reference in `register_value->klass`, as
5970 // we are comparing two poisoned references.
5971 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005972
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005973 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005974 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005975 __ j(kEqual, &do_put);
5976 // If heap poisoning is enabled, the `temp` reference has
5977 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 __ MaybeUnpoisonHeapReference(temp);
5979
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005980 // If heap poisoning is enabled, no need to unpoison the
5981 // heap reference loaded below, as it is only used for a
5982 // comparison with null.
5983 __ cmpl(Address(temp, super_offset), Immediate(0));
5984 __ j(kNotEqual, slow_path->GetEntryLabel());
5985 __ Bind(&do_put);
5986 } else {
5987 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005988 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005989 }
5990
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005991 Register card = locations->GetTemp(1).AsRegister<Register>();
5992 codegen_->MarkGCCard(
5993 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
5994
5995 if (can_value_be_null) {
5996 DCHECK(do_store.IsLinked());
5997 __ Bind(&do_store);
5998 }
5999
6000 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006001 if (kPoisonHeapReferences) {
6002 __ movl(temp, register_value);
6003 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006004 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006005 }
6006
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006007 __ movl(address, source);
6008
6009 if (can_value_be_null || !needs_type_check) {
6010 codegen_->MaybeRecordImplicitNullCheck(instruction);
6011 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006012
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006013 if (slow_path != nullptr) {
6014 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006015 }
6016
6017 break;
6018 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006021 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006022 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006023 if (value.IsRegister()) {
6024 __ movl(address, value.AsRegister<Register>());
6025 } else {
6026 DCHECK(value.IsConstant()) << value;
6027 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6028 __ movl(address, Immediate(v));
6029 }
6030 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006031 break;
6032 }
6033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006035 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006036 if (value.IsRegisterPair()) {
6037 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6038 value.AsRegisterPairLow<Register>());
6039 codegen_->MaybeRecordImplicitNullCheck(instruction);
6040 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6041 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006042 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006043 DCHECK(value.IsConstant());
6044 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6045 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6046 Immediate(Low32Bits(val)));
6047 codegen_->MaybeRecordImplicitNullCheck(instruction);
6048 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6049 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050 }
6051 break;
6052 }
6053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006054 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006055 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006056 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006057 if (value.IsFpuRegister()) {
6058 __ movss(address, value.AsFpuRegister<XmmRegister>());
6059 } else {
6060 DCHECK(value.IsConstant());
6061 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6062 __ movl(address, Immediate(v));
6063 }
6064 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006065 break;
6066 }
6067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006068 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006069 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006070 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006071 if (value.IsFpuRegister()) {
6072 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6073 } else {
6074 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006075 Address address_hi =
6076 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006077 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6078 __ movl(address, Immediate(Low32Bits(v)));
6079 codegen_->MaybeRecordImplicitNullCheck(instruction);
6080 __ movl(address_hi, Immediate(High32Bits(v)));
6081 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006082 break;
6083 }
6084
Aart Bik66c158e2018-01-31 12:55:04 -08006085 case DataType::Type::kUint32:
6086 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006087 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006088 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006089 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006090 }
6091}
6092
6093void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006094 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006095 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006096 if (!instruction->IsEmittedAtUseSite()) {
6097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099}
6100
6101void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006102 if (instruction->IsEmittedAtUseSite()) {
6103 return;
6104 }
6105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006106 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006107 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006108 Register obj = locations->InAt(0).AsRegister<Register>();
6109 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006110 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006111 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006112 // Mask out most significant bit in case the array is String's array of char.
6113 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006114 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006115 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006116}
6117
6118void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006119 RegisterSet caller_saves = RegisterSet::Empty();
6120 InvokeRuntimeCallingConvention calling_convention;
6121 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6122 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6123 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006124 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006125 HInstruction* length = instruction->InputAt(1);
6126 if (!length->IsEmittedAtUseSite()) {
6127 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6128 }
jessicahandojo4877b792016-09-08 19:49:13 -07006129 // Need register to see array's length.
6130 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6131 locations->AddTemp(Location::RequiresRegister());
6132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006133}
6134
6135void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006136 const bool is_string_compressed_char_at =
6137 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006138 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006139 Location index_loc = locations->InAt(0);
6140 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006141 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006142 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006143
Mark Mendell99dbd682015-04-22 16:18:52 -04006144 if (length_loc.IsConstant()) {
6145 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6146 if (index_loc.IsConstant()) {
6147 // BCE will remove the bounds check if we are guarenteed to pass.
6148 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6149 if (index < 0 || index >= length) {
6150 codegen_->AddSlowPath(slow_path);
6151 __ jmp(slow_path->GetEntryLabel());
6152 } else {
6153 // Some optimization after BCE may have generated this, and we should not
6154 // generate a bounds check if it is a valid range.
6155 }
6156 return;
6157 }
6158
6159 // We have to reverse the jump condition because the length is the constant.
6160 Register index_reg = index_loc.AsRegister<Register>();
6161 __ cmpl(index_reg, Immediate(length));
6162 codegen_->AddSlowPath(slow_path);
6163 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006164 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006165 HInstruction* array_length = instruction->InputAt(1);
6166 if (array_length->IsEmittedAtUseSite()) {
6167 // Address the length field in the array.
6168 DCHECK(array_length->IsArrayLength());
6169 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6170 Location array_loc = array_length->GetLocations()->InAt(0);
6171 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006172 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006173 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6174 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006175 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6176 __ movl(length_reg, array_len);
6177 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006178 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006179 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006180 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006181 // Checking bounds for general case:
6182 // Array of char or string's array with feature compression off.
6183 if (index_loc.IsConstant()) {
6184 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6185 __ cmpl(array_len, Immediate(value));
6186 } else {
6187 __ cmpl(array_len, index_loc.AsRegister<Register>());
6188 }
6189 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006190 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006191 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006192 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006193 }
6194 codegen_->AddSlowPath(slow_path);
6195 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006196 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006197}
6198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006199void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006200 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006201}
6202
6203void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006204 if (instruction->GetNext()->IsSuspendCheck() &&
6205 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6206 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6207 // The back edge will generate the suspend check.
6208 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6209 }
6210
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006211 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6212}
6213
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006214void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006215 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6216 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006217 // In suspend check slow path, usually there are no caller-save registers at all.
6218 // If SIMD instructions are present, however, we force spilling all live SIMD
6219 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006220 locations->SetCustomSlowPathCallerSaves(
6221 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006222}
6223
6224void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006225 HBasicBlock* block = instruction->GetBlock();
6226 if (block->GetLoopInformation() != nullptr) {
6227 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6228 // The back edge will generate the suspend check.
6229 return;
6230 }
6231 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6232 // The goto will generate the suspend check.
6233 return;
6234 }
6235 GenerateSuspendCheck(instruction, nullptr);
6236}
6237
6238void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6239 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006240 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006241 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6242 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006243 slow_path =
6244 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006245 instruction->SetSlowPath(slow_path);
6246 codegen_->AddSlowPath(slow_path);
6247 if (successor != nullptr) {
6248 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006249 }
6250 } else {
6251 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6252 }
6253
Andreas Gampe542451c2016-07-26 09:02:02 -07006254 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006255 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006256 if (successor == nullptr) {
6257 __ j(kNotEqual, slow_path->GetEntryLabel());
6258 __ Bind(slow_path->GetReturnLabel());
6259 } else {
6260 __ j(kEqual, codegen_->GetLabelOf(successor));
6261 __ jmp(slow_path->GetEntryLabel());
6262 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006263}
6264
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006265X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6266 return codegen_->GetAssembler();
6267}
6268
Aart Bikcfe50bb2017-12-12 14:54:12 -08006269void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006270 ScratchRegisterScope ensure_scratch(
6271 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6272 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6273 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006274
Aart Bikcfe50bb2017-12-12 14:54:12 -08006275 // Now that temp register is available (possibly spilled), move blocks of memory.
6276 for (int i = 0; i < number_of_words; i++) {
6277 __ movl(temp_reg, Address(ESP, src + stack_offset));
6278 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6279 stack_offset += kX86WordSize;
6280 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006281}
6282
6283void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006284 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006285 Location source = move->GetSource();
6286 Location destination = move->GetDestination();
6287
6288 if (source.IsRegister()) {
6289 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006290 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006291 } else if (destination.IsFpuRegister()) {
6292 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006293 } else {
6294 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006295 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006296 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006297 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006299 // Create stack space for 2 elements.
6300 __ subl(ESP, Immediate(2 * elem_size));
6301 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6302 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6303 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6304 // And remove the temporary stack space we allocated.
6305 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006306 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006307 if (destination.IsRegister()) {
6308 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6309 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006310 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006311 } else if (destination.IsRegisterPair()) {
6312 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6313 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6314 __ psrlq(src_reg, Immediate(32));
6315 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006316 } else if (destination.IsStackSlot()) {
6317 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006318 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006319 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006320 } else {
6321 DCHECK(destination.IsSIMDStackSlot());
6322 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006323 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006324 } else if (source.IsStackSlot()) {
6325 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006326 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006327 } else if (destination.IsFpuRegister()) {
6328 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006329 } else {
6330 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006331 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006332 }
6333 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006334 if (destination.IsRegisterPair()) {
6335 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6336 __ movl(destination.AsRegisterPairHigh<Register>(),
6337 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6338 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006339 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6340 } else {
6341 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006342 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006343 }
Aart Bik5576f372017-03-23 16:17:37 -07006344 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006345 if (destination.IsFpuRegister()) {
6346 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6347 } else {
6348 DCHECK(destination.IsSIMDStackSlot());
6349 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6350 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006351 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006352 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006353 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006354 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006355 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006356 if (value == 0) {
6357 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6358 } else {
6359 __ movl(destination.AsRegister<Register>(), Immediate(value));
6360 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006361 } else {
6362 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006363 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006364 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006365 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006366 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006367 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006368 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006369 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006370 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6371 if (value == 0) {
6372 // Easy handling of 0.0.
6373 __ xorps(dest, dest);
6374 } else {
6375 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006376 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6377 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6378 __ movl(temp, Immediate(value));
6379 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006380 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006381 } else {
6382 DCHECK(destination.IsStackSlot()) << destination;
6383 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6384 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006385 } else if (constant->IsLongConstant()) {
6386 int64_t value = constant->AsLongConstant()->GetValue();
6387 int32_t low_value = Low32Bits(value);
6388 int32_t high_value = High32Bits(value);
6389 Immediate low(low_value);
6390 Immediate high(high_value);
6391 if (destination.IsDoubleStackSlot()) {
6392 __ movl(Address(ESP, destination.GetStackIndex()), low);
6393 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6394 } else {
6395 __ movl(destination.AsRegisterPairLow<Register>(), low);
6396 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6397 }
6398 } else {
6399 DCHECK(constant->IsDoubleConstant());
6400 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006401 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006402 int32_t low_value = Low32Bits(value);
6403 int32_t high_value = High32Bits(value);
6404 Immediate low(low_value);
6405 Immediate high(high_value);
6406 if (destination.IsFpuRegister()) {
6407 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6408 if (value == 0) {
6409 // Easy handling of 0.0.
6410 __ xorpd(dest, dest);
6411 } else {
6412 __ pushl(high);
6413 __ pushl(low);
6414 __ movsd(dest, Address(ESP, 0));
6415 __ addl(ESP, Immediate(8));
6416 }
6417 } else {
6418 DCHECK(destination.IsDoubleStackSlot()) << destination;
6419 __ movl(Address(ESP, destination.GetStackIndex()), low);
6420 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6421 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006422 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006423 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006424 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006425 }
6426}
6427
Mark Mendella5c19ce2015-04-01 12:51:05 -04006428void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006429 Register suggested_scratch = reg == EAX ? EBX : EAX;
6430 ScratchRegisterScope ensure_scratch(
6431 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6432
6433 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6434 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6435 __ movl(Address(ESP, mem + stack_offset), reg);
6436 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006437}
6438
Mark Mendell7c8d0092015-01-26 11:21:33 -05006439void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006440 ScratchRegisterScope ensure_scratch(
6441 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6442
6443 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6444 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6445 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6446 __ movss(Address(ESP, mem + stack_offset), reg);
6447 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006448}
6449
Aart Bikcfe50bb2017-12-12 14:54:12 -08006450void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6451 size_t extra_slot = 4 * kX86WordSize;
6452 __ subl(ESP, Immediate(extra_slot));
6453 __ movups(Address(ESP, 0), XmmRegister(reg));
6454 ExchangeMemory(0, mem + extra_slot, 4);
6455 __ movups(XmmRegister(reg), Address(ESP, 0));
6456 __ addl(ESP, Immediate(extra_slot));
6457}
6458
6459void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006460 ScratchRegisterScope ensure_scratch1(
6461 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006462
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006463 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6464 ScratchRegisterScope ensure_scratch2(
6465 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006466
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006467 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6468 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006469
6470 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6471 for (int i = 0; i < number_of_words; i++) {
6472 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6473 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6474 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6475 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6476 stack_offset += kX86WordSize;
6477 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006478}
6479
6480void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006481 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006482 Location source = move->GetSource();
6483 Location destination = move->GetDestination();
6484
6485 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006486 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6487 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6488 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6489 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6490 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006491 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006492 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006493 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006494 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006495 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006496 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006497 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6498 // Use XOR Swap algorithm to avoid a temporary.
6499 DCHECK_NE(source.reg(), destination.reg());
6500 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6501 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6502 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6503 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6504 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6505 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6506 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006507 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6508 // Take advantage of the 16 bytes in the XMM register.
6509 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6510 Address stack(ESP, destination.GetStackIndex());
6511 // Load the double into the high doubleword.
6512 __ movhpd(reg, stack);
6513
6514 // Store the low double into the destination.
6515 __ movsd(stack, reg);
6516
6517 // Move the high double to the low double.
6518 __ psrldq(reg, Immediate(8));
6519 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6520 // Take advantage of the 16 bytes in the XMM register.
6521 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6522 Address stack(ESP, source.GetStackIndex());
6523 // Load the double into the high doubleword.
6524 __ movhpd(reg, stack);
6525
6526 // Store the low double into the destination.
6527 __ movsd(stack, reg);
6528
6529 // Move the high double to the low double.
6530 __ psrldq(reg, Immediate(8));
6531 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006532 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6533 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6534 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6535 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6536 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6537 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6538 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006539 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006540 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006541 }
6542}
6543
6544void ParallelMoveResolverX86::SpillScratch(int reg) {
6545 __ pushl(static_cast<Register>(reg));
6546}
6547
6548void ParallelMoveResolverX86::RestoreScratch(int reg) {
6549 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006550}
6551
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006552HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6553 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006554 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006555 case HLoadClass::LoadKind::kInvalid:
6556 LOG(FATAL) << "UNREACHABLE";
6557 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006558 case HLoadClass::LoadKind::kReferrersClass:
6559 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006560 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006561 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006562 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006563 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006564 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006565 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006566 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006567 DCHECK(Runtime::Current()->UseJitCompilation());
6568 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006569 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006570 break;
6571 }
6572 return desired_class_load_kind;
6573}
6574
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006575void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006576 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006577 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006578 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006579 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006580 cls,
6581 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006582 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006583 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006584 return;
6585 }
Vladimir Marko41559982017-01-06 14:04:23 +00006586 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006587
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006588 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6589 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006590 ? LocationSummary::kCallOnSlowPath
6591 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006592 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006593 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006594 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006595 }
6596
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006597 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006598 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006599 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006600 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006601 locations->SetInAt(0, Location::RequiresRegister());
6602 }
6603 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006604 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6605 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6606 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006607 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006608 } else {
6609 // For non-Baker read barrier we have a temp-clobbering call.
6610 }
6611 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006612}
6613
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006614Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006615 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006616 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006617 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006618 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006619 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006620 PatchInfo<Label>* info = &jit_class_patches_.back();
6621 return &info->label;
6622}
6623
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006624// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6625// move.
6626void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006627 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006628 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006629 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006630 return;
6631 }
Vladimir Marko41559982017-01-06 14:04:23 +00006632 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006633
Vladimir Marko41559982017-01-06 14:04:23 +00006634 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006635 Location out_loc = locations->Out();
6636 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006637
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006638 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006639 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6640 ? kWithoutReadBarrier
6641 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006642 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006643 case HLoadClass::LoadKind::kReferrersClass: {
6644 DCHECK(!cls->CanCallRuntime());
6645 DCHECK(!cls->MustGenerateClinitCheck());
6646 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6647 Register current_method = locations->InAt(0).AsRegister<Register>();
6648 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006649 cls,
6650 out_loc,
6651 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006652 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006653 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006654 break;
6655 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006656 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006657 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6658 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006659 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006660 Register method_address = locations->InAt(0).AsRegister<Register>();
6661 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006662 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006663 break;
6664 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006665 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006666 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6667 Register method_address = locations->InAt(0).AsRegister<Register>();
6668 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006669 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6670 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006671 break;
6672 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006673 case HLoadClass::LoadKind::kBssEntry: {
6674 Register method_address = locations->InAt(0).AsRegister<Register>();
6675 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6676 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6677 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006678 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006679 generate_null_check = true;
6680 break;
6681 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006682 case HLoadClass::LoadKind::kJitBootImageAddress: {
6683 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6684 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6685 DCHECK_NE(address, 0u);
6686 __ movl(out, Immediate(address));
6687 break;
6688 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006689 case HLoadClass::LoadKind::kJitTableAddress: {
6690 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6691 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006692 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006693 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006694 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006695 break;
6696 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006697 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006698 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006699 LOG(FATAL) << "UNREACHABLE";
6700 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006701 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006702
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006703 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6704 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006705 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006706 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006707
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006708 if (generate_null_check) {
6709 __ testl(out, out);
6710 __ j(kEqual, slow_path->GetEntryLabel());
6711 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006712
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006713 if (cls->MustGenerateClinitCheck()) {
6714 GenerateClassInitializationCheck(slow_path, out);
6715 } else {
6716 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006717 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006718 }
6719}
6720
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006721void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6722 InvokeRuntimeCallingConvention calling_convention;
6723 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6724 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6725}
6726
6727void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6728 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6729}
6730
Orion Hodson18259d72018-04-12 11:18:23 +01006731void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6732 InvokeRuntimeCallingConvention calling_convention;
6733 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6734 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6735}
6736
6737void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6738 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6739}
6740
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006741void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6742 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006743 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006744 locations->SetInAt(0, Location::RequiresRegister());
6745 if (check->HasUses()) {
6746 locations->SetOut(Location::SameAsFirstInput());
6747 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006748 // Rely on the type initialization to save everything we need.
6749 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006750}
6751
6752void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006753 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006754 SlowPathCode* slow_path =
6755 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006756 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006757 GenerateClassInitializationCheck(slow_path,
6758 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006759}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006760
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006761void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006762 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006763 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6764 const size_t status_byte_offset =
6765 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006766 constexpr uint32_t shifted_visibly_initialized_value =
6767 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006768
Vladimir Markobf121912019-06-04 13:49:05 +01006769 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006770 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006771 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006772}
6773
Vladimir Marko175e7862018-03-27 09:03:13 +00006774void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6775 Register temp) {
6776 uint32_t path_to_root = check->GetBitstringPathToRoot();
6777 uint32_t mask = check->GetBitstringMask();
6778 DCHECK(IsPowerOfTwo(mask + 1));
6779 size_t mask_bits = WhichPowerOf2(mask + 1);
6780
6781 if (mask_bits == 16u) {
6782 // Compare the bitstring in memory.
6783 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6784 } else {
6785 // /* uint32_t */ temp = temp->status_
6786 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6787 // Compare the bitstring bits using SUB.
6788 __ subl(temp, Immediate(path_to_root));
6789 // Shift out bits that do not contribute to the comparison.
6790 __ shll(temp, Immediate(32u - mask_bits));
6791 }
6792}
6793
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006794HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6795 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006796 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006797 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006798 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006799 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006800 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006801 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006802 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006803 case HLoadString::LoadKind::kJitTableAddress:
6804 DCHECK(Runtime::Current()->UseJitCompilation());
6805 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006806 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006807 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006808 }
6809 return desired_string_load_kind;
6810}
6811
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006812void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006813 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006814 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006815 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006816 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006817 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006818 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819 locations->SetInAt(0, Location::RequiresRegister());
6820 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006821 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006822 locations->SetOut(Location::RegisterLocation(EAX));
6823 } else {
6824 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006825 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6826 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006827 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006828 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006829 } else {
6830 // For non-Baker read barrier we have a temp-clobbering call.
6831 }
6832 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006833 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006834}
6835
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006836Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006837 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006838 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006839 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006840 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006841 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006842 PatchInfo<Label>* info = &jit_string_patches_.back();
6843 return &info->label;
6844}
6845
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006846// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6847// move.
6848void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006849 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006850 Location out_loc = locations->Out();
6851 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006852
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006853 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006854 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006855 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6856 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006857 Register method_address = locations->InAt(0).AsRegister<Register>();
6858 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006859 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006860 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006861 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006862 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006863 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6864 Register method_address = locations->InAt(0).AsRegister<Register>();
6865 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006866 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6867 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006868 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006869 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006870 case HLoadString::LoadKind::kBssEntry: {
6871 Register method_address = locations->InAt(0).AsRegister<Register>();
6872 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6873 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006874 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006875 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006876 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006877 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006878 codegen_->AddSlowPath(slow_path);
6879 __ testl(out, out);
6880 __ j(kEqual, slow_path->GetEntryLabel());
6881 __ Bind(slow_path->GetExitLabel());
6882 return;
6883 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006884 case HLoadString::LoadKind::kJitBootImageAddress: {
6885 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6886 DCHECK_NE(address, 0u);
6887 __ movl(out, Immediate(address));
6888 return;
6889 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006890 case HLoadString::LoadKind::kJitTableAddress: {
6891 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6892 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006893 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006894 // /* GcRoot<mirror::String> */ out = *address
6895 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6896 return;
6897 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006898 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006899 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006900 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006901
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006902 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006903 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006904 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006905 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006906 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6907 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006908}
6909
David Brazdilcb1c0552015-08-04 16:22:25 +01006910static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006911 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006912}
6913
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006914void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6915 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006916 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006917 locations->SetOut(Location::RequiresRegister());
6918}
6919
6920void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006921 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6922}
6923
6924void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006925 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006926}
6927
6928void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6929 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006930}
6931
6932void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006933 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6934 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006935 InvokeRuntimeCallingConvention calling_convention;
6936 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6937}
6938
6939void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006940 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006941 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006942}
6943
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006944// Temp is used for read barrier.
6945static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6946 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006947 !kUseBakerReadBarrier &&
6948 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006949 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006950 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6951 return 1;
6952 }
6953 return 0;
6954}
6955
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006956// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6957// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006958// The other checks have one temp for loading the object's class.
6959static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006960 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006961 return 2;
6962 }
6963 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006964}
6965
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006966void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006967 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006968 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006969 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006970 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006971 case TypeCheckKind::kExactCheck:
6972 case TypeCheckKind::kAbstractClassCheck:
6973 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006974 case TypeCheckKind::kArrayObjectCheck: {
6975 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6976 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6977 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006978 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006979 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006980 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006981 case TypeCheckKind::kUnresolvedCheck:
6982 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006983 call_kind = LocationSummary::kCallOnSlowPath;
6984 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006985 case TypeCheckKind::kBitstringCheck:
6986 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006987 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006988
Vladimir Markoca6fff82017-10-03 14:49:14 +01006989 LocationSummary* locations =
6990 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006991 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006992 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006993 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006994 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006995 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6996 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6997 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6998 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6999 } else {
7000 locations->SetInAt(1, Location::Any());
7001 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007002 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7003 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007004 // When read barriers are enabled, we need a temporary register for some cases.
7005 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007006}
7007
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007008void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007009 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007010 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007011 Location obj_loc = locations->InAt(0);
7012 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007013 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007014 Location out_loc = locations->Out();
7015 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007016 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7017 DCHECK_LE(num_temps, 1u);
7018 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007019 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007020 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7021 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7022 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007023 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007024 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007025
7026 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007027 // Avoid null check if we know obj is not null.
7028 if (instruction->MustDoNullCheck()) {
7029 __ testl(obj, obj);
7030 __ j(kEqual, &zero);
7031 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007032
Roland Levillain7c1559a2015-12-15 10:55:36 +00007033 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007034 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007035 ReadBarrierOption read_barrier_option =
7036 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007037 // /* HeapReference<Class> */ out = obj->klass_
7038 GenerateReferenceLoadTwoRegisters(instruction,
7039 out_loc,
7040 obj_loc,
7041 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007042 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007043 if (cls.IsRegister()) {
7044 __ cmpl(out, cls.AsRegister<Register>());
7045 } else {
7046 DCHECK(cls.IsStackSlot()) << cls;
7047 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7048 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007049
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007050 // Classes must be equal for the instanceof to succeed.
7051 __ j(kNotEqual, &zero);
7052 __ movl(out, Immediate(1));
7053 __ jmp(&done);
7054 break;
7055 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007056
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007057 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007058 ReadBarrierOption read_barrier_option =
7059 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007060 // /* HeapReference<Class> */ out = obj->klass_
7061 GenerateReferenceLoadTwoRegisters(instruction,
7062 out_loc,
7063 obj_loc,
7064 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007065 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007066 // If the class is abstract, we eagerly fetch the super class of the
7067 // object to avoid doing a comparison we know will fail.
7068 NearLabel loop;
7069 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007070 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007071 GenerateReferenceLoadOneRegister(instruction,
7072 out_loc,
7073 super_offset,
7074 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007075 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007076 __ testl(out, out);
7077 // If `out` is null, we use it for the result, and jump to `done`.
7078 __ j(kEqual, &done);
7079 if (cls.IsRegister()) {
7080 __ cmpl(out, cls.AsRegister<Register>());
7081 } else {
7082 DCHECK(cls.IsStackSlot()) << cls;
7083 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7084 }
7085 __ j(kNotEqual, &loop);
7086 __ movl(out, Immediate(1));
7087 if (zero.IsLinked()) {
7088 __ jmp(&done);
7089 }
7090 break;
7091 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007092
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007093 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007094 ReadBarrierOption read_barrier_option =
7095 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007096 // /* HeapReference<Class> */ out = obj->klass_
7097 GenerateReferenceLoadTwoRegisters(instruction,
7098 out_loc,
7099 obj_loc,
7100 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007101 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007102 // Walk over the class hierarchy to find a match.
7103 NearLabel loop, success;
7104 __ Bind(&loop);
7105 if (cls.IsRegister()) {
7106 __ cmpl(out, cls.AsRegister<Register>());
7107 } else {
7108 DCHECK(cls.IsStackSlot()) << cls;
7109 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7110 }
7111 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007112 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007113 GenerateReferenceLoadOneRegister(instruction,
7114 out_loc,
7115 super_offset,
7116 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007117 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007118 __ testl(out, out);
7119 __ j(kNotEqual, &loop);
7120 // If `out` is null, we use it for the result, and jump to `done`.
7121 __ jmp(&done);
7122 __ Bind(&success);
7123 __ movl(out, Immediate(1));
7124 if (zero.IsLinked()) {
7125 __ jmp(&done);
7126 }
7127 break;
7128 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007129
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007131 ReadBarrierOption read_barrier_option =
7132 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007133 // /* HeapReference<Class> */ out = obj->klass_
7134 GenerateReferenceLoadTwoRegisters(instruction,
7135 out_loc,
7136 obj_loc,
7137 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007138 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007139 // Do an exact check.
7140 NearLabel exact_check;
7141 if (cls.IsRegister()) {
7142 __ cmpl(out, cls.AsRegister<Register>());
7143 } else {
7144 DCHECK(cls.IsStackSlot()) << cls;
7145 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7146 }
7147 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007148 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007149 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007150 GenerateReferenceLoadOneRegister(instruction,
7151 out_loc,
7152 component_offset,
7153 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007154 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007155 __ testl(out, out);
7156 // If `out` is null, we use it for the result, and jump to `done`.
7157 __ j(kEqual, &done);
7158 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7159 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007160 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007161 __ movl(out, Immediate(1));
7162 __ jmp(&done);
7163 break;
7164 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007166 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007167 // No read barrier since the slow path will retry upon failure.
7168 // /* HeapReference<Class> */ out = obj->klass_
7169 GenerateReferenceLoadTwoRegisters(instruction,
7170 out_loc,
7171 obj_loc,
7172 class_offset,
7173 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007174 if (cls.IsRegister()) {
7175 __ cmpl(out, cls.AsRegister<Register>());
7176 } else {
7177 DCHECK(cls.IsStackSlot()) << cls;
7178 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7179 }
7180 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007181 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007182 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007183 codegen_->AddSlowPath(slow_path);
7184 __ j(kNotEqual, slow_path->GetEntryLabel());
7185 __ movl(out, Immediate(1));
7186 if (zero.IsLinked()) {
7187 __ jmp(&done);
7188 }
7189 break;
7190 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007191
Calin Juravle98893e12015-10-02 21:05:03 +01007192 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007193 case TypeCheckKind::kInterfaceCheck: {
7194 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007195 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007196 // cases.
7197 //
7198 // We cannot directly call the InstanceofNonTrivial runtime
7199 // entry point without resorting to a type checking slow path
7200 // here (i.e. by calling InvokeRuntime directly), as it would
7201 // require to assign fixed registers for the inputs of this
7202 // HInstanceOf instruction (following the runtime calling
7203 // convention), which might be cluttered by the potential first
7204 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007205 //
7206 // TODO: Introduce a new runtime entry point taking the object
7207 // to test (instead of its class) as argument, and let it deal
7208 // with the read barrier issues. This will let us refactor this
7209 // case of the `switch` code as it was previously (with a direct
7210 // call to the runtime not using a type checking slow path).
7211 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007212 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007213 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007214 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007215 codegen_->AddSlowPath(slow_path);
7216 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007217 if (zero.IsLinked()) {
7218 __ jmp(&done);
7219 }
7220 break;
7221 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007222
7223 case TypeCheckKind::kBitstringCheck: {
7224 // /* HeapReference<Class> */ temp = obj->klass_
7225 GenerateReferenceLoadTwoRegisters(instruction,
7226 out_loc,
7227 obj_loc,
7228 class_offset,
7229 kWithoutReadBarrier);
7230
7231 GenerateBitstringTypeCheckCompare(instruction, out);
7232 __ j(kNotEqual, &zero);
7233 __ movl(out, Immediate(1));
7234 __ jmp(&done);
7235 break;
7236 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007237 }
7238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007239 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007240 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007241 __ xorl(out, out);
7242 }
7243
7244 if (done.IsLinked()) {
7245 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007246 }
7247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007248 if (slow_path != nullptr) {
7249 __ Bind(slow_path->GetExitLabel());
7250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007251}
7252
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007253void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007254 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007255 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007256 LocationSummary* locations =
7257 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007258 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007259 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7260 // Require a register for the interface check since there is a loop that compares the class to
7261 // a memory address.
7262 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007263 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7264 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7265 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7266 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007267 } else {
7268 locations->SetInAt(1, Location::Any());
7269 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007270 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007271 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7272}
7273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007274void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007275 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007276 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007277 Location obj_loc = locations->InAt(0);
7278 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007279 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007280 Location temp_loc = locations->GetTemp(0);
7281 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007282 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7283 DCHECK_GE(num_temps, 1u);
7284 DCHECK_LE(num_temps, 2u);
7285 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7286 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7287 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7288 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7289 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7290 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7291 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7292 const uint32_t object_array_data_offset =
7293 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007294
Vladimir Marko87584542017-12-12 17:47:52 +00007295 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007296 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007297 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7298 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007299 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300
Roland Levillain0d5a2812015-11-13 10:07:31 +00007301 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007302 // Avoid null check if we know obj is not null.
7303 if (instruction->MustDoNullCheck()) {
7304 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007305 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007306 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007307
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007309 case TypeCheckKind::kExactCheck:
7310 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007311 // /* HeapReference<Class> */ temp = obj->klass_
7312 GenerateReferenceLoadTwoRegisters(instruction,
7313 temp_loc,
7314 obj_loc,
7315 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007316 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007317
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007318 if (cls.IsRegister()) {
7319 __ cmpl(temp, cls.AsRegister<Register>());
7320 } else {
7321 DCHECK(cls.IsStackSlot()) << cls;
7322 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7323 }
7324 // Jump to slow path for throwing the exception or doing a
7325 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007326 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007327 break;
7328 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007330 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007331 // /* HeapReference<Class> */ temp = obj->klass_
7332 GenerateReferenceLoadTwoRegisters(instruction,
7333 temp_loc,
7334 obj_loc,
7335 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007336 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007338 // If the class is abstract, we eagerly fetch the super class of the
7339 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007340 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007341 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007343 GenerateReferenceLoadOneRegister(instruction,
7344 temp_loc,
7345 super_offset,
7346 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007347 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007349 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7350 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007351 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007352 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007353
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007354 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007355 if (cls.IsRegister()) {
7356 __ cmpl(temp, cls.AsRegister<Register>());
7357 } else {
7358 DCHECK(cls.IsStackSlot()) << cls;
7359 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7360 }
7361 __ j(kNotEqual, &loop);
7362 break;
7363 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007365 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007366 // /* HeapReference<Class> */ temp = obj->klass_
7367 GenerateReferenceLoadTwoRegisters(instruction,
7368 temp_loc,
7369 obj_loc,
7370 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007371 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007372
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007373 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007374 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007375 __ Bind(&loop);
7376 if (cls.IsRegister()) {
7377 __ cmpl(temp, cls.AsRegister<Register>());
7378 } else {
7379 DCHECK(cls.IsStackSlot()) << cls;
7380 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7381 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007382 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007383
Roland Levillain0d5a2812015-11-13 10:07:31 +00007384 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007385 GenerateReferenceLoadOneRegister(instruction,
7386 temp_loc,
7387 super_offset,
7388 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007389 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007390
7391 // If the class reference currently in `temp` is not null, jump
7392 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007393 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007394 __ j(kNotZero, &loop);
7395 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007396 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 break;
7398 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007399
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007400 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007401 // /* HeapReference<Class> */ temp = obj->klass_
7402 GenerateReferenceLoadTwoRegisters(instruction,
7403 temp_loc,
7404 obj_loc,
7405 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007406 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007407
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007408 // Do an exact check.
7409 if (cls.IsRegister()) {
7410 __ cmpl(temp, cls.AsRegister<Register>());
7411 } else {
7412 DCHECK(cls.IsStackSlot()) << cls;
7413 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7414 }
7415 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007416
7417 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007418 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007419 GenerateReferenceLoadOneRegister(instruction,
7420 temp_loc,
7421 component_offset,
7422 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007423 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007424
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007425 // If the component type is null (i.e. the object not an array), jump to the slow path to
7426 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007427 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007428 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007429
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007430 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007431 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007432 break;
7433 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007434
Calin Juravle98893e12015-10-02 21:05:03 +01007435 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007436 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007437 // We cannot directly call the CheckCast runtime entry point
7438 // without resorting to a type checking slow path here (i.e. by
7439 // calling InvokeRuntime directly), as it would require to
7440 // assign fixed registers for the inputs of this HInstanceOf
7441 // instruction (following the runtime calling convention), which
7442 // might be cluttered by the potential first read barrier
7443 // emission at the beginning of this method.
7444 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007445 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007446
7447 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007448 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7449 // We can not get false positives by doing this.
7450 // /* HeapReference<Class> */ temp = obj->klass_
7451 GenerateReferenceLoadTwoRegisters(instruction,
7452 temp_loc,
7453 obj_loc,
7454 class_offset,
7455 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007456
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007457 // /* HeapReference<Class> */ temp = temp->iftable_
7458 GenerateReferenceLoadTwoRegisters(instruction,
7459 temp_loc,
7460 temp_loc,
7461 iftable_offset,
7462 kWithoutReadBarrier);
7463 // Iftable is never null.
7464 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7465 // Maybe poison the `cls` for direct comparison with memory.
7466 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7467 // Loop through the iftable and check if any class matches.
7468 NearLabel start_loop;
7469 __ Bind(&start_loop);
7470 // Need to subtract first to handle the empty array case.
7471 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7472 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7473 // Go to next interface if the classes do not match.
7474 __ cmpl(cls.AsRegister<Register>(),
7475 CodeGeneratorX86::ArrayAddress(temp,
7476 maybe_temp2_loc,
7477 TIMES_4,
7478 object_array_data_offset));
7479 __ j(kNotEqual, &start_loop);
7480 // If `cls` was poisoned above, unpoison it.
7481 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007482 break;
7483 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007484
7485 case TypeCheckKind::kBitstringCheck: {
7486 // /* HeapReference<Class> */ temp = obj->klass_
7487 GenerateReferenceLoadTwoRegisters(instruction,
7488 temp_loc,
7489 obj_loc,
7490 class_offset,
7491 kWithoutReadBarrier);
7492
7493 GenerateBitstringTypeCheckCompare(instruction, temp);
7494 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7495 break;
7496 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007497 }
7498 __ Bind(&done);
7499
Roland Levillain0d5a2812015-11-13 10:07:31 +00007500 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007501}
7502
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007503void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007504 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7505 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007506 InvokeRuntimeCallingConvention calling_convention;
7507 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7508}
7509
7510void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007511 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7512 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007513 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007514 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007515 if (instruction->IsEnter()) {
7516 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7517 } else {
7518 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7519 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007520}
7521
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307522void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7523 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7524 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7525 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7526 locations->SetInAt(0, Location::RequiresRegister());
7527 locations->SetInAt(1, Location::RequiresRegister());
7528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7529}
7530
7531void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7532 LocationSummary* locations = instruction->GetLocations();
7533 Location first = locations->InAt(0);
7534 Location second = locations->InAt(1);
7535 Location dest = locations->Out();
7536 if (instruction->GetResultType() == DataType::Type::kInt32) {
7537 __ andn(dest.AsRegister<Register>(),
7538 first.AsRegister<Register>(),
7539 second.AsRegister<Register>());
7540 } else {
7541 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7542 __ andn(dest.AsRegisterPairLow<Register>(),
7543 first.AsRegisterPairLow<Register>(),
7544 second.AsRegisterPairLow<Register>());
7545 __ andn(dest.AsRegisterPairHigh<Register>(),
7546 first.AsRegisterPairHigh<Register>(),
7547 second.AsRegisterPairHigh<Register>());
7548 }
7549}
7550
7551void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7552 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7553 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7554 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7555 locations->SetInAt(0, Location::RequiresRegister());
7556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7557}
7558
7559void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7560 HX86MaskOrResetLeastSetBit* instruction) {
7561 LocationSummary* locations = instruction->GetLocations();
7562 Location src = locations->InAt(0);
7563 Location dest = locations->Out();
7564 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7565 switch (instruction->GetOpKind()) {
7566 case HInstruction::kAnd:
7567 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7568 break;
7569 case HInstruction::kXor:
7570 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7571 break;
7572 default:
7573 LOG(FATAL) << "Unreachable";
7574 }
7575}
7576
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007577void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7578void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7579void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7580
7581void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7582 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007583 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007584 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7585 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007586 locations->SetInAt(0, Location::RequiresRegister());
7587 locations->SetInAt(1, Location::Any());
7588 locations->SetOut(Location::SameAsFirstInput());
7589}
7590
7591void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7592 HandleBitwiseOperation(instruction);
7593}
7594
7595void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7596 HandleBitwiseOperation(instruction);
7597}
7598
7599void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7600 HandleBitwiseOperation(instruction);
7601}
7602
7603void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7604 LocationSummary* locations = instruction->GetLocations();
7605 Location first = locations->InAt(0);
7606 Location second = locations->InAt(1);
7607 DCHECK(first.Equals(locations->Out()));
7608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007609 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007610 if (second.IsRegister()) {
7611 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007612 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007613 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007614 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007615 } else {
7616 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007617 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007618 }
7619 } else if (second.IsConstant()) {
7620 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007621 __ andl(first.AsRegister<Register>(),
7622 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007623 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007624 __ orl(first.AsRegister<Register>(),
7625 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007626 } else {
7627 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007628 __ xorl(first.AsRegister<Register>(),
7629 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007630 }
7631 } else {
7632 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007633 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007634 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007635 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007636 } else {
7637 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007638 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007639 }
7640 }
7641 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007642 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007643 if (second.IsRegisterPair()) {
7644 if (instruction->IsAnd()) {
7645 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7646 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7647 } else if (instruction->IsOr()) {
7648 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7649 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7650 } else {
7651 DCHECK(instruction->IsXor());
7652 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7653 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7654 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007655 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007656 if (instruction->IsAnd()) {
7657 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7658 __ andl(first.AsRegisterPairHigh<Register>(),
7659 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7660 } else if (instruction->IsOr()) {
7661 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7662 __ orl(first.AsRegisterPairHigh<Register>(),
7663 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7664 } else {
7665 DCHECK(instruction->IsXor());
7666 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7667 __ xorl(first.AsRegisterPairHigh<Register>(),
7668 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7669 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007670 } else {
7671 DCHECK(second.IsConstant()) << second;
7672 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007673 int32_t low_value = Low32Bits(value);
7674 int32_t high_value = High32Bits(value);
7675 Immediate low(low_value);
7676 Immediate high(high_value);
7677 Register first_low = first.AsRegisterPairLow<Register>();
7678 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007679 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007680 if (low_value == 0) {
7681 __ xorl(first_low, first_low);
7682 } else if (low_value != -1) {
7683 __ andl(first_low, low);
7684 }
7685 if (high_value == 0) {
7686 __ xorl(first_high, first_high);
7687 } else if (high_value != -1) {
7688 __ andl(first_high, high);
7689 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007690 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007691 if (low_value != 0) {
7692 __ orl(first_low, low);
7693 }
7694 if (high_value != 0) {
7695 __ orl(first_high, high);
7696 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007697 } else {
7698 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007699 if (low_value != 0) {
7700 __ xorl(first_low, low);
7701 }
7702 if (high_value != 0) {
7703 __ xorl(first_high, high);
7704 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007705 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007706 }
7707 }
7708}
7709
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007710void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7711 HInstruction* instruction,
7712 Location out,
7713 uint32_t offset,
7714 Location maybe_temp,
7715 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007716 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007717 if (read_barrier_option == kWithReadBarrier) {
7718 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007719 if (kUseBakerReadBarrier) {
7720 // Load with fast path based Baker's read barrier.
7721 // /* HeapReference<Object> */ out = *(out + offset)
7722 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007723 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007724 } else {
7725 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007726 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007727 // in the following move operation, as we will need it for the
7728 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007729 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007730 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007731 // /* HeapReference<Object> */ out = *(out + offset)
7732 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007733 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007734 }
7735 } else {
7736 // Plain load with no read barrier.
7737 // /* HeapReference<Object> */ out = *(out + offset)
7738 __ movl(out_reg, Address(out_reg, offset));
7739 __ MaybeUnpoisonHeapReference(out_reg);
7740 }
7741}
7742
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007743void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7744 HInstruction* instruction,
7745 Location out,
7746 Location obj,
7747 uint32_t offset,
7748 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007749 Register out_reg = out.AsRegister<Register>();
7750 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007751 if (read_barrier_option == kWithReadBarrier) {
7752 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007753 if (kUseBakerReadBarrier) {
7754 // Load with fast path based Baker's read barrier.
7755 // /* HeapReference<Object> */ out = *(obj + offset)
7756 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007757 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007758 } else {
7759 // Load with slow path based read barrier.
7760 // /* HeapReference<Object> */ out = *(obj + offset)
7761 __ movl(out_reg, Address(obj_reg, offset));
7762 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7763 }
7764 } else {
7765 // Plain load with no read barrier.
7766 // /* HeapReference<Object> */ out = *(obj + offset)
7767 __ movl(out_reg, Address(obj_reg, offset));
7768 __ MaybeUnpoisonHeapReference(out_reg);
7769 }
7770}
7771
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007772void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7773 HInstruction* instruction,
7774 Location root,
7775 const Address& address,
7776 Label* fixup_label,
7777 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007778 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007779 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007780 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007781 if (kUseBakerReadBarrier) {
7782 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7783 // Baker's read barrier are used:
7784 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007785 // root = obj.field;
7786 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7787 // if (temp != null) {
7788 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007789 // }
7790
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007791 // /* GcRoot<mirror::Object> */ root = *address
7792 __ movl(root_reg, address);
7793 if (fixup_label != nullptr) {
7794 __ Bind(fixup_label);
7795 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007796 static_assert(
7797 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7798 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7799 "have different sizes.");
7800 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7801 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7802 "have different sizes.");
7803
Vladimir Marko953437b2016-08-24 08:30:46 +00007804 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007805 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007806 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007807 codegen_->AddSlowPath(slow_path);
7808
Roland Levillaind966ce72017-02-09 16:20:14 +00007809 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7810 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007811 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007812 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7813 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007814 __ j(kNotEqual, slow_path->GetEntryLabel());
7815 __ Bind(slow_path->GetExitLabel());
7816 } else {
7817 // GC root loaded through a slow path for read barriers other
7818 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007819 // /* GcRoot<mirror::Object>* */ root = address
7820 __ leal(root_reg, address);
7821 if (fixup_label != nullptr) {
7822 __ Bind(fixup_label);
7823 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007824 // /* mirror::Object* */ root = root->Read()
7825 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7826 }
7827 } else {
7828 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007829 // /* GcRoot<mirror::Object> */ root = *address
7830 __ movl(root_reg, address);
7831 if (fixup_label != nullptr) {
7832 __ Bind(fixup_label);
7833 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007834 // Note that GC roots are not affected by heap poisoning, thus we
7835 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007836 }
7837}
7838
7839void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7840 Location ref,
7841 Register obj,
7842 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007843 bool needs_null_check) {
7844 DCHECK(kEmitCompilerReadBarrier);
7845 DCHECK(kUseBakerReadBarrier);
7846
7847 // /* HeapReference<Object> */ ref = *(obj + offset)
7848 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007849 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007850}
7851
7852void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7853 Location ref,
7854 Register obj,
7855 uint32_t data_offset,
7856 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007857 bool needs_null_check) {
7858 DCHECK(kEmitCompilerReadBarrier);
7859 DCHECK(kUseBakerReadBarrier);
7860
Roland Levillain3d312422016-06-23 13:53:42 +01007861 static_assert(
7862 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7863 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007864 // /* HeapReference<Object> */ ref =
7865 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007866 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007867 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007868}
7869
7870void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7871 Location ref,
7872 Register obj,
7873 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007874 bool needs_null_check,
7875 bool always_update_field,
7876 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007877 DCHECK(kEmitCompilerReadBarrier);
7878 DCHECK(kUseBakerReadBarrier);
7879
7880 // In slow path based read barriers, the read barrier call is
7881 // inserted after the original load. However, in fast path based
7882 // Baker's read barriers, we need to perform the load of
7883 // mirror::Object::monitor_ *before* the original reference load.
7884 // This load-load ordering is required by the read barrier.
7885 // The fast path/slow path (for Baker's algorithm) should look like:
7886 //
7887 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7888 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7889 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007890 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007891 // if (is_gray) {
7892 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7893 // }
7894 //
7895 // Note: the original implementation in ReadBarrier::Barrier is
7896 // slightly more complex as:
7897 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007898 // the high-bits of rb_state, which are expected to be all zeroes
7899 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7900 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007901 // - it performs additional checks that we do not do here for
7902 // performance reasons.
7903
7904 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007905 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7906
Vladimir Marko953437b2016-08-24 08:30:46 +00007907 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007908 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007910 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7911 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7912 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7913
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007914 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007915 // ref = ReadBarrier::Mark(ref);
7916 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7917 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007918 if (needs_null_check) {
7919 MaybeRecordImplicitNullCheck(instruction);
7920 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007921
7922 // Load fence to prevent load-load reordering.
7923 // Note that this is a no-op, thanks to the x86 memory model.
7924 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7925
7926 // The actual reference load.
7927 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007928 __ movl(ref_reg, src); // Flags are unaffected.
7929
7930 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7931 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007932 SlowPathCode* slow_path;
7933 if (always_update_field) {
7934 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007935 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007936 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007937 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007938 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007939 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007940 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007941 AddSlowPath(slow_path);
7942
7943 // We have done the "if" of the gray bit check above, now branch based on the flags.
7944 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007945
7946 // Object* ref = ref_addr->AsMirrorPtr()
7947 __ MaybeUnpoisonHeapReference(ref_reg);
7948
Roland Levillain7c1559a2015-12-15 10:55:36 +00007949 __ Bind(slow_path->GetExitLabel());
7950}
7951
7952void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7953 Location out,
7954 Location ref,
7955 Location obj,
7956 uint32_t offset,
7957 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007958 DCHECK(kEmitCompilerReadBarrier);
7959
Roland Levillain7c1559a2015-12-15 10:55:36 +00007960 // Insert a slow path based read barrier *after* the reference load.
7961 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007962 // If heap poisoning is enabled, the unpoisoning of the loaded
7963 // reference will be carried out by the runtime within the slow
7964 // path.
7965 //
7966 // Note that `ref` currently does not get unpoisoned (when heap
7967 // poisoning is enabled), which is alright as the `ref` argument is
7968 // not used by the artReadBarrierSlow entry point.
7969 //
7970 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007971 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007972 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7973 AddSlowPath(slow_path);
7974
Roland Levillain0d5a2812015-11-13 10:07:31 +00007975 __ jmp(slow_path->GetEntryLabel());
7976 __ Bind(slow_path->GetExitLabel());
7977}
7978
Roland Levillain7c1559a2015-12-15 10:55:36 +00007979void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7980 Location out,
7981 Location ref,
7982 Location obj,
7983 uint32_t offset,
7984 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007985 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007986 // Baker's read barriers shall be handled by the fast path
7987 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7988 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007989 // If heap poisoning is enabled, unpoisoning will be taken care of
7990 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007991 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007992 } else if (kPoisonHeapReferences) {
7993 __ UnpoisonHeapReference(out.AsRegister<Register>());
7994 }
7995}
7996
Roland Levillain7c1559a2015-12-15 10:55:36 +00007997void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7998 Location out,
7999 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008000 DCHECK(kEmitCompilerReadBarrier);
8001
Roland Levillain7c1559a2015-12-15 10:55:36 +00008002 // Insert a slow path based read barrier *after* the GC root load.
8003 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008004 // Note that GC roots are not affected by heap poisoning, so we do
8005 // not need to do anything special for this here.
8006 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008007 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008008 AddSlowPath(slow_path);
8009
Roland Levillain0d5a2812015-11-13 10:07:31 +00008010 __ jmp(slow_path->GetEntryLabel());
8011 __ Bind(slow_path->GetExitLabel());
8012}
8013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008014void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008015 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008016 LOG(FATAL) << "Unreachable";
8017}
8018
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008019void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008020 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008021 LOG(FATAL) << "Unreachable";
8022}
8023
Mark Mendellfe57faa2015-09-18 09:26:15 -04008024// Simple implementation of packed switch - generate cascaded compare/jumps.
8025void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8026 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008027 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008028 locations->SetInAt(0, Location::RequiresRegister());
8029}
8030
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008031void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8032 int32_t lower_bound,
8033 uint32_t num_entries,
8034 HBasicBlock* switch_block,
8035 HBasicBlock* default_block) {
8036 // Figure out the correct compare values and jump conditions.
8037 // Handle the first compare/branch as a special case because it might
8038 // jump to the default case.
8039 DCHECK_GT(num_entries, 2u);
8040 Condition first_condition;
8041 uint32_t index;
8042 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8043 if (lower_bound != 0) {
8044 first_condition = kLess;
8045 __ cmpl(value_reg, Immediate(lower_bound));
8046 __ j(first_condition, codegen_->GetLabelOf(default_block));
8047 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008048
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008049 index = 1;
8050 } else {
8051 // Handle all the compare/jumps below.
8052 first_condition = kBelow;
8053 index = 0;
8054 }
8055
8056 // Handle the rest of the compare/jumps.
8057 for (; index + 1 < num_entries; index += 2) {
8058 int32_t compare_to_value = lower_bound + index + 1;
8059 __ cmpl(value_reg, Immediate(compare_to_value));
8060 // Jump to successors[index] if value < case_value[index].
8061 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8062 // Jump to successors[index + 1] if value == case_value[index + 1].
8063 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8064 }
8065
8066 if (index != num_entries) {
8067 // There are an odd number of entries. Handle the last one.
8068 DCHECK_EQ(index + 1, num_entries);
8069 __ cmpl(value_reg, Immediate(lower_bound + index));
8070 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008071 }
8072
8073 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008074 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8075 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008076 }
8077}
8078
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008079void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8080 int32_t lower_bound = switch_instr->GetStartValue();
8081 uint32_t num_entries = switch_instr->GetNumEntries();
8082 LocationSummary* locations = switch_instr->GetLocations();
8083 Register value_reg = locations->InAt(0).AsRegister<Register>();
8084
8085 GenPackedSwitchWithCompares(value_reg,
8086 lower_bound,
8087 num_entries,
8088 switch_instr->GetBlock(),
8089 switch_instr->GetDefaultBlock());
8090}
8091
Mark Mendell805b3b52015-09-18 14:10:29 -04008092void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8093 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008094 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008095 locations->SetInAt(0, Location::RequiresRegister());
8096
8097 // Constant area pointer.
8098 locations->SetInAt(1, Location::RequiresRegister());
8099
8100 // And the temporary we need.
8101 locations->AddTemp(Location::RequiresRegister());
8102}
8103
8104void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8105 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008106 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008107 LocationSummary* locations = switch_instr->GetLocations();
8108 Register value_reg = locations->InAt(0).AsRegister<Register>();
8109 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8110
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008111 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8112 GenPackedSwitchWithCompares(value_reg,
8113 lower_bound,
8114 num_entries,
8115 switch_instr->GetBlock(),
8116 default_block);
8117 return;
8118 }
8119
Mark Mendell805b3b52015-09-18 14:10:29 -04008120 // Optimizing has a jump area.
8121 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8122 Register constant_area = locations->InAt(1).AsRegister<Register>();
8123
8124 // Remove the bias, if needed.
8125 if (lower_bound != 0) {
8126 __ leal(temp_reg, Address(value_reg, -lower_bound));
8127 value_reg = temp_reg;
8128 }
8129
8130 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008131 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008132 __ cmpl(value_reg, Immediate(num_entries - 1));
8133 __ j(kAbove, codegen_->GetLabelOf(default_block));
8134
8135 // We are in the range of the table.
8136 // Load (target-constant_area) from the jump table, indexing by the value.
8137 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8138
8139 // Compute the actual target address by adding in constant_area.
8140 __ addl(temp_reg, constant_area);
8141
8142 // And jump.
8143 __ jmp(temp_reg);
8144}
8145
Mark Mendell0616ae02015-04-17 12:49:27 -04008146void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8147 HX86ComputeBaseMethodAddress* insn) {
8148 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008149 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008150 locations->SetOut(Location::RequiresRegister());
8151}
8152
8153void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8154 HX86ComputeBaseMethodAddress* insn) {
8155 LocationSummary* locations = insn->GetLocations();
8156 Register reg = locations->Out().AsRegister<Register>();
8157
8158 // Generate call to next instruction.
8159 Label next_instruction;
8160 __ call(&next_instruction);
8161 __ Bind(&next_instruction);
8162
8163 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008164 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008165
8166 // Grab the return address off the stack.
8167 __ popl(reg);
8168}
8169
8170void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8171 HX86LoadFromConstantTable* insn) {
8172 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008173 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008174
8175 locations->SetInAt(0, Location::RequiresRegister());
8176 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8177
8178 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008179 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008180 return;
8181 }
8182
8183 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008184 case DataType::Type::kFloat32:
8185 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008186 locations->SetOut(Location::RequiresFpuRegister());
8187 break;
8188
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008189 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008190 locations->SetOut(Location::RequiresRegister());
8191 break;
8192
8193 default:
8194 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8195 }
8196}
8197
8198void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008199 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008200 return;
8201 }
8202
8203 LocationSummary* locations = insn->GetLocations();
8204 Location out = locations->Out();
8205 Register const_area = locations->InAt(0).AsRegister<Register>();
8206 HConstant *value = insn->GetConstant();
8207
8208 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008209 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008210 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008211 codegen_->LiteralFloatAddress(
8212 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008213 break;
8214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008215 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008216 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008217 codegen_->LiteralDoubleAddress(
8218 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008219 break;
8220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008221 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008222 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008223 codegen_->LiteralInt32Address(
8224 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008225 break;
8226
8227 default:
8228 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8229 }
8230}
8231
Mark Mendell0616ae02015-04-17 12:49:27 -04008232/**
8233 * Class to handle late fixup of offsets into constant area.
8234 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008235class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008236 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008237 RIPFixup(CodeGeneratorX86& codegen,
8238 HX86ComputeBaseMethodAddress* base_method_address,
8239 size_t offset)
8240 : codegen_(&codegen),
8241 base_method_address_(base_method_address),
8242 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008243
8244 protected:
8245 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8246
8247 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008248 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008249
8250 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008251 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008252 // Patch the correct offset for the instruction. The place to patch is the
8253 // last 4 bytes of the instruction.
8254 // The value to patch is the distance from the offset in the constant area
8255 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008256 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008257 int32_t relative_position =
8258 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008259
8260 // Patch in the right value.
8261 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8262 }
8263
Mark Mendell0616ae02015-04-17 12:49:27 -04008264 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008265 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008266};
8267
Mark Mendell805b3b52015-09-18 14:10:29 -04008268/**
8269 * Class to handle late fixup of offsets to a jump table that will be created in the
8270 * constant area.
8271 */
8272class JumpTableRIPFixup : public RIPFixup {
8273 public:
8274 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008275 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8276 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008277
8278 void CreateJumpTable() {
8279 X86Assembler* assembler = codegen_->GetAssembler();
8280
8281 // Ensure that the reference to the jump table has the correct offset.
8282 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8283 SetOffset(offset_in_constant_table);
8284
8285 // The label values in the jump table are computed relative to the
8286 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008287 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008288
8289 // Populate the jump table with the correct values for the jump table.
8290 int32_t num_entries = switch_instr_->GetNumEntries();
8291 HBasicBlock* block = switch_instr_->GetBlock();
8292 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8293 // The value that we want is the target offset - the position of the table.
8294 for (int32_t i = 0; i < num_entries; i++) {
8295 HBasicBlock* b = successors[i];
8296 Label* l = codegen_->GetLabelOf(b);
8297 DCHECK(l->IsBound());
8298 int32_t offset_to_block = l->Position() - relative_offset;
8299 assembler->AppendInt32(offset_to_block);
8300 }
8301 }
8302
8303 private:
8304 const HX86PackedSwitch* switch_instr_;
8305};
8306
8307void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8308 // Generate the constant area if needed.
8309 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308310
Mark Mendell805b3b52015-09-18 14:10:29 -04008311 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8312 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8313 // byte values.
8314 assembler->Align(4, 0);
8315 constant_area_start_ = assembler->CodeSize();
8316
8317 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008318 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008319 jump_table->CreateJumpTable();
8320 }
8321
8322 // And now add the constant area to the generated code.
8323 assembler->AddConstantArea();
8324 }
8325
8326 // And finish up.
8327 CodeGenerator::Finalize(allocator);
8328}
8329
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008330Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8331 HX86ComputeBaseMethodAddress* method_base,
8332 Register reg) {
8333 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008334 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008335 return Address(reg, kDummy32BitOffset, fixup);
8336}
8337
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008338Address CodeGeneratorX86::LiteralFloatAddress(float v,
8339 HX86ComputeBaseMethodAddress* method_base,
8340 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008341 AssemblerFixup* fixup =
8342 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008343 return Address(reg, kDummy32BitOffset, fixup);
8344}
8345
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008346Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8347 HX86ComputeBaseMethodAddress* method_base,
8348 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008349 AssemblerFixup* fixup =
8350 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008351 return Address(reg, kDummy32BitOffset, fixup);
8352}
8353
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008354Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8355 HX86ComputeBaseMethodAddress* method_base,
8356 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008357 AssemblerFixup* fixup =
8358 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008359 return Address(reg, kDummy32BitOffset, fixup);
8360}
8361
Aart Bika19616e2016-02-01 18:57:58 -08008362void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8363 if (value == 0) {
8364 __ xorl(dest, dest);
8365 } else {
8366 __ movl(dest, Immediate(value));
8367 }
8368}
8369
8370void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8371 if (value == 0) {
8372 __ testl(dest, dest);
8373 } else {
8374 __ cmpl(dest, Immediate(value));
8375 }
8376}
8377
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008378void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8379 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008380 GenerateIntCompare(lhs_reg, rhs);
8381}
8382
8383void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008384 if (rhs.IsConstant()) {
8385 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008386 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008387 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008388 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008389 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008390 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008391 }
8392}
8393
8394Address CodeGeneratorX86::ArrayAddress(Register obj,
8395 Location index,
8396 ScaleFactor scale,
8397 uint32_t data_offset) {
8398 return index.IsConstant() ?
8399 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8400 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8401}
8402
Mark Mendell805b3b52015-09-18 14:10:29 -04008403Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8404 Register reg,
8405 Register value) {
8406 // Create a fixup to be used to create and address the jump table.
8407 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008408 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008409
8410 // We have to populate the jump tables.
8411 fixups_to_jump_tables_.push_back(table_fixup);
8412
8413 // We want a scaled address, as we are extracting the correct offset from the table.
8414 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8415}
8416
Andreas Gampe85b62f22015-09-09 13:15:38 -07008417// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008418void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008419 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008420 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008421 return;
8422 }
8423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008424 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008425
8426 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8427 if (target.Equals(return_loc)) {
8428 return;
8429 }
8430
8431 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8432 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008433 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008434 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008435 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8436 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008437 GetMoveResolver()->EmitNativeCode(&parallel_move);
8438 } else {
8439 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008440 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008441 parallel_move.AddMove(return_loc, target, type, nullptr);
8442 GetMoveResolver()->EmitNativeCode(&parallel_move);
8443 }
8444}
8445
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008446void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8447 const uint8_t* roots_data,
8448 const PatchInfo<Label>& info,
8449 uint64_t index_in_table) const {
8450 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8451 uintptr_t address =
8452 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008453 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008454 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8455 dchecked_integral_cast<uint32_t>(address);
8456}
8457
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008458void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8459 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008460 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008461 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008462 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008463 }
8464
8465 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008466 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008467 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008468 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008469 }
8470}
8471
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008472void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8473 ATTRIBUTE_UNUSED) {
8474 LOG(FATAL) << "Unreachable";
8475}
8476
8477void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8478 ATTRIBUTE_UNUSED) {
8479 LOG(FATAL) << "Unreachable";
8480}
8481
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308482bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8483 return codegen_->GetInstructionSetFeatures().HasAVX();
8484}
8485bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8486 return codegen_->GetInstructionSetFeatures().HasAVX2();
8487}
8488bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8489 return codegen_->GetInstructionSetFeatures().HasAVX();
8490}
8491bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8492 return codegen_->GetInstructionSetFeatures().HasAVX2();
8493}
8494
Roland Levillain4d027112015-07-01 15:41:14 +01008495#undef __
8496
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008497} // namespace x86
8498} // namespace art