blob: 3077be05ac2ee5c6fb754f6e433c1b91c0e0a583 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000019#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "intrinsics.h"
29#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000030#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010031#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070033#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000035#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010036#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010040#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041
Vladimir Marko0a516052019-10-14 13:00:44 +000042namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010043
Roland Levillain0d5a2812015-11-13 10:07:31 +000044template<class MirrorType>
45class GcRoot;
46
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047namespace x86 {
48
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010050static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050051static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
Mark Mendell24f2dfa2015-01-14 19:51:45 -050053static constexpr int kC2ConditionMask = 0x400;
54
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000055static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000056
Aart Bik1f8d51b2018-02-15 10:42:37 -080057static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
58static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
59
Vladimir Marko3232dbb2018-07-25 15:42:46 +010060static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
61 InvokeRuntimeCallingConvention calling_convention;
62 RegisterSet caller_saves = RegisterSet::Empty();
63 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
64 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
65 // that the the kPrimNot result register is the same as the first argument register.
66 return caller_saves;
67}
68
Roland Levillain7cbd27f2016-08-11 23:53:33 +010069// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
70#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070071#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072
Andreas Gampe85b62f22015-09-09 13:15:38 -070073class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010077 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010078 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000080 if (instruction_->CanThrowIntoCatchBlock()) {
81 // Live registers will be restored in the catch block if caught.
82 SaveLiveRegisters(codegen, instruction_->GetLocations());
83 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010084 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010085 instruction_,
86 instruction_->GetDexPc(),
87 this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 }
90
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010093 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010094
Nicolas Geoffraye5038322014-07-04 09:41:32 +010095 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100103 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100104 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100106 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000107 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000108 }
109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100110 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100112 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100113
Calin Juravled0d48522014-11-04 16:40:20 +0000114 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000115 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
116};
117
Andreas Gampe85b62f22015-09-09 13:15:38 -0700118class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000119 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000120 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
121 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100123 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000124 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 if (is_div_) {
126 __ negl(reg_);
127 } else {
128 __ movl(reg_, Immediate(0));
129 }
Calin Juravled0d48522014-11-04 16:40:20 +0000130 __ jmp(GetExitLabel());
131 }
132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100133 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100134
Calin Juravled0d48522014-11-04 16:40:20 +0000135 private:
136 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Andreas Gampe85b62f22015-09-09 13:15:38 -0700141class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000143 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100145 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000149 // We're moving two locations to locations that could overlap, so we need a parallel
150 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000151 if (instruction_->CanThrowIntoCatchBlock()) {
152 // Live registers will be restored in the catch block if caught.
153 SaveLiveRegisters(codegen, instruction_->GetLocations());
154 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400155
156 // Are we using an array length from memory?
157 HInstruction* array_length = instruction_->InputAt(1);
158 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400160 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
161 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100162 HArrayLength* length = array_length->AsArrayLength();
163 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400164 Location array_loc = array_length->GetLocations()->InAt(0);
165 Address array_len(array_loc.AsRegister<Register>(), len_offset);
166 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
167 // Check for conflicts with index.
168 if (length_loc.Equals(locations->InAt(0))) {
169 // We know we aren't using parameter 2.
170 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
171 }
172 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100173 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100174 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700175 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400176 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100180 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400181 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100182 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100184 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
185 ? kQuickThrowStringBounds
186 : kQuickThrowArrayBounds;
187 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100188 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 }
191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100192 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100194 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100195
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
198};
199
Andreas Gampe85b62f22015-09-09 13:15:38 -0700200class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000203 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100205 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700206 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100207 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700209 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100210 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000211 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700212 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100213 if (successor_ == nullptr) {
214 __ jmp(GetReturnLabel());
215 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100216 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 }
219
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 Label* GetReturnLabel() {
221 DCHECK(successor_ == nullptr);
222 return &return_label_;
223 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000224
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100225 HBasicBlock* GetSuccessor() const {
226 return successor_;
227 }
228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100229 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100230
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000231 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100232 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000233 Label return_label_;
234
235 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
236};
237
Vladimir Markoaad75c62016-10-03 08:46:48 +0000238class LoadStringSlowPathX86 : public SlowPathCode {
239 public:
240 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
241
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100242 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000243 LocationSummary* locations = instruction_->GetLocations();
244 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
245
246 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
250 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000251 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
252 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000253 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
254 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
255 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
256 RestoreLiveRegisters(codegen, locations);
257
Vladimir Markoaad75c62016-10-03 08:46:48 +0000258 __ jmp(GetExitLabel());
259 }
260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100261 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000262
263 private:
264 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
265};
266
Andreas Gampe85b62f22015-09-09 13:15:38 -0700267class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100269 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
270 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100272 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 }
274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100275 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100277 Location out = locations->Out();
278 const uint32_t dex_pc = instruction_->GetDexPc();
279 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
280 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
283 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100287 if (must_resolve_type) {
288 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
289 dex::TypeIndex type_index = cls_->GetTypeIndex();
290 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100291 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
292 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100293 // If we also must_do_clinit, the resolved type is now in the correct register.
294 } else {
295 DCHECK(must_do_clinit);
296 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
297 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
298 }
299 if (must_do_clinit) {
300 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
301 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000302 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000303
304 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 if (out.IsValid()) {
306 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
307 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000308 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310 __ jmp(GetExitLabel());
311 }
312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100313 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100314
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000319 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
320};
321
Andreas Gampe85b62f22015-09-09 13:15:38 -0700322class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000324 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000325 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100327 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 DCHECK(instruction_->IsCheckCast()
330 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
333 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000335 if (kPoisonHeapReferences &&
336 instruction_->IsCheckCast() &&
337 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
338 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
339 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
340 }
341
Vladimir Marko87584542017-12-12 17:47:52 +0000342 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343 SaveLiveRegisters(codegen, locations);
344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 // We're moving two locations to locations that could overlap, so we need a parallel
347 // move resolver.
348 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800349 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100351 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800352 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800353 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100356 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 instruction_,
358 instruction_->GetDexPc(),
359 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 } else {
362 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800363 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
364 instruction_,
365 instruction_->GetDexPc(),
366 this);
367 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368 }
369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 if (!is_fatal_) {
371 if (instruction_->IsInstanceOf()) {
372 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
373 }
374 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 __ jmp(GetExitLabel());
377 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378 }
379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100380 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
381 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100382
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000383 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000385
386 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
387};
388
Andreas Gampe85b62f22015-09-09 13:15:38 -0700389class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 public:
Aart Bik42249c32016-01-07 15:33:50 -0800391 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000392 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100394 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100395 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100397 LocationSummary* locations = instruction_->GetLocations();
398 SaveLiveRegisters(codegen, locations);
399 InvokeRuntimeCallingConvention calling_convention;
400 x86_codegen->Load32BitValue(
401 calling_convention.GetRegisterAt(0),
402 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100403 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100404 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700405 }
406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100407 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100408
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700409 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
411};
412
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413class ArraySetSlowPathX86 : public SlowPathCode {
414 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000415 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100417 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100418 LocationSummary* locations = instruction_->GetLocations();
419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, locations);
421
422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100423 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 parallel_move.AddMove(
425 locations->InAt(0),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100427 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100428 nullptr);
429 parallel_move.AddMove(
430 locations->InAt(1),
431 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100432 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 nullptr);
434 parallel_move.AddMove(
435 locations->InAt(2),
436 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100437 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 nullptr);
439 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
440
441 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100442 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000443 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 RestoreLiveRegisters(codegen, locations);
445 __ jmp(GetExitLabel());
446 }
447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100448 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100449
450 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100451 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
452};
453
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100454// Slow path marking an object reference `ref` during a read
455// barrier. The field `obj.field` in the object `obj` holding this
456// reference does not get updated by this slow path after marking (see
457// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
458//
459// This means that after the execution of this slow path, `ref` will
460// always be up-to-date, but `obj.field` may not; i.e., after the
461// flip, `ref` will be a to-space reference, but `obj.field` will
462// probably still be a from-space reference (unless it gets updated by
463// another thread, or if another thread installed another object
464// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
466 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100467 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
468 Location ref,
469 bool unpoison_ref_before_marking)
470 : SlowPathCode(instruction),
471 ref_(ref),
472 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473 DCHECK(kEmitCompilerReadBarrier);
474 }
475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100476 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100478 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000479 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100482 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000483 DCHECK(instruction_->IsInstanceFieldGet() ||
484 instruction_->IsStaticFieldGet() ||
485 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100486 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 instruction_->IsLoadClass() ||
488 instruction_->IsLoadString() ||
489 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100490 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100491 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
492 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 << "Unexpected instruction in read barrier marking slow path: "
494 << instruction_->DebugName();
495
496 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 }
Roland Levillain4359e612016-07-20 11:32:19 +0100501 // No need to save live registers; it's taken care of by the
502 // entrypoint. Also, there is no need to update the stack mask,
503 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 DCHECK_NE(ref_reg, ESP);
506 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in EAX):
511 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100513 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100515 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
Roland Levillain97c46462017-05-11 14:04:03 +0100521 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100522 // This runtime call does not require a stack map.
523 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524 __ jmp(GetExitLabel());
525 }
526
527 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528 // The location (register) of the marked object reference.
529 const Location ref_;
530 // Should the reference in `ref_` be unpoisoned prior to marking it?
531 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532
533 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
534};
535
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100536// Slow path marking an object reference `ref` during a read barrier,
537// and if needed, atomically updating the field `obj.field` in the
538// object `obj` holding this reference after marking (contrary to
539// ReadBarrierMarkSlowPathX86 above, which never tries to update
540// `obj.field`).
541//
542// This means that after the execution of this slow path, both `ref`
543// and `obj.field` will be up-to-date; i.e., after the flip, both will
544// hold the same to-space reference (unless another thread installed
545// another object reference (different from `ref`) in `obj.field`).
546class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
547 public:
548 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
549 Location ref,
550 Register obj,
551 const Address& field_addr,
552 bool unpoison_ref_before_marking,
553 Register temp)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp_(temp) {
560 DCHECK(kEmitCompilerReadBarrier);
561 }
562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100565 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100566 LocationSummary* locations = instruction_->GetLocations();
567 Register ref_reg = ref_.AsRegister<Register>();
568 DCHECK(locations->CanCall());
569 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
570 // This slow path is only used by the UnsafeCASObject intrinsic.
571 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
572 << "Unexpected instruction in read barrier marking and field updating slow path: "
573 << instruction_->DebugName();
574 DCHECK(instruction_->GetLocations()->Intrinsified());
575 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
576
577 __ Bind(GetEntryLabel());
578 if (unpoison_ref_before_marking_) {
579 // Object* ref = ref_addr->AsMirrorPtr()
580 __ MaybeUnpoisonHeapReference(ref_reg);
581 }
582
583 // Save the old (unpoisoned) reference.
584 __ movl(temp_, ref_reg);
585
586 // No need to save live registers; it's taken care of by the
587 // entrypoint. Also, there is no need to update the stack mask,
588 // as this runtime call will not trigger a garbage collection.
589 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
590 DCHECK_NE(ref_reg, ESP);
591 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
592 // "Compact" slow path, saving two moves.
593 //
594 // Instead of using the standard runtime calling convention (input
595 // and output in EAX):
596 //
597 // EAX <- ref
598 // EAX <- ReadBarrierMark(EAX)
599 // ref <- EAX
600 //
601 // we just use rX (the register containing `ref`) as input and output
602 // of a dedicated entrypoint:
603 //
604 // rX <- ReadBarrierMarkRegX(rX)
605 //
Roland Levillain97c46462017-05-11 14:04:03 +0100606 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100607 // This runtime call does not require a stack map.
608 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
609
610 // If the new reference is different from the old reference,
611 // update the field in the holder (`*field_addr`).
612 //
613 // Note that this field could also hold a different object, if
614 // another thread had concurrently changed it. In that case, the
615 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
616 // operation below would abort the CAS, leaving the field as-is.
617 NearLabel done;
618 __ cmpl(temp_, ref_reg);
619 __ j(kEqual, &done);
620
621 // Update the the holder's field atomically. This may fail if
622 // mutator updates before us, but it's OK. This is achieved
623 // using a strong compare-and-set (CAS) operation with relaxed
624 // memory synchronization ordering, where the expected value is
625 // the old reference and the desired value is the new reference.
626 // This operation is implemented with a 32-bit LOCK CMPXLCHG
627 // instruction, which requires the expected value (the old
628 // reference) to be in EAX. Save EAX beforehand, and move the
629 // expected value (stored in `temp_`) into EAX.
630 __ pushl(EAX);
631 __ movl(EAX, temp_);
632
633 // Convenience aliases.
634 Register base = obj_;
635 Register expected = EAX;
636 Register value = ref_reg;
637
638 bool base_equals_value = (base == value);
639 if (kPoisonHeapReferences) {
640 if (base_equals_value) {
641 // If `base` and `value` are the same register location, move
642 // `value` to a temporary register. This way, poisoning
643 // `value` won't invalidate `base`.
644 value = temp_;
645 __ movl(value, base);
646 }
647
648 // Check that the register allocator did not assign the location
649 // of `expected` (EAX) to `value` nor to `base`, so that heap
650 // poisoning (when enabled) works as intended below.
651 // - If `value` were equal to `expected`, both references would
652 // be poisoned twice, meaning they would not be poisoned at
653 // all, as heap poisoning uses address negation.
654 // - If `base` were equal to `expected`, poisoning `expected`
655 // would invalidate `base`.
656 DCHECK_NE(value, expected);
657 DCHECK_NE(base, expected);
658
659 __ PoisonHeapReference(expected);
660 __ PoisonHeapReference(value);
661 }
662
663 __ LockCmpxchgl(field_addr_, value);
664
665 // If heap poisoning is enabled, we need to unpoison the values
666 // that were poisoned earlier.
667 if (kPoisonHeapReferences) {
668 if (base_equals_value) {
669 // `value` has been moved to a temporary register, no need
670 // to unpoison it.
671 } else {
672 __ UnpoisonHeapReference(value);
673 }
674 // No need to unpoison `expected` (EAX), as it is be overwritten below.
675 }
676
677 // Restore EAX.
678 __ popl(EAX);
679
680 __ Bind(&done);
681 __ jmp(GetExitLabel());
682 }
683
684 private:
685 // The location (register) of the marked object reference.
686 const Location ref_;
687 // The register containing the object holding the marked object reference field.
688 const Register obj_;
689 // The address of the marked reference field. The base of this address must be `obj_`.
690 const Address field_addr_;
691
692 // Should the reference in `ref_` be unpoisoned prior to marking it?
693 const bool unpoison_ref_before_marking_;
694
695 const Register temp_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
698};
699
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700// Slow path generating a read barrier for a heap reference.
701class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
702 public:
703 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
704 Location out,
705 Location ref,
706 Location obj,
707 uint32_t offset,
708 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000709 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 out_(out),
711 ref_(ref),
712 obj_(obj),
713 offset_(offset),
714 index_(index) {
715 DCHECK(kEmitCompilerReadBarrier);
716 // If `obj` is equal to `out` or `ref`, it means the initial object
717 // has been overwritten by (or after) the heap object reference load
718 // to be instrumented, e.g.:
719 //
720 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000721 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722 //
723 // In that case, we have lost the information about the original
724 // object, and the emitted read barrier cannot work properly.
725 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
726 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
727 }
728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100729 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
731 LocationSummary* locations = instruction_->GetLocations();
732 Register reg_out = out_.AsRegister<Register>();
733 DCHECK(locations->CanCall());
734 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100735 DCHECK(instruction_->IsInstanceFieldGet() ||
736 instruction_->IsStaticFieldGet() ||
737 instruction_->IsArrayGet() ||
738 instruction_->IsInstanceOf() ||
739 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700740 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000741 << "Unexpected instruction in read barrier for heap reference slow path: "
742 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000743
744 __ Bind(GetEntryLabel());
745 SaveLiveRegisters(codegen, locations);
746
747 // We may have to change the index's value, but as `index_` is a
748 // constant member (like other "inputs" of this slow path),
749 // introduce a copy of it, `index`.
750 Location index = index_;
751 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000753 if (instruction_->IsArrayGet()) {
754 // Compute the actual memory offset and store it in `index`.
755 Register index_reg = index_.AsRegister<Register>();
756 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
757 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
758 // We are about to change the value of `index_reg` (see the
759 // calls to art::x86::X86Assembler::shll and
760 // art::x86::X86Assembler::AddImmediate below), but it has
761 // not been saved by the previous call to
762 // art::SlowPathCode::SaveLiveRegisters, as it is a
763 // callee-save register --
764 // art::SlowPathCode::SaveLiveRegisters does not consider
765 // callee-save registers, as it has been designed with the
766 // assumption that callee-save registers are supposed to be
767 // handled by the called function. So, as a callee-save
768 // register, `index_reg` _would_ eventually be saved onto
769 // the stack, but it would be too late: we would have
770 // changed its value earlier. Therefore, we manually save
771 // it here into another freely available register,
772 // `free_reg`, chosen of course among the caller-save
773 // registers (as a callee-save `free_reg` register would
774 // exhibit the same problem).
775 //
776 // Note we could have requested a temporary register from
777 // the register allocator instead; but we prefer not to, as
778 // this is a slow path, and we know we can find a
779 // caller-save register that is available.
780 Register free_reg = FindAvailableCallerSaveRegister(codegen);
781 __ movl(free_reg, index_reg);
782 index_reg = free_reg;
783 index = Location::RegisterLocation(index_reg);
784 } else {
785 // The initial register stored in `index_` has already been
786 // saved in the call to art::SlowPathCode::SaveLiveRegisters
787 // (as it is not a callee-save register), so we can freely
788 // use it.
789 }
790 // Shifting the index value contained in `index_reg` by the scale
791 // factor (2) cannot overflow in practice, as the runtime is
792 // unable to allocate object arrays with a size larger than
793 // 2^26 - 1 (that is, 2^28 - 4 bytes).
794 __ shll(index_reg, Immediate(TIMES_4));
795 static_assert(
796 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
797 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
798 __ AddImmediate(index_reg, Immediate(offset_));
799 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100800 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
801 // intrinsics, `index_` is not shifted by a scale factor of 2
802 // (as in the case of ArrayGet), as it is actually an offset
803 // to an object field within an object.
804 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000805 DCHECK(instruction_->GetLocations()->Intrinsified());
806 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
807 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
808 << instruction_->AsInvoke()->GetIntrinsic();
809 DCHECK_EQ(offset_, 0U);
810 DCHECK(index_.IsRegisterPair());
811 // UnsafeGet's offset location is a register pair, the low
812 // part contains the correct offset.
813 index = index_.ToLow();
814 }
815 }
816
817 // We're moving two or three locations to locations that could
818 // overlap, so we need a parallel move resolver.
819 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 parallel_move.AddMove(ref_,
822 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100823 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000824 nullptr);
825 parallel_move.AddMove(obj_,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100827 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 nullptr);
829 if (index.IsValid()) {
830 parallel_move.AddMove(index,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 nullptr);
834 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
835 } else {
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
838 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100839 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 CheckEntrypointTypes<
841 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
842 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
843
844 RestoreLiveRegisters(codegen, locations);
845 __ jmp(GetExitLabel());
846 }
847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100848 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849
850 private:
851 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
852 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
853 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
854 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
855 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
856 return static_cast<Register>(i);
857 }
858 }
859 // We shall never fail to find a free caller-save register, as
860 // there are more than two core caller-save registers on x86
861 // (meaning it is possible to find one which is different from
862 // `ref` and `obj`).
863 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
864 LOG(FATAL) << "Could not find a free caller-save register";
865 UNREACHABLE();
866 }
867
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 const Location out_;
869 const Location ref_;
870 const Location obj_;
871 const uint32_t offset_;
872 // An additional location containing an index to an array.
873 // Only used for HArrayGet and the UnsafeGetObject &
874 // UnsafeGetObjectVolatile intrinsics.
875 const Location index_;
876
877 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
878};
879
880// Slow path generating a read barrier for a GC root.
881class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
882 public:
883 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000885 DCHECK(kEmitCompilerReadBarrier);
886 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100888 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889 LocationSummary* locations = instruction_->GetLocations();
890 Register reg_out = out_.AsRegister<Register>();
891 DCHECK(locations->CanCall());
892 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000893 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
894 << "Unexpected instruction in read barrier for GC root slow path: "
895 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 __ Bind(GetEntryLabel());
898 SaveLiveRegisters(codegen, locations);
899
900 InvokeRuntimeCallingConvention calling_convention;
901 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
902 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100903 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
908 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
909
910 RestoreLiveRegisters(codegen, locations);
911 __ jmp(GetExitLabel());
912 }
913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100914 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000915
916 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000917 const Location out_;
918 const Location root_;
919
920 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
921};
922
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100924// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
925#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926
Aart Bike9f37602015-10-09 11:15:55 -0700927inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700928 switch (cond) {
929 case kCondEQ: return kEqual;
930 case kCondNE: return kNotEqual;
931 case kCondLT: return kLess;
932 case kCondLE: return kLessEqual;
933 case kCondGT: return kGreater;
934 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700935 case kCondB: return kBelow;
936 case kCondBE: return kBelowEqual;
937 case kCondA: return kAbove;
938 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700939 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 LOG(FATAL) << "Unreachable";
941 UNREACHABLE();
942}
943
Aart Bike9f37602015-10-09 11:15:55 -0700944// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700949 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100950 case kCondLT: return kBelow;
951 case kCondLE: return kBelowEqual;
952 case kCondGT: return kAbove;
953 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700954 // Unsigned remain unchanged.
955 case kCondB: return kBelow;
956 case kCondBE: return kBelowEqual;
957 case kCondA: return kAbove;
958 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959 }
960 LOG(FATAL) << "Unreachable";
961 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700962}
963
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100964void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100965 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966}
967
968void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100969 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970}
971
Vladimir Markoa0431112018-06-25 09:32:54 +0100972const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
973 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
974}
975
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100976size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
977 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
978 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100979}
980
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100981size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
982 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
983 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100984}
985
Mark Mendell7c8d0092015-01-26 11:21:33 -0500986size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700988 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700989 } else {
990 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
991 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100992 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500993}
994
995size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700996 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700997 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700998 } else {
999 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1000 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001001 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001002}
1003
Calin Juravle175dc732015-08-25 15:42:32 +01001004void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1005 HInstruction* instruction,
1006 uint32_t dex_pc,
1007 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001008 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001009 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1010 if (EntrypointRequiresStackMap(entrypoint)) {
1011 RecordPcInfo(instruction, dex_pc, slow_path);
1012 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001013}
1014
Roland Levillaindec8f632016-07-22 17:10:06 +01001015void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1016 HInstruction* instruction,
1017 SlowPathCode* slow_path) {
1018 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001019 GenerateInvokeRuntime(entry_point_offset);
1020}
1021
1022void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001023 __ fs()->call(Address::Absolute(entry_point_offset));
1024}
1025
Mark Mendellfb8d2792015-03-31 22:16:59 -04001026CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001027 const CompilerOptions& compiler_options,
1028 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001029 : CodeGenerator(graph,
1030 kNumberOfCpuRegisters,
1031 kNumberOfXmmRegisters,
1032 kNumberOfRegisterPairs,
1033 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1034 arraysize(kCoreCalleeSaves))
1035 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001036 0,
1037 compiler_options,
1038 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001039 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001040 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001041 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 move_resolver_(graph->GetAllocator(), this),
1043 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1045 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001048 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001050 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001051 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1052 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001053 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001055 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001056 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 // Use a fake return address register to mimic Quick.
1058 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001059}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001060
David Brazdil58282f42016-01-14 12:45:10 +00001061void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001063 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001064}
1065
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001066InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001067 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001068 assembler_(codegen->GetAssembler()),
1069 codegen_(codegen) {}
1070
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001072 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073}
1074
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001075void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1076 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1077 Register reg = EAX;
1078 if (is_frame_entry) {
1079 reg = kMethodRegisterArgument;
1080 } else {
1081 __ pushl(EAX);
1082 __ movl(EAX, Address(ESP, kX86WordSize));
1083 }
1084 NearLabel overflow;
1085 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1086 Immediate(ArtMethod::MaxCounter()));
1087 __ j(kEqual, &overflow);
1088 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1089 Immediate(1));
1090 __ Bind(&overflow);
1091 if (!is_frame_entry) {
1092 __ popl(EAX);
1093 }
1094 }
1095
1096 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1097 ScopedObjectAccess soa(Thread::Current());
1098 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
1099 uint32_t address = reinterpret_cast32<uint32_t>(info);
1100 NearLabel done;
1101 if (HasEmptyFrame()) {
1102 CHECK(is_frame_entry);
1103 // Alignment
1104 __ subl(ESP, Immediate(8));
1105 __ cfi().AdjustCFAOffset(8);
1106 // We need a temporary. The stub also expects the method at bottom of stack.
1107 __ pushl(EAX);
1108 __ cfi().AdjustCFAOffset(4);
1109 __ movl(EAX, Immediate(address));
1110 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()), Immediate(1));
1111 __ j(kCarryClear, &done);
1112 GenerateInvokeRuntime(
1113 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1114 __ Bind(&done);
1115 // We don't strictly require to restore EAX, but this makes the generated
1116 // code easier to reason about.
1117 __ popl(EAX);
1118 __ cfi().AdjustCFAOffset(-4);
1119 __ addl(ESP, Immediate(8));
1120 __ cfi().AdjustCFAOffset(-8);
1121 } else {
1122 if (!RequiresCurrentMethod()) {
1123 CHECK(is_frame_entry);
1124 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1125 }
1126 // We need a temporary.
1127 __ pushl(EAX);
1128 __ cfi().AdjustCFAOffset(4);
1129 __ movl(EAX, Immediate(address));
1130 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()), Immediate(1));
1131 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1132 __ cfi().AdjustCFAOffset(-4);
1133 __ j(kCarryClear, &done);
1134 GenerateInvokeRuntime(
1135 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1136 __ Bind(&done);
1137 }
1138 }
1139}
1140
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001141void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001142 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001143 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001144 bool skip_overflow_check =
1145 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001146 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001147
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001148 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001149 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1150 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001152 }
1153
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001154 if (!HasEmptyFrame()) {
1155 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1156 Register reg = kCoreCalleeSaves[i];
1157 if (allocated_registers_.ContainsCoreRegister(reg)) {
1158 __ pushl(reg);
1159 __ cfi().AdjustCFAOffset(kX86WordSize);
1160 __ cfi().RelOffset(DWARFReg(reg), 0);
1161 }
1162 }
Mark Mendell5f874182015-03-04 15:42:45 -05001163
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001164 int adjust = GetFrameSize() - FrameEntrySpillSize();
1165 __ subl(ESP, Immediate(adjust));
1166 __ cfi().AdjustCFAOffset(adjust);
1167 // Save the current method if we need it. Note that we do not
1168 // do this in HCurrentMethod, as the instruction might have been removed
1169 // in the SSA graph.
1170 if (RequiresCurrentMethod()) {
1171 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1172 }
1173
1174 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1175 // Initialize should_deoptimize flag to 0.
1176 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001177 }
1178 }
1179
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001180 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001181}
1182
1183void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001184 __ cfi().RememberState();
1185 if (!HasEmptyFrame()) {
1186 int adjust = GetFrameSize() - FrameEntrySpillSize();
1187 __ addl(ESP, Immediate(adjust));
1188 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001189
David Srbeckyc34dc932015-04-12 09:27:43 +01001190 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1191 Register reg = kCoreCalleeSaves[i];
1192 if (allocated_registers_.ContainsCoreRegister(reg)) {
1193 __ popl(reg);
1194 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1195 __ cfi().Restore(DWARFReg(reg));
1196 }
Mark Mendell5f874182015-03-04 15:42:45 -05001197 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001198 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001199 __ ret();
1200 __ cfi().RestoreState();
1201 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001202}
1203
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001204void CodeGeneratorX86::Bind(HBasicBlock* block) {
1205 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001206}
1207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001209 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001210 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001211 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001212 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 case DataType::Type::kInt8:
1214 case DataType::Type::kUint16:
1215 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001216 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001218 return Location::RegisterLocation(EAX);
1219
Aart Bik66c158e2018-01-31 12:55:04 -08001220 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001222 return Location::RegisterPairLocation(EAX, EDX);
1223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001225 return Location::NoLocation();
1226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 case DataType::Type::kFloat64:
1228 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001229 return Location::FpuRegisterLocation(XMM0);
1230 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001231
1232 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001233}
1234
1235Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1236 return Location::RegisterLocation(kMethodRegisterArgument);
1237}
1238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001240 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001241 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001243 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001244 case DataType::Type::kInt8:
1245 case DataType::Type::kUint16:
1246 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001247 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001248 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001249 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001250 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001251 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001252 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001253 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001254 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001255 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001257 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001258 uint32_t index = gp_index_;
1259 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001260 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001262 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1263 calling_convention.GetRegisterPairAt(index));
1264 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001265 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001266 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1267 }
1268 }
1269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001271 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001272 stack_index_++;
1273 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1274 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1275 } else {
1276 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1277 }
1278 }
1279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001280 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001281 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001282 stack_index_ += 2;
1283 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1284 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1285 } else {
1286 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001287 }
1288 }
1289
Aart Bik66c158e2018-01-31 12:55:04 -08001290 case DataType::Type::kUint32:
1291 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001292 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001293 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001294 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001295 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001296 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001297}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001298
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001299void CodeGeneratorX86::Move32(Location destination, Location source) {
1300 if (source.Equals(destination)) {
1301 return;
1302 }
1303 if (destination.IsRegister()) {
1304 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 } else {
1309 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001312 } else if (destination.IsFpuRegister()) {
1313 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001315 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317 } else {
1318 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001320 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001322 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001323 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001324 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001325 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001327 } else if (source.IsConstant()) {
1328 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001329 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001330 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001331 } else {
1332 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001333 __ pushl(Address(ESP, source.GetStackIndex()));
1334 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001335 }
1336 }
1337}
1338
1339void CodeGeneratorX86::Move64(Location destination, Location source) {
1340 if (source.Equals(destination)) {
1341 return;
1342 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001343 if (destination.IsRegisterPair()) {
1344 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001345 EmitParallelMoves(
1346 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1347 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001349 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001350 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001351 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001352 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001353 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1354 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1355 __ psrlq(src_reg, Immediate(32));
1356 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001357 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001358 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001359 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001360 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1361 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1363 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001364 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001365 if (source.IsFpuRegister()) {
1366 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1367 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001368 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001369 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001370 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001371 // Create stack space for 2 elements.
1372 __ subl(ESP, Immediate(2 * elem_size));
1373 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1374 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1375 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1376 // And remove the temporary stack space we allocated.
1377 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 } else {
1379 LOG(FATAL) << "Unimplemented";
1380 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001381 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001382 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001383 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001384 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001385 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001386 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001387 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001388 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001389 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001390 } else if (source.IsConstant()) {
1391 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001392 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1393 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001394 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001395 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1396 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001398 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001399 EmitParallelMoves(
1400 Location::StackSlot(source.GetStackIndex()),
1401 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001402 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001403 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001404 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001405 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001406 }
1407 }
1408}
1409
Calin Juravle175dc732015-08-25 15:42:32 +01001410void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1411 DCHECK(location.IsRegister());
1412 __ movl(location.AsRegister<Register>(), Immediate(value));
1413}
1414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001415void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001416 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001417 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1418 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1419 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001420 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001421 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001422 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001423 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001424}
1425
1426void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1427 if (location.IsRegister()) {
1428 locations->AddTemp(location);
1429 } else if (location.IsRegisterPair()) {
1430 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1431 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1432 } else {
1433 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1434 }
1435}
1436
David Brazdilfc6a86a2015-06-26 10:33:45 +00001437void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001438 if (successor->IsExitBlock()) {
1439 DCHECK(got->GetPrevious()->AlwaysThrows());
1440 return; // no code needed
1441 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001442
1443 HBasicBlock* block = got->GetBlock();
1444 HInstruction* previous = got->GetPrevious();
1445
1446 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001447 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001448 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001449 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1450 return;
1451 }
1452
1453 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1454 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1455 }
1456 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001458 }
1459}
1460
David Brazdilfc6a86a2015-06-26 10:33:45 +00001461void LocationsBuilderX86::VisitGoto(HGoto* got) {
1462 got->SetLocations(nullptr);
1463}
1464
1465void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1466 HandleGoto(got, got->GetSuccessor());
1467}
1468
1469void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1470 try_boundary->SetLocations(nullptr);
1471}
1472
1473void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1474 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1475 if (!successor->IsExitBlock()) {
1476 HandleGoto(try_boundary, successor);
1477 }
1478}
1479
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001480void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001481 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001482}
1483
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001484void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001485}
1486
Mark Mendell152408f2015-12-31 12:28:50 -05001487template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001488void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001489 LabelType* true_label,
1490 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001491 if (cond->IsFPConditionTrueIfNaN()) {
1492 __ j(kUnordered, true_label);
1493 } else if (cond->IsFPConditionFalseIfNaN()) {
1494 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001495 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001496 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001497}
1498
Mark Mendell152408f2015-12-31 12:28:50 -05001499template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001500void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001501 LabelType* true_label,
1502 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001503 LocationSummary* locations = cond->GetLocations();
1504 Location left = locations->InAt(0);
1505 Location right = locations->InAt(1);
1506 IfCondition if_cond = cond->GetCondition();
1507
Mark Mendellc4701932015-04-10 13:18:51 -04001508 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001509 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001510 IfCondition true_high_cond = if_cond;
1511 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001512 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001513
1514 // Set the conditions for the test, remembering that == needs to be
1515 // decided using the low words.
1516 switch (if_cond) {
1517 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001518 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001519 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001520 break;
1521 case kCondLT:
1522 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001523 break;
1524 case kCondLE:
1525 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001526 break;
1527 case kCondGT:
1528 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001529 break;
1530 case kCondGE:
1531 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001532 break;
Aart Bike9f37602015-10-09 11:15:55 -07001533 case kCondB:
1534 false_high_cond = kCondA;
1535 break;
1536 case kCondBE:
1537 true_high_cond = kCondB;
1538 break;
1539 case kCondA:
1540 false_high_cond = kCondB;
1541 break;
1542 case kCondAE:
1543 true_high_cond = kCondA;
1544 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001545 }
1546
1547 if (right.IsConstant()) {
1548 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001549 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001550 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001551
Aart Bika19616e2016-02-01 18:57:58 -08001552 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001553 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001554 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001555 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001556 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001557 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001558 __ j(X86Condition(true_high_cond), true_label);
1559 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001560 }
1561 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001562 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001563 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001564 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001565 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001566
1567 __ cmpl(left_high, right_high);
1568 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001569 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001570 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001571 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001572 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001573 __ j(X86Condition(true_high_cond), true_label);
1574 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001575 }
1576 // Must be equal high, so compare the lows.
1577 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001578 } else {
1579 DCHECK(right.IsDoubleStackSlot());
1580 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1581 if (if_cond == kCondNE) {
1582 __ j(X86Condition(true_high_cond), true_label);
1583 } else if (if_cond == kCondEQ) {
1584 __ j(X86Condition(false_high_cond), false_label);
1585 } else {
1586 __ j(X86Condition(true_high_cond), true_label);
1587 __ j(X86Condition(false_high_cond), false_label);
1588 }
1589 // Must be equal high, so compare the lows.
1590 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001591 }
1592 // The last comparison might be unsigned.
1593 __ j(final_condition, true_label);
1594}
1595
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001596void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1597 Location rhs,
1598 HInstruction* insn,
1599 bool is_double) {
1600 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1601 if (is_double) {
1602 if (rhs.IsFpuRegister()) {
1603 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1604 } else if (const_area != nullptr) {
1605 DCHECK(const_area->IsEmittedAtUseSite());
1606 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1607 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001608 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1609 const_area->GetBaseMethodAddress(),
1610 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001611 } else {
1612 DCHECK(rhs.IsDoubleStackSlot());
1613 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1614 }
1615 } else {
1616 if (rhs.IsFpuRegister()) {
1617 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1618 } else if (const_area != nullptr) {
1619 DCHECK(const_area->IsEmittedAtUseSite());
1620 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1621 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001622 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1623 const_area->GetBaseMethodAddress(),
1624 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001625 } else {
1626 DCHECK(rhs.IsStackSlot());
1627 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1628 }
1629 }
1630}
1631
Mark Mendell152408f2015-12-31 12:28:50 -05001632template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001633void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001634 LabelType* true_target_in,
1635 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001636 // Generated branching requires both targets to be explicit. If either of the
1637 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001638 LabelType fallthrough_target;
1639 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1640 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001641
Mark Mendellc4701932015-04-10 13:18:51 -04001642 LocationSummary* locations = condition->GetLocations();
1643 Location left = locations->InAt(0);
1644 Location right = locations->InAt(1);
1645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001646 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001647 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001648 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001649 GenerateLongComparesAndJumps(condition, true_target, false_target);
1650 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001651 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001652 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001653 GenerateFPJumps(condition, true_target, false_target);
1654 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001656 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001657 GenerateFPJumps(condition, true_target, false_target);
1658 break;
1659 default:
1660 LOG(FATAL) << "Unexpected compare type " << type;
1661 }
1662
David Brazdil0debae72015-11-12 18:37:00 +00001663 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001664 __ jmp(false_target);
1665 }
David Brazdil0debae72015-11-12 18:37:00 +00001666
1667 if (fallthrough_target.IsLinked()) {
1668 __ Bind(&fallthrough_target);
1669 }
Mark Mendellc4701932015-04-10 13:18:51 -04001670}
1671
David Brazdil0debae72015-11-12 18:37:00 +00001672static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1673 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1674 // are set only strictly before `branch`. We can't use the eflags on long/FP
1675 // conditions if they are materialized due to the complex branching.
1676 return cond->IsCondition() &&
1677 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1679 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001680}
1681
Mark Mendell152408f2015-12-31 12:28:50 -05001682template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001683void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001684 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001685 LabelType* true_target,
1686 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001687 HInstruction* cond = instruction->InputAt(condition_input_index);
1688
1689 if (true_target == nullptr && false_target == nullptr) {
1690 // Nothing to do. The code always falls through.
1691 return;
1692 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001693 // Constant condition, statically compared against "true" (integer value 1).
1694 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (true_target != nullptr) {
1696 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001697 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001698 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001699 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001700 if (false_target != nullptr) {
1701 __ jmp(false_target);
1702 }
1703 }
1704 return;
1705 }
1706
1707 // The following code generates these patterns:
1708 // (1) true_target == nullptr && false_target != nullptr
1709 // - opposite condition true => branch to false_target
1710 // (2) true_target != nullptr && false_target == nullptr
1711 // - condition true => branch to true_target
1712 // (3) true_target != nullptr && false_target != nullptr
1713 // - condition true => branch to true_target
1714 // - branch to false_target
1715 if (IsBooleanValueOrMaterializedCondition(cond)) {
1716 if (AreEflagsSetFrom(cond, instruction)) {
1717 if (true_target == nullptr) {
1718 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1719 } else {
1720 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1721 }
1722 } else {
1723 // Materialized condition, compare against 0.
1724 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1725 if (lhs.IsRegister()) {
1726 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1727 } else {
1728 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1729 }
1730 if (true_target == nullptr) {
1731 __ j(kEqual, false_target);
1732 } else {
1733 __ j(kNotEqual, true_target);
1734 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001735 }
1736 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001737 // Condition has not been materialized, use its inputs as the comparison and
1738 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001739 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001740
1741 // If this is a long or FP comparison that has been folded into
1742 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001743 DataType::Type type = condition->InputAt(0)->GetType();
1744 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001745 GenerateCompareTestAndBranch(condition, true_target, false_target);
1746 return;
1747 }
1748
1749 Location lhs = condition->GetLocations()->InAt(0);
1750 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001752 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001753 if (true_target == nullptr) {
1754 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1755 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001756 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001757 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001758 }
David Brazdil0debae72015-11-12 18:37:00 +00001759
1760 // If neither branch falls through (case 3), the conditional branch to `true_target`
1761 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1762 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001763 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001764 }
1765}
1766
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001767void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001768 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001769 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001770 locations->SetInAt(0, Location::Any());
1771 }
1772}
1773
1774void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001775 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1776 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1777 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1778 nullptr : codegen_->GetLabelOf(true_successor);
1779 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1780 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001781 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001782}
1783
1784void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001785 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001786 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001787 InvokeRuntimeCallingConvention calling_convention;
1788 RegisterSet caller_saves = RegisterSet::Empty();
1789 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1790 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001791 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001792 locations->SetInAt(0, Location::Any());
1793 }
1794}
1795
1796void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001797 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001798 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001799 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001800 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001801 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001802}
1803
Mingyao Yang063fc772016-08-02 11:02:54 -07001804void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001805 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001806 LocationSummary(flag, LocationSummary::kNoCall);
1807 locations->SetOut(Location::RequiresRegister());
1808}
1809
1810void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1811 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1812 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1813}
1814
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001815static bool SelectCanUseCMOV(HSelect* select) {
1816 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001818 return false;
1819 }
1820
1821 // A FP condition doesn't generate the single CC that we need.
1822 // In 32 bit mode, a long condition doesn't generate a single CC either.
1823 HInstruction* condition = select->GetCondition();
1824 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 DataType::Type compare_type = condition->InputAt(0)->GetType();
1826 if (compare_type == DataType::Type::kInt64 ||
1827 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001828 return false;
1829 }
1830 }
1831
1832 // We can generate a CMOV for this Select.
1833 return true;
1834}
1835
David Brazdil74eb1b22015-12-14 11:44:01 +00001836void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001839 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001840 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001841 } else {
1842 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001843 if (SelectCanUseCMOV(select)) {
1844 if (select->InputAt(1)->IsConstant()) {
1845 // Cmov can't handle a constant value.
1846 locations->SetInAt(1, Location::RequiresRegister());
1847 } else {
1848 locations->SetInAt(1, Location::Any());
1849 }
1850 } else {
1851 locations->SetInAt(1, Location::Any());
1852 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001853 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001854 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1855 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001856 }
1857 locations->SetOut(Location::SameAsFirstInput());
1858}
1859
1860void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1861 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001862 DCHECK(locations->InAt(0).Equals(locations->Out()));
1863 if (SelectCanUseCMOV(select)) {
1864 // If both the condition and the source types are integer, we can generate
1865 // a CMOV to implement Select.
1866
1867 HInstruction* select_condition = select->GetCondition();
1868 Condition cond = kNotEqual;
1869
1870 // Figure out how to test the 'condition'.
1871 if (select_condition->IsCondition()) {
1872 HCondition* condition = select_condition->AsCondition();
1873 if (!condition->IsEmittedAtUseSite()) {
1874 // This was a previously materialized condition.
1875 // Can we use the existing condition code?
1876 if (AreEflagsSetFrom(condition, select)) {
1877 // Materialization was the previous instruction. Condition codes are right.
1878 cond = X86Condition(condition->GetCondition());
1879 } else {
1880 // No, we have to recreate the condition code.
1881 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1882 __ testl(cond_reg, cond_reg);
1883 }
1884 } else {
1885 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1887 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001888 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001889 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001890 cond = X86Condition(condition->GetCondition());
1891 }
1892 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001893 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001894 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1895 __ testl(cond_reg, cond_reg);
1896 }
1897
1898 // If the condition is true, overwrite the output, which already contains false.
1899 Location false_loc = locations->InAt(0);
1900 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001902 // 64 bit conditional move.
1903 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1904 Register false_low = false_loc.AsRegisterPairLow<Register>();
1905 if (true_loc.IsRegisterPair()) {
1906 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1907 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1908 } else {
1909 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1910 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1911 }
1912 } else {
1913 // 32 bit conditional move.
1914 Register false_reg = false_loc.AsRegister<Register>();
1915 if (true_loc.IsRegister()) {
1916 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1917 } else {
1918 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1919 }
1920 }
1921 } else {
1922 NearLabel false_target;
1923 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001924 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001925 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1926 __ Bind(&false_target);
1927 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001928}
1929
David Srbecky0cf44932015-12-09 14:09:59 +00001930void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001931 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001932}
1933
David Srbeckyd28f4a02016-03-14 17:14:24 +00001934void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1935 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001936}
1937
1938void CodeGeneratorX86::GenerateNop() {
1939 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001940}
1941
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001943 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001944 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001945 // Handle the long/FP comparisons made in instruction simplification.
1946 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001948 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001949 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001950 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001951 locations->SetOut(Location::RequiresRegister());
1952 }
1953 break;
1954 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001955 case DataType::Type::kFloat32:
1956 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001957 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001958 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1959 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1960 } else if (cond->InputAt(1)->IsConstant()) {
1961 locations->SetInAt(1, Location::RequiresFpuRegister());
1962 } else {
1963 locations->SetInAt(1, Location::Any());
1964 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001965 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001966 locations->SetOut(Location::RequiresRegister());
1967 }
1968 break;
1969 }
1970 default:
1971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001973 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001974 // We need a byte register.
1975 locations->SetOut(Location::RegisterLocation(ECX));
1976 }
1977 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001978 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979}
1980
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001982 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001983 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001984 }
Mark Mendellc4701932015-04-10 13:18:51 -04001985
1986 LocationSummary* locations = cond->GetLocations();
1987 Location lhs = locations->InAt(0);
1988 Location rhs = locations->InAt(1);
1989 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001990 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001991
1992 switch (cond->InputAt(0)->GetType()) {
1993 default: {
1994 // Integer case.
1995
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001996 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001997 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001998 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001999 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002000 return;
2001 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002003 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2004 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002006 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002007 GenerateFPJumps(cond, &true_label, &false_label);
2008 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002010 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002011 GenerateFPJumps(cond, &true_label, &false_label);
2012 break;
2013 }
2014
2015 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002016 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002017
Roland Levillain4fa13f62015-07-06 18:11:54 +01002018 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002019 __ Bind(&false_label);
2020 __ xorl(reg, reg);
2021 __ jmp(&done_label);
2022
Roland Levillain4fa13f62015-07-06 18:11:54 +01002023 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002024 __ Bind(&true_label);
2025 __ movl(reg, Immediate(1));
2026 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002027}
2028
2029void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002030 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002031}
2032
2033void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002035}
2036
2037void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002039}
2040
2041void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002042 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002043}
2044
2045void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002047}
2048
2049void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002051}
2052
2053void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002054 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002055}
2056
2057void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002058 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002059}
2060
2061void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002062 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002063}
2064
2065void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002066 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002067}
2068
2069void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002070 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002071}
2072
2073void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002074 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002075}
2076
Aart Bike9f37602015-10-09 11:15:55 -07002077void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002078 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002079}
2080
2081void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002082 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002083}
2084
2085void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002086 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002087}
2088
2089void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002090 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002091}
2092
2093void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002094 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002095}
2096
2097void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002098 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002099}
2100
2101void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002102 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002103}
2104
2105void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002107}
2108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002110 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002111 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002112 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113}
2114
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002115void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002116 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117}
2118
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002119void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002121 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002122 locations->SetOut(Location::ConstantLocation(constant));
2123}
2124
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002125void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002126 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002127}
2128
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002130 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002131 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002132 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133}
2134
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002135void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002136 // Will be generated at use site.
2137}
2138
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002139void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2140 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002141 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002142 locations->SetOut(Location::ConstantLocation(constant));
2143}
2144
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002145void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002146 // Will be generated at use site.
2147}
2148
2149void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2150 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002151 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002152 locations->SetOut(Location::ConstantLocation(constant));
2153}
2154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002155void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002156 // Will be generated at use site.
2157}
2158
Igor Murashkind01745e2017-04-05 16:40:31 -07002159void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2160 constructor_fence->SetLocations(nullptr);
2161}
2162
2163void InstructionCodeGeneratorX86::VisitConstructorFence(
2164 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2165 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2166}
2167
Calin Juravle27df7582015-04-17 19:12:31 +01002168void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2169 memory_barrier->SetLocations(nullptr);
2170}
2171
2172void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002173 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002174}
2175
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002176void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002177 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178}
2179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002180void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002181 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002182}
2183
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002184void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002186 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002187 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002188 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002190 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002191 case DataType::Type::kInt8:
2192 case DataType::Type::kUint16:
2193 case DataType::Type::kInt16:
2194 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002195 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002196 break;
2197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002199 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002200 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201 break;
2202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 case DataType::Type::kFloat32:
2204 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002205 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002207 break;
2208
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002210 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002211 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002212}
2213
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002214void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002215 if (kIsDebugBuild) {
2216 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002217 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002219 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 case DataType::Type::kInt8:
2221 case DataType::Type::kUint16:
2222 case DataType::Type::kInt16:
2223 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002224 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002225 break;
2226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002228 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2229 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002230 break;
2231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 case DataType::Type::kFloat32:
2233 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 break;
2236
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002237 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002239 }
2240 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002242}
2243
Calin Juravle175dc732015-08-25 15:42:32 +01002244void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2245 // The trampoline uses the same calling convention as dex calling conventions,
2246 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2247 // the method_idx.
2248 HandleInvoke(invoke);
2249}
2250
2251void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2252 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2253}
2254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002255void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002256 // Explicit clinit checks triggered by static invokes must have been pruned by
2257 // art::PrepareForRegisterAllocation.
2258 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002259
Mark Mendellfb8d2792015-03-31 22:16:59 -04002260 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002261 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002262 if (invoke->GetLocations()->CanCall() &&
2263 invoke->HasPcRelativeMethodLoadKind() &&
2264 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002265 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002266 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002267 return;
2268 }
2269
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002270 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002271
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002272 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002273 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002274 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002275 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276}
2277
Mark Mendell09ed1a32015-03-25 08:30:06 -04002278static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2279 if (invoke->GetLocations()->Intrinsified()) {
2280 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2281 intrinsic.Dispatch(invoke);
2282 return true;
2283 }
2284 return false;
2285}
2286
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002287void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002288 // Explicit clinit checks triggered by static invokes must have been pruned by
2289 // art::PrepareForRegisterAllocation.
2290 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002291
Mark Mendell09ed1a32015-03-25 08:30:06 -04002292 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2293 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002294 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002295
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002296 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002297 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002298 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002299}
2300
2301void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002302 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2303 if (intrinsic.TryDispatch(invoke)) {
2304 return;
2305 }
2306
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002307 HandleInvoke(invoke);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002308 if (codegen_->GetCompilerOptions().IsBaseline() && !Runtime::Current()->IsAotCompiler()) {
2309 // Add one temporary for inline cache update.
2310 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2311 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002312}
2313
2314void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002315 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002316 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002317}
2318
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002319void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002320 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2321 return;
2322 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002323
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002324 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002325 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002326}
2327
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002328void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002329 // This call to HandleInvoke allocates a temporary (core) register
2330 // which is also used to transfer the hidden argument from FP to
2331 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002332 HandleInvoke(invoke);
2333 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002334 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002335
2336 if (codegen_->GetCompilerOptions().IsBaseline() && !Runtime::Current()->IsAotCompiler()) {
2337 // Add one temporary for inline cache update.
2338 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2339 }
2340}
2341
2342void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2343 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002344 // We know the destination of an intrinsic, so no need to record inline
2345 // caches (also the intrinsic location builder doesn't request an additional
2346 // temporary).
2347 if (!instruction->GetLocations()->Intrinsified() &&
2348 GetCompilerOptions().IsBaseline() &&
2349 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002350 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2351 ScopedObjectAccess soa(Thread::Current());
2352 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
2353 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2354 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2355 if (kIsDebugBuild) {
2356 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2357 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2358 }
2359 Register temp = EBP;
2360 NearLabel done;
2361 __ movl(temp, Immediate(address));
2362 // Fast path for a monomorphic cache.
2363 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2364 __ j(kEqual, &done);
2365 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2366 __ Bind(&done);
2367 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002368}
2369
2370void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2371 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002372 LocationSummary* locations = invoke->GetLocations();
2373 Register temp = locations->GetTemp(0).AsRegister<Register>();
2374 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002375 Location receiver = locations->InAt(0);
2376 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2377
Roland Levillain0d5a2812015-11-13 10:07:31 +00002378 // Set the hidden argument. This is safe to do this here, as XMM7
2379 // won't be modified thereafter, before the `call` instruction.
2380 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002381 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002382 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002383
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002384 if (receiver.IsStackSlot()) {
2385 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002386 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002387 __ movl(temp, Address(temp, class_offset));
2388 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002389 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002391 }
Roland Levillain4d027112015-07-01 15:41:14 +01002392 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002393 // Instead of simply (possibly) unpoisoning `temp` here, we should
2394 // emit a read barrier for the previous class reference load.
2395 // However this is not required in practice, as this is an
2396 // intermediate/temporary reference and because the current
2397 // concurrent copying collector keeps the from-space memory
2398 // intact/accessible until the end of the marking phase (the
2399 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002400 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002401
2402 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2403
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002404 // temp = temp->GetAddressOfIMT()
2405 __ movl(temp,
2406 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002407 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002408 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002409 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002410 __ movl(temp, Address(temp, method_offset));
2411 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002412 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002413 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002414
2415 DCHECK(!codegen_->IsLeafMethod());
2416 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2417}
2418
Orion Hodsonac141392017-01-13 11:53:47 +00002419void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2420 HandleInvoke(invoke);
2421}
2422
2423void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2424 codegen_->GenerateInvokePolymorphicCall(invoke);
2425}
2426
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002427void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2428 HandleInvoke(invoke);
2429}
2430
2431void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2432 codegen_->GenerateInvokeCustomCall(invoke);
2433}
2434
Roland Levillain88cb1752014-10-20 16:36:47 +01002435void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2436 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002437 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002438 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kInt32:
2440 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002441 locations->SetInAt(0, Location::RequiresRegister());
2442 locations->SetOut(Location::SameAsFirstInput());
2443 break;
2444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002445 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002446 locations->SetInAt(0, Location::RequiresFpuRegister());
2447 locations->SetOut(Location::SameAsFirstInput());
2448 locations->AddTemp(Location::RequiresRegister());
2449 locations->AddTemp(Location::RequiresFpuRegister());
2450 break;
2451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002453 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002454 locations->SetOut(Location::SameAsFirstInput());
2455 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002456 break;
2457
2458 default:
2459 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2460 }
2461}
2462
2463void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2464 LocationSummary* locations = neg->GetLocations();
2465 Location out = locations->Out();
2466 Location in = locations->InAt(0);
2467 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002469 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002470 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002471 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002472 break;
2473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002475 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002476 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002477 __ negl(out.AsRegisterPairLow<Register>());
2478 // Negation is similar to subtraction from zero. The least
2479 // significant byte triggers a borrow when it is different from
2480 // zero; to take it into account, add 1 to the most significant
2481 // byte if the carry flag (CF) is set to 1 after the first NEGL
2482 // operation.
2483 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2484 __ negl(out.AsRegisterPairHigh<Register>());
2485 break;
2486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002488 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 Register constant = locations->GetTemp(0).AsRegister<Register>();
2490 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002491 // Implement float negation with an exclusive or with value
2492 // 0x80000000 (mask for bit 31, representing the sign of a
2493 // single-precision floating-point number).
2494 __ movl(constant, Immediate(INT32_C(0x80000000)));
2495 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002497 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002498 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002499
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002501 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002502 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002503 // Implement double negation with an exclusive or with value
2504 // 0x8000000000000000 (mask for bit 63, representing the sign of
2505 // a double-precision floating-point number).
2506 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002508 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002509 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002510
2511 default:
2512 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2513 }
2514}
2515
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002516void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2517 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002518 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002520 locations->SetInAt(0, Location::RequiresFpuRegister());
2521 locations->SetInAt(1, Location::RequiresRegister());
2522 locations->SetOut(Location::SameAsFirstInput());
2523 locations->AddTemp(Location::RequiresFpuRegister());
2524}
2525
2526void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2527 LocationSummary* locations = neg->GetLocations();
2528 Location out = locations->Out();
2529 DCHECK(locations->InAt(0).Equals(out));
2530
2531 Register constant_area = locations->InAt(1).AsRegister<Register>();
2532 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002534 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2535 neg->GetBaseMethodAddress(),
2536 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002537 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2538 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002539 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2540 neg->GetBaseMethodAddress(),
2541 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002542 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2543 }
2544}
2545
Roland Levillaindff1f282014-11-05 14:15:05 +00002546void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 DataType::Type result_type = conversion->GetResultType();
2548 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002549 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2550 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002551
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002552 // The float-to-long and double-to-long type conversions rely on a
2553 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002554 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2556 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002557 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002558 : LocationSummary::kNoCall;
2559 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002560 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002561
Roland Levillaindff1f282014-11-05 14:15:05 +00002562 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002563 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002565 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002566 case DataType::Type::kUint8:
2567 case DataType::Type::kInt8:
2568 case DataType::Type::kUint16:
2569 case DataType::Type::kInt16:
2570 case DataType::Type::kInt32:
2571 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2572 // Make the output overlap to please the register allocator. This greatly simplifies
2573 // the validation of the linear scan implementation
2574 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2575 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002577 HInstruction* input = conversion->InputAt(0);
2578 Location input_location = input->IsConstant()
2579 ? Location::ConstantLocation(input->AsConstant())
2580 : Location::RegisterPairLocation(EAX, EDX);
2581 locations->SetInAt(0, input_location);
2582 // Make the output overlap to please the register allocator. This greatly simplifies
2583 // the validation of the linear scan implementation
2584 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2585 break;
2586 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002587
2588 default:
2589 LOG(FATAL) << "Unexpected type conversion from " << input_type
2590 << " to " << result_type;
2591 }
2592 break;
2593
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002594 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002596 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2597 locations->SetInAt(0, Location::Any());
2598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002599 break;
2600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002602 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002604 locations->SetInAt(0, Location::Any());
2605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002608 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002609 locations->SetInAt(0, Location::RequiresFpuRegister());
2610 locations->SetOut(Location::RequiresRegister());
2611 locations->AddTemp(Location::RequiresFpuRegister());
2612 break;
2613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002615 locations->SetInAt(0, Location::RequiresFpuRegister());
2616 locations->SetOut(Location::RequiresRegister());
2617 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 }
2624 break;
2625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002626 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002627 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002629 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002631 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 case DataType::Type::kInt16:
2633 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002634 locations->SetInAt(0, Location::RegisterLocation(EAX));
2635 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2636 break;
2637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002638 case DataType::Type::kFloat32:
2639 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002640 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002641 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2642 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2643
Vladimir Marko949c91f2015-01-27 10:48:44 +00002644 // The runtime helper puts the result in EAX, EDX.
2645 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002646 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002647 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002648
2649 default:
2650 LOG(FATAL) << "Unexpected type conversion from " << input_type
2651 << " to " << result_type;
2652 }
2653 break;
2654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002656 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002658 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002659 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 case DataType::Type::kInt16:
2662 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002663 locations->SetInAt(0, Location::RequiresRegister());
2664 locations->SetOut(Location::RequiresFpuRegister());
2665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002668 locations->SetInAt(0, Location::Any());
2669 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002670 break;
2671
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002673 locations->SetInAt(0, Location::RequiresFpuRegister());
2674 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002675 break;
2676
2677 default:
2678 LOG(FATAL) << "Unexpected type conversion from " << input_type
2679 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002680 }
Roland Levillaincff13742014-11-17 14:32:17 +00002681 break;
2682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002684 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002686 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002688 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kInt16:
2690 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002691 locations->SetInAt(0, Location::RequiresRegister());
2692 locations->SetOut(Location::RequiresFpuRegister());
2693 break;
2694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002696 locations->SetInAt(0, Location::Any());
2697 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002698 break;
2699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002701 locations->SetInAt(0, Location::RequiresFpuRegister());
2702 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 }
2715}
2716
2717void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2718 LocationSummary* locations = conversion->GetLocations();
2719 Location out = locations->Out();
2720 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 DataType::Type result_type = conversion->GetResultType();
2722 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002723 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2724 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002725 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002726 case DataType::Type::kUint8:
2727 switch (input_type) {
2728 case DataType::Type::kInt8:
2729 case DataType::Type::kUint16:
2730 case DataType::Type::kInt16:
2731 case DataType::Type::kInt32:
2732 if (in.IsRegister()) {
2733 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2734 } else {
2735 DCHECK(in.GetConstant()->IsIntConstant());
2736 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2737 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2738 }
2739 break;
2740 case DataType::Type::kInt64:
2741 if (in.IsRegisterPair()) {
2742 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2743 } else {
2744 DCHECK(in.GetConstant()->IsLongConstant());
2745 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2746 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2747 }
2748 break;
2749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
2753 }
2754 break;
2755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002757 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002758 case DataType::Type::kUint8:
2759 case DataType::Type::kUint16:
2760 case DataType::Type::kInt16:
2761 case DataType::Type::kInt32:
2762 if (in.IsRegister()) {
2763 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2764 } else {
2765 DCHECK(in.GetConstant()->IsIntConstant());
2766 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2767 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2768 }
2769 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002770 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002771 if (in.IsRegisterPair()) {
2772 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2773 } else {
2774 DCHECK(in.GetConstant()->IsLongConstant());
2775 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2776 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2777 }
2778 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002779
2780 default:
2781 LOG(FATAL) << "Unexpected type conversion from " << input_type
2782 << " to " << result_type;
2783 }
2784 break;
2785
2786 case DataType::Type::kUint16:
2787 switch (input_type) {
2788 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 case DataType::Type::kInt16:
2790 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002791 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002792 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2793 } else if (in.IsStackSlot()) {
2794 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002795 } else {
2796 DCHECK(in.GetConstant()->IsIntConstant());
2797 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002798 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2799 }
2800 break;
2801 case DataType::Type::kInt64:
2802 if (in.IsRegisterPair()) {
2803 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2804 } else if (in.IsDoubleStackSlot()) {
2805 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2806 } else {
2807 DCHECK(in.GetConstant()->IsLongConstant());
2808 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2809 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002810 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002811 break;
2812
2813 default:
2814 LOG(FATAL) << "Unexpected type conversion from " << input_type
2815 << " to " << result_type;
2816 }
2817 break;
2818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002820 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002822 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002823 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002824 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002825 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002826 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002827 } else {
2828 DCHECK(in.GetConstant()->IsIntConstant());
2829 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002831 }
2832 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002833 case DataType::Type::kInt64:
2834 if (in.IsRegisterPair()) {
2835 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2836 } else if (in.IsDoubleStackSlot()) {
2837 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2838 } else {
2839 DCHECK(in.GetConstant()->IsLongConstant());
2840 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2841 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2842 }
2843 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002844
2845 default:
2846 LOG(FATAL) << "Unexpected type conversion from " << input_type
2847 << " to " << result_type;
2848 }
2849 break;
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002852 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002854 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002856 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002857 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002858 } else {
2859 DCHECK(in.IsConstant());
2860 DCHECK(in.GetConstant()->IsLongConstant());
2861 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002862 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002863 }
2864 break;
2865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002867 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2868 Register output = out.AsRegister<Register>();
2869 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002870 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002871
2872 __ movl(output, Immediate(kPrimIntMax));
2873 // temp = int-to-float(output)
2874 __ cvtsi2ss(temp, output);
2875 // if input >= temp goto done
2876 __ comiss(input, temp);
2877 __ j(kAboveEqual, &done);
2878 // if input == NaN goto nan
2879 __ j(kUnordered, &nan);
2880 // output = float-to-int-truncate(input)
2881 __ cvttss2si(output, input);
2882 __ jmp(&done);
2883 __ Bind(&nan);
2884 // output = 0
2885 __ xorl(output, output);
2886 __ Bind(&done);
2887 break;
2888 }
2889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002891 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2892 Register output = out.AsRegister<Register>();
2893 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002894 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002895
2896 __ movl(output, Immediate(kPrimIntMax));
2897 // temp = int-to-double(output)
2898 __ cvtsi2sd(temp, output);
2899 // if input >= temp goto done
2900 __ comisd(input, temp);
2901 __ j(kAboveEqual, &done);
2902 // if input == NaN goto nan
2903 __ j(kUnordered, &nan);
2904 // output = double-to-int-truncate(input)
2905 __ cvttsd2si(output, input);
2906 __ jmp(&done);
2907 __ Bind(&nan);
2908 // output = 0
2909 __ xorl(output, output);
2910 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002911 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002912 }
Roland Levillain946e1432014-11-11 17:35:19 +00002913
2914 default:
2915 LOG(FATAL) << "Unexpected type conversion from " << input_type
2916 << " to " << result_type;
2917 }
2918 break;
2919
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002921 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002923 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002925 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kInt16:
2927 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002928 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2929 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002931 __ cdq();
2932 break;
2933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002935 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002936 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002937 break;
2938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002940 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002941 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002942 break;
2943
2944 default:
2945 LOG(FATAL) << "Unexpected type conversion from " << input_type
2946 << " to " << result_type;
2947 }
2948 break;
2949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002951 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002953 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002955 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kInt16:
2957 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002958 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002959 break;
2960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002962 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002963
Roland Levillain232ade02015-04-20 15:14:36 +01002964 // Create stack space for the call to
2965 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2966 // TODO: enhance register allocator to ask for stack temporaries.
2967 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002969 __ subl(ESP, Immediate(adjustment));
2970 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002971
Roland Levillain232ade02015-04-20 15:14:36 +01002972 // Load the value to the FP stack, using temporaries if needed.
2973 PushOntoFPStack(in, 0, adjustment, false, true);
2974
2975 if (out.IsStackSlot()) {
2976 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2977 } else {
2978 __ fstps(Address(ESP, 0));
2979 Location stack_temp = Location::StackSlot(0);
2980 codegen_->Move32(out, stack_temp);
2981 }
2982
2983 // Remove the temporary stack space we allocated.
2984 if (adjustment != 0) {
2985 __ addl(ESP, Immediate(adjustment));
2986 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002987 break;
2988 }
2989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002991 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002992 break;
2993
2994 default:
2995 LOG(FATAL) << "Unexpected type conversion from " << input_type
2996 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002997 }
Roland Levillaincff13742014-11-17 14:32:17 +00002998 break;
2999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003001 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003003 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003005 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 case DataType::Type::kInt16:
3007 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003008 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003009 break;
3010
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003011 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003012 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003013
Roland Levillain232ade02015-04-20 15:14:36 +01003014 // Create stack space for the call to
3015 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3016 // TODO: enhance register allocator to ask for stack temporaries.
3017 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01003019 __ subl(ESP, Immediate(adjustment));
3020 }
3021
3022 // Load the value to the FP stack, using temporaries if needed.
3023 PushOntoFPStack(in, 0, adjustment, false, true);
3024
3025 if (out.IsDoubleStackSlot()) {
3026 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3027 } else {
3028 __ fstpl(Address(ESP, 0));
3029 Location stack_temp = Location::DoubleStackSlot(0);
3030 codegen_->Move64(out, stack_temp);
3031 }
3032
3033 // Remove the temporary stack space we allocated.
3034 if (adjustment != 0) {
3035 __ addl(ESP, Immediate(adjustment));
3036 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003037 break;
3038 }
3039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003041 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003042 break;
3043
3044 default:
3045 LOG(FATAL) << "Unexpected type conversion from " << input_type
3046 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003047 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003048 break;
3049
3050 default:
3051 LOG(FATAL) << "Unexpected type conversion from " << input_type
3052 << " to " << result_type;
3053 }
3054}
3055
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003056void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003057 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003058 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003059 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003061 locations->SetInAt(0, Location::RequiresRegister());
3062 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3064 break;
3065 }
3066
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003068 locations->SetInAt(0, Location::RequiresRegister());
3069 locations->SetInAt(1, Location::Any());
3070 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003071 break;
3072 }
3073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kFloat32:
3075 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003076 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003077 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3078 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003079 } else if (add->InputAt(1)->IsConstant()) {
3080 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003081 } else {
3082 locations->SetInAt(1, Location::Any());
3083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003084 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003085 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003086 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003087
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003088 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003089 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003090 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003091 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003092}
3093
3094void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3095 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003096 Location first = locations->InAt(0);
3097 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003098 Location out = locations->Out();
3099
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003100 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003102 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003103 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3104 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003105 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3106 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003107 } else {
3108 __ leal(out.AsRegister<Register>(), Address(
3109 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3110 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003111 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003112 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3113 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3114 __ addl(out.AsRegister<Register>(), Immediate(value));
3115 } else {
3116 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3117 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003118 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003119 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003121 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003122 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003126 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003127 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3128 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003129 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003130 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3131 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003132 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003133 } else {
3134 DCHECK(second.IsConstant()) << second;
3135 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3136 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3137 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003138 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003139 break;
3140 }
3141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003142 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003143 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003145 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3146 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003147 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 __ addss(first.AsFpuRegister<XmmRegister>(),
3149 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003150 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3151 const_area->GetBaseMethodAddress(),
3152 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003153 } else {
3154 DCHECK(second.IsStackSlot());
3155 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003156 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003157 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003158 }
3159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003161 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003162 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003163 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3164 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003165 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003166 __ addsd(first.AsFpuRegister<XmmRegister>(),
3167 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003168 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3169 const_area->GetBaseMethodAddress(),
3170 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003171 } else {
3172 DCHECK(second.IsDoubleStackSlot());
3173 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003174 }
3175 break;
3176 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003177
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003178 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003179 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003180 }
3181}
3182
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003183void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003184 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003185 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003186 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003187 case DataType::Type::kInt32:
3188 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003189 locations->SetInAt(0, Location::RequiresRegister());
3190 locations->SetInAt(1, Location::Any());
3191 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003192 break;
3193 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003194 case DataType::Type::kFloat32:
3195 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003196 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003197 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3198 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003199 } else if (sub->InputAt(1)->IsConstant()) {
3200 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003201 } else {
3202 locations->SetInAt(1, Location::Any());
3203 }
Calin Juravle11351682014-10-23 15:38:15 +01003204 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003205 break;
Calin Juravle11351682014-10-23 15:38:15 +01003206 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003207
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003208 default:
Calin Juravle11351682014-10-23 15:38:15 +01003209 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003210 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003211}
3212
3213void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3214 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003215 Location first = locations->InAt(0);
3216 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003217 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003218 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003219 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003220 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003222 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003223 __ subl(first.AsRegister<Register>(),
3224 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003225 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003227 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003228 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003229 }
3230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003232 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003233 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3234 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003235 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003236 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003237 __ sbbl(first.AsRegisterPairHigh<Register>(),
3238 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003239 } else {
3240 DCHECK(second.IsConstant()) << second;
3241 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3242 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3243 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003244 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003245 break;
3246 }
3247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003248 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003249 if (second.IsFpuRegister()) {
3250 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3251 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3252 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003253 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003254 __ subss(first.AsFpuRegister<XmmRegister>(),
3255 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003256 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3257 const_area->GetBaseMethodAddress(),
3258 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003259 } else {
3260 DCHECK(second.IsStackSlot());
3261 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3262 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003263 break;
Calin Juravle11351682014-10-23 15:38:15 +01003264 }
3265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003266 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003267 if (second.IsFpuRegister()) {
3268 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3269 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3270 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003271 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003272 __ subsd(first.AsFpuRegister<XmmRegister>(),
3273 codegen_->LiteralDoubleAddress(
3274 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003275 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003276 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3277 } else {
3278 DCHECK(second.IsDoubleStackSlot());
3279 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3280 }
Calin Juravle11351682014-10-23 15:38:15 +01003281 break;
3282 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003283
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003284 default:
Calin Juravle11351682014-10-23 15:38:15 +01003285 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003286 }
3287}
3288
Calin Juravle34bacdf2014-10-07 20:23:36 +01003289void LocationsBuilderX86::VisitMul(HMul* mul) {
3290 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003291 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003292 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003294 locations->SetInAt(0, Location::RequiresRegister());
3295 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003296 if (mul->InputAt(1)->IsIntConstant()) {
3297 // Can use 3 operand multiply.
3298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3299 } else {
3300 locations->SetOut(Location::SameAsFirstInput());
3301 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003302 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003303 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003304 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003305 locations->SetInAt(1, Location::Any());
3306 locations->SetOut(Location::SameAsFirstInput());
3307 // Needed for imul on 32bits with 64bits output.
3308 locations->AddTemp(Location::RegisterLocation(EAX));
3309 locations->AddTemp(Location::RegisterLocation(EDX));
3310 break;
3311 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003312 case DataType::Type::kFloat32:
3313 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003314 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003315 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3316 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003317 } else if (mul->InputAt(1)->IsConstant()) {
3318 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003319 } else {
3320 locations->SetInAt(1, Location::Any());
3321 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003322 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003323 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003324 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003325
3326 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003327 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003328 }
3329}
3330
3331void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3332 LocationSummary* locations = mul->GetLocations();
3333 Location first = locations->InAt(0);
3334 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003335 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003336
3337 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003339 // The constant may have ended up in a register, so test explicitly to avoid
3340 // problems where the output may not be the same as the first operand.
3341 if (mul->InputAt(1)->IsIntConstant()) {
3342 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3343 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3344 } else if (second.IsRegister()) {
3345 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003347 } else {
3348 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003349 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003351 }
3352 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003354 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355 Register in1_hi = first.AsRegisterPairHigh<Register>();
3356 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 Register eax = locations->GetTemp(0).AsRegister<Register>();
3358 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003359
3360 DCHECK_EQ(EAX, eax);
3361 DCHECK_EQ(EDX, edx);
3362
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003363 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003364 // output: in1
3365 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3366 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3367 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003368 if (second.IsConstant()) {
3369 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003370
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003371 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3372 int32_t low_value = Low32Bits(value);
3373 int32_t high_value = High32Bits(value);
3374 Immediate low(low_value);
3375 Immediate high(high_value);
3376
3377 __ movl(eax, high);
3378 // eax <- in1.lo * in2.hi
3379 __ imull(eax, in1_lo);
3380 // in1.hi <- in1.hi * in2.lo
3381 __ imull(in1_hi, low);
3382 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3383 __ addl(in1_hi, eax);
3384 // move in2_lo to eax to prepare for double precision
3385 __ movl(eax, low);
3386 // edx:eax <- in1.lo * in2.lo
3387 __ mull(in1_lo);
3388 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3389 __ addl(in1_hi, edx);
3390 // in1.lo <- (in1.lo * in2.lo)[31:0];
3391 __ movl(in1_lo, eax);
3392 } else if (second.IsRegisterPair()) {
3393 Register in2_hi = second.AsRegisterPairHigh<Register>();
3394 Register in2_lo = second.AsRegisterPairLow<Register>();
3395
3396 __ movl(eax, in2_hi);
3397 // eax <- in1.lo * in2.hi
3398 __ imull(eax, in1_lo);
3399 // in1.hi <- in1.hi * in2.lo
3400 __ imull(in1_hi, in2_lo);
3401 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3402 __ addl(in1_hi, eax);
3403 // move in1_lo to eax to prepare for double precision
3404 __ movl(eax, in1_lo);
3405 // edx:eax <- in1.lo * in2.lo
3406 __ mull(in2_lo);
3407 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3408 __ addl(in1_hi, edx);
3409 // in1.lo <- (in1.lo * in2.lo)[31:0];
3410 __ movl(in1_lo, eax);
3411 } else {
3412 DCHECK(second.IsDoubleStackSlot()) << second;
3413 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3414 Address in2_lo(ESP, second.GetStackIndex());
3415
3416 __ movl(eax, in2_hi);
3417 // eax <- in1.lo * in2.hi
3418 __ imull(eax, in1_lo);
3419 // in1.hi <- in1.hi * in2.lo
3420 __ imull(in1_hi, in2_lo);
3421 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3422 __ addl(in1_hi, eax);
3423 // move in1_lo to eax to prepare for double precision
3424 __ movl(eax, in1_lo);
3425 // edx:eax <- in1.lo * in2.lo
3426 __ mull(in2_lo);
3427 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3428 __ addl(in1_hi, edx);
3429 // in1.lo <- (in1.lo * in2.lo)[31:0];
3430 __ movl(in1_lo, eax);
3431 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003432
3433 break;
3434 }
3435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003436 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003437 DCHECK(first.Equals(locations->Out()));
3438 if (second.IsFpuRegister()) {
3439 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3440 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3441 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003442 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003443 __ mulss(first.AsFpuRegister<XmmRegister>(),
3444 codegen_->LiteralFloatAddress(
3445 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003446 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003447 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3448 } else {
3449 DCHECK(second.IsStackSlot());
3450 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3451 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003452 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003453 }
3454
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003455 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003456 DCHECK(first.Equals(locations->Out()));
3457 if (second.IsFpuRegister()) {
3458 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3459 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3460 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003461 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003462 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3463 codegen_->LiteralDoubleAddress(
3464 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003465 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003466 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3467 } else {
3468 DCHECK(second.IsDoubleStackSlot());
3469 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3470 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003471 break;
3472 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003473
3474 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003475 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003476 }
3477}
3478
Roland Levillain232ade02015-04-20 15:14:36 +01003479void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3480 uint32_t temp_offset,
3481 uint32_t stack_adjustment,
3482 bool is_fp,
3483 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003484 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003485 DCHECK(!is_wide);
3486 if (is_fp) {
3487 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3488 } else {
3489 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3490 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003491 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003492 DCHECK(is_wide);
3493 if (is_fp) {
3494 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3495 } else {
3496 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3497 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003498 } else {
3499 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003500 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003501 Location stack_temp = Location::StackSlot(temp_offset);
3502 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003503 if (is_fp) {
3504 __ flds(Address(ESP, temp_offset));
3505 } else {
3506 __ filds(Address(ESP, temp_offset));
3507 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003508 } else {
3509 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3510 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003511 if (is_fp) {
3512 __ fldl(Address(ESP, temp_offset));
3513 } else {
3514 __ fildl(Address(ESP, temp_offset));
3515 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003516 }
3517 }
3518}
3519
3520void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 DataType::Type type = rem->GetResultType();
3522 bool is_float = type == DataType::Type::kFloat32;
3523 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003524 LocationSummary* locations = rem->GetLocations();
3525 Location first = locations->InAt(0);
3526 Location second = locations->InAt(1);
3527 Location out = locations->Out();
3528
3529 // Create stack space for 2 elements.
3530 // TODO: enhance register allocator to ask for stack temporaries.
3531 __ subl(ESP, Immediate(2 * elem_size));
3532
3533 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003534 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003535 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3536 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003537
3538 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003539 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003540 __ Bind(&retry);
3541 __ fprem();
3542
3543 // Move FP status to AX.
3544 __ fstsw();
3545
3546 // And see if the argument reduction is complete. This is signaled by the
3547 // C2 FPU flag bit set to 0.
3548 __ andl(EAX, Immediate(kC2ConditionMask));
3549 __ j(kNotEqual, &retry);
3550
3551 // We have settled on the final value. Retrieve it into an XMM register.
3552 // Store FP top of stack to real stack.
3553 if (is_float) {
3554 __ fsts(Address(ESP, 0));
3555 } else {
3556 __ fstl(Address(ESP, 0));
3557 }
3558
3559 // Pop the 2 items from the FP stack.
3560 __ fucompp();
3561
3562 // Load the value from the stack into an XMM register.
3563 DCHECK(out.IsFpuRegister()) << out;
3564 if (is_float) {
3565 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3566 } else {
3567 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3568 }
3569
3570 // And remove the temporary stack space we allocated.
3571 __ addl(ESP, Immediate(2 * elem_size));
3572}
3573
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574
3575void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3576 DCHECK(instruction->IsDiv() || instruction->IsRem());
3577
3578 LocationSummary* locations = instruction->GetLocations();
3579 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003580 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581
3582 Register out_register = locations->Out().AsRegister<Register>();
3583 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003584 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585
3586 DCHECK(imm == 1 || imm == -1);
3587
3588 if (instruction->IsRem()) {
3589 __ xorl(out_register, out_register);
3590 } else {
3591 __ movl(out_register, input_register);
3592 if (imm == -1) {
3593 __ negl(out_register);
3594 }
3595 }
3596}
3597
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303598void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3599 LocationSummary* locations = instruction->GetLocations();
3600 Location second = locations->InAt(1);
3601
3602 Register out = locations->Out().AsRegister<Register>();
3603 Register numerator = locations->InAt(0).AsRegister<Register>();
3604
3605 int32_t imm = Int64FromConstant(second.GetConstant());
3606 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3607 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3608
3609 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3610 NearLabel done;
3611 __ movl(out, numerator);
3612 __ andl(out, Immediate(abs_imm-1));
3613 __ j(Condition::kZero, &done);
3614 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3615 __ testl(numerator, numerator);
3616 __ cmovl(Condition::kLess, out, tmp);
3617 __ Bind(&done);
3618}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003619
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003620void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003621 LocationSummary* locations = instruction->GetLocations();
3622
3623 Register out_register = locations->Out().AsRegister<Register>();
3624 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003625 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003626 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3627 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 Register num = locations->GetTemp(0).AsRegister<Register>();
3630
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003631 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632 __ testl(input_register, input_register);
3633 __ cmovl(kGreaterEqual, num, input_register);
3634 int shift = CTZ(imm);
3635 __ sarl(num, Immediate(shift));
3636
3637 if (imm < 0) {
3638 __ negl(num);
3639 }
3640
3641 __ movl(out_register, num);
3642}
3643
3644void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3645 DCHECK(instruction->IsDiv() || instruction->IsRem());
3646
3647 LocationSummary* locations = instruction->GetLocations();
3648 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3649
3650 Register eax = locations->InAt(0).AsRegister<Register>();
3651 Register out = locations->Out().AsRegister<Register>();
3652 Register num;
3653 Register edx;
3654
3655 if (instruction->IsDiv()) {
3656 edx = locations->GetTemp(0).AsRegister<Register>();
3657 num = locations->GetTemp(1).AsRegister<Register>();
3658 } else {
3659 edx = locations->Out().AsRegister<Register>();
3660 num = locations->GetTemp(0).AsRegister<Register>();
3661 }
3662
3663 DCHECK_EQ(EAX, eax);
3664 DCHECK_EQ(EDX, edx);
3665 if (instruction->IsDiv()) {
3666 DCHECK_EQ(EAX, out);
3667 } else {
3668 DCHECK_EQ(EDX, out);
3669 }
3670
3671 int64_t magic;
3672 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003673 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003674
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003675 // Save the numerator.
3676 __ movl(num, eax);
3677
3678 // EAX = magic
3679 __ movl(eax, Immediate(magic));
3680
3681 // EDX:EAX = magic * numerator
3682 __ imull(num);
3683
3684 if (imm > 0 && magic < 0) {
3685 // EDX += num
3686 __ addl(edx, num);
3687 } else if (imm < 0 && magic > 0) {
3688 __ subl(edx, num);
3689 }
3690
3691 // Shift if needed.
3692 if (shift != 0) {
3693 __ sarl(edx, Immediate(shift));
3694 }
3695
3696 // EDX += 1 if EDX < 0
3697 __ movl(eax, edx);
3698 __ shrl(edx, Immediate(31));
3699 __ addl(edx, eax);
3700
3701 if (instruction->IsRem()) {
3702 __ movl(eax, num);
3703 __ imull(edx, Immediate(imm));
3704 __ subl(eax, edx);
3705 __ movl(edx, eax);
3706 } else {
3707 __ movl(eax, edx);
3708 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003709}
3710
Calin Juravlebacfec32014-11-14 15:54:36 +00003711void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3712 DCHECK(instruction->IsDiv() || instruction->IsRem());
3713
3714 LocationSummary* locations = instruction->GetLocations();
3715 Location out = locations->Out();
3716 Location first = locations->InAt(0);
3717 Location second = locations->InAt(1);
3718 bool is_div = instruction->IsDiv();
3719
3720 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003721 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 DCHECK_EQ(EAX, first.AsRegister<Register>());
3723 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003724
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003725 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003726 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003727
3728 if (imm == 0) {
3729 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3730 } else if (imm == 1 || imm == -1) {
3731 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303732 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3733 if (is_div) {
3734 DivByPowerOfTwo(instruction->AsDiv());
3735 } else {
3736 RemByPowerOfTwo(instruction->AsRem());
3737 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003738 } else {
3739 DCHECK(imm <= -2 || imm >= 2);
3740 GenerateDivRemWithAnyConstant(instruction);
3741 }
3742 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003743 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003744 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003745 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003746
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003747 Register second_reg = second.AsRegister<Register>();
3748 // 0x80000000/-1 triggers an arithmetic exception!
3749 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3750 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003751
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003752 __ cmpl(second_reg, Immediate(-1));
3753 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003754
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003755 // edx:eax <- sign-extended of eax
3756 __ cdq();
3757 // eax = quotient, edx = remainder
3758 __ idivl(second_reg);
3759 __ Bind(slow_path->GetExitLabel());
3760 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003761 break;
3762 }
3763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003765 InvokeRuntimeCallingConvention calling_convention;
3766 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3767 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3768 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3769 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3770 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3771 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3772
3773 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003774 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003775 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003776 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003777 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003778 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 break;
3781 }
3782
3783 default:
3784 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3785 }
3786}
3787
Calin Juravle7c4954d2014-10-28 16:57:40 +00003788void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003789 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003790 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003791 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003792 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003793
Calin Juravle7c4954d2014-10-28 16:57:40 +00003794 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003795 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003796 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003797 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003798 locations->SetOut(Location::SameAsFirstInput());
3799 // Intel uses edx:eax as the dividend.
3800 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003801 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3802 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3803 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003804 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003805 locations->AddTemp(Location::RequiresRegister());
3806 }
Calin Juravled0d48522014-11-04 16:40:20 +00003807 break;
3808 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003810 InvokeRuntimeCallingConvention calling_convention;
3811 locations->SetInAt(0, Location::RegisterPairLocation(
3812 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3813 locations->SetInAt(1, Location::RegisterPairLocation(
3814 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3815 // Runtime helper puts the result in EAX, EDX.
3816 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003817 break;
3818 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003819 case DataType::Type::kFloat32:
3820 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003821 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003822 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3823 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003824 } else if (div->InputAt(1)->IsConstant()) {
3825 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003826 } else {
3827 locations->SetInAt(1, Location::Any());
3828 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003829 locations->SetOut(Location::SameAsFirstInput());
3830 break;
3831 }
3832
3833 default:
3834 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3835 }
3836}
3837
3838void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3839 LocationSummary* locations = div->GetLocations();
3840 Location first = locations->InAt(0);
3841 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003842
3843 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003844 case DataType::Type::kInt32:
3845 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003846 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003847 break;
3848 }
3849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003851 if (second.IsFpuRegister()) {
3852 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3853 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3854 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003855 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003856 __ divss(first.AsFpuRegister<XmmRegister>(),
3857 codegen_->LiteralFloatAddress(
3858 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003859 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003860 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3861 } else {
3862 DCHECK(second.IsStackSlot());
3863 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3864 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003865 break;
3866 }
3867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003869 if (second.IsFpuRegister()) {
3870 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3871 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3872 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003873 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003874 __ divsd(first.AsFpuRegister<XmmRegister>(),
3875 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003876 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3877 const_area->GetBaseMethodAddress(),
3878 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003879 } else {
3880 DCHECK(second.IsDoubleStackSlot());
3881 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3882 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003883 break;
3884 }
3885
3886 default:
3887 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3888 }
3889}
3890
Calin Juravlebacfec32014-11-14 15:54:36 +00003891void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003892 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003894 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003895 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003896 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003897 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003898
Calin Juravled2ec87d2014-12-08 14:24:46 +00003899 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003900 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003901 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003902 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003903 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003904 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3905 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3906 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003907 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003908 locations->AddTemp(Location::RequiresRegister());
3909 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003910 break;
3911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003912 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003913 InvokeRuntimeCallingConvention calling_convention;
3914 locations->SetInAt(0, Location::RegisterPairLocation(
3915 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3916 locations->SetInAt(1, Location::RegisterPairLocation(
3917 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3918 // Runtime helper puts the result in EAX, EDX.
3919 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3920 break;
3921 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 case DataType::Type::kFloat64:
3923 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003924 locations->SetInAt(0, Location::Any());
3925 locations->SetInAt(1, Location::Any());
3926 locations->SetOut(Location::RequiresFpuRegister());
3927 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003928 break;
3929 }
3930
3931 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003932 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003933 }
3934}
3935
3936void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003938 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003939 case DataType::Type::kInt32:
3940 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003941 GenerateDivRemIntegral(rem);
3942 break;
3943 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003944 case DataType::Type::kFloat32:
3945 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003946 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003947 break;
3948 }
3949 default:
3950 LOG(FATAL) << "Unexpected rem type " << type;
3951 }
3952}
3953
Aart Bik1f8d51b2018-02-15 10:42:37 -08003954static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3955 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3956 switch (minmax->GetResultType()) {
3957 case DataType::Type::kInt32:
3958 locations->SetInAt(0, Location::RequiresRegister());
3959 locations->SetInAt(1, Location::RequiresRegister());
3960 locations->SetOut(Location::SameAsFirstInput());
3961 break;
3962 case DataType::Type::kInt64:
3963 locations->SetInAt(0, Location::RequiresRegister());
3964 locations->SetInAt(1, Location::RequiresRegister());
3965 locations->SetOut(Location::SameAsFirstInput());
3966 // Register to use to perform a long subtract to set cc.
3967 locations->AddTemp(Location::RequiresRegister());
3968 break;
3969 case DataType::Type::kFloat32:
3970 locations->SetInAt(0, Location::RequiresFpuRegister());
3971 locations->SetInAt(1, Location::RequiresFpuRegister());
3972 locations->SetOut(Location::SameAsFirstInput());
3973 locations->AddTemp(Location::RequiresRegister());
3974 break;
3975 case DataType::Type::kFloat64:
3976 locations->SetInAt(0, Location::RequiresFpuRegister());
3977 locations->SetInAt(1, Location::RequiresFpuRegister());
3978 locations->SetOut(Location::SameAsFirstInput());
3979 break;
3980 default:
3981 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3982 }
3983}
3984
Aart Bik351df3e2018-03-07 11:54:57 -08003985void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3986 bool is_min,
3987 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003988 Location op1_loc = locations->InAt(0);
3989 Location op2_loc = locations->InAt(1);
3990
3991 // Shortcut for same input locations.
3992 if (op1_loc.Equals(op2_loc)) {
3993 // Can return immediately, as op1_loc == out_loc.
3994 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3995 // a copy here.
3996 DCHECK(locations->Out().Equals(op1_loc));
3997 return;
3998 }
3999
4000 if (type == DataType::Type::kInt64) {
4001 // Need to perform a subtract to get the sign right.
4002 // op1 is already in the same location as the output.
4003 Location output = locations->Out();
4004 Register output_lo = output.AsRegisterPairLow<Register>();
4005 Register output_hi = output.AsRegisterPairHigh<Register>();
4006
4007 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4008 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4009
4010 // The comparison is performed by subtracting the second operand from
4011 // the first operand and then setting the status flags in the same
4012 // manner as the SUB instruction."
4013 __ cmpl(output_lo, op2_lo);
4014
4015 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4016 Register temp = locations->GetTemp(0).AsRegister<Register>();
4017 __ movl(temp, output_hi);
4018 __ sbbl(temp, op2_hi);
4019
4020 // Now the condition code is correct.
4021 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4022 __ cmovl(cond, output_lo, op2_lo);
4023 __ cmovl(cond, output_hi, op2_hi);
4024 } else {
4025 DCHECK_EQ(type, DataType::Type::kInt32);
4026 Register out = locations->Out().AsRegister<Register>();
4027 Register op2 = op2_loc.AsRegister<Register>();
4028
4029 // (out := op1)
4030 // out <=? op2
4031 // if out is min jmp done
4032 // out := op2
4033 // done:
4034
4035 __ cmpl(out, op2);
4036 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4037 __ cmovl(cond, out, op2);
4038 }
4039}
4040
4041void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4042 bool is_min,
4043 DataType::Type type) {
4044 Location op1_loc = locations->InAt(0);
4045 Location op2_loc = locations->InAt(1);
4046 Location out_loc = locations->Out();
4047 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4048
4049 // Shortcut for same input locations.
4050 if (op1_loc.Equals(op2_loc)) {
4051 DCHECK(out_loc.Equals(op1_loc));
4052 return;
4053 }
4054
4055 // (out := op1)
4056 // out <=? op2
4057 // if Nan jmp Nan_label
4058 // if out is min jmp done
4059 // if op2 is min jmp op2_label
4060 // handle -0/+0
4061 // jmp done
4062 // Nan_label:
4063 // out := NaN
4064 // op2_label:
4065 // out := op2
4066 // done:
4067 //
4068 // This removes one jmp, but needs to copy one input (op1) to out.
4069 //
4070 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4071
4072 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4073
4074 NearLabel nan, done, op2_label;
4075 if (type == DataType::Type::kFloat64) {
4076 __ ucomisd(out, op2);
4077 } else {
4078 DCHECK_EQ(type, DataType::Type::kFloat32);
4079 __ ucomiss(out, op2);
4080 }
4081
4082 __ j(Condition::kParityEven, &nan);
4083
4084 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4085 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4086
4087 // Handle 0.0/-0.0.
4088 if (is_min) {
4089 if (type == DataType::Type::kFloat64) {
4090 __ orpd(out, op2);
4091 } else {
4092 __ orps(out, op2);
4093 }
4094 } else {
4095 if (type == DataType::Type::kFloat64) {
4096 __ andpd(out, op2);
4097 } else {
4098 __ andps(out, op2);
4099 }
4100 }
4101 __ jmp(&done);
4102
4103 // NaN handling.
4104 __ Bind(&nan);
4105 if (type == DataType::Type::kFloat64) {
4106 // TODO: Use a constant from the constant table (requires extra input).
4107 __ LoadLongConstant(out, kDoubleNaN);
4108 } else {
4109 Register constant = locations->GetTemp(0).AsRegister<Register>();
4110 __ movl(constant, Immediate(kFloatNaN));
4111 __ movd(out, constant);
4112 }
4113 __ jmp(&done);
4114
4115 // out := op2;
4116 __ Bind(&op2_label);
4117 if (type == DataType::Type::kFloat64) {
4118 __ movsd(out, op2);
4119 } else {
4120 __ movss(out, op2);
4121 }
4122
4123 // Done.
4124 __ Bind(&done);
4125}
4126
Aart Bik351df3e2018-03-07 11:54:57 -08004127void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4128 DataType::Type type = minmax->GetResultType();
4129 switch (type) {
4130 case DataType::Type::kInt32:
4131 case DataType::Type::kInt64:
4132 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4133 break;
4134 case DataType::Type::kFloat32:
4135 case DataType::Type::kFloat64:
4136 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4137 break;
4138 default:
4139 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4140 }
4141}
4142
Aart Bik1f8d51b2018-02-15 10:42:37 -08004143void LocationsBuilderX86::VisitMin(HMin* min) {
4144 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4145}
4146
4147void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004148 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004149}
4150
4151void LocationsBuilderX86::VisitMax(HMax* max) {
4152 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4153}
4154
4155void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004156 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004157}
4158
Aart Bik3dad3412018-02-28 12:01:46 -08004159void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4160 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4161 switch (abs->GetResultType()) {
4162 case DataType::Type::kInt32:
4163 locations->SetInAt(0, Location::RegisterLocation(EAX));
4164 locations->SetOut(Location::SameAsFirstInput());
4165 locations->AddTemp(Location::RegisterLocation(EDX));
4166 break;
4167 case DataType::Type::kInt64:
4168 locations->SetInAt(0, Location::RequiresRegister());
4169 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4170 locations->AddTemp(Location::RequiresRegister());
4171 break;
4172 case DataType::Type::kFloat32:
4173 locations->SetInAt(0, Location::RequiresFpuRegister());
4174 locations->SetOut(Location::SameAsFirstInput());
4175 locations->AddTemp(Location::RequiresFpuRegister());
4176 locations->AddTemp(Location::RequiresRegister());
4177 break;
4178 case DataType::Type::kFloat64:
4179 locations->SetInAt(0, Location::RequiresFpuRegister());
4180 locations->SetOut(Location::SameAsFirstInput());
4181 locations->AddTemp(Location::RequiresFpuRegister());
4182 break;
4183 default:
4184 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4185 }
4186}
4187
4188void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4189 LocationSummary* locations = abs->GetLocations();
4190 switch (abs->GetResultType()) {
4191 case DataType::Type::kInt32: {
4192 Register out = locations->Out().AsRegister<Register>();
4193 DCHECK_EQ(out, EAX);
4194 Register temp = locations->GetTemp(0).AsRegister<Register>();
4195 DCHECK_EQ(temp, EDX);
4196 // Sign extend EAX into EDX.
4197 __ cdq();
4198 // XOR EAX with sign.
4199 __ xorl(EAX, EDX);
4200 // Subtract out sign to correct.
4201 __ subl(EAX, EDX);
4202 // The result is in EAX.
4203 break;
4204 }
4205 case DataType::Type::kInt64: {
4206 Location input = locations->InAt(0);
4207 Register input_lo = input.AsRegisterPairLow<Register>();
4208 Register input_hi = input.AsRegisterPairHigh<Register>();
4209 Location output = locations->Out();
4210 Register output_lo = output.AsRegisterPairLow<Register>();
4211 Register output_hi = output.AsRegisterPairHigh<Register>();
4212 Register temp = locations->GetTemp(0).AsRegister<Register>();
4213 // Compute the sign into the temporary.
4214 __ movl(temp, input_hi);
4215 __ sarl(temp, Immediate(31));
4216 // Store the sign into the output.
4217 __ movl(output_lo, temp);
4218 __ movl(output_hi, temp);
4219 // XOR the input to the output.
4220 __ xorl(output_lo, input_lo);
4221 __ xorl(output_hi, input_hi);
4222 // Subtract the sign.
4223 __ subl(output_lo, temp);
4224 __ sbbl(output_hi, temp);
4225 break;
4226 }
4227 case DataType::Type::kFloat32: {
4228 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4229 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4230 Register constant = locations->GetTemp(1).AsRegister<Register>();
4231 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4232 __ movd(temp, constant);
4233 __ andps(out, temp);
4234 break;
4235 }
4236 case DataType::Type::kFloat64: {
4237 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4238 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4239 // TODO: Use a constant from the constant table (requires extra input).
4240 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4241 __ andpd(out, temp);
4242 break;
4243 }
4244 default:
4245 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4246 }
4247}
4248
Calin Juravled0d48522014-11-04 16:40:20 +00004249void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004250 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004251 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004252 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004253 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004254 case DataType::Type::kInt8:
4255 case DataType::Type::kUint16:
4256 case DataType::Type::kInt16:
4257 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004258 locations->SetInAt(0, Location::Any());
4259 break;
4260 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004261 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004262 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4263 if (!instruction->IsConstant()) {
4264 locations->AddTemp(Location::RequiresRegister());
4265 }
4266 break;
4267 }
4268 default:
4269 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4270 }
Calin Juravled0d48522014-11-04 16:40:20 +00004271}
4272
4273void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004274 SlowPathCode* slow_path =
4275 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004276 codegen_->AddSlowPath(slow_path);
4277
4278 LocationSummary* locations = instruction->GetLocations();
4279 Location value = locations->InAt(0);
4280
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004281 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004282 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004283 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004284 case DataType::Type::kInt8:
4285 case DataType::Type::kUint16:
4286 case DataType::Type::kInt16:
4287 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004288 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004289 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004290 __ j(kEqual, slow_path->GetEntryLabel());
4291 } else if (value.IsStackSlot()) {
4292 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4293 __ j(kEqual, slow_path->GetEntryLabel());
4294 } else {
4295 DCHECK(value.IsConstant()) << value;
4296 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004297 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004298 }
4299 }
4300 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004301 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004302 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004303 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004304 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004305 __ movl(temp, value.AsRegisterPairLow<Register>());
4306 __ orl(temp, value.AsRegisterPairHigh<Register>());
4307 __ j(kEqual, slow_path->GetEntryLabel());
4308 } else {
4309 DCHECK(value.IsConstant()) << value;
4310 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4311 __ jmp(slow_path->GetEntryLabel());
4312 }
4313 }
4314 break;
4315 }
4316 default:
4317 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004318 }
Calin Juravled0d48522014-11-04 16:40:20 +00004319}
4320
Calin Juravle9aec02f2014-11-18 23:06:35 +00004321void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4322 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4323
4324 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004325 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004326
4327 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004328 case DataType::Type::kInt32:
4329 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004330 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004331 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004332 // The shift count needs to be in CL or a constant.
4333 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004334 locations->SetOut(Location::SameAsFirstInput());
4335 break;
4336 }
4337 default:
4338 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4339 }
4340}
4341
4342void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4343 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4344
4345 LocationSummary* locations = op->GetLocations();
4346 Location first = locations->InAt(0);
4347 Location second = locations->InAt(1);
4348 DCHECK(first.Equals(locations->Out()));
4349
4350 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004351 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004352 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004353 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004354 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004356 DCHECK_EQ(ECX, second_reg);
4357 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004358 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004359 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004360 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004361 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004362 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004363 }
4364 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004365 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004366 if (shift == 0) {
4367 return;
4368 }
4369 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004370 if (op->IsShl()) {
4371 __ shll(first_reg, imm);
4372 } else if (op->IsShr()) {
4373 __ sarl(first_reg, imm);
4374 } else {
4375 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004376 }
4377 }
4378 break;
4379 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004380 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004381 if (second.IsRegister()) {
4382 Register second_reg = second.AsRegister<Register>();
4383 DCHECK_EQ(ECX, second_reg);
4384 if (op->IsShl()) {
4385 GenerateShlLong(first, second_reg);
4386 } else if (op->IsShr()) {
4387 GenerateShrLong(first, second_reg);
4388 } else {
4389 GenerateUShrLong(first, second_reg);
4390 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004391 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004392 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004393 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004394 // Nothing to do if the shift is 0, as the input is already the output.
4395 if (shift != 0) {
4396 if (op->IsShl()) {
4397 GenerateShlLong(first, shift);
4398 } else if (op->IsShr()) {
4399 GenerateShrLong(first, shift);
4400 } else {
4401 GenerateUShrLong(first, shift);
4402 }
4403 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004404 }
4405 break;
4406 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004407 default:
4408 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4409 }
4410}
4411
Mark P Mendell73945692015-04-29 14:56:17 +00004412void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4413 Register low = loc.AsRegisterPairLow<Register>();
4414 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004415 if (shift == 1) {
4416 // This is just an addition.
4417 __ addl(low, low);
4418 __ adcl(high, high);
4419 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004420 // Shift by 32 is easy. High gets low, and low gets 0.
4421 codegen_->EmitParallelMoves(
4422 loc.ToLow(),
4423 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004425 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4426 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004427 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004428 } else if (shift > 32) {
4429 // Low part becomes 0. High part is low part << (shift-32).
4430 __ movl(high, low);
4431 __ shll(high, Immediate(shift - 32));
4432 __ xorl(low, low);
4433 } else {
4434 // Between 1 and 31.
4435 __ shld(high, low, Immediate(shift));
4436 __ shll(low, Immediate(shift));
4437 }
4438}
4439
Calin Juravle9aec02f2014-11-18 23:06:35 +00004440void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004441 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004442 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4443 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4444 __ testl(shifter, Immediate(32));
4445 __ j(kEqual, &done);
4446 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4447 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4448 __ Bind(&done);
4449}
4450
Mark P Mendell73945692015-04-29 14:56:17 +00004451void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4452 Register low = loc.AsRegisterPairLow<Register>();
4453 Register high = loc.AsRegisterPairHigh<Register>();
4454 if (shift == 32) {
4455 // Need to copy the sign.
4456 DCHECK_NE(low, high);
4457 __ movl(low, high);
4458 __ sarl(high, Immediate(31));
4459 } else if (shift > 32) {
4460 DCHECK_NE(low, high);
4461 // High part becomes sign. Low part is shifted by shift - 32.
4462 __ movl(low, high);
4463 __ sarl(high, Immediate(31));
4464 __ sarl(low, Immediate(shift - 32));
4465 } else {
4466 // Between 1 and 31.
4467 __ shrd(low, high, Immediate(shift));
4468 __ sarl(high, Immediate(shift));
4469 }
4470}
4471
Calin Juravle9aec02f2014-11-18 23:06:35 +00004472void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004473 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004474 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4475 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4476 __ testl(shifter, Immediate(32));
4477 __ j(kEqual, &done);
4478 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4479 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4480 __ Bind(&done);
4481}
4482
Mark P Mendell73945692015-04-29 14:56:17 +00004483void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4484 Register low = loc.AsRegisterPairLow<Register>();
4485 Register high = loc.AsRegisterPairHigh<Register>();
4486 if (shift == 32) {
4487 // Shift by 32 is easy. Low gets high, and high gets 0.
4488 codegen_->EmitParallelMoves(
4489 loc.ToHigh(),
4490 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004491 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004492 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4493 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004494 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004495 } else if (shift > 32) {
4496 // Low part is high >> (shift - 32). High part becomes 0.
4497 __ movl(low, high);
4498 __ shrl(low, Immediate(shift - 32));
4499 __ xorl(high, high);
4500 } else {
4501 // Between 1 and 31.
4502 __ shrd(low, high, Immediate(shift));
4503 __ shrl(high, Immediate(shift));
4504 }
4505}
4506
Calin Juravle9aec02f2014-11-18 23:06:35 +00004507void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004508 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004509 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4510 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4511 __ testl(shifter, Immediate(32));
4512 __ j(kEqual, &done);
4513 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4514 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4515 __ Bind(&done);
4516}
4517
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004518void LocationsBuilderX86::VisitRor(HRor* ror) {
4519 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004520 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004521
4522 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004524 // Add the temporary needed.
4525 locations->AddTemp(Location::RequiresRegister());
4526 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004527 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004528 locations->SetInAt(0, Location::RequiresRegister());
4529 // The shift count needs to be in CL (unless it is a constant).
4530 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4531 locations->SetOut(Location::SameAsFirstInput());
4532 break;
4533 default:
4534 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4535 UNREACHABLE();
4536 }
4537}
4538
4539void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4540 LocationSummary* locations = ror->GetLocations();
4541 Location first = locations->InAt(0);
4542 Location second = locations->InAt(1);
4543
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004544 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004545 Register first_reg = first.AsRegister<Register>();
4546 if (second.IsRegister()) {
4547 Register second_reg = second.AsRegister<Register>();
4548 __ rorl(first_reg, second_reg);
4549 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004551 __ rorl(first_reg, imm);
4552 }
4553 return;
4554 }
4555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004556 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004557 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4558 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4559 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4560 if (second.IsRegister()) {
4561 Register second_reg = second.AsRegister<Register>();
4562 DCHECK_EQ(second_reg, ECX);
4563 __ movl(temp_reg, first_reg_hi);
4564 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4565 __ shrd(first_reg_lo, temp_reg, second_reg);
4566 __ movl(temp_reg, first_reg_hi);
4567 __ testl(second_reg, Immediate(32));
4568 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4569 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4570 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004572 if (shift_amt == 0) {
4573 // Already fine.
4574 return;
4575 }
4576 if (shift_amt == 32) {
4577 // Just swap.
4578 __ movl(temp_reg, first_reg_lo);
4579 __ movl(first_reg_lo, first_reg_hi);
4580 __ movl(first_reg_hi, temp_reg);
4581 return;
4582 }
4583
4584 Immediate imm(shift_amt);
4585 // Save the constents of the low value.
4586 __ movl(temp_reg, first_reg_lo);
4587
4588 // Shift right into low, feeding bits from high.
4589 __ shrd(first_reg_lo, first_reg_hi, imm);
4590
4591 // Shift right into high, feeding bits from the original low.
4592 __ shrd(first_reg_hi, temp_reg, imm);
4593
4594 // Swap if needed.
4595 if (shift_amt > 32) {
4596 __ movl(temp_reg, first_reg_lo);
4597 __ movl(first_reg_lo, first_reg_hi);
4598 __ movl(first_reg_hi, temp_reg);
4599 }
4600 }
4601}
4602
Calin Juravle9aec02f2014-11-18 23:06:35 +00004603void LocationsBuilderX86::VisitShl(HShl* shl) {
4604 HandleShift(shl);
4605}
4606
4607void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4608 HandleShift(shl);
4609}
4610
4611void LocationsBuilderX86::VisitShr(HShr* shr) {
4612 HandleShift(shr);
4613}
4614
4615void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4616 HandleShift(shr);
4617}
4618
4619void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4620 HandleShift(ushr);
4621}
4622
4623void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4624 HandleShift(ushr);
4625}
4626
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004627void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004628 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4629 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004630 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004631 InvokeRuntimeCallingConvention calling_convention;
4632 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004633}
4634
4635void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004636 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4637 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4638 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004639}
4640
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004641void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004642 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4643 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004644 locations->SetOut(Location::RegisterLocation(EAX));
4645 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004646 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4647 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004648}
4649
4650void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004651 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4652 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004653 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004654 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004655 DCHECK(!codegen_->IsLeafMethod());
4656}
4657
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004658void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004659 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004660 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004661 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4662 if (location.IsStackSlot()) {
4663 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4664 } else if (location.IsDoubleStackSlot()) {
4665 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004666 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004667 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004668}
4669
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004670void InstructionCodeGeneratorX86::VisitParameterValue(
4671 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4672}
4673
4674void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4675 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004676 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004677 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4678}
4679
4680void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004681}
4682
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004683void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4684 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004685 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004686 locations->SetInAt(0, Location::RequiresRegister());
4687 locations->SetOut(Location::RequiresRegister());
4688}
4689
4690void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4691 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004692 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004693 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004694 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004695 __ movl(locations->Out().AsRegister<Register>(),
4696 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004697 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004698 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004699 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004700 __ movl(locations->Out().AsRegister<Register>(),
4701 Address(locations->InAt(0).AsRegister<Register>(),
4702 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4703 // temp = temp->GetImtEntryAt(method_offset);
4704 __ movl(locations->Out().AsRegister<Register>(),
4705 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004706 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004707}
4708
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004709void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004710 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004711 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004712 locations->SetInAt(0, Location::RequiresRegister());
4713 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004714}
4715
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004716void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4717 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004718 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004719 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004720 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004721 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004722 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004723 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004724 break;
4725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004726 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004727 __ notl(out.AsRegisterPairLow<Register>());
4728 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004729 break;
4730
4731 default:
4732 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4733 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004734}
4735
David Brazdil66d126e2015-04-03 16:02:44 +01004736void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4737 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004738 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004739 locations->SetInAt(0, Location::RequiresRegister());
4740 locations->SetOut(Location::SameAsFirstInput());
4741}
4742
4743void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004744 LocationSummary* locations = bool_not->GetLocations();
4745 Location in = locations->InAt(0);
4746 Location out = locations->Out();
4747 DCHECK(in.Equals(out));
4748 __ xorl(out.AsRegister<Register>(), Immediate(1));
4749}
4750
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004751void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004752 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004753 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004754 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004755 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004756 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004758 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004759 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004760 case DataType::Type::kInt32:
4761 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004762 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004763 locations->SetInAt(1, Location::Any());
4764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4765 break;
4766 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004767 case DataType::Type::kFloat32:
4768 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004769 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004770 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4771 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4772 } else if (compare->InputAt(1)->IsConstant()) {
4773 locations->SetInAt(1, Location::RequiresFpuRegister());
4774 } else {
4775 locations->SetInAt(1, Location::Any());
4776 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004777 locations->SetOut(Location::RequiresRegister());
4778 break;
4779 }
4780 default:
4781 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4782 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004783}
4784
4785void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004786 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004787 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004788 Location left = locations->InAt(0);
4789 Location right = locations->InAt(1);
4790
Mark Mendell0c9497d2015-08-21 09:30:05 -04004791 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004792 Condition less_cond = kLess;
4793
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004794 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004796 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004797 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004799 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004800 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004801 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004802 break;
4803 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004804 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004805 Register left_low = left.AsRegisterPairLow<Register>();
4806 Register left_high = left.AsRegisterPairHigh<Register>();
4807 int32_t val_low = 0;
4808 int32_t val_high = 0;
4809 bool right_is_const = false;
4810
4811 if (right.IsConstant()) {
4812 DCHECK(right.GetConstant()->IsLongConstant());
4813 right_is_const = true;
4814 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4815 val_low = Low32Bits(val);
4816 val_high = High32Bits(val);
4817 }
4818
Calin Juravleddb7df22014-11-25 20:56:51 +00004819 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004820 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004821 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004822 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004823 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004824 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004825 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004827 __ j(kLess, &less); // Signed compare.
4828 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004829 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004830 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004831 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004832 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004833 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004834 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004835 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836 }
Aart Bika19616e2016-02-01 18:57:58 -08004837 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004838 break;
4839 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004840 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004841 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004842 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004843 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004844 break;
4845 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004846 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004847 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004848 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004849 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004850 break;
4851 }
4852 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004853 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004854 }
Aart Bika19616e2016-02-01 18:57:58 -08004855
Calin Juravleddb7df22014-11-25 20:56:51 +00004856 __ movl(out, Immediate(0));
4857 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004858 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004859
4860 __ Bind(&greater);
4861 __ movl(out, Immediate(1));
4862 __ jmp(&done);
4863
4864 __ Bind(&less);
4865 __ movl(out, Immediate(-1));
4866
4867 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004868}
4869
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004870void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004871 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004872 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004873 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004874 locations->SetInAt(i, Location::Any());
4875 }
4876 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004877}
4878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004879void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004880 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004881}
4882
Roland Levillain7c1559a2015-12-15 10:55:36 +00004883void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004884 /*
4885 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4886 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4887 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4888 */
4889 switch (kind) {
4890 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004891 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004892 break;
4893 }
4894 case MemBarrierKind::kAnyStore:
4895 case MemBarrierKind::kLoadAny:
4896 case MemBarrierKind::kStoreStore: {
4897 // nop
4898 break;
4899 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004900 case MemBarrierKind::kNTStoreStore:
4901 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004902 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004903 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004904 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004905}
4906
Vladimir Markodc151b22015-10-15 18:02:30 +01004907HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4908 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004909 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004910 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004911}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004912
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004913Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4914 Register temp) {
4915 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004916 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004917 if (!invoke->GetLocations()->Intrinsified()) {
4918 return location.AsRegister<Register>();
4919 }
4920 // For intrinsics we allow any location, so it may be on the stack.
4921 if (!location.IsRegister()) {
4922 __ movl(temp, Address(ESP, location.GetStackIndex()));
4923 return temp;
4924 }
4925 // For register locations, check if the register was saved. If so, get it from the stack.
4926 // Note: There is a chance that the register was saved but not overwritten, so we could
4927 // save one load. However, since this is just an intrinsic slow path we prefer this
4928 // simple and more robust approach rather that trying to determine if that's the case.
4929 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004930 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4931 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4932 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4933 __ movl(temp, Address(ESP, stack_offset));
4934 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004935 }
4936 return location.AsRegister<Register>();
4937}
4938
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004939void CodeGeneratorX86::GenerateStaticOrDirectCall(
4940 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004941 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4942 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004943 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004944 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004945 uint32_t offset =
4946 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4947 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004948 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004949 }
Vladimir Marko58155012015-08-19 12:49:41 +00004950 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004951 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004952 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004953 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004954 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004955 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4956 temp.AsRegister<Register>());
4957 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004958 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004959 break;
4960 }
Vladimir Markob066d432018-01-03 13:14:37 +00004961 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4962 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4963 temp.AsRegister<Register>());
4964 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4965 RecordBootImageRelRoPatch(
4966 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004967 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004968 break;
4969 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004970 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004971 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4972 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004973 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004974 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004975 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004976 break;
4977 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004978 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4979 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4980 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004981 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4982 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4983 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004984 }
Vladimir Marko58155012015-08-19 12:49:41 +00004985 }
4986
4987 switch (invoke->GetCodePtrLocation()) {
4988 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4989 __ call(GetFrameEntryLabel());
4990 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004991 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4992 // (callee_method + offset_of_quick_compiled_code)()
4993 __ call(Address(callee_method.AsRegister<Register>(),
4994 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004995 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004996 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004997 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004998 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004999
5000 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005001}
5002
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005003void CodeGeneratorX86::GenerateVirtualCall(
5004 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005005 Register temp = temp_in.AsRegister<Register>();
5006 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5007 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005008
5009 // Use the calling convention instead of the location of the receiver, as
5010 // intrinsics may have put the receiver in a different register. In the intrinsics
5011 // slow path, the arguments have been moved to the right place, so here we are
5012 // guaranteed that the receiver is the first register of the calling convention.
5013 InvokeDexCallingConvention calling_convention;
5014 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005015 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005016 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005017 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005018 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005019 // Instead of simply (possibly) unpoisoning `temp` here, we should
5020 // emit a read barrier for the previous class reference load.
5021 // However this is not required in practice, as this is an
5022 // intermediate/temporary reference and because the current
5023 // concurrent copying collector keeps the from-space memory
5024 // intact/accessible until the end of the marking phase (the
5025 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005026 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005027
5028 MaybeGenerateInlineCacheCheck(invoke, temp);
5029
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005030 // temp = temp->GetMethodAt(method_offset);
5031 __ movl(temp, Address(temp, method_offset));
5032 // call temp->GetEntryPoint();
5033 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005034 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005035 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005036}
5037
Vladimir Marko6fd16062018-06-26 11:02:04 +01005038void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5039 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005040 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005041 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005042 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005043}
5044
Vladimir Markob066d432018-01-03 13:14:37 +00005045void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5046 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005047 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005048 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005049 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005050}
5051
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005052void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01005053 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005054 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005055 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005056 boot_image_method_patches_.emplace_back(
5057 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005058 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005059}
5060
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005061void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
5062 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5063 HX86ComputeBaseMethodAddress* method_address =
5064 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005065 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005066 method_bss_entry_patches_.emplace_back(
5067 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5068 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005069}
5070
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005071void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5072 HX86ComputeBaseMethodAddress* method_address =
5073 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5074 boot_image_type_patches_.emplace_back(
5075 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005076 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005077}
5078
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005079Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005080 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005081 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5082 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005083 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005084 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005085}
5086
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005087void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5088 HX86ComputeBaseMethodAddress* method_address =
5089 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5090 boot_image_string_patches_.emplace_back(
5091 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5092 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005093}
5094
Vladimir Markoaad75c62016-10-03 08:46:48 +00005095Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005096 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005097 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005098 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005099 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005100 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005101}
5102
Vladimir Markoeebb8212018-06-05 14:57:24 +01005103void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005104 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005105 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005106 if (GetCompilerOptions().IsBootImage()) {
5107 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5108 HX86ComputeBaseMethodAddress* method_address =
5109 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5110 DCHECK(method_address != nullptr);
5111 Register method_address_reg =
5112 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5113 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5114 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005115 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005116 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5117 HX86ComputeBaseMethodAddress* method_address =
5118 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5119 DCHECK(method_address != nullptr);
5120 Register method_address_reg =
5121 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5122 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005123 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005124 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005125 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005126 gc::Heap* heap = Runtime::Current()->GetHeap();
5127 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005128 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005129 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5130 }
5131}
5132
Vladimir Marko6fd16062018-06-26 11:02:04 +01005133void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5134 uint32_t boot_image_offset) {
5135 DCHECK(invoke->IsStatic());
5136 InvokeRuntimeCallingConvention calling_convention;
5137 Register argument = calling_convention.GetRegisterAt(0);
5138 if (GetCompilerOptions().IsBootImage()) {
5139 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5140 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5141 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5142 HX86ComputeBaseMethodAddress* method_address =
5143 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5144 DCHECK(method_address != nullptr);
5145 Register method_address_reg =
5146 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5147 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5148 MethodReference target_method = invoke->GetTargetMethod();
5149 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5150 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5151 __ Bind(&boot_image_type_patches_.back().label);
5152 } else {
5153 LoadBootImageAddress(argument, boot_image_offset, invoke);
5154 }
5155 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5156 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5157}
5158
Vladimir Markoaad75c62016-10-03 08:46:48 +00005159// The label points to the end of the "movl" or another instruction but the literal offset
5160// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5161constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5162
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005163template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005164inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005165 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005166 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005167 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005168 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005169 linker_patches->push_back(Factory(literal_offset,
5170 info.target_dex_file,
5171 GetMethodAddressOffset(info.method_address),
5172 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005173 }
5174}
5175
Vladimir Marko6fd16062018-06-26 11:02:04 +01005176template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5177linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5178 const DexFile* target_dex_file,
5179 uint32_t pc_insn_offset,
5180 uint32_t boot_image_offset) {
5181 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5182 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005183}
5184
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005185void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005186 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005187 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005188 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005189 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005190 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005191 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005192 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005193 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005194 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005195 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005196 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005197 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5198 boot_image_method_patches_, linker_patches);
5199 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5200 boot_image_type_patches_, linker_patches);
5201 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005202 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005203 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005204 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005205 DCHECK(boot_image_type_patches_.empty());
5206 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005207 }
5208 if (GetCompilerOptions().IsBootImage()) {
5209 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5210 boot_image_other_patches_, linker_patches);
5211 } else {
5212 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5213 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005214 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005215 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5216 method_bss_entry_patches_, linker_patches);
5217 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5218 type_bss_entry_patches_, linker_patches);
5219 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5220 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005221 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005222}
5223
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005224void CodeGeneratorX86::MarkGCCard(Register temp,
5225 Register card,
5226 Register object,
5227 Register value,
5228 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005229 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005230 if (value_can_be_null) {
5231 __ testl(value, value);
5232 __ j(kEqual, &is_null);
5233 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005234 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005235 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005236 // Calculate the offset (in the card table) of the card corresponding to
5237 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005238 __ movl(temp, object);
5239 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005240 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5241 // `object`'s card.
5242 //
5243 // Register `card` contains the address of the card table. Note that the card
5244 // table's base is biased during its creation so that it always starts at an
5245 // address whose least-significant byte is equal to `kCardDirty` (see
5246 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5247 // below writes the `kCardDirty` (byte) value into the `object`'s card
5248 // (located at `card + object >> kCardShift`).
5249 //
5250 // This dual use of the value in register `card` (1. to calculate the location
5251 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5252 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005253 __ movb(Address(temp, card, TIMES_1, 0),
5254 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005255 if (value_can_be_null) {
5256 __ Bind(&is_null);
5257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258}
5259
Calin Juravle52c48962014-12-16 17:02:57 +00005260void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5261 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005262
5263 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005264 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005265 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005266 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5267 kEmitCompilerReadBarrier
5268 ? LocationSummary::kCallOnSlowPath
5269 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005270 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005271 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005272 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005273 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005276 locations->SetOut(Location::RequiresFpuRegister());
5277 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005278 // The output overlaps in case of long: we don't want the low move
5279 // to overwrite the object's location. Likewise, in the case of
5280 // an object field get with read barriers enabled, we do not want
5281 // the move to overwrite the object's location, as we need it to emit
5282 // the read barrier.
5283 locations->SetOut(
5284 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005285 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005286 Location::kOutputOverlap :
5287 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005288 }
Calin Juravle52c48962014-12-16 17:02:57 +00005289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005290 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005291 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005292 // So we use an XMM register as a temp to achieve atomicity (first
5293 // load the temp into the XMM and then copy the XMM into the
5294 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005295 locations->AddTemp(Location::RequiresFpuRegister());
5296 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005297}
5298
Calin Juravle52c48962014-12-16 17:02:57 +00005299void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5300 const FieldInfo& field_info) {
5301 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005302
Calin Juravle52c48962014-12-16 17:02:57 +00005303 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005304 Location base_loc = locations->InAt(0);
5305 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005306 Location out = locations->Out();
5307 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005308 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5309 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005310 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5311
Vladimir Marko61b92282017-10-11 13:23:17 +01005312 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005313 case DataType::Type::kBool:
5314 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005315 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005316 break;
5317 }
5318
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005319 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005320 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005321 break;
5322 }
5323
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005324 case DataType::Type::kUint16: {
5325 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005326 break;
5327 }
5328
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005329 case DataType::Type::kInt16: {
5330 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005331 break;
5332 }
5333
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005334 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005335 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005336 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005338 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005339 // /* HeapReference<Object> */ out = *(base + offset)
5340 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005341 // Note that a potential implicit null check is handled in this
5342 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5343 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005344 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005345 if (is_volatile) {
5346 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5347 }
5348 } else {
5349 __ movl(out.AsRegister<Register>(), Address(base, offset));
5350 codegen_->MaybeRecordImplicitNullCheck(instruction);
5351 if (is_volatile) {
5352 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5353 }
5354 // If read barriers are enabled, emit read barriers other than
5355 // Baker's using a slow path (and also unpoison the loaded
5356 // reference, if heap poisoning is enabled).
5357 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5358 }
5359 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005360 }
5361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005362 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005363 if (is_volatile) {
5364 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5365 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005366 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005367 __ movd(out.AsRegisterPairLow<Register>(), temp);
5368 __ psrlq(temp, Immediate(32));
5369 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5370 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005371 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005372 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005373 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005374 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5375 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005376 break;
5377 }
5378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005379 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005380 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005381 break;
5382 }
5383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005385 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005386 break;
5387 }
5388
Aart Bik66c158e2018-01-31 12:55:04 -08005389 case DataType::Type::kUint32:
5390 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005392 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005393 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005394 }
Calin Juravle52c48962014-12-16 17:02:57 +00005395
Vladimir Marko61b92282017-10-11 13:23:17 +01005396 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005397 // Potential implicit null checks, in the case of reference or
5398 // long fields, are handled in the previous switch statement.
5399 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005400 codegen_->MaybeRecordImplicitNullCheck(instruction);
5401 }
5402
Calin Juravle52c48962014-12-16 17:02:57 +00005403 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005404 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005405 // Memory barriers, in the case of references, are also handled
5406 // in the previous switch statement.
5407 } else {
5408 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5409 }
Roland Levillain4d027112015-07-01 15:41:14 +01005410 }
Calin Juravle52c48962014-12-16 17:02:57 +00005411}
5412
5413void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5414 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5415
5416 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005417 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005418 locations->SetInAt(0, Location::RequiresRegister());
5419 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005420 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005421 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005422
5423 // The register allocator does not support multiple
5424 // inputs that die at entry with one in a specific register.
5425 if (is_byte_type) {
5426 // Ensure the value is in a byte register.
5427 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005428 } else if (DataType::IsFloatingPointType(field_type)) {
5429 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005430 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5431 locations->SetInAt(1, Location::RequiresFpuRegister());
5432 } else {
5433 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5434 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005436 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005437 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005438
Calin Juravle52c48962014-12-16 17:02:57 +00005439 // 64bits value can be atomically written to an address with movsd and an XMM register.
5440 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5441 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5442 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5443 // isolated cases when we need this it isn't worth adding the extra complexity.
5444 locations->AddTemp(Location::RequiresFpuRegister());
5445 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005446 } else {
5447 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5448
5449 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5450 // Temporary registers for the write barrier.
5451 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5452 // Ensure the card is in a byte register.
5453 locations->AddTemp(Location::RegisterLocation(ECX));
5454 }
Calin Juravle52c48962014-12-16 17:02:57 +00005455 }
5456}
5457
5458void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005459 const FieldInfo& field_info,
5460 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005461 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5462
5463 LocationSummary* locations = instruction->GetLocations();
5464 Register base = locations->InAt(0).AsRegister<Register>();
5465 Location value = locations->InAt(1);
5466 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005467 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005468 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005469 bool needs_write_barrier =
5470 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005471
5472 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005473 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005474 }
5475
Mark Mendell81489372015-11-04 11:30:41 -05005476 bool maybe_record_implicit_null_check_done = false;
5477
Calin Juravle52c48962014-12-16 17:02:57 +00005478 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005479 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005480 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005481 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005482 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5483 break;
5484 }
5485
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005486 case DataType::Type::kUint16:
5487 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005488 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005489 __ movw(Address(base, offset),
5490 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005491 } else {
5492 __ movw(Address(base, offset), value.AsRegister<Register>());
5493 }
Calin Juravle52c48962014-12-16 17:02:57 +00005494 break;
5495 }
5496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 case DataType::Type::kInt32:
5498 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005499 if (kPoisonHeapReferences && needs_write_barrier) {
5500 // Note that in the case where `value` is a null reference,
5501 // we do not enter this block, as the reference does not
5502 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005504 Register temp = locations->GetTemp(0).AsRegister<Register>();
5505 __ movl(temp, value.AsRegister<Register>());
5506 __ PoisonHeapReference(temp);
5507 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005508 } else if (value.IsConstant()) {
5509 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5510 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005511 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005512 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005513 __ movl(Address(base, offset), value.AsRegister<Register>());
5514 }
Calin Juravle52c48962014-12-16 17:02:57 +00005515 break;
5516 }
5517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005518 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005519 if (is_volatile) {
5520 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5521 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5522 __ movd(temp1, value.AsRegisterPairLow<Register>());
5523 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5524 __ punpckldq(temp1, temp2);
5525 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005526 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005527 } else if (value.IsConstant()) {
5528 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5529 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5530 codegen_->MaybeRecordImplicitNullCheck(instruction);
5531 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005532 } else {
5533 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005534 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005535 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5536 }
Mark Mendell81489372015-11-04 11:30:41 -05005537 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005538 break;
5539 }
5540
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005541 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005542 if (value.IsConstant()) {
5543 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5544 __ movl(Address(base, offset), Immediate(v));
5545 } else {
5546 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5547 }
Calin Juravle52c48962014-12-16 17:02:57 +00005548 break;
5549 }
5550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005552 if (value.IsConstant()) {
5553 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5554 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5555 codegen_->MaybeRecordImplicitNullCheck(instruction);
5556 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5557 maybe_record_implicit_null_check_done = true;
5558 } else {
5559 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5560 }
Calin Juravle52c48962014-12-16 17:02:57 +00005561 break;
5562 }
5563
Aart Bik66c158e2018-01-31 12:55:04 -08005564 case DataType::Type::kUint32:
5565 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005567 LOG(FATAL) << "Unreachable type " << field_type;
5568 UNREACHABLE();
5569 }
5570
Mark Mendell81489372015-11-04 11:30:41 -05005571 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005572 codegen_->MaybeRecordImplicitNullCheck(instruction);
5573 }
5574
Roland Levillain4d027112015-07-01 15:41:14 +01005575 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005576 Register temp = locations->GetTemp(0).AsRegister<Register>();
5577 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005578 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005579 }
5580
Calin Juravle52c48962014-12-16 17:02:57 +00005581 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005582 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005583 }
5584}
5585
5586void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5587 HandleFieldGet(instruction, instruction->GetFieldInfo());
5588}
5589
5590void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5591 HandleFieldGet(instruction, instruction->GetFieldInfo());
5592}
5593
5594void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5595 HandleFieldSet(instruction, instruction->GetFieldInfo());
5596}
5597
5598void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005599 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005600}
5601
5602void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5603 HandleFieldSet(instruction, instruction->GetFieldInfo());
5604}
5605
5606void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005607 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005608}
5609
5610void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5611 HandleFieldGet(instruction, instruction->GetFieldInfo());
5612}
5613
5614void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5615 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005616}
5617
Vladimir Marko552a1342017-10-31 10:56:47 +00005618void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5619 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5620}
5621
5622void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5623 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5624 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5625}
5626
Calin Juravlee460d1d2015-09-29 04:52:17 +01005627void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5628 HUnresolvedInstanceFieldGet* instruction) {
5629 FieldAccessCallingConventionX86 calling_convention;
5630 codegen_->CreateUnresolvedFieldLocationSummary(
5631 instruction, instruction->GetFieldType(), calling_convention);
5632}
5633
5634void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5635 HUnresolvedInstanceFieldGet* instruction) {
5636 FieldAccessCallingConventionX86 calling_convention;
5637 codegen_->GenerateUnresolvedFieldAccess(instruction,
5638 instruction->GetFieldType(),
5639 instruction->GetFieldIndex(),
5640 instruction->GetDexPc(),
5641 calling_convention);
5642}
5643
5644void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5645 HUnresolvedInstanceFieldSet* instruction) {
5646 FieldAccessCallingConventionX86 calling_convention;
5647 codegen_->CreateUnresolvedFieldLocationSummary(
5648 instruction, instruction->GetFieldType(), calling_convention);
5649}
5650
5651void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5652 HUnresolvedInstanceFieldSet* instruction) {
5653 FieldAccessCallingConventionX86 calling_convention;
5654 codegen_->GenerateUnresolvedFieldAccess(instruction,
5655 instruction->GetFieldType(),
5656 instruction->GetFieldIndex(),
5657 instruction->GetDexPc(),
5658 calling_convention);
5659}
5660
5661void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5662 HUnresolvedStaticFieldGet* instruction) {
5663 FieldAccessCallingConventionX86 calling_convention;
5664 codegen_->CreateUnresolvedFieldLocationSummary(
5665 instruction, instruction->GetFieldType(), calling_convention);
5666}
5667
5668void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5669 HUnresolvedStaticFieldGet* instruction) {
5670 FieldAccessCallingConventionX86 calling_convention;
5671 codegen_->GenerateUnresolvedFieldAccess(instruction,
5672 instruction->GetFieldType(),
5673 instruction->GetFieldIndex(),
5674 instruction->GetDexPc(),
5675 calling_convention);
5676}
5677
5678void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5679 HUnresolvedStaticFieldSet* instruction) {
5680 FieldAccessCallingConventionX86 calling_convention;
5681 codegen_->CreateUnresolvedFieldLocationSummary(
5682 instruction, instruction->GetFieldType(), calling_convention);
5683}
5684
5685void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5686 HUnresolvedStaticFieldSet* instruction) {
5687 FieldAccessCallingConventionX86 calling_convention;
5688 codegen_->GenerateUnresolvedFieldAccess(instruction,
5689 instruction->GetFieldType(),
5690 instruction->GetFieldIndex(),
5691 instruction->GetDexPc(),
5692 calling_convention);
5693}
5694
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005696 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5697 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5698 ? Location::RequiresRegister()
5699 : Location::Any();
5700 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005701}
5702
Calin Juravle2ae48182016-03-16 14:05:09 +00005703void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5704 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005705 return;
5706 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005707 LocationSummary* locations = instruction->GetLocations();
5708 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005709
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005710 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005711 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005712}
5713
Calin Juravle2ae48182016-03-16 14:05:09 +00005714void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005715 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005716 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005717
5718 LocationSummary* locations = instruction->GetLocations();
5719 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720
5721 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005722 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005723 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005724 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005725 } else {
5726 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005727 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005728 __ jmp(slow_path->GetEntryLabel());
5729 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005730 }
5731 __ j(kEqual, slow_path->GetEntryLabel());
5732}
5733
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005734void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005735 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005736}
5737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005738void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005739 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005740 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005742 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5743 object_array_get_with_read_barrier
5744 ? LocationSummary::kCallOnSlowPath
5745 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005746 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005747 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005748 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005749 locations->SetInAt(0, Location::RequiresRegister());
5750 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005752 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5753 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005754 // The output overlaps in case of long: we don't want the low move
5755 // to overwrite the array's location. Likewise, in the case of an
5756 // object array get with read barriers enabled, we do not want the
5757 // move to overwrite the array's location, as we need it to emit
5758 // the read barrier.
5759 locations->SetOut(
5760 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005761 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5762 ? Location::kOutputOverlap
5763 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005764 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005765}
5766
5767void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5768 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005769 Location obj_loc = locations->InAt(0);
5770 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005771 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005772 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005773 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005776 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005777 case DataType::Type::kBool:
5778 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005779 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005780 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005781 break;
5782 }
5783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005785 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005786 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005787 break;
5788 }
5789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005790 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005791 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005792 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5793 // Branch cases into compressed and uncompressed for each index's type.
5794 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5795 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005796 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005798 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5799 "Expecting 0=compressed, 1=uncompressed");
5800 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005801 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5802 __ jmp(&done);
5803 __ Bind(&not_compressed);
5804 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5805 __ Bind(&done);
5806 } else {
5807 // Common case for charAt of array of char or when string compression's
5808 // feature is turned off.
5809 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005811 break;
5812 }
5813
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005814 case DataType::Type::kInt16: {
5815 Register out = out_loc.AsRegister<Register>();
5816 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5817 break;
5818 }
5819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005821 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005822 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005823 break;
5824 }
5825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005827 static_assert(
5828 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5829 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005830 // /* HeapReference<Object> */ out =
5831 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5832 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005833 // Note that a potential implicit null check is handled in this
5834 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5835 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005836 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005837 } else {
5838 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005839 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5840 codegen_->MaybeRecordImplicitNullCheck(instruction);
5841 // If read barriers are enabled, emit read barriers other than
5842 // Baker's using a slow path (and also unpoison the loaded
5843 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005844 if (index.IsConstant()) {
5845 uint32_t offset =
5846 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005847 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5848 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005849 codegen_->MaybeGenerateReadBarrierSlow(
5850 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5851 }
5852 }
5853 break;
5854 }
5855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005856 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005857 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005858 __ movl(out_loc.AsRegisterPairLow<Register>(),
5859 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5860 codegen_->MaybeRecordImplicitNullCheck(instruction);
5861 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5862 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005863 break;
5864 }
5865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005867 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005868 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005869 break;
5870 }
5871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005873 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005874 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005875 break;
5876 }
5877
Aart Bik66c158e2018-01-31 12:55:04 -08005878 case DataType::Type::kUint32:
5879 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005881 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005882 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005883 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005886 // Potential implicit null checks, in the case of reference or
5887 // long arrays, are handled in the previous switch statement.
5888 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005889 codegen_->MaybeRecordImplicitNullCheck(instruction);
5890 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005891}
5892
5893void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005896 bool needs_write_barrier =
5897 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005898 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005899
Vladimir Markoca6fff82017-10-03 14:49:14 +01005900 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005901 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005902 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005903
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005904 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005905 // We need the inputs to be different than the output in case of long operation.
5906 // In case of a byte operation, the register allocator does not support multiple
5907 // inputs that die at entry with one in a specific register.
5908 locations->SetInAt(0, Location::RequiresRegister());
5909 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5910 if (is_byte_type) {
5911 // Ensure the value is in a byte register.
5912 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005914 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005915 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005916 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5917 }
5918 if (needs_write_barrier) {
5919 // Temporary registers for the write barrier.
5920 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5921 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005922 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005923 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005924}
5925
5926void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5927 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928 Location array_loc = locations->InAt(0);
5929 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005930 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005931 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005932 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005933 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005934 bool needs_write_barrier =
5935 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936
5937 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005939 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005941 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005942 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005943 if (value.IsRegister()) {
5944 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005945 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005946 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005947 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005949 break;
5950 }
5951
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005952 case DataType::Type::kUint16:
5953 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005954 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005955 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005956 if (value.IsRegister()) {
5957 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005958 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005959 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005960 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005961 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005962 break;
5963 }
5964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005965 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005966 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005967 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005969 if (!value.IsRegister()) {
5970 // Just setting null.
5971 DCHECK(instruction->InputAt(2)->IsNullConstant());
5972 DCHECK(value.IsConstant()) << value;
5973 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005975 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005976 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005977 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005978 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005979
5980 DCHECK(needs_write_barrier);
5981 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005982 Location temp_loc = locations->GetTemp(0);
5983 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005984
5985 bool can_value_be_null = instruction->GetValueCanBeNull();
5986 NearLabel do_store;
5987 if (can_value_be_null) {
5988 __ testl(register_value, register_value);
5989 __ j(kEqual, &do_store);
5990 }
5991
5992 SlowPathCode* slow_path = nullptr;
5993 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005994 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005995 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005996
5997 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5998 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5999 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006000
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006001 // Note that when Baker read barriers are enabled, the type
6002 // checks are performed without read barriers. This is fine,
6003 // even in the case where a class object is in the from-space
6004 // after the flip, as a comparison involving such a type would
6005 // not produce a false positive; it may of course produce a
6006 // false negative, in which case we would take the ArraySet
6007 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006008
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006009 // /* HeapReference<Class> */ temp = array->klass_
6010 __ movl(temp, Address(array, class_offset));
6011 codegen_->MaybeRecordImplicitNullCheck(instruction);
6012 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006013
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006014 // /* HeapReference<Class> */ temp = temp->component_type_
6015 __ movl(temp, Address(temp, component_offset));
6016 // If heap poisoning is enabled, no need to unpoison `temp`
6017 // nor the object reference in `register_value->klass`, as
6018 // we are comparing two poisoned references.
6019 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006020
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006021 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006022 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006023 __ j(kEqual, &do_put);
6024 // If heap poisoning is enabled, the `temp` reference has
6025 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006026 __ MaybeUnpoisonHeapReference(temp);
6027
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006028 // If heap poisoning is enabled, no need to unpoison the
6029 // heap reference loaded below, as it is only used for a
6030 // comparison with null.
6031 __ cmpl(Address(temp, super_offset), Immediate(0));
6032 __ j(kNotEqual, slow_path->GetEntryLabel());
6033 __ Bind(&do_put);
6034 } else {
6035 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006036 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006037 }
6038
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006039 Register card = locations->GetTemp(1).AsRegister<Register>();
6040 codegen_->MarkGCCard(
6041 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6042
6043 if (can_value_be_null) {
6044 DCHECK(do_store.IsLinked());
6045 __ Bind(&do_store);
6046 }
6047
6048 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006049 if (kPoisonHeapReferences) {
6050 __ movl(temp, register_value);
6051 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006052 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006053 }
6054
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006055 __ movl(address, source);
6056
6057 if (can_value_be_null || !needs_type_check) {
6058 codegen_->MaybeRecordImplicitNullCheck(instruction);
6059 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006060
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006061 if (slow_path != nullptr) {
6062 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006063 }
6064
6065 break;
6066 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006068 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006069 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006070 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006071 if (value.IsRegister()) {
6072 __ movl(address, value.AsRegister<Register>());
6073 } else {
6074 DCHECK(value.IsConstant()) << value;
6075 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6076 __ movl(address, Immediate(v));
6077 }
6078 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006079 break;
6080 }
6081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006084 if (value.IsRegisterPair()) {
6085 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6086 value.AsRegisterPairLow<Register>());
6087 codegen_->MaybeRecordImplicitNullCheck(instruction);
6088 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6089 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006090 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006091 DCHECK(value.IsConstant());
6092 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6093 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6094 Immediate(Low32Bits(val)));
6095 codegen_->MaybeRecordImplicitNullCheck(instruction);
6096 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6097 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098 }
6099 break;
6100 }
6101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006102 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006103 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006104 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006105 if (value.IsFpuRegister()) {
6106 __ movss(address, value.AsFpuRegister<XmmRegister>());
6107 } else {
6108 DCHECK(value.IsConstant());
6109 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6110 __ movl(address, Immediate(v));
6111 }
6112 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006113 break;
6114 }
6115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006116 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006117 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006118 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006119 if (value.IsFpuRegister()) {
6120 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6121 } else {
6122 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006123 Address address_hi =
6124 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006125 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6126 __ movl(address, Immediate(Low32Bits(v)));
6127 codegen_->MaybeRecordImplicitNullCheck(instruction);
6128 __ movl(address_hi, Immediate(High32Bits(v)));
6129 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006130 break;
6131 }
6132
Aart Bik66c158e2018-01-31 12:55:04 -08006133 case DataType::Type::kUint32:
6134 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006135 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006136 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006137 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006138 }
6139}
6140
6141void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006142 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006143 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006144 if (!instruction->IsEmittedAtUseSite()) {
6145 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6146 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006147}
6148
6149void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006150 if (instruction->IsEmittedAtUseSite()) {
6151 return;
6152 }
6153
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006154 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006155 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006156 Register obj = locations->InAt(0).AsRegister<Register>();
6157 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006158 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006159 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006160 // Mask out most significant bit in case the array is String's array of char.
6161 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006162 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006163 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006164}
6165
6166void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006167 RegisterSet caller_saves = RegisterSet::Empty();
6168 InvokeRuntimeCallingConvention calling_convention;
6169 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6170 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6171 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006172 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006173 HInstruction* length = instruction->InputAt(1);
6174 if (!length->IsEmittedAtUseSite()) {
6175 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6176 }
jessicahandojo4877b792016-09-08 19:49:13 -07006177 // Need register to see array's length.
6178 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6179 locations->AddTemp(Location::RequiresRegister());
6180 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006181}
6182
6183void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006184 const bool is_string_compressed_char_at =
6185 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006186 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006187 Location index_loc = locations->InAt(0);
6188 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006189 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006190 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006191
Mark Mendell99dbd682015-04-22 16:18:52 -04006192 if (length_loc.IsConstant()) {
6193 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6194 if (index_loc.IsConstant()) {
6195 // BCE will remove the bounds check if we are guarenteed to pass.
6196 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6197 if (index < 0 || index >= length) {
6198 codegen_->AddSlowPath(slow_path);
6199 __ jmp(slow_path->GetEntryLabel());
6200 } else {
6201 // Some optimization after BCE may have generated this, and we should not
6202 // generate a bounds check if it is a valid range.
6203 }
6204 return;
6205 }
6206
6207 // We have to reverse the jump condition because the length is the constant.
6208 Register index_reg = index_loc.AsRegister<Register>();
6209 __ cmpl(index_reg, Immediate(length));
6210 codegen_->AddSlowPath(slow_path);
6211 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006212 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006213 HInstruction* array_length = instruction->InputAt(1);
6214 if (array_length->IsEmittedAtUseSite()) {
6215 // Address the length field in the array.
6216 DCHECK(array_length->IsArrayLength());
6217 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6218 Location array_loc = array_length->GetLocations()->InAt(0);
6219 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006220 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006221 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6222 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006223 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6224 __ movl(length_reg, array_len);
6225 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006226 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006227 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006228 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006229 // Checking bounds for general case:
6230 // Array of char or string's array with feature compression off.
6231 if (index_loc.IsConstant()) {
6232 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6233 __ cmpl(array_len, Immediate(value));
6234 } else {
6235 __ cmpl(array_len, index_loc.AsRegister<Register>());
6236 }
6237 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006238 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006239 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006240 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006241 }
6242 codegen_->AddSlowPath(slow_path);
6243 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006244 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006245}
6246
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006247void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006248 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006249}
6250
6251void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006252 if (instruction->GetNext()->IsSuspendCheck() &&
6253 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6254 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6255 // The back edge will generate the suspend check.
6256 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6257 }
6258
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006259 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6260}
6261
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006262void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006263 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6264 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006265 // In suspend check slow path, usually there are no caller-save registers at all.
6266 // If SIMD instructions are present, however, we force spilling all live SIMD
6267 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006268 locations->SetCustomSlowPathCallerSaves(
6269 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006270}
6271
6272void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006273 HBasicBlock* block = instruction->GetBlock();
6274 if (block->GetLoopInformation() != nullptr) {
6275 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6276 // The back edge will generate the suspend check.
6277 return;
6278 }
6279 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6280 // The goto will generate the suspend check.
6281 return;
6282 }
6283 GenerateSuspendCheck(instruction, nullptr);
6284}
6285
6286void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6287 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006288 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006289 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6290 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006291 slow_path =
6292 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006293 instruction->SetSlowPath(slow_path);
6294 codegen_->AddSlowPath(slow_path);
6295 if (successor != nullptr) {
6296 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006297 }
6298 } else {
6299 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6300 }
6301
Andreas Gampe542451c2016-07-26 09:02:02 -07006302 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006303 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006304 if (successor == nullptr) {
6305 __ j(kNotEqual, slow_path->GetEntryLabel());
6306 __ Bind(slow_path->GetReturnLabel());
6307 } else {
6308 __ j(kEqual, codegen_->GetLabelOf(successor));
6309 __ jmp(slow_path->GetEntryLabel());
6310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006311}
6312
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006313X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6314 return codegen_->GetAssembler();
6315}
6316
Aart Bikcfe50bb2017-12-12 14:54:12 -08006317void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006318 ScratchRegisterScope ensure_scratch(
6319 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6320 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6321 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006322
Aart Bikcfe50bb2017-12-12 14:54:12 -08006323 // Now that temp register is available (possibly spilled), move blocks of memory.
6324 for (int i = 0; i < number_of_words; i++) {
6325 __ movl(temp_reg, Address(ESP, src + stack_offset));
6326 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6327 stack_offset += kX86WordSize;
6328 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006329}
6330
6331void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006332 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006333 Location source = move->GetSource();
6334 Location destination = move->GetDestination();
6335
6336 if (source.IsRegister()) {
6337 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006338 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006339 } else if (destination.IsFpuRegister()) {
6340 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006341 } else {
6342 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006343 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006344 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006345 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006347 // Create stack space for 2 elements.
6348 __ subl(ESP, Immediate(2 * elem_size));
6349 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6350 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6351 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6352 // And remove the temporary stack space we allocated.
6353 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006354 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006355 if (destination.IsRegister()) {
6356 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6357 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006358 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006359 } else if (destination.IsRegisterPair()) {
6360 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6361 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6362 __ psrlq(src_reg, Immediate(32));
6363 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006364 } else if (destination.IsStackSlot()) {
6365 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006366 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006367 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006368 } else {
6369 DCHECK(destination.IsSIMDStackSlot());
6370 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006371 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006372 } else if (source.IsStackSlot()) {
6373 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006374 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006375 } else if (destination.IsFpuRegister()) {
6376 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006377 } else {
6378 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006379 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006380 }
6381 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006382 if (destination.IsRegisterPair()) {
6383 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6384 __ movl(destination.AsRegisterPairHigh<Register>(),
6385 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6386 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006387 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6388 } else {
6389 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006390 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006391 }
Aart Bik5576f372017-03-23 16:17:37 -07006392 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006393 if (destination.IsFpuRegister()) {
6394 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6395 } else {
6396 DCHECK(destination.IsSIMDStackSlot());
6397 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6398 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006399 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006400 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006401 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006402 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006403 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006404 if (value == 0) {
6405 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6406 } else {
6407 __ movl(destination.AsRegister<Register>(), Immediate(value));
6408 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006409 } else {
6410 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006411 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006412 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006413 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006414 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006415 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006416 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006417 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006418 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6419 if (value == 0) {
6420 // Easy handling of 0.0.
6421 __ xorps(dest, dest);
6422 } else {
6423 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006424 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6425 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6426 __ movl(temp, Immediate(value));
6427 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006428 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006429 } else {
6430 DCHECK(destination.IsStackSlot()) << destination;
6431 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6432 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006433 } else if (constant->IsLongConstant()) {
6434 int64_t value = constant->AsLongConstant()->GetValue();
6435 int32_t low_value = Low32Bits(value);
6436 int32_t high_value = High32Bits(value);
6437 Immediate low(low_value);
6438 Immediate high(high_value);
6439 if (destination.IsDoubleStackSlot()) {
6440 __ movl(Address(ESP, destination.GetStackIndex()), low);
6441 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6442 } else {
6443 __ movl(destination.AsRegisterPairLow<Register>(), low);
6444 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6445 }
6446 } else {
6447 DCHECK(constant->IsDoubleConstant());
6448 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006449 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006450 int32_t low_value = Low32Bits(value);
6451 int32_t high_value = High32Bits(value);
6452 Immediate low(low_value);
6453 Immediate high(high_value);
6454 if (destination.IsFpuRegister()) {
6455 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6456 if (value == 0) {
6457 // Easy handling of 0.0.
6458 __ xorpd(dest, dest);
6459 } else {
6460 __ pushl(high);
6461 __ pushl(low);
6462 __ movsd(dest, Address(ESP, 0));
6463 __ addl(ESP, Immediate(8));
6464 }
6465 } else {
6466 DCHECK(destination.IsDoubleStackSlot()) << destination;
6467 __ movl(Address(ESP, destination.GetStackIndex()), low);
6468 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6469 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006470 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006471 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006472 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006473 }
6474}
6475
Mark Mendella5c19ce2015-04-01 12:51:05 -04006476void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006477 Register suggested_scratch = reg == EAX ? EBX : EAX;
6478 ScratchRegisterScope ensure_scratch(
6479 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6480
6481 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6482 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6483 __ movl(Address(ESP, mem + stack_offset), reg);
6484 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006485}
6486
Mark Mendell7c8d0092015-01-26 11:21:33 -05006487void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006488 ScratchRegisterScope ensure_scratch(
6489 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6490
6491 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6492 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6493 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6494 __ movss(Address(ESP, mem + stack_offset), reg);
6495 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006496}
6497
Aart Bikcfe50bb2017-12-12 14:54:12 -08006498void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6499 size_t extra_slot = 4 * kX86WordSize;
6500 __ subl(ESP, Immediate(extra_slot));
6501 __ movups(Address(ESP, 0), XmmRegister(reg));
6502 ExchangeMemory(0, mem + extra_slot, 4);
6503 __ movups(XmmRegister(reg), Address(ESP, 0));
6504 __ addl(ESP, Immediate(extra_slot));
6505}
6506
6507void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006508 ScratchRegisterScope ensure_scratch1(
6509 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006510
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006511 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6512 ScratchRegisterScope ensure_scratch2(
6513 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006514
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006515 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6516 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006517
6518 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6519 for (int i = 0; i < number_of_words; i++) {
6520 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6521 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6522 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6523 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6524 stack_offset += kX86WordSize;
6525 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006526}
6527
6528void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006529 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006530 Location source = move->GetSource();
6531 Location destination = move->GetDestination();
6532
6533 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006534 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6535 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6536 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6537 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6538 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006539 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006540 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006541 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006542 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006543 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006544 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006545 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6546 // Use XOR Swap algorithm to avoid a temporary.
6547 DCHECK_NE(source.reg(), destination.reg());
6548 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6549 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6550 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6551 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6552 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6553 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6554 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006555 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6556 // Take advantage of the 16 bytes in the XMM register.
6557 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6558 Address stack(ESP, destination.GetStackIndex());
6559 // Load the double into the high doubleword.
6560 __ movhpd(reg, stack);
6561
6562 // Store the low double into the destination.
6563 __ movsd(stack, reg);
6564
6565 // Move the high double to the low double.
6566 __ psrldq(reg, Immediate(8));
6567 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6568 // Take advantage of the 16 bytes in the XMM register.
6569 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6570 Address stack(ESP, source.GetStackIndex());
6571 // Load the double into the high doubleword.
6572 __ movhpd(reg, stack);
6573
6574 // Store the low double into the destination.
6575 __ movsd(stack, reg);
6576
6577 // Move the high double to the low double.
6578 __ psrldq(reg, Immediate(8));
6579 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006580 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6581 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6582 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6583 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6584 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6585 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6586 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006587 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006588 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006589 }
6590}
6591
6592void ParallelMoveResolverX86::SpillScratch(int reg) {
6593 __ pushl(static_cast<Register>(reg));
6594}
6595
6596void ParallelMoveResolverX86::RestoreScratch(int reg) {
6597 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006598}
6599
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006600HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6601 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006602 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006603 case HLoadClass::LoadKind::kInvalid:
6604 LOG(FATAL) << "UNREACHABLE";
6605 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006606 case HLoadClass::LoadKind::kReferrersClass:
6607 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006608 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006609 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006610 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006611 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006612 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006613 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006614 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006615 DCHECK(Runtime::Current()->UseJitCompilation());
6616 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006617 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006618 break;
6619 }
6620 return desired_class_load_kind;
6621}
6622
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006623void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006624 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006625 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006626 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006627 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006628 cls,
6629 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006630 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006631 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006632 return;
6633 }
Vladimir Marko41559982017-01-06 14:04:23 +00006634 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006635
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006636 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6637 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006638 ? LocationSummary::kCallOnSlowPath
6639 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006640 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006641 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006642 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006643 }
6644
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006645 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006646 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006647 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006648 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006649 locations->SetInAt(0, Location::RequiresRegister());
6650 }
6651 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006652 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6653 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6654 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006655 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006656 } else {
6657 // For non-Baker read barrier we have a temp-clobbering call.
6658 }
6659 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006660}
6661
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006662Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006663 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006664 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006665 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006666 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006667 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006668 PatchInfo<Label>* info = &jit_class_patches_.back();
6669 return &info->label;
6670}
6671
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006672// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6673// move.
6674void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006675 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006676 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006677 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006678 return;
6679 }
Vladimir Marko41559982017-01-06 14:04:23 +00006680 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006681
Vladimir Marko41559982017-01-06 14:04:23 +00006682 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 Location out_loc = locations->Out();
6684 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006686 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006687 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6688 ? kWithoutReadBarrier
6689 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006690 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006691 case HLoadClass::LoadKind::kReferrersClass: {
6692 DCHECK(!cls->CanCallRuntime());
6693 DCHECK(!cls->MustGenerateClinitCheck());
6694 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6695 Register current_method = locations->InAt(0).AsRegister<Register>();
6696 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006697 cls,
6698 out_loc,
6699 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006700 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006701 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006702 break;
6703 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006704 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006705 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6706 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006707 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006708 Register method_address = locations->InAt(0).AsRegister<Register>();
6709 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006710 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006711 break;
6712 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006713 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006714 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6715 Register method_address = locations->InAt(0).AsRegister<Register>();
6716 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006717 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6718 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006719 break;
6720 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006721 case HLoadClass::LoadKind::kBssEntry: {
6722 Register method_address = locations->InAt(0).AsRegister<Register>();
6723 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6724 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6725 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006726 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006727 generate_null_check = true;
6728 break;
6729 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006730 case HLoadClass::LoadKind::kJitBootImageAddress: {
6731 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6732 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6733 DCHECK_NE(address, 0u);
6734 __ movl(out, Immediate(address));
6735 break;
6736 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006737 case HLoadClass::LoadKind::kJitTableAddress: {
6738 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6739 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006740 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006741 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006742 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006743 break;
6744 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006745 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006746 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006747 LOG(FATAL) << "UNREACHABLE";
6748 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006749 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006750
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006751 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6752 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006753 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006754 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006755
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006756 if (generate_null_check) {
6757 __ testl(out, out);
6758 __ j(kEqual, slow_path->GetEntryLabel());
6759 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006760
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006761 if (cls->MustGenerateClinitCheck()) {
6762 GenerateClassInitializationCheck(slow_path, out);
6763 } else {
6764 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006765 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766 }
6767}
6768
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006769void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6770 InvokeRuntimeCallingConvention calling_convention;
6771 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6772 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6773}
6774
6775void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6776 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6777}
6778
Orion Hodson18259d72018-04-12 11:18:23 +01006779void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6780 InvokeRuntimeCallingConvention calling_convention;
6781 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6782 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6783}
6784
6785void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6786 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6787}
6788
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006789void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6790 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006791 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006792 locations->SetInAt(0, Location::RequiresRegister());
6793 if (check->HasUses()) {
6794 locations->SetOut(Location::SameAsFirstInput());
6795 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006796 // Rely on the type initialization to save everything we need.
6797 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006798}
6799
6800void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006801 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006802 SlowPathCode* slow_path =
6803 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006804 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006805 GenerateClassInitializationCheck(slow_path,
6806 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006807}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006809void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006810 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006811 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6812 const size_t status_byte_offset =
6813 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006814 constexpr uint32_t shifted_visibly_initialized_value =
6815 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006816
Vladimir Markobf121912019-06-04 13:49:05 +01006817 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006818 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006819 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006820}
6821
Vladimir Marko175e7862018-03-27 09:03:13 +00006822void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6823 Register temp) {
6824 uint32_t path_to_root = check->GetBitstringPathToRoot();
6825 uint32_t mask = check->GetBitstringMask();
6826 DCHECK(IsPowerOfTwo(mask + 1));
6827 size_t mask_bits = WhichPowerOf2(mask + 1);
6828
6829 if (mask_bits == 16u) {
6830 // Compare the bitstring in memory.
6831 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6832 } else {
6833 // /* uint32_t */ temp = temp->status_
6834 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6835 // Compare the bitstring bits using SUB.
6836 __ subl(temp, Immediate(path_to_root));
6837 // Shift out bits that do not contribute to the comparison.
6838 __ shll(temp, Immediate(32u - mask_bits));
6839 }
6840}
6841
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006842HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6843 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006844 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006845 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006846 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006847 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006848 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006849 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006850 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006851 case HLoadString::LoadKind::kJitTableAddress:
6852 DCHECK(Runtime::Current()->UseJitCompilation());
6853 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006854 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006855 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006856 }
6857 return desired_string_load_kind;
6858}
6859
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006860void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006861 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006862 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006863 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006864 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006865 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006866 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006867 locations->SetInAt(0, Location::RequiresRegister());
6868 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006869 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006870 locations->SetOut(Location::RegisterLocation(EAX));
6871 } else {
6872 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006873 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6874 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006875 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006876 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006877 } else {
6878 // For non-Baker read barrier we have a temp-clobbering call.
6879 }
6880 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006881 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006882}
6883
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006884Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006885 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006886 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006887 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006888 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006889 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006890 PatchInfo<Label>* info = &jit_string_patches_.back();
6891 return &info->label;
6892}
6893
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006894// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6895// move.
6896void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006897 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006898 Location out_loc = locations->Out();
6899 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006900
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006901 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006902 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006903 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6904 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006905 Register method_address = locations->InAt(0).AsRegister<Register>();
6906 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006907 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006908 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006909 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006910 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006911 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6912 Register method_address = locations->InAt(0).AsRegister<Register>();
6913 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006914 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6915 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006916 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006917 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006918 case HLoadString::LoadKind::kBssEntry: {
6919 Register method_address = locations->InAt(0).AsRegister<Register>();
6920 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6921 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006922 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006923 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006924 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006925 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006926 codegen_->AddSlowPath(slow_path);
6927 __ testl(out, out);
6928 __ j(kEqual, slow_path->GetEntryLabel());
6929 __ Bind(slow_path->GetExitLabel());
6930 return;
6931 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006932 case HLoadString::LoadKind::kJitBootImageAddress: {
6933 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6934 DCHECK_NE(address, 0u);
6935 __ movl(out, Immediate(address));
6936 return;
6937 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006938 case HLoadString::LoadKind::kJitTableAddress: {
6939 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6940 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006941 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006942 // /* GcRoot<mirror::String> */ out = *address
6943 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6944 return;
6945 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006946 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006947 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006948 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006949
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006950 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006951 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006952 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006953 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006954 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6955 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006956}
6957
David Brazdilcb1c0552015-08-04 16:22:25 +01006958static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006959 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006960}
6961
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006962void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6963 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006964 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006965 locations->SetOut(Location::RequiresRegister());
6966}
6967
6968void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006969 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6970}
6971
6972void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006973 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006974}
6975
6976void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6977 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006978}
6979
6980void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006981 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6982 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006983 InvokeRuntimeCallingConvention calling_convention;
6984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6985}
6986
6987void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006988 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006989 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006990}
6991
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006992// Temp is used for read barrier.
6993static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6994 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006995 !kUseBakerReadBarrier &&
6996 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006997 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006998 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6999 return 1;
7000 }
7001 return 0;
7002}
7003
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007004// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7005// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007006// The other checks have one temp for loading the object's class.
7007static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007008 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007009 return 2;
7010 }
7011 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007012}
7013
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007014void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007015 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007016 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007017 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007018 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007019 case TypeCheckKind::kExactCheck:
7020 case TypeCheckKind::kAbstractClassCheck:
7021 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007022 case TypeCheckKind::kArrayObjectCheck: {
7023 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7024 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7025 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007026 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007027 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007028 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007029 case TypeCheckKind::kUnresolvedCheck:
7030 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007031 call_kind = LocationSummary::kCallOnSlowPath;
7032 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007033 case TypeCheckKind::kBitstringCheck:
7034 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007035 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007036
Vladimir Markoca6fff82017-10-03 14:49:14 +01007037 LocationSummary* locations =
7038 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007039 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007040 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007041 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007042 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007043 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7044 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7045 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7046 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7047 } else {
7048 locations->SetInAt(1, Location::Any());
7049 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007050 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7051 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007052 // When read barriers are enabled, we need a temporary register for some cases.
7053 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007054}
7055
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007056void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007057 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007058 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007059 Location obj_loc = locations->InAt(0);
7060 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007061 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007062 Location out_loc = locations->Out();
7063 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007064 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7065 DCHECK_LE(num_temps, 1u);
7066 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007067 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007068 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7069 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7070 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007071 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007072 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007073
7074 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007075 // Avoid null check if we know obj is not null.
7076 if (instruction->MustDoNullCheck()) {
7077 __ testl(obj, obj);
7078 __ j(kEqual, &zero);
7079 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007080
Roland Levillain7c1559a2015-12-15 10:55:36 +00007081 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007082 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007083 ReadBarrierOption read_barrier_option =
7084 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007085 // /* HeapReference<Class> */ out = obj->klass_
7086 GenerateReferenceLoadTwoRegisters(instruction,
7087 out_loc,
7088 obj_loc,
7089 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007090 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007091 if (cls.IsRegister()) {
7092 __ cmpl(out, cls.AsRegister<Register>());
7093 } else {
7094 DCHECK(cls.IsStackSlot()) << cls;
7095 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7096 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007097
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007098 // Classes must be equal for the instanceof to succeed.
7099 __ j(kNotEqual, &zero);
7100 __ movl(out, Immediate(1));
7101 __ jmp(&done);
7102 break;
7103 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007104
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007105 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007106 ReadBarrierOption read_barrier_option =
7107 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007108 // /* HeapReference<Class> */ out = obj->klass_
7109 GenerateReferenceLoadTwoRegisters(instruction,
7110 out_loc,
7111 obj_loc,
7112 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007113 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007114 // If the class is abstract, we eagerly fetch the super class of the
7115 // object to avoid doing a comparison we know will fail.
7116 NearLabel loop;
7117 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007118 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007119 GenerateReferenceLoadOneRegister(instruction,
7120 out_loc,
7121 super_offset,
7122 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007123 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007124 __ testl(out, out);
7125 // If `out` is null, we use it for the result, and jump to `done`.
7126 __ j(kEqual, &done);
7127 if (cls.IsRegister()) {
7128 __ cmpl(out, cls.AsRegister<Register>());
7129 } else {
7130 DCHECK(cls.IsStackSlot()) << cls;
7131 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7132 }
7133 __ j(kNotEqual, &loop);
7134 __ movl(out, Immediate(1));
7135 if (zero.IsLinked()) {
7136 __ jmp(&done);
7137 }
7138 break;
7139 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007140
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007141 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007142 ReadBarrierOption read_barrier_option =
7143 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007144 // /* HeapReference<Class> */ out = obj->klass_
7145 GenerateReferenceLoadTwoRegisters(instruction,
7146 out_loc,
7147 obj_loc,
7148 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007149 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007150 // Walk over the class hierarchy to find a match.
7151 NearLabel loop, success;
7152 __ Bind(&loop);
7153 if (cls.IsRegister()) {
7154 __ cmpl(out, cls.AsRegister<Register>());
7155 } else {
7156 DCHECK(cls.IsStackSlot()) << cls;
7157 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7158 }
7159 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007160 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007161 GenerateReferenceLoadOneRegister(instruction,
7162 out_loc,
7163 super_offset,
7164 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007165 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007166 __ testl(out, out);
7167 __ j(kNotEqual, &loop);
7168 // If `out` is null, we use it for the result, and jump to `done`.
7169 __ jmp(&done);
7170 __ Bind(&success);
7171 __ movl(out, Immediate(1));
7172 if (zero.IsLinked()) {
7173 __ jmp(&done);
7174 }
7175 break;
7176 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007178 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007179 ReadBarrierOption read_barrier_option =
7180 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007181 // /* HeapReference<Class> */ out = obj->klass_
7182 GenerateReferenceLoadTwoRegisters(instruction,
7183 out_loc,
7184 obj_loc,
7185 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007186 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007187 // Do an exact check.
7188 NearLabel exact_check;
7189 if (cls.IsRegister()) {
7190 __ cmpl(out, cls.AsRegister<Register>());
7191 } else {
7192 DCHECK(cls.IsStackSlot()) << cls;
7193 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7194 }
7195 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007196 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007197 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007198 GenerateReferenceLoadOneRegister(instruction,
7199 out_loc,
7200 component_offset,
7201 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007202 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007203 __ testl(out, out);
7204 // If `out` is null, we use it for the result, and jump to `done`.
7205 __ j(kEqual, &done);
7206 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7207 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007208 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007209 __ movl(out, Immediate(1));
7210 __ jmp(&done);
7211 break;
7212 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007213
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007214 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007215 // No read barrier since the slow path will retry upon failure.
7216 // /* HeapReference<Class> */ out = obj->klass_
7217 GenerateReferenceLoadTwoRegisters(instruction,
7218 out_loc,
7219 obj_loc,
7220 class_offset,
7221 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007222 if (cls.IsRegister()) {
7223 __ cmpl(out, cls.AsRegister<Register>());
7224 } else {
7225 DCHECK(cls.IsStackSlot()) << cls;
7226 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7227 }
7228 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007229 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007230 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007231 codegen_->AddSlowPath(slow_path);
7232 __ j(kNotEqual, slow_path->GetEntryLabel());
7233 __ movl(out, Immediate(1));
7234 if (zero.IsLinked()) {
7235 __ jmp(&done);
7236 }
7237 break;
7238 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007239
Calin Juravle98893e12015-10-02 21:05:03 +01007240 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007241 case TypeCheckKind::kInterfaceCheck: {
7242 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007243 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007244 // cases.
7245 //
7246 // We cannot directly call the InstanceofNonTrivial runtime
7247 // entry point without resorting to a type checking slow path
7248 // here (i.e. by calling InvokeRuntime directly), as it would
7249 // require to assign fixed registers for the inputs of this
7250 // HInstanceOf instruction (following the runtime calling
7251 // convention), which might be cluttered by the potential first
7252 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007253 //
7254 // TODO: Introduce a new runtime entry point taking the object
7255 // to test (instead of its class) as argument, and let it deal
7256 // with the read barrier issues. This will let us refactor this
7257 // case of the `switch` code as it was previously (with a direct
7258 // call to the runtime not using a type checking slow path).
7259 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007260 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007261 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007262 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007263 codegen_->AddSlowPath(slow_path);
7264 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007265 if (zero.IsLinked()) {
7266 __ jmp(&done);
7267 }
7268 break;
7269 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007270
7271 case TypeCheckKind::kBitstringCheck: {
7272 // /* HeapReference<Class> */ temp = obj->klass_
7273 GenerateReferenceLoadTwoRegisters(instruction,
7274 out_loc,
7275 obj_loc,
7276 class_offset,
7277 kWithoutReadBarrier);
7278
7279 GenerateBitstringTypeCheckCompare(instruction, out);
7280 __ j(kNotEqual, &zero);
7281 __ movl(out, Immediate(1));
7282 __ jmp(&done);
7283 break;
7284 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007285 }
7286
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007287 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007288 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007289 __ xorl(out, out);
7290 }
7291
7292 if (done.IsLinked()) {
7293 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007294 }
7295
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007296 if (slow_path != nullptr) {
7297 __ Bind(slow_path->GetExitLabel());
7298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007299}
7300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007301void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007302 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007303 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007304 LocationSummary* locations =
7305 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007306 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007307 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7308 // Require a register for the interface check since there is a loop that compares the class to
7309 // a memory address.
7310 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007311 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7312 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7313 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7314 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007315 } else {
7316 locations->SetInAt(1, Location::Any());
7317 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007318 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007319 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7320}
7321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007322void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007323 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007324 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007325 Location obj_loc = locations->InAt(0);
7326 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007327 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 Location temp_loc = locations->GetTemp(0);
7329 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007330 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7331 DCHECK_GE(num_temps, 1u);
7332 DCHECK_LE(num_temps, 2u);
7333 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7334 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7335 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7336 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7337 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7338 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7339 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7340 const uint32_t object_array_data_offset =
7341 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007342
Vladimir Marko87584542017-12-12 17:47:52 +00007343 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007345 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7346 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007347 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007348
Roland Levillain0d5a2812015-11-13 10:07:31 +00007349 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007350 // Avoid null check if we know obj is not null.
7351 if (instruction->MustDoNullCheck()) {
7352 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007354 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007355
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007357 case TypeCheckKind::kExactCheck:
7358 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007359 // /* HeapReference<Class> */ temp = obj->klass_
7360 GenerateReferenceLoadTwoRegisters(instruction,
7361 temp_loc,
7362 obj_loc,
7363 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007364 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007366 if (cls.IsRegister()) {
7367 __ cmpl(temp, cls.AsRegister<Register>());
7368 } else {
7369 DCHECK(cls.IsStackSlot()) << cls;
7370 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7371 }
7372 // Jump to slow path for throwing the exception or doing a
7373 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007374 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007375 break;
7376 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007377
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007378 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007379 // /* HeapReference<Class> */ temp = obj->klass_
7380 GenerateReferenceLoadTwoRegisters(instruction,
7381 temp_loc,
7382 obj_loc,
7383 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007384 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007385
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007386 // If the class is abstract, we eagerly fetch the super class of the
7387 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007388 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007389 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007390 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007391 GenerateReferenceLoadOneRegister(instruction,
7392 temp_loc,
7393 super_offset,
7394 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007395 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007396
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007397 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7398 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007399 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007400 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007401
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007402 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007403 if (cls.IsRegister()) {
7404 __ cmpl(temp, cls.AsRegister<Register>());
7405 } else {
7406 DCHECK(cls.IsStackSlot()) << cls;
7407 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7408 }
7409 __ j(kNotEqual, &loop);
7410 break;
7411 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007413 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007414 // /* HeapReference<Class> */ temp = obj->klass_
7415 GenerateReferenceLoadTwoRegisters(instruction,
7416 temp_loc,
7417 obj_loc,
7418 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007419 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007420
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007421 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007422 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007423 __ Bind(&loop);
7424 if (cls.IsRegister()) {
7425 __ cmpl(temp, cls.AsRegister<Register>());
7426 } else {
7427 DCHECK(cls.IsStackSlot()) << cls;
7428 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7429 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007430 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007431
Roland Levillain0d5a2812015-11-13 10:07:31 +00007432 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007433 GenerateReferenceLoadOneRegister(instruction,
7434 temp_loc,
7435 super_offset,
7436 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007437 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007438
7439 // If the class reference currently in `temp` is not null, jump
7440 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007441 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007442 __ j(kNotZero, &loop);
7443 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007444 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007445 break;
7446 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007448 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007449 // /* HeapReference<Class> */ temp = obj->klass_
7450 GenerateReferenceLoadTwoRegisters(instruction,
7451 temp_loc,
7452 obj_loc,
7453 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007454 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007455
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007456 // Do an exact check.
7457 if (cls.IsRegister()) {
7458 __ cmpl(temp, cls.AsRegister<Register>());
7459 } else {
7460 DCHECK(cls.IsStackSlot()) << cls;
7461 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7462 }
7463 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007464
7465 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007466 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007467 GenerateReferenceLoadOneRegister(instruction,
7468 temp_loc,
7469 component_offset,
7470 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007471 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007472
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007473 // If the component type is null (i.e. the object not an array), jump to the slow path to
7474 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007475 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007476 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007477
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007478 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007479 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007480 break;
7481 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007482
Calin Juravle98893e12015-10-02 21:05:03 +01007483 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007484 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007485 // We cannot directly call the CheckCast runtime entry point
7486 // without resorting to a type checking slow path here (i.e. by
7487 // calling InvokeRuntime directly), as it would require to
7488 // assign fixed registers for the inputs of this HInstanceOf
7489 // instruction (following the runtime calling convention), which
7490 // might be cluttered by the potential first read barrier
7491 // emission at the beginning of this method.
7492 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007493 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007494
7495 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007496 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7497 // We can not get false positives by doing this.
7498 // /* HeapReference<Class> */ temp = obj->klass_
7499 GenerateReferenceLoadTwoRegisters(instruction,
7500 temp_loc,
7501 obj_loc,
7502 class_offset,
7503 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007504
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007505 // /* HeapReference<Class> */ temp = temp->iftable_
7506 GenerateReferenceLoadTwoRegisters(instruction,
7507 temp_loc,
7508 temp_loc,
7509 iftable_offset,
7510 kWithoutReadBarrier);
7511 // Iftable is never null.
7512 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7513 // Maybe poison the `cls` for direct comparison with memory.
7514 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7515 // Loop through the iftable and check if any class matches.
7516 NearLabel start_loop;
7517 __ Bind(&start_loop);
7518 // Need to subtract first to handle the empty array case.
7519 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7520 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7521 // Go to next interface if the classes do not match.
7522 __ cmpl(cls.AsRegister<Register>(),
7523 CodeGeneratorX86::ArrayAddress(temp,
7524 maybe_temp2_loc,
7525 TIMES_4,
7526 object_array_data_offset));
7527 __ j(kNotEqual, &start_loop);
7528 // If `cls` was poisoned above, unpoison it.
7529 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007530 break;
7531 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007532
7533 case TypeCheckKind::kBitstringCheck: {
7534 // /* HeapReference<Class> */ temp = obj->klass_
7535 GenerateReferenceLoadTwoRegisters(instruction,
7536 temp_loc,
7537 obj_loc,
7538 class_offset,
7539 kWithoutReadBarrier);
7540
7541 GenerateBitstringTypeCheckCompare(instruction, temp);
7542 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7543 break;
7544 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007545 }
7546 __ Bind(&done);
7547
Roland Levillain0d5a2812015-11-13 10:07:31 +00007548 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007549}
7550
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007551void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007552 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7553 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007554 InvokeRuntimeCallingConvention calling_convention;
7555 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7556}
7557
7558void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007559 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7560 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007561 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007562 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007563 if (instruction->IsEnter()) {
7564 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7565 } else {
7566 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7567 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007568}
7569
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307570void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7571 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7572 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7573 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7574 locations->SetInAt(0, Location::RequiresRegister());
7575 locations->SetInAt(1, Location::RequiresRegister());
7576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7577}
7578
7579void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7580 LocationSummary* locations = instruction->GetLocations();
7581 Location first = locations->InAt(0);
7582 Location second = locations->InAt(1);
7583 Location dest = locations->Out();
7584 if (instruction->GetResultType() == DataType::Type::kInt32) {
7585 __ andn(dest.AsRegister<Register>(),
7586 first.AsRegister<Register>(),
7587 second.AsRegister<Register>());
7588 } else {
7589 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7590 __ andn(dest.AsRegisterPairLow<Register>(),
7591 first.AsRegisterPairLow<Register>(),
7592 second.AsRegisterPairLow<Register>());
7593 __ andn(dest.AsRegisterPairHigh<Register>(),
7594 first.AsRegisterPairHigh<Register>(),
7595 second.AsRegisterPairHigh<Register>());
7596 }
7597}
7598
7599void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7600 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7601 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7602 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7603 locations->SetInAt(0, Location::RequiresRegister());
7604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7605}
7606
7607void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7608 HX86MaskOrResetLeastSetBit* instruction) {
7609 LocationSummary* locations = instruction->GetLocations();
7610 Location src = locations->InAt(0);
7611 Location dest = locations->Out();
7612 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7613 switch (instruction->GetOpKind()) {
7614 case HInstruction::kAnd:
7615 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7616 break;
7617 case HInstruction::kXor:
7618 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7619 break;
7620 default:
7621 LOG(FATAL) << "Unreachable";
7622 }
7623}
7624
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007625void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7626void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7627void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7628
7629void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7630 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007631 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007632 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7633 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007634 locations->SetInAt(0, Location::RequiresRegister());
7635 locations->SetInAt(1, Location::Any());
7636 locations->SetOut(Location::SameAsFirstInput());
7637}
7638
7639void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7640 HandleBitwiseOperation(instruction);
7641}
7642
7643void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7644 HandleBitwiseOperation(instruction);
7645}
7646
7647void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7648 HandleBitwiseOperation(instruction);
7649}
7650
7651void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7652 LocationSummary* locations = instruction->GetLocations();
7653 Location first = locations->InAt(0);
7654 Location second = locations->InAt(1);
7655 DCHECK(first.Equals(locations->Out()));
7656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007657 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007658 if (second.IsRegister()) {
7659 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007660 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007661 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007662 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007663 } else {
7664 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007665 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007666 }
7667 } else if (second.IsConstant()) {
7668 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007669 __ andl(first.AsRegister<Register>(),
7670 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007671 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007672 __ orl(first.AsRegister<Register>(),
7673 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007674 } else {
7675 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007676 __ xorl(first.AsRegister<Register>(),
7677 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007678 }
7679 } else {
7680 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007681 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007682 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007683 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007684 } else {
7685 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007686 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007687 }
7688 }
7689 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007690 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007691 if (second.IsRegisterPair()) {
7692 if (instruction->IsAnd()) {
7693 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7694 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7695 } else if (instruction->IsOr()) {
7696 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7697 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7698 } else {
7699 DCHECK(instruction->IsXor());
7700 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7701 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7702 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007703 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007704 if (instruction->IsAnd()) {
7705 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7706 __ andl(first.AsRegisterPairHigh<Register>(),
7707 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7708 } else if (instruction->IsOr()) {
7709 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7710 __ orl(first.AsRegisterPairHigh<Register>(),
7711 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7712 } else {
7713 DCHECK(instruction->IsXor());
7714 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7715 __ xorl(first.AsRegisterPairHigh<Register>(),
7716 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7717 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007718 } else {
7719 DCHECK(second.IsConstant()) << second;
7720 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007721 int32_t low_value = Low32Bits(value);
7722 int32_t high_value = High32Bits(value);
7723 Immediate low(low_value);
7724 Immediate high(high_value);
7725 Register first_low = first.AsRegisterPairLow<Register>();
7726 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007727 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007728 if (low_value == 0) {
7729 __ xorl(first_low, first_low);
7730 } else if (low_value != -1) {
7731 __ andl(first_low, low);
7732 }
7733 if (high_value == 0) {
7734 __ xorl(first_high, first_high);
7735 } else if (high_value != -1) {
7736 __ andl(first_high, high);
7737 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007738 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007739 if (low_value != 0) {
7740 __ orl(first_low, low);
7741 }
7742 if (high_value != 0) {
7743 __ orl(first_high, high);
7744 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007745 } else {
7746 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007747 if (low_value != 0) {
7748 __ xorl(first_low, low);
7749 }
7750 if (high_value != 0) {
7751 __ xorl(first_high, high);
7752 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007753 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007754 }
7755 }
7756}
7757
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007758void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7759 HInstruction* instruction,
7760 Location out,
7761 uint32_t offset,
7762 Location maybe_temp,
7763 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007764 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007765 if (read_barrier_option == kWithReadBarrier) {
7766 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007767 if (kUseBakerReadBarrier) {
7768 // Load with fast path based Baker's read barrier.
7769 // /* HeapReference<Object> */ out = *(out + offset)
7770 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007771 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007772 } else {
7773 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007774 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007775 // in the following move operation, as we will need it for the
7776 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007777 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007778 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007779 // /* HeapReference<Object> */ out = *(out + offset)
7780 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007781 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007782 }
7783 } else {
7784 // Plain load with no read barrier.
7785 // /* HeapReference<Object> */ out = *(out + offset)
7786 __ movl(out_reg, Address(out_reg, offset));
7787 __ MaybeUnpoisonHeapReference(out_reg);
7788 }
7789}
7790
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007791void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7792 HInstruction* instruction,
7793 Location out,
7794 Location obj,
7795 uint32_t offset,
7796 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007797 Register out_reg = out.AsRegister<Register>();
7798 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007799 if (read_barrier_option == kWithReadBarrier) {
7800 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007801 if (kUseBakerReadBarrier) {
7802 // Load with fast path based Baker's read barrier.
7803 // /* HeapReference<Object> */ out = *(obj + offset)
7804 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007805 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007806 } else {
7807 // Load with slow path based read barrier.
7808 // /* HeapReference<Object> */ out = *(obj + offset)
7809 __ movl(out_reg, Address(obj_reg, offset));
7810 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7811 }
7812 } else {
7813 // Plain load with no read barrier.
7814 // /* HeapReference<Object> */ out = *(obj + offset)
7815 __ movl(out_reg, Address(obj_reg, offset));
7816 __ MaybeUnpoisonHeapReference(out_reg);
7817 }
7818}
7819
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007820void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7821 HInstruction* instruction,
7822 Location root,
7823 const Address& address,
7824 Label* fixup_label,
7825 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007826 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007827 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007828 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007829 if (kUseBakerReadBarrier) {
7830 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7831 // Baker's read barrier are used:
7832 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007833 // root = obj.field;
7834 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7835 // if (temp != null) {
7836 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007837 // }
7838
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007839 // /* GcRoot<mirror::Object> */ root = *address
7840 __ movl(root_reg, address);
7841 if (fixup_label != nullptr) {
7842 __ Bind(fixup_label);
7843 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007844 static_assert(
7845 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7846 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7847 "have different sizes.");
7848 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7849 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7850 "have different sizes.");
7851
Vladimir Marko953437b2016-08-24 08:30:46 +00007852 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007853 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007854 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007855 codegen_->AddSlowPath(slow_path);
7856
Roland Levillaind966ce72017-02-09 16:20:14 +00007857 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7858 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007859 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007860 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7861 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007862 __ j(kNotEqual, slow_path->GetEntryLabel());
7863 __ Bind(slow_path->GetExitLabel());
7864 } else {
7865 // GC root loaded through a slow path for read barriers other
7866 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007867 // /* GcRoot<mirror::Object>* */ root = address
7868 __ leal(root_reg, address);
7869 if (fixup_label != nullptr) {
7870 __ Bind(fixup_label);
7871 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007872 // /* mirror::Object* */ root = root->Read()
7873 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7874 }
7875 } else {
7876 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007877 // /* GcRoot<mirror::Object> */ root = *address
7878 __ movl(root_reg, address);
7879 if (fixup_label != nullptr) {
7880 __ Bind(fixup_label);
7881 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007882 // Note that GC roots are not affected by heap poisoning, thus we
7883 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007884 }
7885}
7886
7887void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7888 Location ref,
7889 Register obj,
7890 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007891 bool needs_null_check) {
7892 DCHECK(kEmitCompilerReadBarrier);
7893 DCHECK(kUseBakerReadBarrier);
7894
7895 // /* HeapReference<Object> */ ref = *(obj + offset)
7896 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007897 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007898}
7899
7900void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7901 Location ref,
7902 Register obj,
7903 uint32_t data_offset,
7904 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007905 bool needs_null_check) {
7906 DCHECK(kEmitCompilerReadBarrier);
7907 DCHECK(kUseBakerReadBarrier);
7908
Roland Levillain3d312422016-06-23 13:53:42 +01007909 static_assert(
7910 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7911 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007912 // /* HeapReference<Object> */ ref =
7913 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007914 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007915 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007916}
7917
7918void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7919 Location ref,
7920 Register obj,
7921 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007922 bool needs_null_check,
7923 bool always_update_field,
7924 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007925 DCHECK(kEmitCompilerReadBarrier);
7926 DCHECK(kUseBakerReadBarrier);
7927
7928 // In slow path based read barriers, the read barrier call is
7929 // inserted after the original load. However, in fast path based
7930 // Baker's read barriers, we need to perform the load of
7931 // mirror::Object::monitor_ *before* the original reference load.
7932 // This load-load ordering is required by the read barrier.
7933 // The fast path/slow path (for Baker's algorithm) should look like:
7934 //
7935 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7936 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7937 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007938 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007939 // if (is_gray) {
7940 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7941 // }
7942 //
7943 // Note: the original implementation in ReadBarrier::Barrier is
7944 // slightly more complex as:
7945 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007946 // the high-bits of rb_state, which are expected to be all zeroes
7947 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7948 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007949 // - it performs additional checks that we do not do here for
7950 // performance reasons.
7951
7952 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007953 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7954
Vladimir Marko953437b2016-08-24 08:30:46 +00007955 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007956 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007957 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007958 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7959 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7960 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7961
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007962 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007963 // ref = ReadBarrier::Mark(ref);
7964 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7965 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007966 if (needs_null_check) {
7967 MaybeRecordImplicitNullCheck(instruction);
7968 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007969
7970 // Load fence to prevent load-load reordering.
7971 // Note that this is a no-op, thanks to the x86 memory model.
7972 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7973
7974 // The actual reference load.
7975 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007976 __ movl(ref_reg, src); // Flags are unaffected.
7977
7978 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7979 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007980 SlowPathCode* slow_path;
7981 if (always_update_field) {
7982 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007983 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007984 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007985 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007986 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007987 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007988 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007989 AddSlowPath(slow_path);
7990
7991 // We have done the "if" of the gray bit check above, now branch based on the flags.
7992 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007993
7994 // Object* ref = ref_addr->AsMirrorPtr()
7995 __ MaybeUnpoisonHeapReference(ref_reg);
7996
Roland Levillain7c1559a2015-12-15 10:55:36 +00007997 __ Bind(slow_path->GetExitLabel());
7998}
7999
8000void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8001 Location out,
8002 Location ref,
8003 Location obj,
8004 uint32_t offset,
8005 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008006 DCHECK(kEmitCompilerReadBarrier);
8007
Roland Levillain7c1559a2015-12-15 10:55:36 +00008008 // Insert a slow path based read barrier *after* the reference load.
8009 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008010 // If heap poisoning is enabled, the unpoisoning of the loaded
8011 // reference will be carried out by the runtime within the slow
8012 // path.
8013 //
8014 // Note that `ref` currently does not get unpoisoned (when heap
8015 // poisoning is enabled), which is alright as the `ref` argument is
8016 // not used by the artReadBarrierSlow entry point.
8017 //
8018 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008019 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008020 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8021 AddSlowPath(slow_path);
8022
Roland Levillain0d5a2812015-11-13 10:07:31 +00008023 __ jmp(slow_path->GetEntryLabel());
8024 __ Bind(slow_path->GetExitLabel());
8025}
8026
Roland Levillain7c1559a2015-12-15 10:55:36 +00008027void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8028 Location out,
8029 Location ref,
8030 Location obj,
8031 uint32_t offset,
8032 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008033 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008034 // Baker's read barriers shall be handled by the fast path
8035 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8036 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008037 // If heap poisoning is enabled, unpoisoning will be taken care of
8038 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008039 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008040 } else if (kPoisonHeapReferences) {
8041 __ UnpoisonHeapReference(out.AsRegister<Register>());
8042 }
8043}
8044
Roland Levillain7c1559a2015-12-15 10:55:36 +00008045void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8046 Location out,
8047 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008048 DCHECK(kEmitCompilerReadBarrier);
8049
Roland Levillain7c1559a2015-12-15 10:55:36 +00008050 // Insert a slow path based read barrier *after* the GC root load.
8051 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008052 // Note that GC roots are not affected by heap poisoning, so we do
8053 // not need to do anything special for this here.
8054 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008055 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008056 AddSlowPath(slow_path);
8057
Roland Levillain0d5a2812015-11-13 10:07:31 +00008058 __ jmp(slow_path->GetEntryLabel());
8059 __ Bind(slow_path->GetExitLabel());
8060}
8061
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008062void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008063 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008064 LOG(FATAL) << "Unreachable";
8065}
8066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008067void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008068 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008069 LOG(FATAL) << "Unreachable";
8070}
8071
Mark Mendellfe57faa2015-09-18 09:26:15 -04008072// Simple implementation of packed switch - generate cascaded compare/jumps.
8073void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8074 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008075 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008076 locations->SetInAt(0, Location::RequiresRegister());
8077}
8078
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008079void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8080 int32_t lower_bound,
8081 uint32_t num_entries,
8082 HBasicBlock* switch_block,
8083 HBasicBlock* default_block) {
8084 // Figure out the correct compare values and jump conditions.
8085 // Handle the first compare/branch as a special case because it might
8086 // jump to the default case.
8087 DCHECK_GT(num_entries, 2u);
8088 Condition first_condition;
8089 uint32_t index;
8090 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8091 if (lower_bound != 0) {
8092 first_condition = kLess;
8093 __ cmpl(value_reg, Immediate(lower_bound));
8094 __ j(first_condition, codegen_->GetLabelOf(default_block));
8095 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008096
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008097 index = 1;
8098 } else {
8099 // Handle all the compare/jumps below.
8100 first_condition = kBelow;
8101 index = 0;
8102 }
8103
8104 // Handle the rest of the compare/jumps.
8105 for (; index + 1 < num_entries; index += 2) {
8106 int32_t compare_to_value = lower_bound + index + 1;
8107 __ cmpl(value_reg, Immediate(compare_to_value));
8108 // Jump to successors[index] if value < case_value[index].
8109 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8110 // Jump to successors[index + 1] if value == case_value[index + 1].
8111 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8112 }
8113
8114 if (index != num_entries) {
8115 // There are an odd number of entries. Handle the last one.
8116 DCHECK_EQ(index + 1, num_entries);
8117 __ cmpl(value_reg, Immediate(lower_bound + index));
8118 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008119 }
8120
8121 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008122 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8123 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008124 }
8125}
8126
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008127void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8128 int32_t lower_bound = switch_instr->GetStartValue();
8129 uint32_t num_entries = switch_instr->GetNumEntries();
8130 LocationSummary* locations = switch_instr->GetLocations();
8131 Register value_reg = locations->InAt(0).AsRegister<Register>();
8132
8133 GenPackedSwitchWithCompares(value_reg,
8134 lower_bound,
8135 num_entries,
8136 switch_instr->GetBlock(),
8137 switch_instr->GetDefaultBlock());
8138}
8139
Mark Mendell805b3b52015-09-18 14:10:29 -04008140void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8141 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008142 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008143 locations->SetInAt(0, Location::RequiresRegister());
8144
8145 // Constant area pointer.
8146 locations->SetInAt(1, Location::RequiresRegister());
8147
8148 // And the temporary we need.
8149 locations->AddTemp(Location::RequiresRegister());
8150}
8151
8152void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8153 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008154 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008155 LocationSummary* locations = switch_instr->GetLocations();
8156 Register value_reg = locations->InAt(0).AsRegister<Register>();
8157 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8158
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008159 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8160 GenPackedSwitchWithCompares(value_reg,
8161 lower_bound,
8162 num_entries,
8163 switch_instr->GetBlock(),
8164 default_block);
8165 return;
8166 }
8167
Mark Mendell805b3b52015-09-18 14:10:29 -04008168 // Optimizing has a jump area.
8169 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8170 Register constant_area = locations->InAt(1).AsRegister<Register>();
8171
8172 // Remove the bias, if needed.
8173 if (lower_bound != 0) {
8174 __ leal(temp_reg, Address(value_reg, -lower_bound));
8175 value_reg = temp_reg;
8176 }
8177
8178 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008179 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008180 __ cmpl(value_reg, Immediate(num_entries - 1));
8181 __ j(kAbove, codegen_->GetLabelOf(default_block));
8182
8183 // We are in the range of the table.
8184 // Load (target-constant_area) from the jump table, indexing by the value.
8185 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8186
8187 // Compute the actual target address by adding in constant_area.
8188 __ addl(temp_reg, constant_area);
8189
8190 // And jump.
8191 __ jmp(temp_reg);
8192}
8193
Mark Mendell0616ae02015-04-17 12:49:27 -04008194void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8195 HX86ComputeBaseMethodAddress* insn) {
8196 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008197 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008198 locations->SetOut(Location::RequiresRegister());
8199}
8200
8201void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8202 HX86ComputeBaseMethodAddress* insn) {
8203 LocationSummary* locations = insn->GetLocations();
8204 Register reg = locations->Out().AsRegister<Register>();
8205
8206 // Generate call to next instruction.
8207 Label next_instruction;
8208 __ call(&next_instruction);
8209 __ Bind(&next_instruction);
8210
8211 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008212 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008213
8214 // Grab the return address off the stack.
8215 __ popl(reg);
8216}
8217
8218void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8219 HX86LoadFromConstantTable* insn) {
8220 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008221 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008222
8223 locations->SetInAt(0, Location::RequiresRegister());
8224 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8225
8226 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008227 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008228 return;
8229 }
8230
8231 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008232 case DataType::Type::kFloat32:
8233 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008234 locations->SetOut(Location::RequiresFpuRegister());
8235 break;
8236
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008237 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008238 locations->SetOut(Location::RequiresRegister());
8239 break;
8240
8241 default:
8242 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8243 }
8244}
8245
8246void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008247 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008248 return;
8249 }
8250
8251 LocationSummary* locations = insn->GetLocations();
8252 Location out = locations->Out();
8253 Register const_area = locations->InAt(0).AsRegister<Register>();
8254 HConstant *value = insn->GetConstant();
8255
8256 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008257 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008258 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008259 codegen_->LiteralFloatAddress(
8260 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008261 break;
8262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008263 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008264 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008265 codegen_->LiteralDoubleAddress(
8266 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008267 break;
8268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008269 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008270 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008271 codegen_->LiteralInt32Address(
8272 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008273 break;
8274
8275 default:
8276 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8277 }
8278}
8279
Mark Mendell0616ae02015-04-17 12:49:27 -04008280/**
8281 * Class to handle late fixup of offsets into constant area.
8282 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008283class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008284 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008285 RIPFixup(CodeGeneratorX86& codegen,
8286 HX86ComputeBaseMethodAddress* base_method_address,
8287 size_t offset)
8288 : codegen_(&codegen),
8289 base_method_address_(base_method_address),
8290 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008291
8292 protected:
8293 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8294
8295 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008296 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008297
8298 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008299 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008300 // Patch the correct offset for the instruction. The place to patch is the
8301 // last 4 bytes of the instruction.
8302 // The value to patch is the distance from the offset in the constant area
8303 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008304 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008305 int32_t relative_position =
8306 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008307
8308 // Patch in the right value.
8309 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8310 }
8311
Mark Mendell0616ae02015-04-17 12:49:27 -04008312 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008313 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008314};
8315
Mark Mendell805b3b52015-09-18 14:10:29 -04008316/**
8317 * Class to handle late fixup of offsets to a jump table that will be created in the
8318 * constant area.
8319 */
8320class JumpTableRIPFixup : public RIPFixup {
8321 public:
8322 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008323 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8324 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008325
8326 void CreateJumpTable() {
8327 X86Assembler* assembler = codegen_->GetAssembler();
8328
8329 // Ensure that the reference to the jump table has the correct offset.
8330 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8331 SetOffset(offset_in_constant_table);
8332
8333 // The label values in the jump table are computed relative to the
8334 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008335 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008336
8337 // Populate the jump table with the correct values for the jump table.
8338 int32_t num_entries = switch_instr_->GetNumEntries();
8339 HBasicBlock* block = switch_instr_->GetBlock();
8340 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8341 // The value that we want is the target offset - the position of the table.
8342 for (int32_t i = 0; i < num_entries; i++) {
8343 HBasicBlock* b = successors[i];
8344 Label* l = codegen_->GetLabelOf(b);
8345 DCHECK(l->IsBound());
8346 int32_t offset_to_block = l->Position() - relative_offset;
8347 assembler->AppendInt32(offset_to_block);
8348 }
8349 }
8350
8351 private:
8352 const HX86PackedSwitch* switch_instr_;
8353};
8354
8355void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8356 // Generate the constant area if needed.
8357 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308358
Mark Mendell805b3b52015-09-18 14:10:29 -04008359 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8360 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8361 // byte values.
8362 assembler->Align(4, 0);
8363 constant_area_start_ = assembler->CodeSize();
8364
8365 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008366 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008367 jump_table->CreateJumpTable();
8368 }
8369
8370 // And now add the constant area to the generated code.
8371 assembler->AddConstantArea();
8372 }
8373
8374 // And finish up.
8375 CodeGenerator::Finalize(allocator);
8376}
8377
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008378Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8379 HX86ComputeBaseMethodAddress* method_base,
8380 Register reg) {
8381 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008382 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008383 return Address(reg, kDummy32BitOffset, fixup);
8384}
8385
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008386Address CodeGeneratorX86::LiteralFloatAddress(float v,
8387 HX86ComputeBaseMethodAddress* method_base,
8388 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008389 AssemblerFixup* fixup =
8390 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008391 return Address(reg, kDummy32BitOffset, fixup);
8392}
8393
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008394Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8395 HX86ComputeBaseMethodAddress* method_base,
8396 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008397 AssemblerFixup* fixup =
8398 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008399 return Address(reg, kDummy32BitOffset, fixup);
8400}
8401
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008402Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8403 HX86ComputeBaseMethodAddress* method_base,
8404 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008405 AssemblerFixup* fixup =
8406 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008407 return Address(reg, kDummy32BitOffset, fixup);
8408}
8409
Aart Bika19616e2016-02-01 18:57:58 -08008410void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8411 if (value == 0) {
8412 __ xorl(dest, dest);
8413 } else {
8414 __ movl(dest, Immediate(value));
8415 }
8416}
8417
8418void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8419 if (value == 0) {
8420 __ testl(dest, dest);
8421 } else {
8422 __ cmpl(dest, Immediate(value));
8423 }
8424}
8425
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008426void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8427 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008428 GenerateIntCompare(lhs_reg, rhs);
8429}
8430
8431void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008432 if (rhs.IsConstant()) {
8433 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008434 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008435 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008436 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008437 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008438 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008439 }
8440}
8441
8442Address CodeGeneratorX86::ArrayAddress(Register obj,
8443 Location index,
8444 ScaleFactor scale,
8445 uint32_t data_offset) {
8446 return index.IsConstant() ?
8447 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8448 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8449}
8450
Mark Mendell805b3b52015-09-18 14:10:29 -04008451Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8452 Register reg,
8453 Register value) {
8454 // Create a fixup to be used to create and address the jump table.
8455 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008456 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008457
8458 // We have to populate the jump tables.
8459 fixups_to_jump_tables_.push_back(table_fixup);
8460
8461 // We want a scaled address, as we are extracting the correct offset from the table.
8462 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8463}
8464
Andreas Gampe85b62f22015-09-09 13:15:38 -07008465// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008466void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008467 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008468 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008469 return;
8470 }
8471
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008472 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008473
8474 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8475 if (target.Equals(return_loc)) {
8476 return;
8477 }
8478
8479 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8480 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008481 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008482 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008483 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8484 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008485 GetMoveResolver()->EmitNativeCode(&parallel_move);
8486 } else {
8487 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008488 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008489 parallel_move.AddMove(return_loc, target, type, nullptr);
8490 GetMoveResolver()->EmitNativeCode(&parallel_move);
8491 }
8492}
8493
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008494void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8495 const uint8_t* roots_data,
8496 const PatchInfo<Label>& info,
8497 uint64_t index_in_table) const {
8498 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8499 uintptr_t address =
8500 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008501 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008502 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8503 dchecked_integral_cast<uint32_t>(address);
8504}
8505
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008506void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8507 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008508 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008509 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008510 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008511 }
8512
8513 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008514 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008515 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008516 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008517 }
8518}
8519
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008520void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8521 ATTRIBUTE_UNUSED) {
8522 LOG(FATAL) << "Unreachable";
8523}
8524
8525void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8526 ATTRIBUTE_UNUSED) {
8527 LOG(FATAL) << "Unreachable";
8528}
8529
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308530bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8531 return codegen_->GetInstructionSetFeatures().HasAVX();
8532}
8533bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8534 return codegen_->GetInstructionSetFeatures().HasAVX2();
8535}
8536bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8537 return codegen_->GetInstructionSetFeatures().HasAVX();
8538}
8539bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8540 return codegen_->GetInstructionSetFeatures().HasAVX2();
8541}
8542
Roland Levillain4d027112015-07-01 15:41:14 +01008543#undef __
8544
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008545} // namespace x86
8546} // namespace art