blob: 99d32407061e524f216befc0496bacda75d95bc2 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Vladimir Marko86c87522020-05-11 16:55:55 +010019#include "arch/x86/jni_frame_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000020#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010027#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070028#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040029#include "intrinsics.h"
30#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000031#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070034#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000036#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010037#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010044
Roland Levillain0d5a2812015-11-13 10:07:31 +000045template<class MirrorType>
46class GcRoot;
47
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000048namespace x86 {
49
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050052static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
Mark Mendell24f2dfa2015-01-14 19:51:45 -050054static constexpr int kC2ConditionMask = 0x400;
55
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000056static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000057
Aart Bik1f8d51b2018-02-15 10:42:37 -080058static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
59static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
60
Vladimir Marko3232dbb2018-07-25 15:42:46 +010061static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
62 InvokeRuntimeCallingConvention calling_convention;
63 RegisterSet caller_saves = RegisterSet::Empty();
64 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
65 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
66 // that the the kPrimNot result register is the same as the first argument register.
67 return caller_saves;
68}
69
Roland Levillain7cbd27f2016-08-11 23:53:33 +010070// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
71#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070072#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073
Andreas Gampe85b62f22015-09-09 13:15:38 -070074class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000076 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010078 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010079 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000081 if (instruction_->CanThrowIntoCatchBlock()) {
82 // Live registers will be restored in the catch block if caught.
83 SaveLiveRegisters(codegen, instruction_->GetLocations());
84 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010085 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010086 instruction_,
87 instruction_->GetDexPc(),
88 this);
Roland Levillain888d0672015-11-23 18:53:50 +000089 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 }
91
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010094 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010095
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010097 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
98};
99
Andreas Gampe85b62f22015-09-09 13:15:38 -0700100class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100105 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000106 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100107 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000108 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000109 }
110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100111 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100113 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100114
Calin Juravled0d48522014-11-04 16:40:20 +0000115 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000116 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
117};
118
Andreas Gampe85b62f22015-09-09 13:15:38 -0700119class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000120 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000121 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
122 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100124 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000125 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negl(reg_);
128 } else {
129 __ movl(reg_, Immediate(0));
130 }
Calin Juravled0d48522014-11-04 16:40:20 +0000131 __ jmp(GetExitLabel());
132 }
133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100134 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100135
Calin Juravled0d48522014-11-04 16:40:20 +0000136 private:
137 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 bool is_div_;
139 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000140};
141
Andreas Gampe85b62f22015-09-09 13:15:38 -0700142class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100146 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000150 // We're moving two locations to locations that could overlap, so we need a parallel
151 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000152 if (instruction_->CanThrowIntoCatchBlock()) {
153 // Live registers will be restored in the catch block if caught.
154 SaveLiveRegisters(codegen, instruction_->GetLocations());
155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156
157 // Are we using an array length from memory?
158 HInstruction* array_length = instruction_->InputAt(1);
159 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
162 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100163 HArrayLength* length = array_length->AsArrayLength();
164 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 Location array_loc = array_length->GetLocations()->InAt(0);
166 Address array_len(array_loc.AsRegister<Register>(), len_offset);
167 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
168 // Check for conflicts with index.
169 if (length_loc.Equals(locations->InAt(0))) {
170 // We know we aren't using parameter 2.
171 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
172 }
173 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100174 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100175 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700176 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400177 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000178 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100179 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400182 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100183 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100185 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
186 ? kQuickThrowStringBounds
187 : kQuickThrowArrayBounds;
188 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100189 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000190 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 }
192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100193 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100195 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
199};
200
Andreas Gampe85b62f22015-09-09 13:15:38 -0700201class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000203 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000204 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100206 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700207 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700210 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100211 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000212 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700213 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100214 if (successor_ == nullptr) {
215 __ jmp(GetReturnLabel());
216 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100217 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100218 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000219 }
220
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 Label* GetReturnLabel() {
222 DCHECK(successor_ == nullptr);
223 return &return_label_;
224 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000225
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100226 HBasicBlock* GetSuccessor() const {
227 return successor_;
228 }
229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100230 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100231
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000232 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000234 Label return_label_;
235
236 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
237};
238
Vladimir Markoaad75c62016-10-03 08:46:48 +0000239class LoadStringSlowPathX86 : public SlowPathCode {
240 public:
241 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
242
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100243 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000244 LocationSummary* locations = instruction_->GetLocations();
245 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
246
247 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
248 __ Bind(GetEntryLabel());
249 SaveLiveRegisters(codegen, locations);
250
251 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000252 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000254 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
256 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
257 RestoreLiveRegisters(codegen, locations);
258
Vladimir Markoaad75c62016-10-03 08:46:48 +0000259 __ jmp(GetExitLabel());
260 }
261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100262 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000263
264 private:
265 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
266};
267
Andreas Gampe85b62f22015-09-09 13:15:38 -0700268class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100270 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
271 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100273 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100276 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 Location out = locations->Out();
279 const uint32_t dex_pc = instruction_->GetDexPc();
280 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
281 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
284 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286
287 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100288 if (must_resolve_type) {
289 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
290 dex::TypeIndex type_index = cls_->GetTypeIndex();
291 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100292 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
293 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100294 // If we also must_do_clinit, the resolved type is now in the correct register.
295 } else {
296 DCHECK(must_do_clinit);
297 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
298 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
299 }
300 if (must_do_clinit) {
301 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000303 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000304
305 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 if (out.IsValid()) {
307 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
308 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000309 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000310 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 __ jmp(GetExitLabel());
312 }
313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100314 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100315
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000316 private:
317 // The class this slow path will load.
318 HLoadClass* const cls_;
319
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000320 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
321};
322
Andreas Gampe85b62f22015-09-09 13:15:38 -0700323class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000326 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100328 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000336 if (kPoisonHeapReferences &&
337 instruction_->IsCheckCast() &&
338 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
339 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
340 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
341 }
342
Vladimir Marko87584542017-12-12 17:47:52 +0000343 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 SaveLiveRegisters(codegen, locations);
345 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800353 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100355 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100357 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100358 instruction_,
359 instruction_->GetDexPc(),
360 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 } else {
363 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800364 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
365 instruction_,
366 instruction_->GetDexPc(),
367 this);
368 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 }
370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 if (!is_fatal_) {
372 if (instruction_->IsInstanceOf()) {
373 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
374 }
375 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 __ jmp(GetExitLabel());
378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 }
380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100381 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
382 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100383
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386
387 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
388};
389
Andreas Gampe85b62f22015-09-09 13:15:38 -0700390class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 public:
Aart Bik42249c32016-01-07 15:33:50 -0800392 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100395 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100396 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100398 LocationSummary* locations = instruction_->GetLocations();
399 SaveLiveRegisters(codegen, locations);
400 InvokeRuntimeCallingConvention calling_convention;
401 x86_codegen->Load32BitValue(
402 calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100404 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 }
407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100408 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100409
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
412};
413
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414class ArraySetSlowPathX86 : public SlowPathCode {
415 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100428 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100443 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100449 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100450
451 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
453};
454
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
467 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
469 Location ref,
470 bool unpoison_ref_before_marking)
471 : SlowPathCode(instruction),
472 ref_(ref),
473 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 DCHECK(kEmitCompilerReadBarrier);
475 }
476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100477 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100479 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100481 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 DCHECK(instruction_->IsInstanceFieldGet() ||
485 instruction_->IsStaticFieldGet() ||
486 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100487 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488 instruction_->IsLoadClass() ||
489 instruction_->IsLoadString() ||
490 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100491 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100492 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
493 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000494 << "Unexpected instruction in read barrier marking slow path: "
495 << instruction_->DebugName();
496
497 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100498 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000499 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000501 }
Roland Levillain4359e612016-07-20 11:32:19 +0100502 // No need to save live registers; it's taken care of by the
503 // entrypoint. Also, there is no need to update the stack mask,
504 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 DCHECK_NE(ref_reg, ESP);
507 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // "Compact" slow path, saving two moves.
509 //
510 // Instead of using the standard runtime calling convention (input
511 // and output in EAX):
512 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100514 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100516 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100518 // of a dedicated entrypoint:
519 //
520 // rX <- ReadBarrierMarkRegX(rX)
521 //
Roland Levillain97c46462017-05-11 14:04:03 +0100522 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100523 // This runtime call does not require a stack map.
524 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000525 __ jmp(GetExitLabel());
526 }
527
528 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100529 // The location (register) of the marked object reference.
530 const Location ref_;
531 // Should the reference in `ref_` be unpoisoned prior to marking it?
532 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000533
534 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
535};
536
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100537// Slow path marking an object reference `ref` during a read barrier,
538// and if needed, atomically updating the field `obj.field` in the
539// object `obj` holding this reference after marking (contrary to
540// ReadBarrierMarkSlowPathX86 above, which never tries to update
541// `obj.field`).
542//
543// This means that after the execution of this slow path, both `ref`
544// and `obj.field` will be up-to-date; i.e., after the flip, both will
545// hold the same to-space reference (unless another thread installed
546// another object reference (different from `ref`) in `obj.field`).
547class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
548 public:
549 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
550 Location ref,
551 Register obj,
552 const Address& field_addr,
553 bool unpoison_ref_before_marking,
554 Register temp)
555 : SlowPathCode(instruction),
556 ref_(ref),
557 obj_(obj),
558 field_addr_(field_addr),
559 unpoison_ref_before_marking_(unpoison_ref_before_marking),
560 temp_(temp) {
561 DCHECK(kEmitCompilerReadBarrier);
562 }
563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100564 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100566 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100567 LocationSummary* locations = instruction_->GetLocations();
568 Register ref_reg = ref_.AsRegister<Register>();
569 DCHECK(locations->CanCall());
570 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
571 // This slow path is only used by the UnsafeCASObject intrinsic.
572 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
573 << "Unexpected instruction in read barrier marking and field updating slow path: "
574 << instruction_->DebugName();
575 DCHECK(instruction_->GetLocations()->Intrinsified());
576 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
577
578 __ Bind(GetEntryLabel());
579 if (unpoison_ref_before_marking_) {
580 // Object* ref = ref_addr->AsMirrorPtr()
581 __ MaybeUnpoisonHeapReference(ref_reg);
582 }
583
584 // Save the old (unpoisoned) reference.
585 __ movl(temp_, ref_reg);
586
587 // No need to save live registers; it's taken care of by the
588 // entrypoint. Also, there is no need to update the stack mask,
589 // as this runtime call will not trigger a garbage collection.
590 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
591 DCHECK_NE(ref_reg, ESP);
592 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
593 // "Compact" slow path, saving two moves.
594 //
595 // Instead of using the standard runtime calling convention (input
596 // and output in EAX):
597 //
598 // EAX <- ref
599 // EAX <- ReadBarrierMark(EAX)
600 // ref <- EAX
601 //
602 // we just use rX (the register containing `ref`) as input and output
603 // of a dedicated entrypoint:
604 //
605 // rX <- ReadBarrierMarkRegX(rX)
606 //
Roland Levillain97c46462017-05-11 14:04:03 +0100607 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100608 // This runtime call does not require a stack map.
609 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
610
611 // If the new reference is different from the old reference,
612 // update the field in the holder (`*field_addr`).
613 //
614 // Note that this field could also hold a different object, if
615 // another thread had concurrently changed it. In that case, the
616 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
617 // operation below would abort the CAS, leaving the field as-is.
618 NearLabel done;
619 __ cmpl(temp_, ref_reg);
620 __ j(kEqual, &done);
621
622 // Update the the holder's field atomically. This may fail if
623 // mutator updates before us, but it's OK. This is achieved
624 // using a strong compare-and-set (CAS) operation with relaxed
625 // memory synchronization ordering, where the expected value is
626 // the old reference and the desired value is the new reference.
627 // This operation is implemented with a 32-bit LOCK CMPXLCHG
628 // instruction, which requires the expected value (the old
629 // reference) to be in EAX. Save EAX beforehand, and move the
630 // expected value (stored in `temp_`) into EAX.
631 __ pushl(EAX);
632 __ movl(EAX, temp_);
633
634 // Convenience aliases.
635 Register base = obj_;
636 Register expected = EAX;
637 Register value = ref_reg;
638
639 bool base_equals_value = (base == value);
640 if (kPoisonHeapReferences) {
641 if (base_equals_value) {
642 // If `base` and `value` are the same register location, move
643 // `value` to a temporary register. This way, poisoning
644 // `value` won't invalidate `base`.
645 value = temp_;
646 __ movl(value, base);
647 }
648
649 // Check that the register allocator did not assign the location
650 // of `expected` (EAX) to `value` nor to `base`, so that heap
651 // poisoning (when enabled) works as intended below.
652 // - If `value` were equal to `expected`, both references would
653 // be poisoned twice, meaning they would not be poisoned at
654 // all, as heap poisoning uses address negation.
655 // - If `base` were equal to `expected`, poisoning `expected`
656 // would invalidate `base`.
657 DCHECK_NE(value, expected);
658 DCHECK_NE(base, expected);
659
660 __ PoisonHeapReference(expected);
661 __ PoisonHeapReference(value);
662 }
663
664 __ LockCmpxchgl(field_addr_, value);
665
666 // If heap poisoning is enabled, we need to unpoison the values
667 // that were poisoned earlier.
668 if (kPoisonHeapReferences) {
669 if (base_equals_value) {
670 // `value` has been moved to a temporary register, no need
671 // to unpoison it.
672 } else {
673 __ UnpoisonHeapReference(value);
674 }
675 // No need to unpoison `expected` (EAX), as it is be overwritten below.
676 }
677
678 // Restore EAX.
679 __ popl(EAX);
680
681 __ Bind(&done);
682 __ jmp(GetExitLabel());
683 }
684
685 private:
686 // The location (register) of the marked object reference.
687 const Location ref_;
688 // The register containing the object holding the marked object reference field.
689 const Register obj_;
690 // The address of the marked reference field. The base of this address must be `obj_`.
691 const Address field_addr_;
692
693 // Should the reference in `ref_` be unpoisoned prior to marking it?
694 const bool unpoison_ref_before_marking_;
695
696 const Register temp_;
697
698 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
699};
700
Roland Levillain0d5a2812015-11-13 10:07:31 +0000701// Slow path generating a read barrier for a heap reference.
702class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
703 public:
704 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
705 Location out,
706 Location ref,
707 Location obj,
708 uint32_t offset,
709 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000710 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711 out_(out),
712 ref_(ref),
713 obj_(obj),
714 offset_(offset),
715 index_(index) {
716 DCHECK(kEmitCompilerReadBarrier);
717 // If `obj` is equal to `out` or `ref`, it means the initial object
718 // has been overwritten by (or after) the heap object reference load
719 // to be instrumented, e.g.:
720 //
721 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000722 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000723 //
724 // In that case, we have lost the information about the original
725 // object, and the emitted read barrier cannot work properly.
726 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
727 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
728 }
729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100730 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
732 LocationSummary* locations = instruction_->GetLocations();
733 Register reg_out = out_.AsRegister<Register>();
734 DCHECK(locations->CanCall());
735 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100736 DCHECK(instruction_->IsInstanceFieldGet() ||
737 instruction_->IsStaticFieldGet() ||
738 instruction_->IsArrayGet() ||
739 instruction_->IsInstanceOf() ||
740 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700741 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000742 << "Unexpected instruction in read barrier for heap reference slow path: "
743 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000744
745 __ Bind(GetEntryLabel());
746 SaveLiveRegisters(codegen, locations);
747
748 // We may have to change the index's value, but as `index_` is a
749 // constant member (like other "inputs" of this slow path),
750 // introduce a copy of it, `index`.
751 Location index = index_;
752 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100753 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000754 if (instruction_->IsArrayGet()) {
755 // Compute the actual memory offset and store it in `index`.
756 Register index_reg = index_.AsRegister<Register>();
757 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
758 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
759 // We are about to change the value of `index_reg` (see the
760 // calls to art::x86::X86Assembler::shll and
761 // art::x86::X86Assembler::AddImmediate below), but it has
762 // not been saved by the previous call to
763 // art::SlowPathCode::SaveLiveRegisters, as it is a
764 // callee-save register --
765 // art::SlowPathCode::SaveLiveRegisters does not consider
766 // callee-save registers, as it has been designed with the
767 // assumption that callee-save registers are supposed to be
768 // handled by the called function. So, as a callee-save
769 // register, `index_reg` _would_ eventually be saved onto
770 // the stack, but it would be too late: we would have
771 // changed its value earlier. Therefore, we manually save
772 // it here into another freely available register,
773 // `free_reg`, chosen of course among the caller-save
774 // registers (as a callee-save `free_reg` register would
775 // exhibit the same problem).
776 //
777 // Note we could have requested a temporary register from
778 // the register allocator instead; but we prefer not to, as
779 // this is a slow path, and we know we can find a
780 // caller-save register that is available.
781 Register free_reg = FindAvailableCallerSaveRegister(codegen);
782 __ movl(free_reg, index_reg);
783 index_reg = free_reg;
784 index = Location::RegisterLocation(index_reg);
785 } else {
786 // The initial register stored in `index_` has already been
787 // saved in the call to art::SlowPathCode::SaveLiveRegisters
788 // (as it is not a callee-save register), so we can freely
789 // use it.
790 }
791 // Shifting the index value contained in `index_reg` by the scale
792 // factor (2) cannot overflow in practice, as the runtime is
793 // unable to allocate object arrays with a size larger than
794 // 2^26 - 1 (that is, 2^28 - 4 bytes).
795 __ shll(index_reg, Immediate(TIMES_4));
796 static_assert(
797 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
798 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
799 __ AddImmediate(index_reg, Immediate(offset_));
800 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100801 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
802 // intrinsics, `index_` is not shifted by a scale factor of 2
803 // (as in the case of ArrayGet), as it is actually an offset
804 // to an object field within an object.
805 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 DCHECK(instruction_->GetLocations()->Intrinsified());
807 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
808 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
809 << instruction_->AsInvoke()->GetIntrinsic();
810 DCHECK_EQ(offset_, 0U);
811 DCHECK(index_.IsRegisterPair());
812 // UnsafeGet's offset location is a register pair, the low
813 // part contains the correct offset.
814 index = index_.ToLow();
815 }
816 }
817
818 // We're moving two or three locations to locations that could
819 // overlap, so we need a parallel move resolver.
820 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100821 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 parallel_move.AddMove(ref_,
823 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100824 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000825 nullptr);
826 parallel_move.AddMove(obj_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 nullptr);
830 if (index.IsValid()) {
831 parallel_move.AddMove(index,
832 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100833 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000834 nullptr);
835 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
836 } else {
837 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
838 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
839 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100840 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000841 CheckEntrypointTypes<
842 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
843 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
844
845 RestoreLiveRegisters(codegen, locations);
846 __ jmp(GetExitLabel());
847 }
848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100849 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000850
851 private:
852 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
853 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
854 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
855 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
856 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
857 return static_cast<Register>(i);
858 }
859 }
860 // We shall never fail to find a free caller-save register, as
861 // there are more than two core caller-save registers on x86
862 // (meaning it is possible to find one which is different from
863 // `ref` and `obj`).
864 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
865 LOG(FATAL) << "Could not find a free caller-save register";
866 UNREACHABLE();
867 }
868
Roland Levillain0d5a2812015-11-13 10:07:31 +0000869 const Location out_;
870 const Location ref_;
871 const Location obj_;
872 const uint32_t offset_;
873 // An additional location containing an index to an array.
874 // Only used for HArrayGet and the UnsafeGetObject &
875 // UnsafeGetObjectVolatile intrinsics.
876 const Location index_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
879};
880
881// Slow path generating a read barrier for a GC root.
882class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
883 public:
884 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000885 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000886 DCHECK(kEmitCompilerReadBarrier);
887 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100889 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890 LocationSummary* locations = instruction_->GetLocations();
891 Register reg_out = out_.AsRegister<Register>();
892 DCHECK(locations->CanCall());
893 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000894 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
895 << "Unexpected instruction in read barrier for GC root slow path: "
896 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897
898 __ Bind(GetEntryLabel());
899 SaveLiveRegisters(codegen, locations);
900
901 InvokeRuntimeCallingConvention calling_convention;
902 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
903 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100904 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 instruction_,
906 instruction_->GetDexPc(),
907 this);
908 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
909 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
910
911 RestoreLiveRegisters(codegen, locations);
912 __ jmp(GetExitLabel());
913 }
914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100915 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000916
917 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918 const Location out_;
919 const Location root_;
920
921 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
922};
923
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100924#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100925// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
926#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100927
Aart Bike9f37602015-10-09 11:15:55 -0700928inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
932 case kCondLT: return kLess;
933 case kCondLE: return kLessEqual;
934 case kCondGT: return kGreater;
935 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700936 case kCondB: return kBelow;
937 case kCondBE: return kBelowEqual;
938 case kCondA: return kAbove;
939 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700940 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100941 LOG(FATAL) << "Unreachable";
942 UNREACHABLE();
943}
944
Aart Bike9f37602015-10-09 11:15:55 -0700945// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100946inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
947 switch (cond) {
948 case kCondEQ: return kEqual;
949 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700950 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 case kCondLT: return kBelow;
952 case kCondLE: return kBelowEqual;
953 case kCondGT: return kAbove;
954 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700955 // Unsigned remain unchanged.
956 case kCondB: return kBelow;
957 case kCondBE: return kBelowEqual;
958 case kCondA: return kAbove;
959 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 }
961 LOG(FATAL) << "Unreachable";
962 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700963}
964
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100965void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100966 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100967}
968
969void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100970 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100971}
972
Vladimir Markoa0431112018-06-25 09:32:54 +0100973const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
974 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
975}
976
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100977size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
978 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
979 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100980}
981
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100982size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
983 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
984 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100985}
986
Mark Mendell7c8d0092015-01-26 11:21:33 -0500987size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700988 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700989 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700990 } else {
991 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
992 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100993 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500994}
995
996size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700997 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700998 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700999 } else {
1000 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1001 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001002 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001003}
1004
Calin Juravle175dc732015-08-25 15:42:32 +01001005void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1006 HInstruction* instruction,
1007 uint32_t dex_pc,
1008 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001009 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001010 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1011 if (EntrypointRequiresStackMap(entrypoint)) {
1012 RecordPcInfo(instruction, dex_pc, slow_path);
1013 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001014}
1015
Roland Levillaindec8f632016-07-22 17:10:06 +01001016void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1017 HInstruction* instruction,
1018 SlowPathCode* slow_path) {
1019 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001020 GenerateInvokeRuntime(entry_point_offset);
1021}
1022
1023void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001024 __ fs()->call(Address::Absolute(entry_point_offset));
1025}
1026
Mark Mendellfb8d2792015-03-31 22:16:59 -04001027CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001028 const CompilerOptions& compiler_options,
1029 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001030 : CodeGenerator(graph,
1031 kNumberOfCpuRegisters,
1032 kNumberOfXmmRegisters,
1033 kNumberOfRegisterPairs,
1034 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1035 arraysize(kCoreCalleeSaves))
1036 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001037 0,
1038 compiler_options,
1039 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001040 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001042 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001043 move_resolver_(graph->GetAllocator(), this),
1044 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001045 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1048 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001049 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001051 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1053 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001054 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001055 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001056 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001058 // Use a fake return address register to mimic Quick.
1059 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001060}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001061
David Brazdil58282f42016-01-14 12:45:10 +00001062void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001063 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001064 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001065}
1066
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001067InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001068 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001069 assembler_(codegen->GetAssembler()),
1070 codegen_(codegen) {}
1071
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001072static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001073 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001074}
1075
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001076void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1077 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1078 Register reg = EAX;
1079 if (is_frame_entry) {
1080 reg = kMethodRegisterArgument;
1081 } else {
1082 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001083 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001084 __ movl(EAX, Address(ESP, kX86WordSize));
1085 }
1086 NearLabel overflow;
1087 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1088 Immediate(ArtMethod::MaxCounter()));
1089 __ j(kEqual, &overflow);
1090 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1091 Immediate(1));
1092 __ Bind(&overflow);
1093 if (!is_frame_entry) {
1094 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001095 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001096 }
1097 }
1098
1099 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1100 ScopedObjectAccess soa(Thread::Current());
1101 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001102 if (info != nullptr) {
1103 uint32_t address = reinterpret_cast32<uint32_t>(info);
1104 NearLabel done;
1105 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001106 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001107 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001108 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001109 // We need a temporary. The stub also expects the method at bottom of stack.
1110 __ pushl(EAX);
1111 __ cfi().AdjustCFAOffset(4);
1112 __ movl(EAX, Immediate(address));
1113 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1114 Immediate(1));
1115 __ j(kCarryClear, &done);
1116 GenerateInvokeRuntime(
1117 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1118 __ Bind(&done);
1119 // We don't strictly require to restore EAX, but this makes the generated
1120 // code easier to reason about.
1121 __ popl(EAX);
1122 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001123 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001124 } else {
1125 if (!RequiresCurrentMethod()) {
1126 CHECK(is_frame_entry);
1127 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1128 }
1129 // We need a temporary.
1130 __ pushl(EAX);
1131 __ cfi().AdjustCFAOffset(4);
1132 __ movl(EAX, Immediate(address));
1133 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1134 Immediate(1));
1135 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1136 __ cfi().AdjustCFAOffset(-4);
1137 __ j(kCarryClear, &done);
1138 GenerateInvokeRuntime(
1139 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1140 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001141 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001142 }
1143 }
1144}
1145
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001146void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001147 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001148 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001149 bool skip_overflow_check =
1150 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001151 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001152
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001153 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001154 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1155 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001156 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001157 }
1158
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001159 if (!HasEmptyFrame()) {
1160 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1161 Register reg = kCoreCalleeSaves[i];
1162 if (allocated_registers_.ContainsCoreRegister(reg)) {
1163 __ pushl(reg);
1164 __ cfi().AdjustCFAOffset(kX86WordSize);
1165 __ cfi().RelOffset(DWARFReg(reg), 0);
1166 }
1167 }
Mark Mendell5f874182015-03-04 15:42:45 -05001168
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001170 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001171 // Save the current method if we need it. Note that we do not
1172 // do this in HCurrentMethod, as the instruction might have been removed
1173 // in the SSA graph.
1174 if (RequiresCurrentMethod()) {
1175 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1176 }
1177
1178 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1179 // Initialize should_deoptimize flag to 0.
1180 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001181 }
1182 }
1183
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001184 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001185}
1186
1187void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001188 __ cfi().RememberState();
1189 if (!HasEmptyFrame()) {
1190 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001191 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001192
David Srbeckyc34dc932015-04-12 09:27:43 +01001193 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1194 Register reg = kCoreCalleeSaves[i];
1195 if (allocated_registers_.ContainsCoreRegister(reg)) {
1196 __ popl(reg);
1197 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1198 __ cfi().Restore(DWARFReg(reg));
1199 }
Mark Mendell5f874182015-03-04 15:42:45 -05001200 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001201 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001202 __ ret();
1203 __ cfi().RestoreState();
1204 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001205}
1206
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001207void CodeGeneratorX86::Bind(HBasicBlock* block) {
1208 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001209}
1210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001211Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001212 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001213 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001215 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kInt8:
1217 case DataType::Type::kUint16:
1218 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001219 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001221 return Location::RegisterLocation(EAX);
1222
Aart Bik66c158e2018-01-31 12:55:04 -08001223 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001225 return Location::RegisterPairLocation(EAX, EDX);
1226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001228 return Location::NoLocation();
1229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 case DataType::Type::kFloat64:
1231 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001232 return Location::FpuRegisterLocation(XMM0);
1233 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001234
1235 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001236}
1237
1238Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1239 return Location::RegisterLocation(kMethodRegisterArgument);
1240}
1241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001243 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001244 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001246 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 case DataType::Type::kInt8:
1248 case DataType::Type::kUint16:
1249 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001250 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001251 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001252 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001253 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001254 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001255 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001256 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001257 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001258 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261 uint32_t index = gp_index_;
1262 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001263 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001264 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001265 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1266 calling_convention.GetRegisterPairAt(index));
1267 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001268 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001269 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1270 }
1271 }
1272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001273 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001274 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001275 stack_index_++;
1276 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1277 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1278 } else {
1279 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1280 }
1281 }
1282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001284 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001285 stack_index_ += 2;
1286 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1287 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1288 } else {
1289 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001290 }
1291 }
1292
Aart Bik66c158e2018-01-31 12:55:04 -08001293 case DataType::Type::kUint32:
1294 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001295 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001296 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001297 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001298 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001299 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001300}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001301
Vladimir Marko86c87522020-05-11 16:55:55 +01001302Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1303 DCHECK_NE(type, DataType::Type::kReference);
1304
1305 Location location;
1306 if (DataType::Is64BitType(type)) {
1307 location = Location::DoubleStackSlot(stack_offset_);
1308 stack_offset_ += 2 * kFramePointerSize;
1309 } else {
1310 location = Location::StackSlot(stack_offset_);
1311 stack_offset_ += kFramePointerSize;
1312 }
1313 if (for_register_allocation_) {
1314 location = Location::Any();
1315 }
1316 return location;
1317}
1318
1319Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1320 // We perform conversion to the managed ABI return register after the call if needed.
1321 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1322 return dex_calling_convention.GetReturnLocation(type);
1323}
1324
1325Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1326 // Pass the method in the hidden argument EAX.
1327 return Location::RegisterLocation(EAX);
1328}
1329
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001330void CodeGeneratorX86::Move32(Location destination, Location source) {
1331 if (source.Equals(destination)) {
1332 return;
1333 }
1334 if (destination.IsRegister()) {
1335 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001336 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001337 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339 } else {
1340 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001341 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001343 } else if (destination.IsFpuRegister()) {
1344 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001345 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001346 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001347 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 } else {
1349 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001351 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001353 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001355 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001356 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001358 } else if (source.IsConstant()) {
1359 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001360 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001361 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362 } else {
1363 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001364 __ pushl(Address(ESP, source.GetStackIndex()));
1365 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 }
1367 }
1368}
1369
1370void CodeGeneratorX86::Move64(Location destination, Location source) {
1371 if (source.Equals(destination)) {
1372 return;
1373 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001374 if (destination.IsRegisterPair()) {
1375 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001376 EmitParallelMoves(
1377 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1378 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001379 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001380 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001381 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001382 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001383 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001384 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1385 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1386 __ psrlq(src_reg, Immediate(32));
1387 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001389 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1392 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001393 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1394 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001395 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001396 if (source.IsFpuRegister()) {
1397 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1398 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001400 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001401 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001402 // Push the 2 source registers to the stack.
1403 __ pushl(source.AsRegisterPairHigh<Register>());
1404 __ cfi().AdjustCFAOffset(elem_size);
1405 __ pushl(source.AsRegisterPairLow<Register>());
1406 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001407 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1408 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001409 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 } else {
1411 LOG(FATAL) << "Unimplemented";
1412 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001413 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001414 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001415 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001416 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001417 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001420 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001422 } else if (source.IsConstant()) {
1423 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001424 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1425 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001426 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001427 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1428 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001429 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001430 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001431 EmitParallelMoves(
1432 Location::StackSlot(source.GetStackIndex()),
1433 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001434 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001435 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001436 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001437 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001438 }
1439 }
1440}
1441
Calin Juravle175dc732015-08-25 15:42:32 +01001442void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1443 DCHECK(location.IsRegister());
1444 __ movl(location.AsRegister<Register>(), Immediate(value));
1445}
1446
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001448 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1450 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1451 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001453 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001454 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001455 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456}
1457
1458void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1459 if (location.IsRegister()) {
1460 locations->AddTemp(location);
1461 } else if (location.IsRegisterPair()) {
1462 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1463 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1464 } else {
1465 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1466 }
1467}
1468
David Brazdilfc6a86a2015-06-26 10:33:45 +00001469void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001470 if (successor->IsExitBlock()) {
1471 DCHECK(got->GetPrevious()->AlwaysThrows());
1472 return; // no code needed
1473 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001474
1475 HBasicBlock* block = got->GetBlock();
1476 HInstruction* previous = got->GetPrevious();
1477
1478 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001479 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001480 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001481 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1482 return;
1483 }
1484
1485 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1486 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1487 }
1488 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001489 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001490 }
1491}
1492
David Brazdilfc6a86a2015-06-26 10:33:45 +00001493void LocationsBuilderX86::VisitGoto(HGoto* got) {
1494 got->SetLocations(nullptr);
1495}
1496
1497void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1498 HandleGoto(got, got->GetSuccessor());
1499}
1500
1501void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1502 try_boundary->SetLocations(nullptr);
1503}
1504
1505void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1506 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1507 if (!successor->IsExitBlock()) {
1508 HandleGoto(try_boundary, successor);
1509 }
1510}
1511
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001512void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001513 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514}
1515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001516void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001517}
1518
Mark Mendell152408f2015-12-31 12:28:50 -05001519template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001520void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001521 LabelType* true_label,
1522 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 if (cond->IsFPConditionTrueIfNaN()) {
1524 __ j(kUnordered, true_label);
1525 } else if (cond->IsFPConditionFalseIfNaN()) {
1526 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001527 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001528 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001529}
1530
Mark Mendell152408f2015-12-31 12:28:50 -05001531template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001532void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001533 LabelType* true_label,
1534 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001535 LocationSummary* locations = cond->GetLocations();
1536 Location left = locations->InAt(0);
1537 Location right = locations->InAt(1);
1538 IfCondition if_cond = cond->GetCondition();
1539
Mark Mendellc4701932015-04-10 13:18:51 -04001540 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001541 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001542 IfCondition true_high_cond = if_cond;
1543 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001544 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001545
1546 // Set the conditions for the test, remembering that == needs to be
1547 // decided using the low words.
1548 switch (if_cond) {
1549 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001550 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001551 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001552 break;
1553 case kCondLT:
1554 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001555 break;
1556 case kCondLE:
1557 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001558 break;
1559 case kCondGT:
1560 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001561 break;
1562 case kCondGE:
1563 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001564 break;
Aart Bike9f37602015-10-09 11:15:55 -07001565 case kCondB:
1566 false_high_cond = kCondA;
1567 break;
1568 case kCondBE:
1569 true_high_cond = kCondB;
1570 break;
1571 case kCondA:
1572 false_high_cond = kCondB;
1573 break;
1574 case kCondAE:
1575 true_high_cond = kCondA;
1576 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001577 }
1578
1579 if (right.IsConstant()) {
1580 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001581 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001582 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001583
Aart Bika19616e2016-02-01 18:57:58 -08001584 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001585 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001586 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001587 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001588 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001589 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001590 __ j(X86Condition(true_high_cond), true_label);
1591 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001592 }
1593 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001594 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001595 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001596 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001597 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001598
1599 __ cmpl(left_high, right_high);
1600 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001601 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001602 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001603 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001604 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001605 __ j(X86Condition(true_high_cond), true_label);
1606 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001607 }
1608 // Must be equal high, so compare the lows.
1609 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001610 } else {
1611 DCHECK(right.IsDoubleStackSlot());
1612 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1613 if (if_cond == kCondNE) {
1614 __ j(X86Condition(true_high_cond), true_label);
1615 } else if (if_cond == kCondEQ) {
1616 __ j(X86Condition(false_high_cond), false_label);
1617 } else {
1618 __ j(X86Condition(true_high_cond), true_label);
1619 __ j(X86Condition(false_high_cond), false_label);
1620 }
1621 // Must be equal high, so compare the lows.
1622 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001623 }
1624 // The last comparison might be unsigned.
1625 __ j(final_condition, true_label);
1626}
1627
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001628void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1629 Location rhs,
1630 HInstruction* insn,
1631 bool is_double) {
1632 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1633 if (is_double) {
1634 if (rhs.IsFpuRegister()) {
1635 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1636 } else if (const_area != nullptr) {
1637 DCHECK(const_area->IsEmittedAtUseSite());
1638 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1639 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001640 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1641 const_area->GetBaseMethodAddress(),
1642 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001643 } else {
1644 DCHECK(rhs.IsDoubleStackSlot());
1645 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1646 }
1647 } else {
1648 if (rhs.IsFpuRegister()) {
1649 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1650 } else if (const_area != nullptr) {
1651 DCHECK(const_area->IsEmittedAtUseSite());
1652 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1653 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001654 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1655 const_area->GetBaseMethodAddress(),
1656 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001657 } else {
1658 DCHECK(rhs.IsStackSlot());
1659 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1660 }
1661 }
1662}
1663
Mark Mendell152408f2015-12-31 12:28:50 -05001664template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001665void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001666 LabelType* true_target_in,
1667 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001668 // Generated branching requires both targets to be explicit. If either of the
1669 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001670 LabelType fallthrough_target;
1671 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1672 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001673
Mark Mendellc4701932015-04-10 13:18:51 -04001674 LocationSummary* locations = condition->GetLocations();
1675 Location left = locations->InAt(0);
1676 Location right = locations->InAt(1);
1677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001679 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001681 GenerateLongComparesAndJumps(condition, true_target, false_target);
1682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001684 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001685 GenerateFPJumps(condition, true_target, false_target);
1686 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001688 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001689 GenerateFPJumps(condition, true_target, false_target);
1690 break;
1691 default:
1692 LOG(FATAL) << "Unexpected compare type " << type;
1693 }
1694
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001696 __ jmp(false_target);
1697 }
David Brazdil0debae72015-11-12 18:37:00 +00001698
1699 if (fallthrough_target.IsLinked()) {
1700 __ Bind(&fallthrough_target);
1701 }
Mark Mendellc4701932015-04-10 13:18:51 -04001702}
1703
David Brazdil0debae72015-11-12 18:37:00 +00001704static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1705 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1706 // are set only strictly before `branch`. We can't use the eflags on long/FP
1707 // conditions if they are materialized due to the complex branching.
1708 return cond->IsCondition() &&
1709 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1711 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001712}
1713
Mark Mendell152408f2015-12-31 12:28:50 -05001714template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001716 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001717 LabelType* true_target,
1718 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001719 HInstruction* cond = instruction->InputAt(condition_input_index);
1720
1721 if (true_target == nullptr && false_target == nullptr) {
1722 // Nothing to do. The code always falls through.
1723 return;
1724 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001725 // Constant condition, statically compared against "true" (integer value 1).
1726 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001727 if (true_target != nullptr) {
1728 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001729 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001730 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001731 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001732 if (false_target != nullptr) {
1733 __ jmp(false_target);
1734 }
1735 }
1736 return;
1737 }
1738
1739 // The following code generates these patterns:
1740 // (1) true_target == nullptr && false_target != nullptr
1741 // - opposite condition true => branch to false_target
1742 // (2) true_target != nullptr && false_target == nullptr
1743 // - condition true => branch to true_target
1744 // (3) true_target != nullptr && false_target != nullptr
1745 // - condition true => branch to true_target
1746 // - branch to false_target
1747 if (IsBooleanValueOrMaterializedCondition(cond)) {
1748 if (AreEflagsSetFrom(cond, instruction)) {
1749 if (true_target == nullptr) {
1750 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1751 } else {
1752 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1753 }
1754 } else {
1755 // Materialized condition, compare against 0.
1756 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1757 if (lhs.IsRegister()) {
1758 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1759 } else {
1760 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1761 }
1762 if (true_target == nullptr) {
1763 __ j(kEqual, false_target);
1764 } else {
1765 __ j(kNotEqual, true_target);
1766 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001767 }
1768 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001769 // Condition has not been materialized, use its inputs as the comparison and
1770 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001771 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001772
1773 // If this is a long or FP comparison that has been folded into
1774 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 DataType::Type type = condition->InputAt(0)->GetType();
1776 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001777 GenerateCompareTestAndBranch(condition, true_target, false_target);
1778 return;
1779 }
1780
1781 Location lhs = condition->GetLocations()->InAt(0);
1782 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001784 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001785 if (true_target == nullptr) {
1786 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1787 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001788 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001789 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001790 }
David Brazdil0debae72015-11-12 18:37:00 +00001791
1792 // If neither branch falls through (case 3), the conditional branch to `true_target`
1793 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1794 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001795 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796 }
1797}
1798
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001800 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001801 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001802 locations->SetInAt(0, Location::Any());
1803 }
1804}
1805
1806void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001807 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1808 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1809 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1810 nullptr : codegen_->GetLabelOf(true_successor);
1811 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1812 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001813 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001814}
1815
1816void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001817 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001818 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001819 InvokeRuntimeCallingConvention calling_convention;
1820 RegisterSet caller_saves = RegisterSet::Empty();
1821 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1822 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001823 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001824 locations->SetInAt(0, Location::Any());
1825 }
1826}
1827
1828void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001829 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001830 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001831 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001832 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001833 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001834}
1835
Mingyao Yang063fc772016-08-02 11:02:54 -07001836void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001838 LocationSummary(flag, LocationSummary::kNoCall);
1839 locations->SetOut(Location::RequiresRegister());
1840}
1841
1842void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1843 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1844 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1845}
1846
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001847static bool SelectCanUseCMOV(HSelect* select) {
1848 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001850 return false;
1851 }
1852
1853 // A FP condition doesn't generate the single CC that we need.
1854 // In 32 bit mode, a long condition doesn't generate a single CC either.
1855 HInstruction* condition = select->GetCondition();
1856 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001857 DataType::Type compare_type = condition->InputAt(0)->GetType();
1858 if (compare_type == DataType::Type::kInt64 ||
1859 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001860 return false;
1861 }
1862 }
1863
1864 // We can generate a CMOV for this Select.
1865 return true;
1866}
1867
David Brazdil74eb1b22015-12-14 11:44:01 +00001868void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001871 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001872 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001873 } else {
1874 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001875 if (SelectCanUseCMOV(select)) {
1876 if (select->InputAt(1)->IsConstant()) {
1877 // Cmov can't handle a constant value.
1878 locations->SetInAt(1, Location::RequiresRegister());
1879 } else {
1880 locations->SetInAt(1, Location::Any());
1881 }
1882 } else {
1883 locations->SetInAt(1, Location::Any());
1884 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001885 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001886 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1887 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001888 }
1889 locations->SetOut(Location::SameAsFirstInput());
1890}
1891
1892void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1893 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001894 DCHECK(locations->InAt(0).Equals(locations->Out()));
1895 if (SelectCanUseCMOV(select)) {
1896 // If both the condition and the source types are integer, we can generate
1897 // a CMOV to implement Select.
1898
1899 HInstruction* select_condition = select->GetCondition();
1900 Condition cond = kNotEqual;
1901
1902 // Figure out how to test the 'condition'.
1903 if (select_condition->IsCondition()) {
1904 HCondition* condition = select_condition->AsCondition();
1905 if (!condition->IsEmittedAtUseSite()) {
1906 // This was a previously materialized condition.
1907 // Can we use the existing condition code?
1908 if (AreEflagsSetFrom(condition, select)) {
1909 // Materialization was the previous instruction. Condition codes are right.
1910 cond = X86Condition(condition->GetCondition());
1911 } else {
1912 // No, we have to recreate the condition code.
1913 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1914 __ testl(cond_reg, cond_reg);
1915 }
1916 } else {
1917 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1919 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001920 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001921 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001922 cond = X86Condition(condition->GetCondition());
1923 }
1924 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001925 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001926 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1927 __ testl(cond_reg, cond_reg);
1928 }
1929
1930 // If the condition is true, overwrite the output, which already contains false.
1931 Location false_loc = locations->InAt(0);
1932 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001933 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001934 // 64 bit conditional move.
1935 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1936 Register false_low = false_loc.AsRegisterPairLow<Register>();
1937 if (true_loc.IsRegisterPair()) {
1938 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1939 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1940 } else {
1941 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1942 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1943 }
1944 } else {
1945 // 32 bit conditional move.
1946 Register false_reg = false_loc.AsRegister<Register>();
1947 if (true_loc.IsRegister()) {
1948 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1949 } else {
1950 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1951 }
1952 }
1953 } else {
1954 NearLabel false_target;
1955 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001956 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001957 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1958 __ Bind(&false_target);
1959 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001960}
1961
David Srbecky0cf44932015-12-09 14:09:59 +00001962void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001963 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001964}
1965
David Srbeckyd28f4a02016-03-14 17:14:24 +00001966void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1967 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001968}
1969
Vladimir Markodec78172020-06-19 15:31:23 +01001970void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
1971 __ subl(ESP, Immediate(adjustment));
1972 __ cfi().AdjustCFAOffset(adjustment);
1973}
1974
1975void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
1976 __ addl(ESP, Immediate(adjustment));
1977 __ cfi().AdjustCFAOffset(-adjustment);
1978}
1979
David Srbeckyc7098ff2016-02-09 14:30:11 +00001980void CodeGeneratorX86::GenerateNop() {
1981 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001982}
1983
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001987 // Handle the long/FP comparisons made in instruction simplification.
1988 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001990 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001991 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001992 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001993 locations->SetOut(Location::RequiresRegister());
1994 }
1995 break;
1996 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kFloat32:
1998 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001999 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002000 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2001 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2002 } else if (cond->InputAt(1)->IsConstant()) {
2003 locations->SetInAt(1, Location::RequiresFpuRegister());
2004 } else {
2005 locations->SetInAt(1, Location::Any());
2006 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002007 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002008 locations->SetOut(Location::RequiresRegister());
2009 }
2010 break;
2011 }
2012 default:
2013 locations->SetInAt(0, Location::RequiresRegister());
2014 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002015 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002016 // We need a byte register.
2017 locations->SetOut(Location::RegisterLocation(ECX));
2018 }
2019 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002020 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021}
2022
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002024 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002025 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002026 }
Mark Mendellc4701932015-04-10 13:18:51 -04002027
2028 LocationSummary* locations = cond->GetLocations();
2029 Location lhs = locations->InAt(0);
2030 Location rhs = locations->InAt(1);
2031 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002032 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002033
2034 switch (cond->InputAt(0)->GetType()) {
2035 default: {
2036 // Integer case.
2037
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002038 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002039 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002040 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002041 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002042 return;
2043 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002045 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2046 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002048 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002049 GenerateFPJumps(cond, &true_label, &false_label);
2050 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002052 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002053 GenerateFPJumps(cond, &true_label, &false_label);
2054 break;
2055 }
2056
2057 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002058 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002059
Roland Levillain4fa13f62015-07-06 18:11:54 +01002060 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002061 __ Bind(&false_label);
2062 __ xorl(reg, reg);
2063 __ jmp(&done_label);
2064
Roland Levillain4fa13f62015-07-06 18:11:54 +01002065 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002066 __ Bind(&true_label);
2067 __ movl(reg, Immediate(1));
2068 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002069}
2070
2071void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002072 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002073}
2074
2075void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002076 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002077}
2078
2079void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002081}
2082
2083void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002085}
2086
2087void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002088 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002089}
2090
2091void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002093}
2094
2095void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002097}
2098
2099void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002100 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002101}
2102
2103void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002105}
2106
2107void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002108 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002109}
2110
2111void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002112 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002113}
2114
2115void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117}
2118
Aart Bike9f37602015-10-09 11:15:55 -07002119void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002120 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002121}
2122
2123void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002124 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002125}
2126
2127void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002129}
2130
2131void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002133}
2134
2135void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002137}
2138
2139void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002141}
2142
2143void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002145}
2146
2147void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002149}
2150
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002153 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002154 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155}
2156
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002157void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002158 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002159}
2160
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2162 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002163 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002164 locations->SetOut(Location::ConstantLocation(constant));
2165}
2166
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002167void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002168 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002169}
2170
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002172 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002173 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002174 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002175}
2176
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002177void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002178 // Will be generated at use site.
2179}
2180
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002183 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002184 locations->SetOut(Location::ConstantLocation(constant));
2185}
2186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002187void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 // Will be generated at use site.
2189}
2190
2191void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002194 locations->SetOut(Location::ConstantLocation(constant));
2195}
2196
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002197void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002198 // Will be generated at use site.
2199}
2200
Igor Murashkind01745e2017-04-05 16:40:31 -07002201void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2202 constructor_fence->SetLocations(nullptr);
2203}
2204
2205void InstructionCodeGeneratorX86::VisitConstructorFence(
2206 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2207 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2208}
2209
Calin Juravle27df7582015-04-17 19:12:31 +01002210void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2211 memory_barrier->SetLocations(nullptr);
2212}
2213
2214void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002215 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002216}
2217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002219 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002220}
2221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002222void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002223 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002224}
2225
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002226void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002227 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002228 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002230 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002232 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kInt8:
2234 case DataType::Type::kUint16:
2235 case DataType::Type::kInt16:
2236 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002238 break;
2239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002241 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002242 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002243 break;
2244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 case DataType::Type::kFloat32:
2246 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002247 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002249 break;
2250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002251 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002252 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002253 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002254}
2255
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002257 switch (ret->InputAt(0)->GetType()) {
2258 case DataType::Type::kReference:
2259 case DataType::Type::kBool:
2260 case DataType::Type::kUint8:
2261 case DataType::Type::kInt8:
2262 case DataType::Type::kUint16:
2263 case DataType::Type::kInt16:
2264 case DataType::Type::kInt32:
2265 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2266 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002267
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002268 case DataType::Type::kInt64:
2269 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2270 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2271 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002273 case DataType::Type::kFloat32:
2274 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2275 if (GetGraph()->IsCompilingOsr()) {
2276 // To simplify callers of an OSR method, we put the return value in both
2277 // floating point and core registers.
2278 __ movd(EAX, XMM0);
2279 }
2280 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002281
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002282 case DataType::Type::kFloat64:
2283 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2284 if (GetGraph()->IsCompilingOsr()) {
2285 // To simplify callers of an OSR method, we put the return value in both
2286 // floating point and core registers.
2287 __ movd(EAX, XMM0);
2288 // Use XMM1 as temporary register to not clobber XMM0.
2289 __ movaps(XMM1, XMM0);
2290 __ psrlq(XMM1, Immediate(32));
2291 __ movd(EDX, XMM1);
2292 }
2293 break;
2294
2295 default:
2296 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002298 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299}
2300
Calin Juravle175dc732015-08-25 15:42:32 +01002301void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2302 // The trampoline uses the same calling convention as dex calling conventions,
2303 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2304 // the method_idx.
2305 HandleInvoke(invoke);
2306}
2307
2308void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2309 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2310}
2311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002313 // Explicit clinit checks triggered by static invokes must have been pruned by
2314 // art::PrepareForRegisterAllocation.
2315 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002316
Mark Mendellfb8d2792015-03-31 22:16:59 -04002317 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002318 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002319 if (invoke->GetLocations()->CanCall() &&
2320 invoke->HasPcRelativeMethodLoadKind() &&
2321 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002322 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002323 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002324 return;
2325 }
2326
Vladimir Marko86c87522020-05-11 16:55:55 +01002327 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2328 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2329 /*for_register_allocation=*/ true);
2330 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2331 } else {
2332 HandleInvoke(invoke);
2333 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002334
Vladimir Marko86c87522020-05-11 16:55:55 +01002335 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002336 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002337 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002338 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002339}
2340
Mark Mendell09ed1a32015-03-25 08:30:06 -04002341static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2342 if (invoke->GetLocations()->Intrinsified()) {
2343 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2344 intrinsic.Dispatch(invoke);
2345 return true;
2346 }
2347 return false;
2348}
2349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002350void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002351 // Explicit clinit checks triggered by static invokes must have been pruned by
2352 // art::PrepareForRegisterAllocation.
2353 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002354
Mark Mendell09ed1a32015-03-25 08:30:06 -04002355 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2356 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002357 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002358
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002359 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002360 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002361 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002362}
2363
2364void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002365 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2366 if (intrinsic.TryDispatch(invoke)) {
2367 return;
2368 }
2369
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002370 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002371
2372 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002373 // Add one temporary for inline cache update.
2374 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2375 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376}
2377
2378void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002379 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002380 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002381}
2382
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002384 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2385 return;
2386 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002387
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002388 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002389 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002390}
2391
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002392void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002393 // This call to HandleInvoke allocates a temporary (core) register
2394 // which is also used to transfer the hidden argument from FP to
2395 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002396 HandleInvoke(invoke);
2397 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002398 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002399
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002400 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002401 // Add one temporary for inline cache update.
2402 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2403 }
2404}
2405
2406void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2407 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002408 // We know the destination of an intrinsic, so no need to record inline
2409 // caches (also the intrinsic location builder doesn't request an additional
2410 // temporary).
2411 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002412 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002413 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002414 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2415 ScopedObjectAccess soa(Thread::Current());
2416 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002417 if (info != nullptr) {
2418 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2419 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2420 if (kIsDebugBuild) {
2421 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2422 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2423 }
2424 Register temp = EBP;
2425 NearLabel done;
2426 __ movl(temp, Immediate(address));
2427 // Fast path for a monomorphic cache.
2428 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2429 __ j(kEqual, &done);
2430 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2431 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002432 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002433 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434}
2435
2436void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2437 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002438 LocationSummary* locations = invoke->GetLocations();
2439 Register temp = locations->GetTemp(0).AsRegister<Register>();
2440 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 Location receiver = locations->InAt(0);
2442 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2443
Roland Levillain0d5a2812015-11-13 10:07:31 +00002444 // Set the hidden argument. This is safe to do this here, as XMM7
2445 // won't be modified thereafter, before the `call` instruction.
2446 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002447 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002448 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002449
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450 if (receiver.IsStackSlot()) {
2451 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002452 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002453 __ movl(temp, Address(temp, class_offset));
2454 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002455 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002457 }
Roland Levillain4d027112015-07-01 15:41:14 +01002458 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002459 // Instead of simply (possibly) unpoisoning `temp` here, we should
2460 // emit a read barrier for the previous class reference load.
2461 // However this is not required in practice, as this is an
2462 // intermediate/temporary reference and because the current
2463 // concurrent copying collector keeps the from-space memory
2464 // intact/accessible until the end of the marking phase (the
2465 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002466 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002467
2468 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2469
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002470 // temp = temp->GetAddressOfIMT()
2471 __ movl(temp,
2472 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002473 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002474 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002475 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002476 __ movl(temp, Address(temp, method_offset));
2477 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002478 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002479 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002480
2481 DCHECK(!codegen_->IsLeafMethod());
2482 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2483}
2484
Orion Hodsonac141392017-01-13 11:53:47 +00002485void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2486 HandleInvoke(invoke);
2487}
2488
2489void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2490 codegen_->GenerateInvokePolymorphicCall(invoke);
2491}
2492
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002493void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2494 HandleInvoke(invoke);
2495}
2496
2497void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2498 codegen_->GenerateInvokeCustomCall(invoke);
2499}
2500
Roland Levillain88cb1752014-10-20 16:36:47 +01002501void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2502 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002503 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002504 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kInt32:
2506 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002507 locations->SetInAt(0, Location::RequiresRegister());
2508 locations->SetOut(Location::SameAsFirstInput());
2509 break;
2510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002512 locations->SetInAt(0, Location::RequiresFpuRegister());
2513 locations->SetOut(Location::SameAsFirstInput());
2514 locations->AddTemp(Location::RequiresRegister());
2515 locations->AddTemp(Location::RequiresFpuRegister());
2516 break;
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002519 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002520 locations->SetOut(Location::SameAsFirstInput());
2521 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2526 }
2527}
2528
2529void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2530 LocationSummary* locations = neg->GetLocations();
2531 Location out = locations->Out();
2532 Location in = locations->InAt(0);
2533 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002535 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002536 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002537 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002538 break;
2539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002541 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002542 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002543 __ negl(out.AsRegisterPairLow<Register>());
2544 // Negation is similar to subtraction from zero. The least
2545 // significant byte triggers a borrow when it is different from
2546 // zero; to take it into account, add 1 to the most significant
2547 // byte if the carry flag (CF) is set to 1 after the first NEGL
2548 // operation.
2549 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2550 __ negl(out.AsRegisterPairHigh<Register>());
2551 break;
2552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002554 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002555 Register constant = locations->GetTemp(0).AsRegister<Register>();
2556 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002557 // Implement float negation with an exclusive or with value
2558 // 0x80000000 (mask for bit 31, representing the sign of a
2559 // single-precision floating-point number).
2560 __ movl(constant, Immediate(INT32_C(0x80000000)));
2561 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002563 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002564 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002567 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002569 // Implement double negation with an exclusive or with value
2570 // 0x8000000000000000 (mask for bit 63, representing the sign of
2571 // a double-precision floating-point number).
2572 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002573 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002574 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002575 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002576
2577 default:
2578 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2579 }
2580}
2581
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002582void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2583 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002584 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002585 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002586 locations->SetInAt(0, Location::RequiresFpuRegister());
2587 locations->SetInAt(1, Location::RequiresRegister());
2588 locations->SetOut(Location::SameAsFirstInput());
2589 locations->AddTemp(Location::RequiresFpuRegister());
2590}
2591
2592void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2593 LocationSummary* locations = neg->GetLocations();
2594 Location out = locations->Out();
2595 DCHECK(locations->InAt(0).Equals(out));
2596
2597 Register constant_area = locations->InAt(1).AsRegister<Register>();
2598 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002599 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002600 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2601 neg->GetBaseMethodAddress(),
2602 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002603 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2604 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002605 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2606 neg->GetBaseMethodAddress(),
2607 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002608 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2609 }
2610}
2611
Roland Levillaindff1f282014-11-05 14:15:05 +00002612void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 DataType::Type result_type = conversion->GetResultType();
2614 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002615 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2616 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002617
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002618 // The float-to-long and double-to-long type conversions rely on a
2619 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002620 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2622 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002623 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002624 : LocationSummary::kNoCall;
2625 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002626 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002627
Roland Levillaindff1f282014-11-05 14:15:05 +00002628 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002629 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002631 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002632 case DataType::Type::kUint8:
2633 case DataType::Type::kInt8:
2634 case DataType::Type::kUint16:
2635 case DataType::Type::kInt16:
2636 case DataType::Type::kInt32:
2637 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2638 // Make the output overlap to please the register allocator. This greatly simplifies
2639 // the validation of the linear scan implementation
2640 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2641 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002643 HInstruction* input = conversion->InputAt(0);
2644 Location input_location = input->IsConstant()
2645 ? Location::ConstantLocation(input->AsConstant())
2646 : Location::RegisterPairLocation(EAX, EDX);
2647 locations->SetInAt(0, input_location);
2648 // Make the output overlap to please the register allocator. This greatly simplifies
2649 // the validation of the linear scan implementation
2650 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2651 break;
2652 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002653
2654 default:
2655 LOG(FATAL) << "Unexpected type conversion from " << input_type
2656 << " to " << result_type;
2657 }
2658 break;
2659
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002662 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2663 locations->SetInAt(0, Location::Any());
2664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002668 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002670 locations->SetInAt(0, Location::Any());
2671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2672 break;
2673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002675 locations->SetInAt(0, Location::RequiresFpuRegister());
2676 locations->SetOut(Location::RequiresRegister());
2677 locations->AddTemp(Location::RequiresFpuRegister());
2678 break;
2679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002681 locations->SetInAt(0, Location::RequiresFpuRegister());
2682 locations->SetOut(Location::RequiresRegister());
2683 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002684 break;
2685
2686 default:
2687 LOG(FATAL) << "Unexpected type conversion from " << input_type
2688 << " to " << result_type;
2689 }
2690 break;
2691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002695 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002697 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 case DataType::Type::kInt16:
2699 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 locations->SetInAt(0, Location::RegisterLocation(EAX));
2701 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2702 break;
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kFloat32:
2705 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002706 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002707 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2708 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2709
Vladimir Marko949c91f2015-01-27 10:48:44 +00002710 // The runtime helper puts the result in EAX, EDX.
2711 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002712 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002713 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002714
2715 default:
2716 LOG(FATAL) << "Unexpected type conversion from " << input_type
2717 << " to " << result_type;
2718 }
2719 break;
2720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002722 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002724 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002726 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kInt16:
2728 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002729 locations->SetInAt(0, Location::RequiresRegister());
2730 locations->SetOut(Location::RequiresFpuRegister());
2731 break;
2732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002734 locations->SetInAt(0, Location::Any());
2735 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002736 break;
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002739 locations->SetInAt(0, Location::RequiresFpuRegister());
2740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002741 break;
2742
2743 default:
2744 LOG(FATAL) << "Unexpected type conversion from " << input_type
2745 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002746 }
Roland Levillaincff13742014-11-17 14:32:17 +00002747 break;
2748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002750 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002752 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002754 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 case DataType::Type::kInt16:
2756 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002757 locations->SetInAt(0, Location::RequiresRegister());
2758 locations->SetOut(Location::RequiresFpuRegister());
2759 break;
2760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002762 locations->SetInAt(0, Location::Any());
2763 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002764 break;
2765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002767 locations->SetInAt(0, Location::RequiresFpuRegister());
2768 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002769 break;
2770
2771 default:
2772 LOG(FATAL) << "Unexpected type conversion from " << input_type
2773 << " to " << result_type;
2774 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 break;
2776
2777 default:
2778 LOG(FATAL) << "Unexpected type conversion from " << input_type
2779 << " to " << result_type;
2780 }
2781}
2782
2783void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2784 LocationSummary* locations = conversion->GetLocations();
2785 Location out = locations->Out();
2786 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 DataType::Type result_type = conversion->GetResultType();
2788 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002789 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2790 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002791 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002792 case DataType::Type::kUint8:
2793 switch (input_type) {
2794 case DataType::Type::kInt8:
2795 case DataType::Type::kUint16:
2796 case DataType::Type::kInt16:
2797 case DataType::Type::kInt32:
2798 if (in.IsRegister()) {
2799 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2800 } else {
2801 DCHECK(in.GetConstant()->IsIntConstant());
2802 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2803 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2804 }
2805 break;
2806 case DataType::Type::kInt64:
2807 if (in.IsRegisterPair()) {
2808 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2809 } else {
2810 DCHECK(in.GetConstant()->IsLongConstant());
2811 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2812 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2813 }
2814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 }
2820 break;
2821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002823 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002824 case DataType::Type::kUint8:
2825 case DataType::Type::kUint16:
2826 case DataType::Type::kInt16:
2827 case DataType::Type::kInt32:
2828 if (in.IsRegister()) {
2829 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2830 } else {
2831 DCHECK(in.GetConstant()->IsIntConstant());
2832 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2833 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2834 }
2835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002837 if (in.IsRegisterPair()) {
2838 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2839 } else {
2840 DCHECK(in.GetConstant()->IsLongConstant());
2841 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2842 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2843 }
2844 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002845
2846 default:
2847 LOG(FATAL) << "Unexpected type conversion from " << input_type
2848 << " to " << result_type;
2849 }
2850 break;
2851
2852 case DataType::Type::kUint16:
2853 switch (input_type) {
2854 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 case DataType::Type::kInt16:
2856 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002857 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002858 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2859 } else if (in.IsStackSlot()) {
2860 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002861 } else {
2862 DCHECK(in.GetConstant()->IsIntConstant());
2863 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002864 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2865 }
2866 break;
2867 case DataType::Type::kInt64:
2868 if (in.IsRegisterPair()) {
2869 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2870 } else if (in.IsDoubleStackSlot()) {
2871 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2872 } else {
2873 DCHECK(in.GetConstant()->IsLongConstant());
2874 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2875 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002876 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002877 break;
2878
2879 default:
2880 LOG(FATAL) << "Unexpected type conversion from " << input_type
2881 << " to " << result_type;
2882 }
2883 break;
2884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002886 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002888 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002889 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002890 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002891 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002892 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002893 } else {
2894 DCHECK(in.GetConstant()->IsIntConstant());
2895 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002896 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002897 }
2898 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002899 case DataType::Type::kInt64:
2900 if (in.IsRegisterPair()) {
2901 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2902 } else if (in.IsDoubleStackSlot()) {
2903 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2904 } else {
2905 DCHECK(in.GetConstant()->IsLongConstant());
2906 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2907 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2908 }
2909 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002910
2911 default:
2912 LOG(FATAL) << "Unexpected type conversion from " << input_type
2913 << " to " << result_type;
2914 }
2915 break;
2916
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002918 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002920 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002922 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002923 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002924 } else {
2925 DCHECK(in.IsConstant());
2926 DCHECK(in.GetConstant()->IsLongConstant());
2927 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002929 }
2930 break;
2931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002933 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2934 Register output = out.AsRegister<Register>();
2935 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002936 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002937
2938 __ movl(output, Immediate(kPrimIntMax));
2939 // temp = int-to-float(output)
2940 __ cvtsi2ss(temp, output);
2941 // if input >= temp goto done
2942 __ comiss(input, temp);
2943 __ j(kAboveEqual, &done);
2944 // if input == NaN goto nan
2945 __ j(kUnordered, &nan);
2946 // output = float-to-int-truncate(input)
2947 __ cvttss2si(output, input);
2948 __ jmp(&done);
2949 __ Bind(&nan);
2950 // output = 0
2951 __ xorl(output, output);
2952 __ Bind(&done);
2953 break;
2954 }
2955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002957 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2958 Register output = out.AsRegister<Register>();
2959 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002960 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002961
2962 __ movl(output, Immediate(kPrimIntMax));
2963 // temp = int-to-double(output)
2964 __ cvtsi2sd(temp, output);
2965 // if input >= temp goto done
2966 __ comisd(input, temp);
2967 __ j(kAboveEqual, &done);
2968 // if input == NaN goto nan
2969 __ j(kUnordered, &nan);
2970 // output = double-to-int-truncate(input)
2971 __ cvttsd2si(output, input);
2972 __ jmp(&done);
2973 __ Bind(&nan);
2974 // output = 0
2975 __ xorl(output, output);
2976 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002977 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002978 }
Roland Levillain946e1432014-11-11 17:35:19 +00002979
2980 default:
2981 LOG(FATAL) << "Unexpected type conversion from " << input_type
2982 << " to " << result_type;
2983 }
2984 break;
2985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002987 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002989 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002991 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 case DataType::Type::kInt16:
2993 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002994 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2995 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002997 __ cdq();
2998 break;
2999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003001 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003002 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003003 break;
3004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003005 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003006 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003007 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003008 break;
3009
3010 default:
3011 LOG(FATAL) << "Unexpected type conversion from " << input_type
3012 << " to " << result_type;
3013 }
3014 break;
3015
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003017 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003019 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003021 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kInt16:
3023 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003025 break;
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003028 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003029
Roland Levillain232ade02015-04-20 15:14:36 +01003030 // Create stack space for the call to
3031 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3032 // TODO: enhance register allocator to ask for stack temporaries.
3033 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003035 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003036 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003037
Roland Levillain232ade02015-04-20 15:14:36 +01003038 // Load the value to the FP stack, using temporaries if needed.
3039 PushOntoFPStack(in, 0, adjustment, false, true);
3040
3041 if (out.IsStackSlot()) {
3042 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3043 } else {
3044 __ fstps(Address(ESP, 0));
3045 Location stack_temp = Location::StackSlot(0);
3046 codegen_->Move32(out, stack_temp);
3047 }
3048
3049 // Remove the temporary stack space we allocated.
3050 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003051 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003052 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003053 break;
3054 }
3055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003057 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003058 break;
3059
3060 default:
3061 LOG(FATAL) << "Unexpected type conversion from " << input_type
3062 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003063 }
Roland Levillaincff13742014-11-17 14:32:17 +00003064 break;
3065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003067 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003069 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003071 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 case DataType::Type::kInt16:
3073 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003075 break;
3076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003078 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003079
Roland Levillain232ade02015-04-20 15:14:36 +01003080 // Create stack space for the call to
3081 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3082 // TODO: enhance register allocator to ask for stack temporaries.
3083 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003084 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003085 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003086 }
3087
3088 // Load the value to the FP stack, using temporaries if needed.
3089 PushOntoFPStack(in, 0, adjustment, false, true);
3090
3091 if (out.IsDoubleStackSlot()) {
3092 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3093 } else {
3094 __ fstpl(Address(ESP, 0));
3095 Location stack_temp = Location::DoubleStackSlot(0);
3096 codegen_->Move64(out, stack_temp);
3097 }
3098
3099 // Remove the temporary stack space we allocated.
3100 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003101 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003102 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003103 break;
3104 }
3105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003107 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003108 break;
3109
3110 default:
3111 LOG(FATAL) << "Unexpected type conversion from " << input_type
3112 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003113 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003114 break;
3115
3116 default:
3117 LOG(FATAL) << "Unexpected type conversion from " << input_type
3118 << " to " << result_type;
3119 }
3120}
3121
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003122void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003123 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003124 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003125 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003127 locations->SetInAt(0, Location::RequiresRegister());
3128 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3130 break;
3131 }
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003134 locations->SetInAt(0, Location::RequiresRegister());
3135 locations->SetInAt(1, Location::Any());
3136 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003137 break;
3138 }
3139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003140 case DataType::Type::kFloat32:
3141 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003142 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003143 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3144 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003145 } else if (add->InputAt(1)->IsConstant()) {
3146 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003147 } else {
3148 locations->SetInAt(1, Location::Any());
3149 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003150 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003151 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003152 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003153
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003154 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003155 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003156 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003157 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003158}
3159
3160void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3161 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003162 Location first = locations->InAt(0);
3163 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003164 Location out = locations->Out();
3165
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003166 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003168 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003169 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3170 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003171 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3172 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003173 } else {
3174 __ leal(out.AsRegister<Register>(), Address(
3175 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3176 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003177 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003178 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3179 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3180 __ addl(out.AsRegister<Register>(), Immediate(value));
3181 } else {
3182 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3183 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003184 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003185 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003187 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003188 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003189 }
3190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003192 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003193 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3194 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003195 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003196 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3197 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003198 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003199 } else {
3200 DCHECK(second.IsConstant()) << second;
3201 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3202 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3203 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003204 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003205 break;
3206 }
3207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003208 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003209 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003211 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3212 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003213 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003214 __ addss(first.AsFpuRegister<XmmRegister>(),
3215 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003216 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3217 const_area->GetBaseMethodAddress(),
3218 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003219 } else {
3220 DCHECK(second.IsStackSlot());
3221 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003222 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003223 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003224 }
3225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003226 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003227 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003229 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3230 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003231 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003232 __ addsd(first.AsFpuRegister<XmmRegister>(),
3233 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003234 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3235 const_area->GetBaseMethodAddress(),
3236 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003237 } else {
3238 DCHECK(second.IsDoubleStackSlot());
3239 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003240 }
3241 break;
3242 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003243
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003244 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003245 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003246 }
3247}
3248
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003249void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003250 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003251 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003252 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kInt32:
3254 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003255 locations->SetInAt(0, Location::RequiresRegister());
3256 locations->SetInAt(1, Location::Any());
3257 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003258 break;
3259 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003260 case DataType::Type::kFloat32:
3261 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003262 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003263 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3264 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003265 } else if (sub->InputAt(1)->IsConstant()) {
3266 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003267 } else {
3268 locations->SetInAt(1, Location::Any());
3269 }
Calin Juravle11351682014-10-23 15:38:15 +01003270 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003271 break;
Calin Juravle11351682014-10-23 15:38:15 +01003272 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003273
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003274 default:
Calin Juravle11351682014-10-23 15:38:15 +01003275 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003276 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003277}
3278
3279void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3280 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003281 Location first = locations->InAt(0);
3282 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003283 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003284 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003285 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003286 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003287 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003288 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003289 __ subl(first.AsRegister<Register>(),
3290 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003291 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003292 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003293 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003294 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003295 }
3296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003297 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003298 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003299 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3300 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003301 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003302 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003303 __ sbbl(first.AsRegisterPairHigh<Register>(),
3304 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003305 } else {
3306 DCHECK(second.IsConstant()) << second;
3307 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3308 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3309 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003310 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003311 break;
3312 }
3313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003315 if (second.IsFpuRegister()) {
3316 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3317 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3318 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003319 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003320 __ subss(first.AsFpuRegister<XmmRegister>(),
3321 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003322 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3323 const_area->GetBaseMethodAddress(),
3324 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003325 } else {
3326 DCHECK(second.IsStackSlot());
3327 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3328 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003329 break;
Calin Juravle11351682014-10-23 15:38:15 +01003330 }
3331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003333 if (second.IsFpuRegister()) {
3334 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3335 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3336 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003337 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 __ subsd(first.AsFpuRegister<XmmRegister>(),
3339 codegen_->LiteralDoubleAddress(
3340 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003341 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003342 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3343 } else {
3344 DCHECK(second.IsDoubleStackSlot());
3345 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3346 }
Calin Juravle11351682014-10-23 15:38:15 +01003347 break;
3348 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003349
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003350 default:
Calin Juravle11351682014-10-23 15:38:15 +01003351 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003352 }
3353}
3354
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355void LocationsBuilderX86::VisitMul(HMul* mul) {
3356 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003357 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003358 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003359 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003360 locations->SetInAt(0, Location::RequiresRegister());
3361 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003362 if (mul->InputAt(1)->IsIntConstant()) {
3363 // Can use 3 operand multiply.
3364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3365 } else {
3366 locations->SetOut(Location::SameAsFirstInput());
3367 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003368 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003370 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003371 locations->SetInAt(1, Location::Any());
3372 locations->SetOut(Location::SameAsFirstInput());
3373 // Needed for imul on 32bits with 64bits output.
3374 locations->AddTemp(Location::RegisterLocation(EAX));
3375 locations->AddTemp(Location::RegisterLocation(EDX));
3376 break;
3377 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003378 case DataType::Type::kFloat32:
3379 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003380 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003381 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3382 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003383 } else if (mul->InputAt(1)->IsConstant()) {
3384 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003385 } else {
3386 locations->SetInAt(1, Location::Any());
3387 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003388 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003389 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003390 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003391
3392 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003393 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003394 }
3395}
3396
3397void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3398 LocationSummary* locations = mul->GetLocations();
3399 Location first = locations->InAt(0);
3400 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003401 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003402
3403 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003404 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003405 // The constant may have ended up in a register, so test explicitly to avoid
3406 // problems where the output may not be the same as the first operand.
3407 if (mul->InputAt(1)->IsIntConstant()) {
3408 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3409 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3410 } else if (second.IsRegister()) {
3411 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003412 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003413 } else {
3414 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003415 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003416 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003417 }
3418 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003420 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003421 Register in1_hi = first.AsRegisterPairHigh<Register>();
3422 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 Register eax = locations->GetTemp(0).AsRegister<Register>();
3424 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003425
3426 DCHECK_EQ(EAX, eax);
3427 DCHECK_EQ(EDX, edx);
3428
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003429 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003430 // output: in1
3431 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3432 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3433 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003434 if (second.IsConstant()) {
3435 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003436
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003437 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3438 int32_t low_value = Low32Bits(value);
3439 int32_t high_value = High32Bits(value);
3440 Immediate low(low_value);
3441 Immediate high(high_value);
3442
3443 __ movl(eax, high);
3444 // eax <- in1.lo * in2.hi
3445 __ imull(eax, in1_lo);
3446 // in1.hi <- in1.hi * in2.lo
3447 __ imull(in1_hi, low);
3448 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3449 __ addl(in1_hi, eax);
3450 // move in2_lo to eax to prepare for double precision
3451 __ movl(eax, low);
3452 // edx:eax <- in1.lo * in2.lo
3453 __ mull(in1_lo);
3454 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3455 __ addl(in1_hi, edx);
3456 // in1.lo <- (in1.lo * in2.lo)[31:0];
3457 __ movl(in1_lo, eax);
3458 } else if (second.IsRegisterPair()) {
3459 Register in2_hi = second.AsRegisterPairHigh<Register>();
3460 Register in2_lo = second.AsRegisterPairLow<Register>();
3461
3462 __ movl(eax, in2_hi);
3463 // eax <- in1.lo * in2.hi
3464 __ imull(eax, in1_lo);
3465 // in1.hi <- in1.hi * in2.lo
3466 __ imull(in1_hi, in2_lo);
3467 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3468 __ addl(in1_hi, eax);
3469 // move in1_lo to eax to prepare for double precision
3470 __ movl(eax, in1_lo);
3471 // edx:eax <- in1.lo * in2.lo
3472 __ mull(in2_lo);
3473 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3474 __ addl(in1_hi, edx);
3475 // in1.lo <- (in1.lo * in2.lo)[31:0];
3476 __ movl(in1_lo, eax);
3477 } else {
3478 DCHECK(second.IsDoubleStackSlot()) << second;
3479 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3480 Address in2_lo(ESP, second.GetStackIndex());
3481
3482 __ movl(eax, in2_hi);
3483 // eax <- in1.lo * in2.hi
3484 __ imull(eax, in1_lo);
3485 // in1.hi <- in1.hi * in2.lo
3486 __ imull(in1_hi, in2_lo);
3487 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3488 __ addl(in1_hi, eax);
3489 // move in1_lo to eax to prepare for double precision
3490 __ movl(eax, in1_lo);
3491 // edx:eax <- in1.lo * in2.lo
3492 __ mull(in2_lo);
3493 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3494 __ addl(in1_hi, edx);
3495 // in1.lo <- (in1.lo * in2.lo)[31:0];
3496 __ movl(in1_lo, eax);
3497 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003498
3499 break;
3500 }
3501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003503 DCHECK(first.Equals(locations->Out()));
3504 if (second.IsFpuRegister()) {
3505 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3506 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3507 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003508 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003509 __ mulss(first.AsFpuRegister<XmmRegister>(),
3510 codegen_->LiteralFloatAddress(
3511 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003512 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003513 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3514 } else {
3515 DCHECK(second.IsStackSlot());
3516 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3517 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003518 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003519 }
3520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003522 DCHECK(first.Equals(locations->Out()));
3523 if (second.IsFpuRegister()) {
3524 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3525 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3526 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003527 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003528 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3529 codegen_->LiteralDoubleAddress(
3530 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003531 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003532 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3533 } else {
3534 DCHECK(second.IsDoubleStackSlot());
3535 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3536 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003537 break;
3538 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003539
3540 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003541 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003542 }
3543}
3544
Roland Levillain232ade02015-04-20 15:14:36 +01003545void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3546 uint32_t temp_offset,
3547 uint32_t stack_adjustment,
3548 bool is_fp,
3549 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003550 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003551 DCHECK(!is_wide);
3552 if (is_fp) {
3553 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3554 } else {
3555 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3556 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003557 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003558 DCHECK(is_wide);
3559 if (is_fp) {
3560 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3561 } else {
3562 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3563 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 } else {
3565 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003566 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003567 Location stack_temp = Location::StackSlot(temp_offset);
3568 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003569 if (is_fp) {
3570 __ flds(Address(ESP, temp_offset));
3571 } else {
3572 __ filds(Address(ESP, temp_offset));
3573 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003574 } else {
3575 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3576 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003577 if (is_fp) {
3578 __ fldl(Address(ESP, temp_offset));
3579 } else {
3580 __ fildl(Address(ESP, temp_offset));
3581 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003582 }
3583 }
3584}
3585
3586void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 DataType::Type type = rem->GetResultType();
3588 bool is_float = type == DataType::Type::kFloat32;
3589 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003590 LocationSummary* locations = rem->GetLocations();
3591 Location first = locations->InAt(0);
3592 Location second = locations->InAt(1);
3593 Location out = locations->Out();
3594
3595 // Create stack space for 2 elements.
3596 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003597 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003598
3599 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003600 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003601 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3602 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003603
3604 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003605 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003606 __ Bind(&retry);
3607 __ fprem();
3608
3609 // Move FP status to AX.
3610 __ fstsw();
3611
3612 // And see if the argument reduction is complete. This is signaled by the
3613 // C2 FPU flag bit set to 0.
3614 __ andl(EAX, Immediate(kC2ConditionMask));
3615 __ j(kNotEqual, &retry);
3616
3617 // We have settled on the final value. Retrieve it into an XMM register.
3618 // Store FP top of stack to real stack.
3619 if (is_float) {
3620 __ fsts(Address(ESP, 0));
3621 } else {
3622 __ fstl(Address(ESP, 0));
3623 }
3624
3625 // Pop the 2 items from the FP stack.
3626 __ fucompp();
3627
3628 // Load the value from the stack into an XMM register.
3629 DCHECK(out.IsFpuRegister()) << out;
3630 if (is_float) {
3631 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3632 } else {
3633 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3634 }
3635
3636 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003637 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003638}
3639
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003640
3641void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3642 DCHECK(instruction->IsDiv() || instruction->IsRem());
3643
3644 LocationSummary* locations = instruction->GetLocations();
3645 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003646 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647
3648 Register out_register = locations->Out().AsRegister<Register>();
3649 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003650 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651
3652 DCHECK(imm == 1 || imm == -1);
3653
3654 if (instruction->IsRem()) {
3655 __ xorl(out_register, out_register);
3656 } else {
3657 __ movl(out_register, input_register);
3658 if (imm == -1) {
3659 __ negl(out_register);
3660 }
3661 }
3662}
3663
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303664void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3665 LocationSummary* locations = instruction->GetLocations();
3666 Location second = locations->InAt(1);
3667
3668 Register out = locations->Out().AsRegister<Register>();
3669 Register numerator = locations->InAt(0).AsRegister<Register>();
3670
3671 int32_t imm = Int64FromConstant(second.GetConstant());
3672 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3673 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3674
3675 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3676 NearLabel done;
3677 __ movl(out, numerator);
3678 __ andl(out, Immediate(abs_imm-1));
3679 __ j(Condition::kZero, &done);
3680 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3681 __ testl(numerator, numerator);
3682 __ cmovl(Condition::kLess, out, tmp);
3683 __ Bind(&done);
3684}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003685
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003686void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003687 LocationSummary* locations = instruction->GetLocations();
3688
3689 Register out_register = locations->Out().AsRegister<Register>();
3690 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003691 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003692 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3693 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003694
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003695 Register num = locations->GetTemp(0).AsRegister<Register>();
3696
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003697 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003698 __ testl(input_register, input_register);
3699 __ cmovl(kGreaterEqual, num, input_register);
3700 int shift = CTZ(imm);
3701 __ sarl(num, Immediate(shift));
3702
3703 if (imm < 0) {
3704 __ negl(num);
3705 }
3706
3707 __ movl(out_register, num);
3708}
3709
3710void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3711 DCHECK(instruction->IsDiv() || instruction->IsRem());
3712
3713 LocationSummary* locations = instruction->GetLocations();
3714 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3715
3716 Register eax = locations->InAt(0).AsRegister<Register>();
3717 Register out = locations->Out().AsRegister<Register>();
3718 Register num;
3719 Register edx;
3720
3721 if (instruction->IsDiv()) {
3722 edx = locations->GetTemp(0).AsRegister<Register>();
3723 num = locations->GetTemp(1).AsRegister<Register>();
3724 } else {
3725 edx = locations->Out().AsRegister<Register>();
3726 num = locations->GetTemp(0).AsRegister<Register>();
3727 }
3728
3729 DCHECK_EQ(EAX, eax);
3730 DCHECK_EQ(EDX, edx);
3731 if (instruction->IsDiv()) {
3732 DCHECK_EQ(EAX, out);
3733 } else {
3734 DCHECK_EQ(EDX, out);
3735 }
3736
3737 int64_t magic;
3738 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003739 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003740
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003741 // Save the numerator.
3742 __ movl(num, eax);
3743
3744 // EAX = magic
3745 __ movl(eax, Immediate(magic));
3746
3747 // EDX:EAX = magic * numerator
3748 __ imull(num);
3749
3750 if (imm > 0 && magic < 0) {
3751 // EDX += num
3752 __ addl(edx, num);
3753 } else if (imm < 0 && magic > 0) {
3754 __ subl(edx, num);
3755 }
3756
3757 // Shift if needed.
3758 if (shift != 0) {
3759 __ sarl(edx, Immediate(shift));
3760 }
3761
3762 // EDX += 1 if EDX < 0
3763 __ movl(eax, edx);
3764 __ shrl(edx, Immediate(31));
3765 __ addl(edx, eax);
3766
3767 if (instruction->IsRem()) {
3768 __ movl(eax, num);
3769 __ imull(edx, Immediate(imm));
3770 __ subl(eax, edx);
3771 __ movl(edx, eax);
3772 } else {
3773 __ movl(eax, edx);
3774 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003775}
3776
Calin Juravlebacfec32014-11-14 15:54:36 +00003777void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3778 DCHECK(instruction->IsDiv() || instruction->IsRem());
3779
3780 LocationSummary* locations = instruction->GetLocations();
3781 Location out = locations->Out();
3782 Location first = locations->InAt(0);
3783 Location second = locations->InAt(1);
3784 bool is_div = instruction->IsDiv();
3785
3786 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003788 DCHECK_EQ(EAX, first.AsRegister<Register>());
3789 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003790
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003791 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003792 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003793
3794 if (imm == 0) {
3795 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3796 } else if (imm == 1 || imm == -1) {
3797 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303798 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3799 if (is_div) {
3800 DivByPowerOfTwo(instruction->AsDiv());
3801 } else {
3802 RemByPowerOfTwo(instruction->AsRem());
3803 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003804 } else {
3805 DCHECK(imm <= -2 || imm >= 2);
3806 GenerateDivRemWithAnyConstant(instruction);
3807 }
3808 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003809 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003810 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003811 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003812
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003813 Register second_reg = second.AsRegister<Register>();
3814 // 0x80000000/-1 triggers an arithmetic exception!
3815 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3816 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003817
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003818 __ cmpl(second_reg, Immediate(-1));
3819 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003820
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003821 // edx:eax <- sign-extended of eax
3822 __ cdq();
3823 // eax = quotient, edx = remainder
3824 __ idivl(second_reg);
3825 __ Bind(slow_path->GetExitLabel());
3826 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003827 break;
3828 }
3829
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003831 InvokeRuntimeCallingConvention calling_convention;
3832 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3833 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3834 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3835 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3836 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3837 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3838
3839 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003840 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003841 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003842 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003843 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003844 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003845 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003846 break;
3847 }
3848
3849 default:
3850 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3851 }
3852}
3853
Calin Juravle7c4954d2014-10-28 16:57:40 +00003854void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003855 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003856 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003857 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003858 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003859
Calin Juravle7c4954d2014-10-28 16:57:40 +00003860 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003861 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003862 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003863 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003864 locations->SetOut(Location::SameAsFirstInput());
3865 // Intel uses edx:eax as the dividend.
3866 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003867 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3868 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3869 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003870 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003871 locations->AddTemp(Location::RequiresRegister());
3872 }
Calin Juravled0d48522014-11-04 16:40:20 +00003873 break;
3874 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003875 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003876 InvokeRuntimeCallingConvention calling_convention;
3877 locations->SetInAt(0, Location::RegisterPairLocation(
3878 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3879 locations->SetInAt(1, Location::RegisterPairLocation(
3880 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3881 // Runtime helper puts the result in EAX, EDX.
3882 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003883 break;
3884 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003885 case DataType::Type::kFloat32:
3886 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003887 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003888 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3889 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003890 } else if (div->InputAt(1)->IsConstant()) {
3891 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003892 } else {
3893 locations->SetInAt(1, Location::Any());
3894 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003895 locations->SetOut(Location::SameAsFirstInput());
3896 break;
3897 }
3898
3899 default:
3900 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3901 }
3902}
3903
3904void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3905 LocationSummary* locations = div->GetLocations();
3906 Location first = locations->InAt(0);
3907 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003908
3909 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 case DataType::Type::kInt32:
3911 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003912 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003913 break;
3914 }
3915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003917 if (second.IsFpuRegister()) {
3918 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3919 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3920 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003921 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003922 __ divss(first.AsFpuRegister<XmmRegister>(),
3923 codegen_->LiteralFloatAddress(
3924 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003925 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003926 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3927 } else {
3928 DCHECK(second.IsStackSlot());
3929 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3930 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003931 break;
3932 }
3933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003935 if (second.IsFpuRegister()) {
3936 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3937 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3938 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003939 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003940 __ divsd(first.AsFpuRegister<XmmRegister>(),
3941 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003942 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3943 const_area->GetBaseMethodAddress(),
3944 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003945 } else {
3946 DCHECK(second.IsDoubleStackSlot());
3947 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3948 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003949 break;
3950 }
3951
3952 default:
3953 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3954 }
3955}
3956
Calin Juravlebacfec32014-11-14 15:54:36 +00003957void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003961 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003962 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003963 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003964
Calin Juravled2ec87d2014-12-08 14:24:46 +00003965 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003966 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003967 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003968 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003969 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003970 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3971 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3972 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003973 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003974 locations->AddTemp(Location::RequiresRegister());
3975 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003976 break;
3977 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003979 InvokeRuntimeCallingConvention calling_convention;
3980 locations->SetInAt(0, Location::RegisterPairLocation(
3981 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3982 locations->SetInAt(1, Location::RegisterPairLocation(
3983 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3984 // Runtime helper puts the result in EAX, EDX.
3985 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3986 break;
3987 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003988 case DataType::Type::kFloat64:
3989 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003990 locations->SetInAt(0, Location::Any());
3991 locations->SetInAt(1, Location::Any());
3992 locations->SetOut(Location::RequiresFpuRegister());
3993 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003994 break;
3995 }
3996
3997 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003998 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003999 }
4000}
4001
4002void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004003 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004004 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004005 case DataType::Type::kInt32:
4006 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004007 GenerateDivRemIntegral(rem);
4008 break;
4009 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004010 case DataType::Type::kFloat32:
4011 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004012 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004013 break;
4014 }
4015 default:
4016 LOG(FATAL) << "Unexpected rem type " << type;
4017 }
4018}
4019
Aart Bik1f8d51b2018-02-15 10:42:37 -08004020static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4021 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4022 switch (minmax->GetResultType()) {
4023 case DataType::Type::kInt32:
4024 locations->SetInAt(0, Location::RequiresRegister());
4025 locations->SetInAt(1, Location::RequiresRegister());
4026 locations->SetOut(Location::SameAsFirstInput());
4027 break;
4028 case DataType::Type::kInt64:
4029 locations->SetInAt(0, Location::RequiresRegister());
4030 locations->SetInAt(1, Location::RequiresRegister());
4031 locations->SetOut(Location::SameAsFirstInput());
4032 // Register to use to perform a long subtract to set cc.
4033 locations->AddTemp(Location::RequiresRegister());
4034 break;
4035 case DataType::Type::kFloat32:
4036 locations->SetInAt(0, Location::RequiresFpuRegister());
4037 locations->SetInAt(1, Location::RequiresFpuRegister());
4038 locations->SetOut(Location::SameAsFirstInput());
4039 locations->AddTemp(Location::RequiresRegister());
4040 break;
4041 case DataType::Type::kFloat64:
4042 locations->SetInAt(0, Location::RequiresFpuRegister());
4043 locations->SetInAt(1, Location::RequiresFpuRegister());
4044 locations->SetOut(Location::SameAsFirstInput());
4045 break;
4046 default:
4047 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4048 }
4049}
4050
Aart Bik351df3e2018-03-07 11:54:57 -08004051void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4052 bool is_min,
4053 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004054 Location op1_loc = locations->InAt(0);
4055 Location op2_loc = locations->InAt(1);
4056
4057 // Shortcut for same input locations.
4058 if (op1_loc.Equals(op2_loc)) {
4059 // Can return immediately, as op1_loc == out_loc.
4060 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4061 // a copy here.
4062 DCHECK(locations->Out().Equals(op1_loc));
4063 return;
4064 }
4065
4066 if (type == DataType::Type::kInt64) {
4067 // Need to perform a subtract to get the sign right.
4068 // op1 is already in the same location as the output.
4069 Location output = locations->Out();
4070 Register output_lo = output.AsRegisterPairLow<Register>();
4071 Register output_hi = output.AsRegisterPairHigh<Register>();
4072
4073 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4074 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4075
4076 // The comparison is performed by subtracting the second operand from
4077 // the first operand and then setting the status flags in the same
4078 // manner as the SUB instruction."
4079 __ cmpl(output_lo, op2_lo);
4080
4081 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4082 Register temp = locations->GetTemp(0).AsRegister<Register>();
4083 __ movl(temp, output_hi);
4084 __ sbbl(temp, op2_hi);
4085
4086 // Now the condition code is correct.
4087 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4088 __ cmovl(cond, output_lo, op2_lo);
4089 __ cmovl(cond, output_hi, op2_hi);
4090 } else {
4091 DCHECK_EQ(type, DataType::Type::kInt32);
4092 Register out = locations->Out().AsRegister<Register>();
4093 Register op2 = op2_loc.AsRegister<Register>();
4094
4095 // (out := op1)
4096 // out <=? op2
4097 // if out is min jmp done
4098 // out := op2
4099 // done:
4100
4101 __ cmpl(out, op2);
4102 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4103 __ cmovl(cond, out, op2);
4104 }
4105}
4106
4107void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4108 bool is_min,
4109 DataType::Type type) {
4110 Location op1_loc = locations->InAt(0);
4111 Location op2_loc = locations->InAt(1);
4112 Location out_loc = locations->Out();
4113 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4114
4115 // Shortcut for same input locations.
4116 if (op1_loc.Equals(op2_loc)) {
4117 DCHECK(out_loc.Equals(op1_loc));
4118 return;
4119 }
4120
4121 // (out := op1)
4122 // out <=? op2
4123 // if Nan jmp Nan_label
4124 // if out is min jmp done
4125 // if op2 is min jmp op2_label
4126 // handle -0/+0
4127 // jmp done
4128 // Nan_label:
4129 // out := NaN
4130 // op2_label:
4131 // out := op2
4132 // done:
4133 //
4134 // This removes one jmp, but needs to copy one input (op1) to out.
4135 //
4136 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4137
4138 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4139
4140 NearLabel nan, done, op2_label;
4141 if (type == DataType::Type::kFloat64) {
4142 __ ucomisd(out, op2);
4143 } else {
4144 DCHECK_EQ(type, DataType::Type::kFloat32);
4145 __ ucomiss(out, op2);
4146 }
4147
4148 __ j(Condition::kParityEven, &nan);
4149
4150 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4151 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4152
4153 // Handle 0.0/-0.0.
4154 if (is_min) {
4155 if (type == DataType::Type::kFloat64) {
4156 __ orpd(out, op2);
4157 } else {
4158 __ orps(out, op2);
4159 }
4160 } else {
4161 if (type == DataType::Type::kFloat64) {
4162 __ andpd(out, op2);
4163 } else {
4164 __ andps(out, op2);
4165 }
4166 }
4167 __ jmp(&done);
4168
4169 // NaN handling.
4170 __ Bind(&nan);
4171 if (type == DataType::Type::kFloat64) {
4172 // TODO: Use a constant from the constant table (requires extra input).
4173 __ LoadLongConstant(out, kDoubleNaN);
4174 } else {
4175 Register constant = locations->GetTemp(0).AsRegister<Register>();
4176 __ movl(constant, Immediate(kFloatNaN));
4177 __ movd(out, constant);
4178 }
4179 __ jmp(&done);
4180
4181 // out := op2;
4182 __ Bind(&op2_label);
4183 if (type == DataType::Type::kFloat64) {
4184 __ movsd(out, op2);
4185 } else {
4186 __ movss(out, op2);
4187 }
4188
4189 // Done.
4190 __ Bind(&done);
4191}
4192
Aart Bik351df3e2018-03-07 11:54:57 -08004193void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4194 DataType::Type type = minmax->GetResultType();
4195 switch (type) {
4196 case DataType::Type::kInt32:
4197 case DataType::Type::kInt64:
4198 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4199 break;
4200 case DataType::Type::kFloat32:
4201 case DataType::Type::kFloat64:
4202 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4203 break;
4204 default:
4205 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4206 }
4207}
4208
Aart Bik1f8d51b2018-02-15 10:42:37 -08004209void LocationsBuilderX86::VisitMin(HMin* min) {
4210 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4211}
4212
4213void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004214 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004215}
4216
4217void LocationsBuilderX86::VisitMax(HMax* max) {
4218 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4219}
4220
4221void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004222 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004223}
4224
Aart Bik3dad3412018-02-28 12:01:46 -08004225void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4226 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4227 switch (abs->GetResultType()) {
4228 case DataType::Type::kInt32:
4229 locations->SetInAt(0, Location::RegisterLocation(EAX));
4230 locations->SetOut(Location::SameAsFirstInput());
4231 locations->AddTemp(Location::RegisterLocation(EDX));
4232 break;
4233 case DataType::Type::kInt64:
4234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4236 locations->AddTemp(Location::RequiresRegister());
4237 break;
4238 case DataType::Type::kFloat32:
4239 locations->SetInAt(0, Location::RequiresFpuRegister());
4240 locations->SetOut(Location::SameAsFirstInput());
4241 locations->AddTemp(Location::RequiresFpuRegister());
4242 locations->AddTemp(Location::RequiresRegister());
4243 break;
4244 case DataType::Type::kFloat64:
4245 locations->SetInAt(0, Location::RequiresFpuRegister());
4246 locations->SetOut(Location::SameAsFirstInput());
4247 locations->AddTemp(Location::RequiresFpuRegister());
4248 break;
4249 default:
4250 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4251 }
4252}
4253
4254void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4255 LocationSummary* locations = abs->GetLocations();
4256 switch (abs->GetResultType()) {
4257 case DataType::Type::kInt32: {
4258 Register out = locations->Out().AsRegister<Register>();
4259 DCHECK_EQ(out, EAX);
4260 Register temp = locations->GetTemp(0).AsRegister<Register>();
4261 DCHECK_EQ(temp, EDX);
4262 // Sign extend EAX into EDX.
4263 __ cdq();
4264 // XOR EAX with sign.
4265 __ xorl(EAX, EDX);
4266 // Subtract out sign to correct.
4267 __ subl(EAX, EDX);
4268 // The result is in EAX.
4269 break;
4270 }
4271 case DataType::Type::kInt64: {
4272 Location input = locations->InAt(0);
4273 Register input_lo = input.AsRegisterPairLow<Register>();
4274 Register input_hi = input.AsRegisterPairHigh<Register>();
4275 Location output = locations->Out();
4276 Register output_lo = output.AsRegisterPairLow<Register>();
4277 Register output_hi = output.AsRegisterPairHigh<Register>();
4278 Register temp = locations->GetTemp(0).AsRegister<Register>();
4279 // Compute the sign into the temporary.
4280 __ movl(temp, input_hi);
4281 __ sarl(temp, Immediate(31));
4282 // Store the sign into the output.
4283 __ movl(output_lo, temp);
4284 __ movl(output_hi, temp);
4285 // XOR the input to the output.
4286 __ xorl(output_lo, input_lo);
4287 __ xorl(output_hi, input_hi);
4288 // Subtract the sign.
4289 __ subl(output_lo, temp);
4290 __ sbbl(output_hi, temp);
4291 break;
4292 }
4293 case DataType::Type::kFloat32: {
4294 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4295 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4296 Register constant = locations->GetTemp(1).AsRegister<Register>();
4297 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4298 __ movd(temp, constant);
4299 __ andps(out, temp);
4300 break;
4301 }
4302 case DataType::Type::kFloat64: {
4303 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4304 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4305 // TODO: Use a constant from the constant table (requires extra input).
4306 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4307 __ andpd(out, temp);
4308 break;
4309 }
4310 default:
4311 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4312 }
4313}
4314
Calin Juravled0d48522014-11-04 16:40:20 +00004315void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004316 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004317 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004319 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 case DataType::Type::kInt8:
4321 case DataType::Type::kUint16:
4322 case DataType::Type::kInt16:
4323 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004324 locations->SetInAt(0, Location::Any());
4325 break;
4326 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004328 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4329 if (!instruction->IsConstant()) {
4330 locations->AddTemp(Location::RequiresRegister());
4331 }
4332 break;
4333 }
4334 default:
4335 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4336 }
Calin Juravled0d48522014-11-04 16:40:20 +00004337}
4338
4339void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004340 SlowPathCode* slow_path =
4341 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004342 codegen_->AddSlowPath(slow_path);
4343
4344 LocationSummary* locations = instruction->GetLocations();
4345 Location value = locations->InAt(0);
4346
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004347 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004348 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004349 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004350 case DataType::Type::kInt8:
4351 case DataType::Type::kUint16:
4352 case DataType::Type::kInt16:
4353 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004354 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004356 __ j(kEqual, slow_path->GetEntryLabel());
4357 } else if (value.IsStackSlot()) {
4358 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4359 __ j(kEqual, slow_path->GetEntryLabel());
4360 } else {
4361 DCHECK(value.IsConstant()) << value;
4362 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004363 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004364 }
4365 }
4366 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004367 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004368 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004369 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004370 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004371 __ movl(temp, value.AsRegisterPairLow<Register>());
4372 __ orl(temp, value.AsRegisterPairHigh<Register>());
4373 __ j(kEqual, slow_path->GetEntryLabel());
4374 } else {
4375 DCHECK(value.IsConstant()) << value;
4376 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4377 __ jmp(slow_path->GetEntryLabel());
4378 }
4379 }
4380 break;
4381 }
4382 default:
4383 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004384 }
Calin Juravled0d48522014-11-04 16:40:20 +00004385}
4386
Calin Juravle9aec02f2014-11-18 23:06:35 +00004387void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4388 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4389
4390 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004391 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004392
4393 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 case DataType::Type::kInt32:
4395 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004396 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004397 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004398 // The shift count needs to be in CL or a constant.
4399 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004400 locations->SetOut(Location::SameAsFirstInput());
4401 break;
4402 }
4403 default:
4404 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4405 }
4406}
4407
4408void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4409 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4410
4411 LocationSummary* locations = op->GetLocations();
4412 Location first = locations->InAt(0);
4413 Location second = locations->InAt(1);
4414 DCHECK(first.Equals(locations->Out()));
4415
4416 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004418 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004419 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004420 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004422 DCHECK_EQ(ECX, second_reg);
4423 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004424 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004425 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004426 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004427 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004428 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004429 }
4430 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004431 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004432 if (shift == 0) {
4433 return;
4434 }
4435 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004436 if (op->IsShl()) {
4437 __ shll(first_reg, imm);
4438 } else if (op->IsShr()) {
4439 __ sarl(first_reg, imm);
4440 } else {
4441 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004442 }
4443 }
4444 break;
4445 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004446 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004447 if (second.IsRegister()) {
4448 Register second_reg = second.AsRegister<Register>();
4449 DCHECK_EQ(ECX, second_reg);
4450 if (op->IsShl()) {
4451 GenerateShlLong(first, second_reg);
4452 } else if (op->IsShr()) {
4453 GenerateShrLong(first, second_reg);
4454 } else {
4455 GenerateUShrLong(first, second_reg);
4456 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004457 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004458 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004459 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004460 // Nothing to do if the shift is 0, as the input is already the output.
4461 if (shift != 0) {
4462 if (op->IsShl()) {
4463 GenerateShlLong(first, shift);
4464 } else if (op->IsShr()) {
4465 GenerateShrLong(first, shift);
4466 } else {
4467 GenerateUShrLong(first, shift);
4468 }
4469 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004470 }
4471 break;
4472 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004473 default:
4474 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4475 }
4476}
4477
Mark P Mendell73945692015-04-29 14:56:17 +00004478void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4479 Register low = loc.AsRegisterPairLow<Register>();
4480 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004481 if (shift == 1) {
4482 // This is just an addition.
4483 __ addl(low, low);
4484 __ adcl(high, high);
4485 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004486 // Shift by 32 is easy. High gets low, and low gets 0.
4487 codegen_->EmitParallelMoves(
4488 loc.ToLow(),
4489 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004490 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004491 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4492 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004493 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004494 } else if (shift > 32) {
4495 // Low part becomes 0. High part is low part << (shift-32).
4496 __ movl(high, low);
4497 __ shll(high, Immediate(shift - 32));
4498 __ xorl(low, low);
4499 } else {
4500 // Between 1 and 31.
4501 __ shld(high, low, Immediate(shift));
4502 __ shll(low, Immediate(shift));
4503 }
4504}
4505
Calin Juravle9aec02f2014-11-18 23:06:35 +00004506void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004507 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004508 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4509 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4510 __ testl(shifter, Immediate(32));
4511 __ j(kEqual, &done);
4512 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4513 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4514 __ Bind(&done);
4515}
4516
Mark P Mendell73945692015-04-29 14:56:17 +00004517void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4518 Register low = loc.AsRegisterPairLow<Register>();
4519 Register high = loc.AsRegisterPairHigh<Register>();
4520 if (shift == 32) {
4521 // Need to copy the sign.
4522 DCHECK_NE(low, high);
4523 __ movl(low, high);
4524 __ sarl(high, Immediate(31));
4525 } else if (shift > 32) {
4526 DCHECK_NE(low, high);
4527 // High part becomes sign. Low part is shifted by shift - 32.
4528 __ movl(low, high);
4529 __ sarl(high, Immediate(31));
4530 __ sarl(low, Immediate(shift - 32));
4531 } else {
4532 // Between 1 and 31.
4533 __ shrd(low, high, Immediate(shift));
4534 __ sarl(high, Immediate(shift));
4535 }
4536}
4537
Calin Juravle9aec02f2014-11-18 23:06:35 +00004538void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004539 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4541 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4542 __ testl(shifter, Immediate(32));
4543 __ j(kEqual, &done);
4544 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4545 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4546 __ Bind(&done);
4547}
4548
Mark P Mendell73945692015-04-29 14:56:17 +00004549void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4550 Register low = loc.AsRegisterPairLow<Register>();
4551 Register high = loc.AsRegisterPairHigh<Register>();
4552 if (shift == 32) {
4553 // Shift by 32 is easy. Low gets high, and high gets 0.
4554 codegen_->EmitParallelMoves(
4555 loc.ToHigh(),
4556 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004557 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004558 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4559 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004560 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004561 } else if (shift > 32) {
4562 // Low part is high >> (shift - 32). High part becomes 0.
4563 __ movl(low, high);
4564 __ shrl(low, Immediate(shift - 32));
4565 __ xorl(high, high);
4566 } else {
4567 // Between 1 and 31.
4568 __ shrd(low, high, Immediate(shift));
4569 __ shrl(high, Immediate(shift));
4570 }
4571}
4572
Calin Juravle9aec02f2014-11-18 23:06:35 +00004573void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004574 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4576 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4577 __ testl(shifter, Immediate(32));
4578 __ j(kEqual, &done);
4579 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4580 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4581 __ Bind(&done);
4582}
4583
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004584void LocationsBuilderX86::VisitRor(HRor* ror) {
4585 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004586 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004587
4588 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004589 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004590 // Add the temporary needed.
4591 locations->AddTemp(Location::RequiresRegister());
4592 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004593 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004594 locations->SetInAt(0, Location::RequiresRegister());
4595 // The shift count needs to be in CL (unless it is a constant).
4596 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4597 locations->SetOut(Location::SameAsFirstInput());
4598 break;
4599 default:
4600 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4601 UNREACHABLE();
4602 }
4603}
4604
4605void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4606 LocationSummary* locations = ror->GetLocations();
4607 Location first = locations->InAt(0);
4608 Location second = locations->InAt(1);
4609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004611 Register first_reg = first.AsRegister<Register>();
4612 if (second.IsRegister()) {
4613 Register second_reg = second.AsRegister<Register>();
4614 __ rorl(first_reg, second_reg);
4615 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004617 __ rorl(first_reg, imm);
4618 }
4619 return;
4620 }
4621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004622 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004623 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4624 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4625 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4626 if (second.IsRegister()) {
4627 Register second_reg = second.AsRegister<Register>();
4628 DCHECK_EQ(second_reg, ECX);
4629 __ movl(temp_reg, first_reg_hi);
4630 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4631 __ shrd(first_reg_lo, temp_reg, second_reg);
4632 __ movl(temp_reg, first_reg_hi);
4633 __ testl(second_reg, Immediate(32));
4634 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4635 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4636 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004638 if (shift_amt == 0) {
4639 // Already fine.
4640 return;
4641 }
4642 if (shift_amt == 32) {
4643 // Just swap.
4644 __ movl(temp_reg, first_reg_lo);
4645 __ movl(first_reg_lo, first_reg_hi);
4646 __ movl(first_reg_hi, temp_reg);
4647 return;
4648 }
4649
4650 Immediate imm(shift_amt);
4651 // Save the constents of the low value.
4652 __ movl(temp_reg, first_reg_lo);
4653
4654 // Shift right into low, feeding bits from high.
4655 __ shrd(first_reg_lo, first_reg_hi, imm);
4656
4657 // Shift right into high, feeding bits from the original low.
4658 __ shrd(first_reg_hi, temp_reg, imm);
4659
4660 // Swap if needed.
4661 if (shift_amt > 32) {
4662 __ movl(temp_reg, first_reg_lo);
4663 __ movl(first_reg_lo, first_reg_hi);
4664 __ movl(first_reg_hi, temp_reg);
4665 }
4666 }
4667}
4668
Calin Juravle9aec02f2014-11-18 23:06:35 +00004669void LocationsBuilderX86::VisitShl(HShl* shl) {
4670 HandleShift(shl);
4671}
4672
4673void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4674 HandleShift(shl);
4675}
4676
4677void LocationsBuilderX86::VisitShr(HShr* shr) {
4678 HandleShift(shr);
4679}
4680
4681void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4682 HandleShift(shr);
4683}
4684
4685void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4686 HandleShift(ushr);
4687}
4688
4689void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4690 HandleShift(ushr);
4691}
4692
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004693void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004694 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4695 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004696 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004697 InvokeRuntimeCallingConvention calling_convention;
4698 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004699}
4700
4701void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004702 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4703 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4704 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004705}
4706
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004707void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004708 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4709 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004710 locations->SetOut(Location::RegisterLocation(EAX));
4711 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004712 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4713 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004714}
4715
4716void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004717 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4718 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004719 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004720 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004721 DCHECK(!codegen_->IsLeafMethod());
4722}
4723
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004724void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004725 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004726 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004727 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4728 if (location.IsStackSlot()) {
4729 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4730 } else if (location.IsDoubleStackSlot()) {
4731 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004732 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004733 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004734}
4735
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004736void InstructionCodeGeneratorX86::VisitParameterValue(
4737 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4738}
4739
4740void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004742 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004743 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4744}
4745
4746void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004747}
4748
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004749void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4750 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004751 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004752 locations->SetInAt(0, Location::RequiresRegister());
4753 locations->SetOut(Location::RequiresRegister());
4754}
4755
4756void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4757 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004758 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004759 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004760 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004761 __ movl(locations->Out().AsRegister<Register>(),
4762 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004763 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004764 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004765 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004766 __ movl(locations->Out().AsRegister<Register>(),
4767 Address(locations->InAt(0).AsRegister<Register>(),
4768 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4769 // temp = temp->GetImtEntryAt(method_offset);
4770 __ movl(locations->Out().AsRegister<Register>(),
4771 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004772 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004773}
4774
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004775void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004776 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004777 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004778 locations->SetInAt(0, Location::RequiresRegister());
4779 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004780}
4781
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004782void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4783 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004784 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004785 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004786 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004787 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004788 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004789 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004790 break;
4791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004793 __ notl(out.AsRegisterPairLow<Register>());
4794 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004795 break;
4796
4797 default:
4798 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4799 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004800}
4801
David Brazdil66d126e2015-04-03 16:02:44 +01004802void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4803 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004804 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004805 locations->SetInAt(0, Location::RequiresRegister());
4806 locations->SetOut(Location::SameAsFirstInput());
4807}
4808
4809void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004810 LocationSummary* locations = bool_not->GetLocations();
4811 Location in = locations->InAt(0);
4812 Location out = locations->Out();
4813 DCHECK(in.Equals(out));
4814 __ xorl(out.AsRegister<Register>(), Immediate(1));
4815}
4816
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004817void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004818 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004819 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004820 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004821 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004822 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004824 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004825 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 case DataType::Type::kInt32:
4827 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004828 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004829 locations->SetInAt(1, Location::Any());
4830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4831 break;
4832 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004833 case DataType::Type::kFloat32:
4834 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004835 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004836 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4837 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4838 } else if (compare->InputAt(1)->IsConstant()) {
4839 locations->SetInAt(1, Location::RequiresFpuRegister());
4840 } else {
4841 locations->SetInAt(1, Location::Any());
4842 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004843 locations->SetOut(Location::RequiresRegister());
4844 break;
4845 }
4846 default:
4847 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4848 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004849}
4850
4851void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004852 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004853 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004854 Location left = locations->InAt(0);
4855 Location right = locations->InAt(1);
4856
Mark Mendell0c9497d2015-08-21 09:30:05 -04004857 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004858 Condition less_cond = kLess;
4859
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004860 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004861 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004862 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004865 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004867 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004868 break;
4869 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004871 Register left_low = left.AsRegisterPairLow<Register>();
4872 Register left_high = left.AsRegisterPairHigh<Register>();
4873 int32_t val_low = 0;
4874 int32_t val_high = 0;
4875 bool right_is_const = false;
4876
4877 if (right.IsConstant()) {
4878 DCHECK(right.GetConstant()->IsLongConstant());
4879 right_is_const = true;
4880 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4881 val_low = Low32Bits(val);
4882 val_high = High32Bits(val);
4883 }
4884
Calin Juravleddb7df22014-11-25 20:56:51 +00004885 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004886 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004887 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004888 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004889 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004890 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004891 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004892 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004893 __ j(kLess, &less); // Signed compare.
4894 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004895 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004896 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004897 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004898 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004899 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004900 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004901 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004902 }
Aart Bika19616e2016-02-01 18:57:58 -08004903 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004904 break;
4905 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004906 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004907 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004908 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004909 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004910 break;
4911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004912 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004913 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004914 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004915 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004916 break;
4917 }
4918 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004919 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004920 }
Aart Bika19616e2016-02-01 18:57:58 -08004921
Calin Juravleddb7df22014-11-25 20:56:51 +00004922 __ movl(out, Immediate(0));
4923 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004924 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004925
4926 __ Bind(&greater);
4927 __ movl(out, Immediate(1));
4928 __ jmp(&done);
4929
4930 __ Bind(&less);
4931 __ movl(out, Immediate(-1));
4932
4933 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004934}
4935
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004936void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004937 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004938 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004939 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004940 locations->SetInAt(i, Location::Any());
4941 }
4942 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004943}
4944
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004945void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004946 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004947}
4948
Roland Levillain7c1559a2015-12-15 10:55:36 +00004949void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004950 /*
4951 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4952 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4953 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4954 */
4955 switch (kind) {
4956 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004957 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004958 break;
4959 }
4960 case MemBarrierKind::kAnyStore:
4961 case MemBarrierKind::kLoadAny:
4962 case MemBarrierKind::kStoreStore: {
4963 // nop
4964 break;
4965 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004966 case MemBarrierKind::kNTStoreStore:
4967 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004968 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004969 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004970 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004971}
4972
Vladimir Markodc151b22015-10-15 18:02:30 +01004973HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4974 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004975 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004976 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004977}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004979Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4980 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004981 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004982 if (!invoke->GetLocations()->Intrinsified()) {
4983 return location.AsRegister<Register>();
4984 }
4985 // For intrinsics we allow any location, so it may be on the stack.
4986 if (!location.IsRegister()) {
4987 __ movl(temp, Address(ESP, location.GetStackIndex()));
4988 return temp;
4989 }
4990 // For register locations, check if the register was saved. If so, get it from the stack.
4991 // Note: There is a chance that the register was saved but not overwritten, so we could
4992 // save one load. However, since this is just an intrinsic slow path we prefer this
4993 // simple and more robust approach rather that trying to determine if that's the case.
4994 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004995 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4996 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4997 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4998 __ movl(temp, Address(ESP, stack_offset));
4999 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005000 }
5001 return location.AsRegister<Register>();
5002}
5003
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005004void CodeGeneratorX86::GenerateStaticOrDirectCall(
5005 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005006 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5007 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005008 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005009 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005010 uint32_t offset =
5011 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5012 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005013 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005014 }
Vladimir Marko58155012015-08-19 12:49:41 +00005015 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005016 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005017 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005018 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005019 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005020 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5021 temp.AsRegister<Register>());
5022 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005023 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005024 break;
5025 }
Vladimir Markob066d432018-01-03 13:14:37 +00005026 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5027 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5028 temp.AsRegister<Register>());
5029 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
5030 RecordBootImageRelRoPatch(
5031 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005032 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005033 break;
5034 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005035 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005036 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5037 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005038 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005039 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005040 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005041 break;
5042 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005043 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5044 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5045 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005046 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5047 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5048 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005049 }
Vladimir Marko58155012015-08-19 12:49:41 +00005050 }
5051
5052 switch (invoke->GetCodePtrLocation()) {
5053 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5054 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005055 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005056 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005057 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005058 size_t out_frame_size =
5059 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5060 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005061 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01005062 // (callee_method + offset_of_jni_entry_point)()
5063 __ call(Address(callee_method.AsRegister<Register>(),
5064 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5065 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5066 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5067 // Create space for conversion.
5068 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005069 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005070 }
5071 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5072 switch (invoke->GetType()) {
5073 case DataType::Type::kBool:
5074 __ movzxb(EAX, AL);
5075 break;
5076 case DataType::Type::kInt8:
5077 __ movsxb(EAX, AL);
5078 break;
5079 case DataType::Type::kUint16:
5080 __ movzxw(EAX, EAX);
5081 break;
5082 case DataType::Type::kInt16:
5083 __ movsxw(EAX, EAX);
5084 break;
5085 case DataType::Type::kFloat32:
5086 __ fstps(Address(ESP, 0));
5087 __ movss(XMM0, Address(ESP, 0));
5088 break;
5089 case DataType::Type::kFloat64:
5090 __ fstpl(Address(ESP, 0));
5091 __ movsd(XMM0, Address(ESP, 0));
5092 break;
5093 case DataType::Type::kInt32:
5094 case DataType::Type::kInt64:
5095 case DataType::Type::kVoid:
5096 break;
5097 default:
5098 DCHECK(false) << invoke->GetType();
5099 break;
5100 }
5101 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005102 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005103 }
5104 break;
5105 }
Vladimir Marko58155012015-08-19 12:49:41 +00005106 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5107 // (callee_method + offset_of_quick_compiled_code)()
5108 __ call(Address(callee_method.AsRegister<Register>(),
5109 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005110 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005111 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005112 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005113 }
5114
5115 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005116}
5117
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005118void CodeGeneratorX86::GenerateVirtualCall(
5119 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005120 Register temp = temp_in.AsRegister<Register>();
5121 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5122 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005123
5124 // Use the calling convention instead of the location of the receiver, as
5125 // intrinsics may have put the receiver in a different register. In the intrinsics
5126 // slow path, the arguments have been moved to the right place, so here we are
5127 // guaranteed that the receiver is the first register of the calling convention.
5128 InvokeDexCallingConvention calling_convention;
5129 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005130 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005131 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005132 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005133 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005134 // Instead of simply (possibly) unpoisoning `temp` here, we should
5135 // emit a read barrier for the previous class reference load.
5136 // However this is not required in practice, as this is an
5137 // intermediate/temporary reference and because the current
5138 // concurrent copying collector keeps the from-space memory
5139 // intact/accessible until the end of the marking phase (the
5140 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005141 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005142
5143 MaybeGenerateInlineCacheCheck(invoke, temp);
5144
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005145 // temp = temp->GetMethodAt(method_offset);
5146 __ movl(temp, Address(temp, method_offset));
5147 // call temp->GetEntryPoint();
5148 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005149 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005150 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005151}
5152
Vladimir Marko6fd16062018-06-26 11:02:04 +01005153void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5154 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005155 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005156 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005157 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005158}
5159
Vladimir Markob066d432018-01-03 13:14:37 +00005160void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5161 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005162 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005163 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005164 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005165}
5166
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005167void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005168 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005169 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005170 boot_image_method_patches_.emplace_back(
5171 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005172 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005173}
5174
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005175void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005176 HX86ComputeBaseMethodAddress* method_address =
5177 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005178 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005179 method_bss_entry_patches_.emplace_back(
5180 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5181 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005182}
5183
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005184void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5185 HX86ComputeBaseMethodAddress* method_address =
5186 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5187 boot_image_type_patches_.emplace_back(
5188 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005189 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005190}
5191
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005192Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005193 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005194 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5195 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005196 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005197 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005198}
5199
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005200void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5201 HX86ComputeBaseMethodAddress* method_address =
5202 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5203 boot_image_string_patches_.emplace_back(
5204 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5205 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005206}
5207
Vladimir Markoaad75c62016-10-03 08:46:48 +00005208Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005209 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005210 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005211 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005212 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005213 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005214}
5215
Vladimir Markoeebb8212018-06-05 14:57:24 +01005216void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005217 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005218 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005219 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005220 HX86ComputeBaseMethodAddress* method_address =
5221 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5222 DCHECK(method_address != nullptr);
5223 Register method_address_reg =
5224 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5225 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5226 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005227 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005228 HX86ComputeBaseMethodAddress* method_address =
5229 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5230 DCHECK(method_address != nullptr);
5231 Register method_address_reg =
5232 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5233 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005234 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005235 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005236 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005237 gc::Heap* heap = Runtime::Current()->GetHeap();
5238 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005239 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005240 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5241 }
5242}
5243
Vladimir Marko6fd16062018-06-26 11:02:04 +01005244void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5245 uint32_t boot_image_offset) {
5246 DCHECK(invoke->IsStatic());
5247 InvokeRuntimeCallingConvention calling_convention;
5248 Register argument = calling_convention.GetRegisterAt(0);
5249 if (GetCompilerOptions().IsBootImage()) {
5250 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5251 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005252 HX86ComputeBaseMethodAddress* method_address =
5253 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5254 DCHECK(method_address != nullptr);
5255 Register method_address_reg =
5256 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5257 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5258 MethodReference target_method = invoke->GetTargetMethod();
5259 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5260 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5261 __ Bind(&boot_image_type_patches_.back().label);
5262 } else {
5263 LoadBootImageAddress(argument, boot_image_offset, invoke);
5264 }
5265 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5266 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5267}
5268
Vladimir Markoaad75c62016-10-03 08:46:48 +00005269// The label points to the end of the "movl" or another instruction but the literal offset
5270// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5271constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5272
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005273template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005274inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005275 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005276 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005277 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005278 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005279 linker_patches->push_back(Factory(literal_offset,
5280 info.target_dex_file,
5281 GetMethodAddressOffset(info.method_address),
5282 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005283 }
5284}
5285
Vladimir Marko6fd16062018-06-26 11:02:04 +01005286template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5287linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5288 const DexFile* target_dex_file,
5289 uint32_t pc_insn_offset,
5290 uint32_t boot_image_offset) {
5291 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5292 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005293}
5294
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005295void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005296 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005297 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005298 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005299 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005300 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005301 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005302 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005303 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005304 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005305 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005306 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005307 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5308 boot_image_method_patches_, linker_patches);
5309 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5310 boot_image_type_patches_, linker_patches);
5311 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005312 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005313 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005314 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005315 DCHECK(boot_image_type_patches_.empty());
5316 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005317 }
5318 if (GetCompilerOptions().IsBootImage()) {
5319 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5320 boot_image_other_patches_, linker_patches);
5321 } else {
5322 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5323 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005324 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005325 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5326 method_bss_entry_patches_, linker_patches);
5327 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5328 type_bss_entry_patches_, linker_patches);
5329 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5330 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005331 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005332}
5333
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005334void CodeGeneratorX86::MarkGCCard(Register temp,
5335 Register card,
5336 Register object,
5337 Register value,
5338 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005339 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005340 if (value_can_be_null) {
5341 __ testl(value, value);
5342 __ j(kEqual, &is_null);
5343 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005344 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005345 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005346 // Calculate the offset (in the card table) of the card corresponding to
5347 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 __ movl(temp, object);
5349 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005350 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5351 // `object`'s card.
5352 //
5353 // Register `card` contains the address of the card table. Note that the card
5354 // table's base is biased during its creation so that it always starts at an
5355 // address whose least-significant byte is equal to `kCardDirty` (see
5356 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5357 // below writes the `kCardDirty` (byte) value into the `object`'s card
5358 // (located at `card + object >> kCardShift`).
5359 //
5360 // This dual use of the value in register `card` (1. to calculate the location
5361 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5362 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005363 __ movb(Address(temp, card, TIMES_1, 0),
5364 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005365 if (value_can_be_null) {
5366 __ Bind(&is_null);
5367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368}
5369
Calin Juravle52c48962014-12-16 17:02:57 +00005370void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5371 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005372
5373 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005374 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005375 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005376 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5377 kEmitCompilerReadBarrier
5378 ? LocationSummary::kCallOnSlowPath
5379 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005380 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005381 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005382 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005383 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005384
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005385 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005386 locations->SetOut(Location::RequiresFpuRegister());
5387 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005388 // The output overlaps in case of long: we don't want the low move
5389 // to overwrite the object's location. Likewise, in the case of
5390 // an object field get with read barriers enabled, we do not want
5391 // the move to overwrite the object's location, as we need it to emit
5392 // the read barrier.
5393 locations->SetOut(
5394 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005395 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005396 Location::kOutputOverlap :
5397 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005398 }
Calin Juravle52c48962014-12-16 17:02:57 +00005399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005400 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005401 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005402 // So we use an XMM register as a temp to achieve atomicity (first
5403 // load the temp into the XMM and then copy the XMM into the
5404 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005405 locations->AddTemp(Location::RequiresFpuRegister());
5406 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005407}
5408
Calin Juravle52c48962014-12-16 17:02:57 +00005409void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5410 const FieldInfo& field_info) {
5411 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005412
Calin Juravle52c48962014-12-16 17:02:57 +00005413 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005414 Location base_loc = locations->InAt(0);
5415 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005416 Location out = locations->Out();
5417 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005418 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5419 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005420 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5421
Vladimir Marko61b92282017-10-11 13:23:17 +01005422 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005423 case DataType::Type::kBool:
5424 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005425 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005426 break;
5427 }
5428
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005430 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005431 break;
5432 }
5433
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005434 case DataType::Type::kUint16: {
5435 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005436 break;
5437 }
5438
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005439 case DataType::Type::kInt16: {
5440 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005441 break;
5442 }
5443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005444 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005445 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005446 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005447
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005448 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005449 // /* HeapReference<Object> */ out = *(base + offset)
5450 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005451 // Note that a potential implicit null check is handled in this
5452 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5453 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005454 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005455 if (is_volatile) {
5456 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5457 }
5458 } else {
5459 __ movl(out.AsRegister<Register>(), Address(base, offset));
5460 codegen_->MaybeRecordImplicitNullCheck(instruction);
5461 if (is_volatile) {
5462 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5463 }
5464 // If read barriers are enabled, emit read barriers other than
5465 // Baker's using a slow path (and also unpoison the loaded
5466 // reference, if heap poisoning is enabled).
5467 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5468 }
5469 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005470 }
5471
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005472 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005473 if (is_volatile) {
5474 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5475 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005476 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005477 __ movd(out.AsRegisterPairLow<Register>(), temp);
5478 __ psrlq(temp, Immediate(32));
5479 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5480 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005481 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005482 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005484 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5485 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005486 break;
5487 }
5488
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005489 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005490 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005491 break;
5492 }
5493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005494 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005495 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005496 break;
5497 }
5498
Aart Bik66c158e2018-01-31 12:55:04 -08005499 case DataType::Type::kUint32:
5500 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005502 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005503 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005504 }
Calin Juravle52c48962014-12-16 17:02:57 +00005505
Vladimir Marko61b92282017-10-11 13:23:17 +01005506 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005507 // Potential implicit null checks, in the case of reference or
5508 // long fields, are handled in the previous switch statement.
5509 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005510 codegen_->MaybeRecordImplicitNullCheck(instruction);
5511 }
5512
Calin Juravle52c48962014-12-16 17:02:57 +00005513 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005514 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005515 // Memory barriers, in the case of references, are also handled
5516 // in the previous switch statement.
5517 } else {
5518 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5519 }
Roland Levillain4d027112015-07-01 15:41:14 +01005520 }
Calin Juravle52c48962014-12-16 17:02:57 +00005521}
5522
5523void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5524 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5525
5526 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005527 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005528 locations->SetInAt(0, Location::RequiresRegister());
5529 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005531 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005532
5533 // The register allocator does not support multiple
5534 // inputs that die at entry with one in a specific register.
5535 if (is_byte_type) {
5536 // Ensure the value is in a byte register.
5537 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 } else if (DataType::IsFloatingPointType(field_type)) {
5539 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005540 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5541 locations->SetInAt(1, Location::RequiresFpuRegister());
5542 } else {
5543 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5544 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005546 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005547 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005548
Calin Juravle52c48962014-12-16 17:02:57 +00005549 // 64bits value can be atomically written to an address with movsd and an XMM register.
5550 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5551 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5552 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5553 // isolated cases when we need this it isn't worth adding the extra complexity.
5554 locations->AddTemp(Location::RequiresFpuRegister());
5555 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005556 } else {
5557 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5558
5559 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5560 // Temporary registers for the write barrier.
5561 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5562 // Ensure the card is in a byte register.
5563 locations->AddTemp(Location::RegisterLocation(ECX));
5564 }
Calin Juravle52c48962014-12-16 17:02:57 +00005565 }
5566}
5567
5568void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005569 const FieldInfo& field_info,
5570 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005571 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5572
5573 LocationSummary* locations = instruction->GetLocations();
5574 Register base = locations->InAt(0).AsRegister<Register>();
5575 Location value = locations->InAt(1);
5576 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005577 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005578 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005579 bool needs_write_barrier =
5580 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005581
5582 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005583 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005584 }
5585
Mark Mendell81489372015-11-04 11:30:41 -05005586 bool maybe_record_implicit_null_check_done = false;
5587
Calin Juravle52c48962014-12-16 17:02:57 +00005588 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005590 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005591 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005592 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5593 break;
5594 }
5595
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005596 case DataType::Type::kUint16:
5597 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005598 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005599 __ movw(Address(base, offset),
5600 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005601 } else {
5602 __ movw(Address(base, offset), value.AsRegister<Register>());
5603 }
Calin Juravle52c48962014-12-16 17:02:57 +00005604 break;
5605 }
5606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005607 case DataType::Type::kInt32:
5608 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005609 if (kPoisonHeapReferences && needs_write_barrier) {
5610 // Note that in the case where `value` is a null reference,
5611 // we do not enter this block, as the reference does not
5612 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005613 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005614 Register temp = locations->GetTemp(0).AsRegister<Register>();
5615 __ movl(temp, value.AsRegister<Register>());
5616 __ PoisonHeapReference(temp);
5617 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005618 } else if (value.IsConstant()) {
5619 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5620 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005621 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005622 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005623 __ movl(Address(base, offset), value.AsRegister<Register>());
5624 }
Calin Juravle52c48962014-12-16 17:02:57 +00005625 break;
5626 }
5627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005628 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005629 if (is_volatile) {
5630 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5631 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5632 __ movd(temp1, value.AsRegisterPairLow<Register>());
5633 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5634 __ punpckldq(temp1, temp2);
5635 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005637 } else if (value.IsConstant()) {
5638 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5639 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5640 codegen_->MaybeRecordImplicitNullCheck(instruction);
5641 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005642 } else {
5643 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005645 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5646 }
Mark Mendell81489372015-11-04 11:30:41 -05005647 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005648 break;
5649 }
5650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005652 if (value.IsConstant()) {
5653 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5654 __ movl(Address(base, offset), Immediate(v));
5655 } else {
5656 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5657 }
Calin Juravle52c48962014-12-16 17:02:57 +00005658 break;
5659 }
5660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005662 if (value.IsConstant()) {
5663 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5664 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5665 codegen_->MaybeRecordImplicitNullCheck(instruction);
5666 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5667 maybe_record_implicit_null_check_done = true;
5668 } else {
5669 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5670 }
Calin Juravle52c48962014-12-16 17:02:57 +00005671 break;
5672 }
5673
Aart Bik66c158e2018-01-31 12:55:04 -08005674 case DataType::Type::kUint32:
5675 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005676 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005677 LOG(FATAL) << "Unreachable type " << field_type;
5678 UNREACHABLE();
5679 }
5680
Mark Mendell81489372015-11-04 11:30:41 -05005681 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005682 codegen_->MaybeRecordImplicitNullCheck(instruction);
5683 }
5684
Roland Levillain4d027112015-07-01 15:41:14 +01005685 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005686 Register temp = locations->GetTemp(0).AsRegister<Register>();
5687 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005688 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005689 }
5690
Calin Juravle52c48962014-12-16 17:02:57 +00005691 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005692 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005693 }
5694}
5695
5696void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5697 HandleFieldGet(instruction, instruction->GetFieldInfo());
5698}
5699
5700void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5701 HandleFieldGet(instruction, instruction->GetFieldInfo());
5702}
5703
5704void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5705 HandleFieldSet(instruction, instruction->GetFieldInfo());
5706}
5707
5708void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005709 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005710}
5711
5712void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5713 HandleFieldSet(instruction, instruction->GetFieldInfo());
5714}
5715
5716void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005717 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005718}
5719
5720void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5721 HandleFieldGet(instruction, instruction->GetFieldInfo());
5722}
5723
5724void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5725 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005726}
5727
Vladimir Marko552a1342017-10-31 10:56:47 +00005728void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5729 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5730}
5731
5732void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5733 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5734 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5735}
5736
Calin Juravlee460d1d2015-09-29 04:52:17 +01005737void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5738 HUnresolvedInstanceFieldGet* instruction) {
5739 FieldAccessCallingConventionX86 calling_convention;
5740 codegen_->CreateUnresolvedFieldLocationSummary(
5741 instruction, instruction->GetFieldType(), calling_convention);
5742}
5743
5744void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5745 HUnresolvedInstanceFieldGet* instruction) {
5746 FieldAccessCallingConventionX86 calling_convention;
5747 codegen_->GenerateUnresolvedFieldAccess(instruction,
5748 instruction->GetFieldType(),
5749 instruction->GetFieldIndex(),
5750 instruction->GetDexPc(),
5751 calling_convention);
5752}
5753
5754void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5755 HUnresolvedInstanceFieldSet* instruction) {
5756 FieldAccessCallingConventionX86 calling_convention;
5757 codegen_->CreateUnresolvedFieldLocationSummary(
5758 instruction, instruction->GetFieldType(), calling_convention);
5759}
5760
5761void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5762 HUnresolvedInstanceFieldSet* instruction) {
5763 FieldAccessCallingConventionX86 calling_convention;
5764 codegen_->GenerateUnresolvedFieldAccess(instruction,
5765 instruction->GetFieldType(),
5766 instruction->GetFieldIndex(),
5767 instruction->GetDexPc(),
5768 calling_convention);
5769}
5770
5771void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5772 HUnresolvedStaticFieldGet* instruction) {
5773 FieldAccessCallingConventionX86 calling_convention;
5774 codegen_->CreateUnresolvedFieldLocationSummary(
5775 instruction, instruction->GetFieldType(), calling_convention);
5776}
5777
5778void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5779 HUnresolvedStaticFieldGet* instruction) {
5780 FieldAccessCallingConventionX86 calling_convention;
5781 codegen_->GenerateUnresolvedFieldAccess(instruction,
5782 instruction->GetFieldType(),
5783 instruction->GetFieldIndex(),
5784 instruction->GetDexPc(),
5785 calling_convention);
5786}
5787
5788void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5789 HUnresolvedStaticFieldSet* instruction) {
5790 FieldAccessCallingConventionX86 calling_convention;
5791 codegen_->CreateUnresolvedFieldLocationSummary(
5792 instruction, instruction->GetFieldType(), calling_convention);
5793}
5794
5795void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5796 HUnresolvedStaticFieldSet* instruction) {
5797 FieldAccessCallingConventionX86 calling_convention;
5798 codegen_->GenerateUnresolvedFieldAccess(instruction,
5799 instruction->GetFieldType(),
5800 instruction->GetFieldIndex(),
5801 instruction->GetDexPc(),
5802 calling_convention);
5803}
5804
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005805void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005806 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5807 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5808 ? Location::RequiresRegister()
5809 : Location::Any();
5810 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005811}
5812
Calin Juravle2ae48182016-03-16 14:05:09 +00005813void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5814 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005815 return;
5816 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005817 LocationSummary* locations = instruction->GetLocations();
5818 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005819
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005820 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005821 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005822}
5823
Calin Juravle2ae48182016-03-16 14:05:09 +00005824void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005825 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005826 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827
5828 LocationSummary* locations = instruction->GetLocations();
5829 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005830
5831 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005832 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005833 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005835 } else {
5836 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005837 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005838 __ jmp(slow_path->GetEntryLabel());
5839 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005840 }
5841 __ j(kEqual, slow_path->GetEntryLabel());
5842}
5843
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005844void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005845 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005846}
5847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005848void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005849 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005851 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005852 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5853 object_array_get_with_read_barrier
5854 ? LocationSummary::kCallOnSlowPath
5855 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005856 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005857 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005858 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005859 locations->SetInAt(0, Location::RequiresRegister());
5860 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005861 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005862 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5863 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864 // The output overlaps in case of long: we don't want the low move
5865 // to overwrite the array's location. Likewise, in the case of an
5866 // object array get with read barriers enabled, we do not want the
5867 // move to overwrite the array's location, as we need it to emit
5868 // the read barrier.
5869 locations->SetOut(
5870 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005871 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5872 ? Location::kOutputOverlap
5873 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005874 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005875}
5876
5877void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5878 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005879 Location obj_loc = locations->InAt(0);
5880 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005881 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005882 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005883 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005886 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005887 case DataType::Type::kBool:
5888 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005889 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005890 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005891 break;
5892 }
5893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005895 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005896 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005897 break;
5898 }
5899
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005900 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005901 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005902 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5903 // Branch cases into compressed and uncompressed for each index's type.
5904 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5905 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005906 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005907 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005908 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5909 "Expecting 0=compressed, 1=uncompressed");
5910 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005911 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5912 __ jmp(&done);
5913 __ Bind(&not_compressed);
5914 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5915 __ Bind(&done);
5916 } else {
5917 // Common case for charAt of array of char or when string compression's
5918 // feature is turned off.
5919 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5920 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005921 break;
5922 }
5923
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005924 case DataType::Type::kInt16: {
5925 Register out = out_loc.AsRegister<Register>();
5926 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5927 break;
5928 }
5929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005930 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005931 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005932 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005933 break;
5934 }
5935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005937 static_assert(
5938 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5939 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005940 // /* HeapReference<Object> */ out =
5941 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5942 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005943 // Note that a potential implicit null check is handled in this
5944 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5945 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005946 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005947 } else {
5948 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005949 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5950 codegen_->MaybeRecordImplicitNullCheck(instruction);
5951 // If read barriers are enabled, emit read barriers other than
5952 // Baker's using a slow path (and also unpoison the loaded
5953 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005954 if (index.IsConstant()) {
5955 uint32_t offset =
5956 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005957 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5958 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005959 codegen_->MaybeGenerateReadBarrierSlow(
5960 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5961 }
5962 }
5963 break;
5964 }
5965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005966 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005967 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005968 __ movl(out_loc.AsRegisterPairLow<Register>(),
5969 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5970 codegen_->MaybeRecordImplicitNullCheck(instruction);
5971 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5972 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973 break;
5974 }
5975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005977 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005978 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005979 break;
5980 }
5981
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005983 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005984 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005985 break;
5986 }
5987
Aart Bik66c158e2018-01-31 12:55:04 -08005988 case DataType::Type::kUint32:
5989 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005990 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005991 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005992 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005993 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005994
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005996 // Potential implicit null checks, in the case of reference or
5997 // long arrays, are handled in the previous switch statement.
5998 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005999 codegen_->MaybeRecordImplicitNullCheck(instruction);
6000 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001}
6002
6003void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006005
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006006 bool needs_write_barrier =
6007 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006008 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006009
Vladimir Markoca6fff82017-10-03 14:49:14 +01006010 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006011 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006012 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006013
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006014 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006015 // We need the inputs to be different than the output in case of long operation.
6016 // In case of a byte operation, the register allocator does not support multiple
6017 // inputs that die at entry with one in a specific register.
6018 locations->SetInAt(0, Location::RequiresRegister());
6019 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6020 if (is_byte_type) {
6021 // Ensure the value is in a byte register.
6022 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006023 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006024 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006025 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006026 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6027 }
6028 if (needs_write_barrier) {
6029 // Temporary registers for the write barrier.
6030 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6031 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006032 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006034}
6035
6036void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6037 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006038 Location array_loc = locations->InAt(0);
6039 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006041 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006043 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006044 bool needs_write_barrier =
6045 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046
6047 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006048 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006049 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006050 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006051 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006052 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006053 if (value.IsRegister()) {
6054 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006055 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006056 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006057 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006058 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006059 break;
6060 }
6061
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006062 case DataType::Type::kUint16:
6063 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006064 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006065 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006066 if (value.IsRegister()) {
6067 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006068 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006069 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006070 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006071 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006072 break;
6073 }
6074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006075 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006076 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006077 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006078
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006079 if (!value.IsRegister()) {
6080 // Just setting null.
6081 DCHECK(instruction->InputAt(2)->IsNullConstant());
6082 DCHECK(value.IsConstant()) << value;
6083 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006084 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006085 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006086 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006087 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006088 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006089
6090 DCHECK(needs_write_barrier);
6091 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006092 Location temp_loc = locations->GetTemp(0);
6093 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006094
6095 bool can_value_be_null = instruction->GetValueCanBeNull();
6096 NearLabel do_store;
6097 if (can_value_be_null) {
6098 __ testl(register_value, register_value);
6099 __ j(kEqual, &do_store);
6100 }
6101
6102 SlowPathCode* slow_path = nullptr;
6103 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006104 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006105 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006106
6107 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6108 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6109 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006110
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006111 // Note that when Baker read barriers are enabled, the type
6112 // checks are performed without read barriers. This is fine,
6113 // even in the case where a class object is in the from-space
6114 // after the flip, as a comparison involving such a type would
6115 // not produce a false positive; it may of course produce a
6116 // false negative, in which case we would take the ArraySet
6117 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006118
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006119 // /* HeapReference<Class> */ temp = array->klass_
6120 __ movl(temp, Address(array, class_offset));
6121 codegen_->MaybeRecordImplicitNullCheck(instruction);
6122 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006123
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006124 // /* HeapReference<Class> */ temp = temp->component_type_
6125 __ movl(temp, Address(temp, component_offset));
6126 // If heap poisoning is enabled, no need to unpoison `temp`
6127 // nor the object reference in `register_value->klass`, as
6128 // we are comparing two poisoned references.
6129 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006130
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006131 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006132 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006133 __ j(kEqual, &do_put);
6134 // If heap poisoning is enabled, the `temp` reference has
6135 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006136 __ MaybeUnpoisonHeapReference(temp);
6137
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006138 // If heap poisoning is enabled, no need to unpoison the
6139 // heap reference loaded below, as it is only used for a
6140 // comparison with null.
6141 __ cmpl(Address(temp, super_offset), Immediate(0));
6142 __ j(kNotEqual, slow_path->GetEntryLabel());
6143 __ Bind(&do_put);
6144 } else {
6145 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006146 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006147 }
6148
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006149 Register card = locations->GetTemp(1).AsRegister<Register>();
6150 codegen_->MarkGCCard(
6151 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6152
6153 if (can_value_be_null) {
6154 DCHECK(do_store.IsLinked());
6155 __ Bind(&do_store);
6156 }
6157
6158 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006159 if (kPoisonHeapReferences) {
6160 __ movl(temp, register_value);
6161 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006162 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006163 }
6164
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006165 __ movl(address, source);
6166
6167 if (can_value_be_null || !needs_type_check) {
6168 codegen_->MaybeRecordImplicitNullCheck(instruction);
6169 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006170
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006171 if (slow_path != nullptr) {
6172 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006173 }
6174
6175 break;
6176 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006178 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006179 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006180 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006181 if (value.IsRegister()) {
6182 __ movl(address, value.AsRegister<Register>());
6183 } else {
6184 DCHECK(value.IsConstant()) << value;
6185 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6186 __ movl(address, Immediate(v));
6187 }
6188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006189 break;
6190 }
6191
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006193 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006194 if (value.IsRegisterPair()) {
6195 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6196 value.AsRegisterPairLow<Register>());
6197 codegen_->MaybeRecordImplicitNullCheck(instruction);
6198 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6199 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006200 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006201 DCHECK(value.IsConstant());
6202 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6203 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6204 Immediate(Low32Bits(val)));
6205 codegen_->MaybeRecordImplicitNullCheck(instruction);
6206 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6207 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006208 }
6209 break;
6210 }
6211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006212 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006213 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006214 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006215 if (value.IsFpuRegister()) {
6216 __ movss(address, value.AsFpuRegister<XmmRegister>());
6217 } else {
6218 DCHECK(value.IsConstant());
6219 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6220 __ movl(address, Immediate(v));
6221 }
6222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006223 break;
6224 }
6225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006227 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006228 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006229 if (value.IsFpuRegister()) {
6230 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6231 } else {
6232 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006233 Address address_hi =
6234 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006235 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6236 __ movl(address, Immediate(Low32Bits(v)));
6237 codegen_->MaybeRecordImplicitNullCheck(instruction);
6238 __ movl(address_hi, Immediate(High32Bits(v)));
6239 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006240 break;
6241 }
6242
Aart Bik66c158e2018-01-31 12:55:04 -08006243 case DataType::Type::kUint32:
6244 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006245 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006246 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006247 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006248 }
6249}
6250
6251void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006252 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006253 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006254 if (!instruction->IsEmittedAtUseSite()) {
6255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6256 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006257}
6258
6259void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006260 if (instruction->IsEmittedAtUseSite()) {
6261 return;
6262 }
6263
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006265 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006266 Register obj = locations->InAt(0).AsRegister<Register>();
6267 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006268 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006269 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006270 // Mask out most significant bit in case the array is String's array of char.
6271 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006272 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006273 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006274}
6275
6276void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006277 RegisterSet caller_saves = RegisterSet::Empty();
6278 InvokeRuntimeCallingConvention calling_convention;
6279 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6280 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6281 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006282 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006283 HInstruction* length = instruction->InputAt(1);
6284 if (!length->IsEmittedAtUseSite()) {
6285 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6286 }
jessicahandojo4877b792016-09-08 19:49:13 -07006287 // Need register to see array's length.
6288 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6289 locations->AddTemp(Location::RequiresRegister());
6290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006291}
6292
6293void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006294 const bool is_string_compressed_char_at =
6295 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006296 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006297 Location index_loc = locations->InAt(0);
6298 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006299 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006300 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006301
Mark Mendell99dbd682015-04-22 16:18:52 -04006302 if (length_loc.IsConstant()) {
6303 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6304 if (index_loc.IsConstant()) {
6305 // BCE will remove the bounds check if we are guarenteed to pass.
6306 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6307 if (index < 0 || index >= length) {
6308 codegen_->AddSlowPath(slow_path);
6309 __ jmp(slow_path->GetEntryLabel());
6310 } else {
6311 // Some optimization after BCE may have generated this, and we should not
6312 // generate a bounds check if it is a valid range.
6313 }
6314 return;
6315 }
6316
6317 // We have to reverse the jump condition because the length is the constant.
6318 Register index_reg = index_loc.AsRegister<Register>();
6319 __ cmpl(index_reg, Immediate(length));
6320 codegen_->AddSlowPath(slow_path);
6321 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006322 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006323 HInstruction* array_length = instruction->InputAt(1);
6324 if (array_length->IsEmittedAtUseSite()) {
6325 // Address the length field in the array.
6326 DCHECK(array_length->IsArrayLength());
6327 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6328 Location array_loc = array_length->GetLocations()->InAt(0);
6329 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006330 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006331 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6332 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006333 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6334 __ movl(length_reg, array_len);
6335 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006336 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006337 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006338 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006339 // Checking bounds for general case:
6340 // Array of char or string's array with feature compression off.
6341 if (index_loc.IsConstant()) {
6342 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6343 __ cmpl(array_len, Immediate(value));
6344 } else {
6345 __ cmpl(array_len, index_loc.AsRegister<Register>());
6346 }
6347 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006348 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006349 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006350 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006351 }
6352 codegen_->AddSlowPath(slow_path);
6353 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006354 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006355}
6356
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006357void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006358 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006359}
6360
6361void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006362 if (instruction->GetNext()->IsSuspendCheck() &&
6363 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6364 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6365 // The back edge will generate the suspend check.
6366 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6367 }
6368
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006369 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6370}
6371
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006372void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006373 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6374 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006375 // In suspend check slow path, usually there are no caller-save registers at all.
6376 // If SIMD instructions are present, however, we force spilling all live SIMD
6377 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006378 locations->SetCustomSlowPathCallerSaves(
6379 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006380}
6381
6382void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006383 HBasicBlock* block = instruction->GetBlock();
6384 if (block->GetLoopInformation() != nullptr) {
6385 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6386 // The back edge will generate the suspend check.
6387 return;
6388 }
6389 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6390 // The goto will generate the suspend check.
6391 return;
6392 }
6393 GenerateSuspendCheck(instruction, nullptr);
6394}
6395
6396void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6397 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006398 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006399 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6400 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006401 slow_path =
6402 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006403 instruction->SetSlowPath(slow_path);
6404 codegen_->AddSlowPath(slow_path);
6405 if (successor != nullptr) {
6406 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006407 }
6408 } else {
6409 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6410 }
6411
Andreas Gampe542451c2016-07-26 09:02:02 -07006412 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006413 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006414 if (successor == nullptr) {
6415 __ j(kNotEqual, slow_path->GetEntryLabel());
6416 __ Bind(slow_path->GetReturnLabel());
6417 } else {
6418 __ j(kEqual, codegen_->GetLabelOf(successor));
6419 __ jmp(slow_path->GetEntryLabel());
6420 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006421}
6422
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006423X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6424 return codegen_->GetAssembler();
6425}
6426
Aart Bikcfe50bb2017-12-12 14:54:12 -08006427void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006428 ScratchRegisterScope ensure_scratch(
6429 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6430 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6431 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006432
Aart Bikcfe50bb2017-12-12 14:54:12 -08006433 // Now that temp register is available (possibly spilled), move blocks of memory.
6434 for (int i = 0; i < number_of_words; i++) {
6435 __ movl(temp_reg, Address(ESP, src + stack_offset));
6436 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6437 stack_offset += kX86WordSize;
6438 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006439}
6440
6441void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006442 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006443 Location source = move->GetSource();
6444 Location destination = move->GetDestination();
6445
6446 if (source.IsRegister()) {
6447 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006448 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006449 } else if (destination.IsFpuRegister()) {
6450 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006451 } else {
6452 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006453 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006454 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006455 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006456 if (destination.IsRegisterPair()) {
6457 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6458 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6459 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6460 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006461 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006462 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006463 __ pushl(source.AsRegisterPairHigh<Register>());
6464 __ cfi().AdjustCFAOffset(elem_size);
6465 __ pushl(source.AsRegisterPairLow<Register>());
6466 __ cfi().AdjustCFAOffset(elem_size);
6467 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006468 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6469 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006470 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006471 } else {
6472 DCHECK(destination.IsDoubleStackSlot());
6473 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6474 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6475 source.AsRegisterPairHigh<Register>());
6476 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006477 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006478 if (destination.IsRegister()) {
6479 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6480 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006481 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006482 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006483 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6484 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006485 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006486 // Store the source register.
6487 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6488 // And pop the values into destination registers.
6489 __ popl(destination.AsRegisterPairLow<Register>());
6490 __ cfi().AdjustCFAOffset(-elem_size);
6491 __ popl(destination.AsRegisterPairHigh<Register>());
6492 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006493 } else if (destination.IsStackSlot()) {
6494 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006495 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006496 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006497 } else {
6498 DCHECK(destination.IsSIMDStackSlot());
6499 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006500 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006501 } else if (source.IsStackSlot()) {
6502 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006503 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006504 } else if (destination.IsFpuRegister()) {
6505 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006506 } else {
6507 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006508 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006509 }
6510 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006511 if (destination.IsRegisterPair()) {
6512 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6513 __ movl(destination.AsRegisterPairHigh<Register>(),
6514 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6515 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006516 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6517 } else {
6518 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006519 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006520 }
Aart Bik5576f372017-03-23 16:17:37 -07006521 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006522 if (destination.IsFpuRegister()) {
6523 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6524 } else {
6525 DCHECK(destination.IsSIMDStackSlot());
6526 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6527 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006528 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006529 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006530 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006531 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006532 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006533 if (value == 0) {
6534 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6535 } else {
6536 __ movl(destination.AsRegister<Register>(), Immediate(value));
6537 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006538 } else {
6539 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006540 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006541 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006542 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006543 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006544 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006545 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006546 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006547 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6548 if (value == 0) {
6549 // Easy handling of 0.0.
6550 __ xorps(dest, dest);
6551 } else {
6552 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006553 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6554 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6555 __ movl(temp, Immediate(value));
6556 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006557 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006558 } else {
6559 DCHECK(destination.IsStackSlot()) << destination;
6560 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6561 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006562 } else if (constant->IsLongConstant()) {
6563 int64_t value = constant->AsLongConstant()->GetValue();
6564 int32_t low_value = Low32Bits(value);
6565 int32_t high_value = High32Bits(value);
6566 Immediate low(low_value);
6567 Immediate high(high_value);
6568 if (destination.IsDoubleStackSlot()) {
6569 __ movl(Address(ESP, destination.GetStackIndex()), low);
6570 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6571 } else {
6572 __ movl(destination.AsRegisterPairLow<Register>(), low);
6573 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6574 }
6575 } else {
6576 DCHECK(constant->IsDoubleConstant());
6577 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006578 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006579 int32_t low_value = Low32Bits(value);
6580 int32_t high_value = High32Bits(value);
6581 Immediate low(low_value);
6582 Immediate high(high_value);
6583 if (destination.IsFpuRegister()) {
6584 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6585 if (value == 0) {
6586 // Easy handling of 0.0.
6587 __ xorpd(dest, dest);
6588 } else {
6589 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006590 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006591 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006592 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006593 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006594 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006595 }
6596 } else {
6597 DCHECK(destination.IsDoubleStackSlot()) << destination;
6598 __ movl(Address(ESP, destination.GetStackIndex()), low);
6599 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6600 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006601 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006602 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006603 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006604 }
6605}
6606
Mark Mendella5c19ce2015-04-01 12:51:05 -04006607void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006608 Register suggested_scratch = reg == EAX ? EBX : EAX;
6609 ScratchRegisterScope ensure_scratch(
6610 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6611
6612 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6613 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6614 __ movl(Address(ESP, mem + stack_offset), reg);
6615 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006616}
6617
Mark Mendell7c8d0092015-01-26 11:21:33 -05006618void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006619 ScratchRegisterScope ensure_scratch(
6620 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6621
6622 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6623 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6624 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6625 __ movss(Address(ESP, mem + stack_offset), reg);
6626 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006627}
6628
Aart Bikcfe50bb2017-12-12 14:54:12 -08006629void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6630 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006631 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006632 __ movups(Address(ESP, 0), XmmRegister(reg));
6633 ExchangeMemory(0, mem + extra_slot, 4);
6634 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006635 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006636}
6637
6638void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006639 ScratchRegisterScope ensure_scratch1(
6640 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006641
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006642 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6643 ScratchRegisterScope ensure_scratch2(
6644 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006645
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006646 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6647 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006648
6649 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6650 for (int i = 0; i < number_of_words; i++) {
6651 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6652 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6653 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6654 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6655 stack_offset += kX86WordSize;
6656 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006657}
6658
6659void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006660 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006661 Location source = move->GetSource();
6662 Location destination = move->GetDestination();
6663
6664 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006665 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6666 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6667 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6668 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6669 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006670 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006671 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006672 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006673 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006674 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006675 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006676 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6677 // Use XOR Swap algorithm to avoid a temporary.
6678 DCHECK_NE(source.reg(), destination.reg());
6679 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6680 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6681 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6682 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6683 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6684 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6685 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006686 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6687 // Take advantage of the 16 bytes in the XMM register.
6688 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6689 Address stack(ESP, destination.GetStackIndex());
6690 // Load the double into the high doubleword.
6691 __ movhpd(reg, stack);
6692
6693 // Store the low double into the destination.
6694 __ movsd(stack, reg);
6695
6696 // Move the high double to the low double.
6697 __ psrldq(reg, Immediate(8));
6698 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6699 // Take advantage of the 16 bytes in the XMM register.
6700 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6701 Address stack(ESP, source.GetStackIndex());
6702 // Load the double into the high doubleword.
6703 __ movhpd(reg, stack);
6704
6705 // Store the low double into the destination.
6706 __ movsd(stack, reg);
6707
6708 // Move the high double to the low double.
6709 __ psrldq(reg, Immediate(8));
6710 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006711 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6712 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6713 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6714 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6715 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6716 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6717 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006718 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006719 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006720 }
6721}
6722
6723void ParallelMoveResolverX86::SpillScratch(int reg) {
6724 __ pushl(static_cast<Register>(reg));
6725}
6726
6727void ParallelMoveResolverX86::RestoreScratch(int reg) {
6728 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006729}
6730
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006731HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6732 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006733 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006734 case HLoadClass::LoadKind::kInvalid:
6735 LOG(FATAL) << "UNREACHABLE";
6736 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006737 case HLoadClass::LoadKind::kReferrersClass:
6738 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006739 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006740 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006741 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006742 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006743 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006744 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006745 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006746 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006747 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006748 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006749 break;
6750 }
6751 return desired_class_load_kind;
6752}
6753
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006754void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006755 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006756 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006757 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006758 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006759 cls,
6760 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006761 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006762 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006763 return;
6764 }
Vladimir Marko41559982017-01-06 14:04:23 +00006765 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006766
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006767 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6768 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006769 ? LocationSummary::kCallOnSlowPath
6770 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006771 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006772 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006773 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006774 }
6775
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006776 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006777 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006778 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006779 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006780 locations->SetInAt(0, Location::RequiresRegister());
6781 }
6782 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006783 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6784 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6785 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006786 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006787 } else {
6788 // For non-Baker read barrier we have a temp-clobbering call.
6789 }
6790 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006791}
6792
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006793Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006794 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006795 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006796 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006797 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006798 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006799 PatchInfo<Label>* info = &jit_class_patches_.back();
6800 return &info->label;
6801}
6802
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006803// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6804// move.
6805void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006806 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006807 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006808 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006809 return;
6810 }
Vladimir Marko41559982017-01-06 14:04:23 +00006811 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006812
Vladimir Marko41559982017-01-06 14:04:23 +00006813 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814 Location out_loc = locations->Out();
6815 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006816
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006817 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006818 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6819 ? kWithoutReadBarrier
6820 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006821 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006822 case HLoadClass::LoadKind::kReferrersClass: {
6823 DCHECK(!cls->CanCallRuntime());
6824 DCHECK(!cls->MustGenerateClinitCheck());
6825 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6826 Register current_method = locations->InAt(0).AsRegister<Register>();
6827 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006828 cls,
6829 out_loc,
6830 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006831 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006832 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006833 break;
6834 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006835 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006836 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6837 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006838 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006839 Register method_address = locations->InAt(0).AsRegister<Register>();
6840 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006841 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006842 break;
6843 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006844 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006845 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6846 Register method_address = locations->InAt(0).AsRegister<Register>();
6847 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006848 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6849 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006850 break;
6851 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006852 case HLoadClass::LoadKind::kBssEntry: {
6853 Register method_address = locations->InAt(0).AsRegister<Register>();
6854 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6855 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6856 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006857 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006858 generate_null_check = true;
6859 break;
6860 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006861 case HLoadClass::LoadKind::kJitBootImageAddress: {
6862 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6863 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6864 DCHECK_NE(address, 0u);
6865 __ movl(out, Immediate(address));
6866 break;
6867 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006868 case HLoadClass::LoadKind::kJitTableAddress: {
6869 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6870 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006871 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006872 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006873 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006874 break;
6875 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006876 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006877 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006878 LOG(FATAL) << "UNREACHABLE";
6879 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006880 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006881
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006882 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6883 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006884 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006885 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006886
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006887 if (generate_null_check) {
6888 __ testl(out, out);
6889 __ j(kEqual, slow_path->GetEntryLabel());
6890 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006891
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006892 if (cls->MustGenerateClinitCheck()) {
6893 GenerateClassInitializationCheck(slow_path, out);
6894 } else {
6895 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006896 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006897 }
6898}
6899
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006900void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6901 InvokeRuntimeCallingConvention calling_convention;
6902 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6903 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6904}
6905
6906void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6907 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6908}
6909
Orion Hodson18259d72018-04-12 11:18:23 +01006910void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6911 InvokeRuntimeCallingConvention calling_convention;
6912 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6913 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6914}
6915
6916void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6917 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6918}
6919
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006920void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6921 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006922 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006923 locations->SetInAt(0, Location::RequiresRegister());
6924 if (check->HasUses()) {
6925 locations->SetOut(Location::SameAsFirstInput());
6926 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006927 // Rely on the type initialization to save everything we need.
6928 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006929}
6930
6931void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006932 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006933 SlowPathCode* slow_path =
6934 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006935 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006936 GenerateClassInitializationCheck(slow_path,
6937 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006938}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006939
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006940void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006941 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006942 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6943 const size_t status_byte_offset =
6944 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006945 constexpr uint32_t shifted_visibly_initialized_value =
6946 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006947
Vladimir Markobf121912019-06-04 13:49:05 +01006948 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006949 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006950 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006951}
6952
Vladimir Marko175e7862018-03-27 09:03:13 +00006953void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6954 Register temp) {
6955 uint32_t path_to_root = check->GetBitstringPathToRoot();
6956 uint32_t mask = check->GetBitstringMask();
6957 DCHECK(IsPowerOfTwo(mask + 1));
6958 size_t mask_bits = WhichPowerOf2(mask + 1);
6959
6960 if (mask_bits == 16u) {
6961 // Compare the bitstring in memory.
6962 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6963 } else {
6964 // /* uint32_t */ temp = temp->status_
6965 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6966 // Compare the bitstring bits using SUB.
6967 __ subl(temp, Immediate(path_to_root));
6968 // Shift out bits that do not contribute to the comparison.
6969 __ shll(temp, Immediate(32u - mask_bits));
6970 }
6971}
6972
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006973HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6974 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006975 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006976 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006977 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006978 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006979 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006980 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006981 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006982 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006983 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006984 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006985 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006986 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006987 }
6988 return desired_string_load_kind;
6989}
6990
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006991void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006992 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006993 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006994 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006995 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006996 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006997 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006998 locations->SetInAt(0, Location::RequiresRegister());
6999 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007000 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007001 locations->SetOut(Location::RegisterLocation(EAX));
7002 } else {
7003 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007004 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7005 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007006 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007007 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007008 } else {
7009 // For non-Baker read barrier we have a temp-clobbering call.
7010 }
7011 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007012 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007013}
7014
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007015Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007016 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007017 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007018 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007019 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007020 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007021 PatchInfo<Label>* info = &jit_string_patches_.back();
7022 return &info->label;
7023}
7024
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007025// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7026// move.
7027void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007028 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007029 Location out_loc = locations->Out();
7030 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007031
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007032 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007033 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007034 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7035 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007036 Register method_address = locations->InAt(0).AsRegister<Register>();
7037 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007038 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007039 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007040 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007041 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007042 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7043 Register method_address = locations->InAt(0).AsRegister<Register>();
7044 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007045 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7046 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007047 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007048 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007049 case HLoadString::LoadKind::kBssEntry: {
7050 Register method_address = locations->InAt(0).AsRegister<Register>();
7051 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
7052 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007053 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007054 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007055 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007056 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007057 codegen_->AddSlowPath(slow_path);
7058 __ testl(out, out);
7059 __ j(kEqual, slow_path->GetEntryLabel());
7060 __ Bind(slow_path->GetExitLabel());
7061 return;
7062 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007063 case HLoadString::LoadKind::kJitBootImageAddress: {
7064 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7065 DCHECK_NE(address, 0u);
7066 __ movl(out, Immediate(address));
7067 return;
7068 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007069 case HLoadString::LoadKind::kJitTableAddress: {
7070 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
7071 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007072 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007073 // /* GcRoot<mirror::String> */ out = *address
7074 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7075 return;
7076 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007077 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007078 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007079 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007080
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007081 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007082 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007083 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007084 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007085 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7086 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007087}
7088
David Brazdilcb1c0552015-08-04 16:22:25 +01007089static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007090 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007091}
7092
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007093void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7094 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007095 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007096 locations->SetOut(Location::RequiresRegister());
7097}
7098
7099void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007100 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7101}
7102
7103void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007104 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007105}
7106
7107void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7108 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007109}
7110
7111void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007112 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7113 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007114 InvokeRuntimeCallingConvention calling_convention;
7115 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7116}
7117
7118void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007119 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007120 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007121}
7122
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007123// Temp is used for read barrier.
7124static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7125 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007126 !kUseBakerReadBarrier &&
7127 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007128 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007129 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7130 return 1;
7131 }
7132 return 0;
7133}
7134
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007135// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7136// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007137// The other checks have one temp for loading the object's class.
7138static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007139 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007140 return 2;
7141 }
7142 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007143}
7144
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007145void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007147 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007148 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007149 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007150 case TypeCheckKind::kExactCheck:
7151 case TypeCheckKind::kAbstractClassCheck:
7152 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007153 case TypeCheckKind::kArrayObjectCheck: {
7154 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7155 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7156 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007157 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007158 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007159 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007160 case TypeCheckKind::kUnresolvedCheck:
7161 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007162 call_kind = LocationSummary::kCallOnSlowPath;
7163 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007164 case TypeCheckKind::kBitstringCheck:
7165 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007166 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007167
Vladimir Markoca6fff82017-10-03 14:49:14 +01007168 LocationSummary* locations =
7169 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007170 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007171 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007172 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007173 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007174 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7175 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7176 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7177 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7178 } else {
7179 locations->SetInAt(1, Location::Any());
7180 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007181 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7182 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007183 // When read barriers are enabled, we need a temporary register for some cases.
7184 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007185}
7186
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007187void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007188 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007189 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007190 Location obj_loc = locations->InAt(0);
7191 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007192 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007193 Location out_loc = locations->Out();
7194 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007195 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7196 DCHECK_LE(num_temps, 1u);
7197 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007198 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007199 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7200 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7201 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007202 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007203 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007204
7205 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007206 // Avoid null check if we know obj is not null.
7207 if (instruction->MustDoNullCheck()) {
7208 __ testl(obj, obj);
7209 __ j(kEqual, &zero);
7210 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007211
Roland Levillain7c1559a2015-12-15 10:55:36 +00007212 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007213 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007214 ReadBarrierOption read_barrier_option =
7215 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007216 // /* HeapReference<Class> */ out = obj->klass_
7217 GenerateReferenceLoadTwoRegisters(instruction,
7218 out_loc,
7219 obj_loc,
7220 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007221 read_barrier_option);
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 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 // Classes must be equal for the instanceof to succeed.
7230 __ j(kNotEqual, &zero);
7231 __ movl(out, Immediate(1));
7232 __ jmp(&done);
7233 break;
7234 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007236 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007237 ReadBarrierOption read_barrier_option =
7238 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007239 // /* HeapReference<Class> */ out = obj->klass_
7240 GenerateReferenceLoadTwoRegisters(instruction,
7241 out_loc,
7242 obj_loc,
7243 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007244 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007245 // If the class is abstract, we eagerly fetch the super class of the
7246 // object to avoid doing a comparison we know will fail.
7247 NearLabel loop;
7248 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007249 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007250 GenerateReferenceLoadOneRegister(instruction,
7251 out_loc,
7252 super_offset,
7253 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007254 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007255 __ testl(out, out);
7256 // If `out` is null, we use it for the result, and jump to `done`.
7257 __ j(kEqual, &done);
7258 if (cls.IsRegister()) {
7259 __ cmpl(out, cls.AsRegister<Register>());
7260 } else {
7261 DCHECK(cls.IsStackSlot()) << cls;
7262 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7263 }
7264 __ j(kNotEqual, &loop);
7265 __ movl(out, Immediate(1));
7266 if (zero.IsLinked()) {
7267 __ jmp(&done);
7268 }
7269 break;
7270 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007271
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007272 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007273 ReadBarrierOption read_barrier_option =
7274 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007275 // /* HeapReference<Class> */ out = obj->klass_
7276 GenerateReferenceLoadTwoRegisters(instruction,
7277 out_loc,
7278 obj_loc,
7279 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007280 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007281 // Walk over the class hierarchy to find a match.
7282 NearLabel loop, success;
7283 __ Bind(&loop);
7284 if (cls.IsRegister()) {
7285 __ cmpl(out, cls.AsRegister<Register>());
7286 } else {
7287 DCHECK(cls.IsStackSlot()) << cls;
7288 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7289 }
7290 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007291 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007292 GenerateReferenceLoadOneRegister(instruction,
7293 out_loc,
7294 super_offset,
7295 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007296 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007297 __ testl(out, out);
7298 __ j(kNotEqual, &loop);
7299 // If `out` is null, we use it for the result, and jump to `done`.
7300 __ jmp(&done);
7301 __ Bind(&success);
7302 __ movl(out, Immediate(1));
7303 if (zero.IsLinked()) {
7304 __ jmp(&done);
7305 }
7306 break;
7307 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007309 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007310 ReadBarrierOption read_barrier_option =
7311 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007312 // /* HeapReference<Class> */ out = obj->klass_
7313 GenerateReferenceLoadTwoRegisters(instruction,
7314 out_loc,
7315 obj_loc,
7316 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007317 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007318 // Do an exact check.
7319 NearLabel exact_check;
7320 if (cls.IsRegister()) {
7321 __ cmpl(out, cls.AsRegister<Register>());
7322 } else {
7323 DCHECK(cls.IsStackSlot()) << cls;
7324 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7325 }
7326 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007327 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007329 GenerateReferenceLoadOneRegister(instruction,
7330 out_loc,
7331 component_offset,
7332 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007333 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007334 __ testl(out, out);
7335 // If `out` is null, we use it for the result, and jump to `done`.
7336 __ j(kEqual, &done);
7337 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7338 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007339 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007340 __ movl(out, Immediate(1));
7341 __ jmp(&done);
7342 break;
7343 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007345 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007346 // No read barrier since the slow path will retry upon failure.
7347 // /* HeapReference<Class> */ out = obj->klass_
7348 GenerateReferenceLoadTwoRegisters(instruction,
7349 out_loc,
7350 obj_loc,
7351 class_offset,
7352 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 if (cls.IsRegister()) {
7354 __ cmpl(out, cls.AsRegister<Register>());
7355 } else {
7356 DCHECK(cls.IsStackSlot()) << cls;
7357 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7358 }
7359 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007360 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007361 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007362 codegen_->AddSlowPath(slow_path);
7363 __ j(kNotEqual, slow_path->GetEntryLabel());
7364 __ movl(out, Immediate(1));
7365 if (zero.IsLinked()) {
7366 __ jmp(&done);
7367 }
7368 break;
7369 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007370
Calin Juravle98893e12015-10-02 21:05:03 +01007371 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007372 case TypeCheckKind::kInterfaceCheck: {
7373 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007374 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007375 // cases.
7376 //
7377 // We cannot directly call the InstanceofNonTrivial runtime
7378 // entry point without resorting to a type checking slow path
7379 // here (i.e. by calling InvokeRuntime directly), as it would
7380 // require to assign fixed registers for the inputs of this
7381 // HInstanceOf instruction (following the runtime calling
7382 // convention), which might be cluttered by the potential first
7383 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007384 //
7385 // TODO: Introduce a new runtime entry point taking the object
7386 // to test (instead of its class) as argument, and let it deal
7387 // with the read barrier issues. This will let us refactor this
7388 // case of the `switch` code as it was previously (with a direct
7389 // call to the runtime not using a type checking slow path).
7390 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007391 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007392 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007393 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007394 codegen_->AddSlowPath(slow_path);
7395 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007396 if (zero.IsLinked()) {
7397 __ jmp(&done);
7398 }
7399 break;
7400 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007401
7402 case TypeCheckKind::kBitstringCheck: {
7403 // /* HeapReference<Class> */ temp = obj->klass_
7404 GenerateReferenceLoadTwoRegisters(instruction,
7405 out_loc,
7406 obj_loc,
7407 class_offset,
7408 kWithoutReadBarrier);
7409
7410 GenerateBitstringTypeCheckCompare(instruction, out);
7411 __ j(kNotEqual, &zero);
7412 __ movl(out, Immediate(1));
7413 __ jmp(&done);
7414 break;
7415 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007416 }
7417
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007418 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007419 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007420 __ xorl(out, out);
7421 }
7422
7423 if (done.IsLinked()) {
7424 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007425 }
7426
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007427 if (slow_path != nullptr) {
7428 __ Bind(slow_path->GetExitLabel());
7429 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007430}
7431
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007432void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007433 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007434 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007435 LocationSummary* locations =
7436 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007437 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007438 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7439 // Require a register for the interface check since there is a loop that compares the class to
7440 // a memory address.
7441 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007442 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7443 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7444 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7445 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007446 } else {
7447 locations->SetInAt(1, Location::Any());
7448 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007449 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007450 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7451}
7452
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007453void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007454 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007455 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007456 Location obj_loc = locations->InAt(0);
7457 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007458 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007459 Location temp_loc = locations->GetTemp(0);
7460 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007461 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7462 DCHECK_GE(num_temps, 1u);
7463 DCHECK_LE(num_temps, 2u);
7464 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7465 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7466 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7467 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7468 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7469 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7470 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7471 const uint32_t object_array_data_offset =
7472 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007473
Vladimir Marko87584542017-12-12 17:47:52 +00007474 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007475 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007476 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7477 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007478 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007479
Roland Levillain0d5a2812015-11-13 10:07:31 +00007480 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007481 // Avoid null check if we know obj is not null.
7482 if (instruction->MustDoNullCheck()) {
7483 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007484 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007485 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007486
Roland Levillain0d5a2812015-11-13 10:07:31 +00007487 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007488 case TypeCheckKind::kExactCheck:
7489 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007490 // /* HeapReference<Class> */ temp = obj->klass_
7491 GenerateReferenceLoadTwoRegisters(instruction,
7492 temp_loc,
7493 obj_loc,
7494 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007495 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007497 if (cls.IsRegister()) {
7498 __ cmpl(temp, cls.AsRegister<Register>());
7499 } else {
7500 DCHECK(cls.IsStackSlot()) << cls;
7501 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7502 }
7503 // Jump to slow path for throwing the exception or doing a
7504 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007505 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007506 break;
7507 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007509 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007510 // /* HeapReference<Class> */ temp = obj->klass_
7511 GenerateReferenceLoadTwoRegisters(instruction,
7512 temp_loc,
7513 obj_loc,
7514 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007515 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007516
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007517 // If the class is abstract, we eagerly fetch the super class of the
7518 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007519 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007520 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007521 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007522 GenerateReferenceLoadOneRegister(instruction,
7523 temp_loc,
7524 super_offset,
7525 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007526 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007527
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007528 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7529 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007530 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007531 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007532
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007533 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007534 if (cls.IsRegister()) {
7535 __ cmpl(temp, cls.AsRegister<Register>());
7536 } else {
7537 DCHECK(cls.IsStackSlot()) << cls;
7538 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7539 }
7540 __ j(kNotEqual, &loop);
7541 break;
7542 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007544 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007545 // /* HeapReference<Class> */ temp = obj->klass_
7546 GenerateReferenceLoadTwoRegisters(instruction,
7547 temp_loc,
7548 obj_loc,
7549 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007550 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007551
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007552 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007553 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007554 __ Bind(&loop);
7555 if (cls.IsRegister()) {
7556 __ cmpl(temp, cls.AsRegister<Register>());
7557 } else {
7558 DCHECK(cls.IsStackSlot()) << cls;
7559 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7560 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007561 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007562
Roland Levillain0d5a2812015-11-13 10:07:31 +00007563 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007564 GenerateReferenceLoadOneRegister(instruction,
7565 temp_loc,
7566 super_offset,
7567 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007568 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007569
7570 // If the class reference currently in `temp` is not null, jump
7571 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007572 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007573 __ j(kNotZero, &loop);
7574 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007575 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007576 break;
7577 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007578
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007579 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007580 // /* HeapReference<Class> */ temp = obj->klass_
7581 GenerateReferenceLoadTwoRegisters(instruction,
7582 temp_loc,
7583 obj_loc,
7584 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007585 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007586
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007587 // Do an exact check.
7588 if (cls.IsRegister()) {
7589 __ cmpl(temp, cls.AsRegister<Register>());
7590 } else {
7591 DCHECK(cls.IsStackSlot()) << cls;
7592 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7593 }
7594 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007595
7596 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007597 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007598 GenerateReferenceLoadOneRegister(instruction,
7599 temp_loc,
7600 component_offset,
7601 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007602 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007603
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007604 // If the component type is null (i.e. the object not an array), jump to the slow path to
7605 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007606 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007607 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007609 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007610 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007611 break;
7612 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007613
Calin Juravle98893e12015-10-02 21:05:03 +01007614 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007615 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007616 // We cannot directly call the CheckCast runtime entry point
7617 // without resorting to a type checking slow path here (i.e. by
7618 // calling InvokeRuntime directly), as it would require to
7619 // assign fixed registers for the inputs of this HInstanceOf
7620 // instruction (following the runtime calling convention), which
7621 // might be cluttered by the potential first read barrier
7622 // emission at the beginning of this method.
7623 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007624 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007625
7626 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007627 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7628 // We can not get false positives by doing this.
7629 // /* HeapReference<Class> */ temp = obj->klass_
7630 GenerateReferenceLoadTwoRegisters(instruction,
7631 temp_loc,
7632 obj_loc,
7633 class_offset,
7634 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007635
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007636 // /* HeapReference<Class> */ temp = temp->iftable_
7637 GenerateReferenceLoadTwoRegisters(instruction,
7638 temp_loc,
7639 temp_loc,
7640 iftable_offset,
7641 kWithoutReadBarrier);
7642 // Iftable is never null.
7643 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7644 // Maybe poison the `cls` for direct comparison with memory.
7645 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7646 // Loop through the iftable and check if any class matches.
7647 NearLabel start_loop;
7648 __ Bind(&start_loop);
7649 // Need to subtract first to handle the empty array case.
7650 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7651 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7652 // Go to next interface if the classes do not match.
7653 __ cmpl(cls.AsRegister<Register>(),
7654 CodeGeneratorX86::ArrayAddress(temp,
7655 maybe_temp2_loc,
7656 TIMES_4,
7657 object_array_data_offset));
7658 __ j(kNotEqual, &start_loop);
7659 // If `cls` was poisoned above, unpoison it.
7660 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007661 break;
7662 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007663
7664 case TypeCheckKind::kBitstringCheck: {
7665 // /* HeapReference<Class> */ temp = obj->klass_
7666 GenerateReferenceLoadTwoRegisters(instruction,
7667 temp_loc,
7668 obj_loc,
7669 class_offset,
7670 kWithoutReadBarrier);
7671
7672 GenerateBitstringTypeCheckCompare(instruction, temp);
7673 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7674 break;
7675 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007676 }
7677 __ Bind(&done);
7678
Roland Levillain0d5a2812015-11-13 10:07:31 +00007679 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007680}
7681
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007682void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007683 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7684 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007685 InvokeRuntimeCallingConvention calling_convention;
7686 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7687}
7688
7689void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007690 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7691 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007692 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007693 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007694 if (instruction->IsEnter()) {
7695 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7696 } else {
7697 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7698 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007699}
7700
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307701void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7702 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7703 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7704 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7705 locations->SetInAt(0, Location::RequiresRegister());
7706 locations->SetInAt(1, Location::RequiresRegister());
7707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7708}
7709
7710void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7711 LocationSummary* locations = instruction->GetLocations();
7712 Location first = locations->InAt(0);
7713 Location second = locations->InAt(1);
7714 Location dest = locations->Out();
7715 if (instruction->GetResultType() == DataType::Type::kInt32) {
7716 __ andn(dest.AsRegister<Register>(),
7717 first.AsRegister<Register>(),
7718 second.AsRegister<Register>());
7719 } else {
7720 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7721 __ andn(dest.AsRegisterPairLow<Register>(),
7722 first.AsRegisterPairLow<Register>(),
7723 second.AsRegisterPairLow<Register>());
7724 __ andn(dest.AsRegisterPairHigh<Register>(),
7725 first.AsRegisterPairHigh<Register>(),
7726 second.AsRegisterPairHigh<Register>());
7727 }
7728}
7729
7730void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7731 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7732 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7733 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7734 locations->SetInAt(0, Location::RequiresRegister());
7735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7736}
7737
7738void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7739 HX86MaskOrResetLeastSetBit* instruction) {
7740 LocationSummary* locations = instruction->GetLocations();
7741 Location src = locations->InAt(0);
7742 Location dest = locations->Out();
7743 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7744 switch (instruction->GetOpKind()) {
7745 case HInstruction::kAnd:
7746 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7747 break;
7748 case HInstruction::kXor:
7749 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7750 break;
7751 default:
7752 LOG(FATAL) << "Unreachable";
7753 }
7754}
7755
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007756void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7757void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7758void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7759
7760void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7761 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007762 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007763 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7764 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007765 locations->SetInAt(0, Location::RequiresRegister());
7766 locations->SetInAt(1, Location::Any());
7767 locations->SetOut(Location::SameAsFirstInput());
7768}
7769
7770void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7771 HandleBitwiseOperation(instruction);
7772}
7773
7774void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7775 HandleBitwiseOperation(instruction);
7776}
7777
7778void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7779 HandleBitwiseOperation(instruction);
7780}
7781
7782void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7783 LocationSummary* locations = instruction->GetLocations();
7784 Location first = locations->InAt(0);
7785 Location second = locations->InAt(1);
7786 DCHECK(first.Equals(locations->Out()));
7787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007788 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007789 if (second.IsRegister()) {
7790 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007791 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007792 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007793 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007794 } else {
7795 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007796 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007797 }
7798 } else if (second.IsConstant()) {
7799 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007800 __ andl(first.AsRegister<Register>(),
7801 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007802 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007803 __ orl(first.AsRegister<Register>(),
7804 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007805 } else {
7806 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007807 __ xorl(first.AsRegister<Register>(),
7808 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007809 }
7810 } else {
7811 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007812 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007813 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007814 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007815 } else {
7816 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007817 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007818 }
7819 }
7820 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007821 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007822 if (second.IsRegisterPair()) {
7823 if (instruction->IsAnd()) {
7824 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7825 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7826 } else if (instruction->IsOr()) {
7827 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7828 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7829 } else {
7830 DCHECK(instruction->IsXor());
7831 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7832 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7833 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007834 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007835 if (instruction->IsAnd()) {
7836 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7837 __ andl(first.AsRegisterPairHigh<Register>(),
7838 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7839 } else if (instruction->IsOr()) {
7840 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7841 __ orl(first.AsRegisterPairHigh<Register>(),
7842 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7843 } else {
7844 DCHECK(instruction->IsXor());
7845 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7846 __ xorl(first.AsRegisterPairHigh<Register>(),
7847 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7848 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007849 } else {
7850 DCHECK(second.IsConstant()) << second;
7851 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007852 int32_t low_value = Low32Bits(value);
7853 int32_t high_value = High32Bits(value);
7854 Immediate low(low_value);
7855 Immediate high(high_value);
7856 Register first_low = first.AsRegisterPairLow<Register>();
7857 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007858 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007859 if (low_value == 0) {
7860 __ xorl(first_low, first_low);
7861 } else if (low_value != -1) {
7862 __ andl(first_low, low);
7863 }
7864 if (high_value == 0) {
7865 __ xorl(first_high, first_high);
7866 } else if (high_value != -1) {
7867 __ andl(first_high, high);
7868 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007869 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007870 if (low_value != 0) {
7871 __ orl(first_low, low);
7872 }
7873 if (high_value != 0) {
7874 __ orl(first_high, high);
7875 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007876 } else {
7877 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007878 if (low_value != 0) {
7879 __ xorl(first_low, low);
7880 }
7881 if (high_value != 0) {
7882 __ xorl(first_high, high);
7883 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007884 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007885 }
7886 }
7887}
7888
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007889void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7890 HInstruction* instruction,
7891 Location out,
7892 uint32_t offset,
7893 Location maybe_temp,
7894 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007895 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007896 if (read_barrier_option == kWithReadBarrier) {
7897 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007898 if (kUseBakerReadBarrier) {
7899 // Load with fast path based Baker's read barrier.
7900 // /* HeapReference<Object> */ out = *(out + offset)
7901 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007902 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007903 } else {
7904 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007905 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007906 // in the following move operation, as we will need it for the
7907 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007908 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007909 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007910 // /* HeapReference<Object> */ out = *(out + offset)
7911 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007912 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007913 }
7914 } else {
7915 // Plain load with no read barrier.
7916 // /* HeapReference<Object> */ out = *(out + offset)
7917 __ movl(out_reg, Address(out_reg, offset));
7918 __ MaybeUnpoisonHeapReference(out_reg);
7919 }
7920}
7921
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007922void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7923 HInstruction* instruction,
7924 Location out,
7925 Location obj,
7926 uint32_t offset,
7927 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007928 Register out_reg = out.AsRegister<Register>();
7929 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007930 if (read_barrier_option == kWithReadBarrier) {
7931 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007932 if (kUseBakerReadBarrier) {
7933 // Load with fast path based Baker's read barrier.
7934 // /* HeapReference<Object> */ out = *(obj + offset)
7935 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007936 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007937 } else {
7938 // Load with slow path based read barrier.
7939 // /* HeapReference<Object> */ out = *(obj + offset)
7940 __ movl(out_reg, Address(obj_reg, offset));
7941 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7942 }
7943 } else {
7944 // Plain load with no read barrier.
7945 // /* HeapReference<Object> */ out = *(obj + offset)
7946 __ movl(out_reg, Address(obj_reg, offset));
7947 __ MaybeUnpoisonHeapReference(out_reg);
7948 }
7949}
7950
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007951void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7952 HInstruction* instruction,
7953 Location root,
7954 const Address& address,
7955 Label* fixup_label,
7956 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007957 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007958 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007959 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007960 if (kUseBakerReadBarrier) {
7961 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7962 // Baker's read barrier are used:
7963 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007964 // root = obj.field;
7965 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7966 // if (temp != null) {
7967 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007968 // }
7969
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007970 // /* GcRoot<mirror::Object> */ root = *address
7971 __ movl(root_reg, address);
7972 if (fixup_label != nullptr) {
7973 __ Bind(fixup_label);
7974 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007975 static_assert(
7976 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7977 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7978 "have different sizes.");
7979 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7980 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7981 "have different sizes.");
7982
Vladimir Marko953437b2016-08-24 08:30:46 +00007983 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007984 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007985 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007986 codegen_->AddSlowPath(slow_path);
7987
Roland Levillaind966ce72017-02-09 16:20:14 +00007988 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7989 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007990 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007991 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7992 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007993 __ j(kNotEqual, slow_path->GetEntryLabel());
7994 __ Bind(slow_path->GetExitLabel());
7995 } else {
7996 // GC root loaded through a slow path for read barriers other
7997 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007998 // /* GcRoot<mirror::Object>* */ root = address
7999 __ leal(root_reg, address);
8000 if (fixup_label != nullptr) {
8001 __ Bind(fixup_label);
8002 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008003 // /* mirror::Object* */ root = root->Read()
8004 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8005 }
8006 } else {
8007 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008008 // /* GcRoot<mirror::Object> */ root = *address
8009 __ movl(root_reg, address);
8010 if (fixup_label != nullptr) {
8011 __ Bind(fixup_label);
8012 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008013 // Note that GC roots are not affected by heap poisoning, thus we
8014 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008015 }
8016}
8017
8018void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8019 Location ref,
8020 Register obj,
8021 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008022 bool needs_null_check) {
8023 DCHECK(kEmitCompilerReadBarrier);
8024 DCHECK(kUseBakerReadBarrier);
8025
8026 // /* HeapReference<Object> */ ref = *(obj + offset)
8027 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008028 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008029}
8030
8031void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8032 Location ref,
8033 Register obj,
8034 uint32_t data_offset,
8035 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008036 bool needs_null_check) {
8037 DCHECK(kEmitCompilerReadBarrier);
8038 DCHECK(kUseBakerReadBarrier);
8039
Roland Levillain3d312422016-06-23 13:53:42 +01008040 static_assert(
8041 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8042 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008043 // /* HeapReference<Object> */ ref =
8044 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008045 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008046 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008047}
8048
8049void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8050 Location ref,
8051 Register obj,
8052 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008053 bool needs_null_check,
8054 bool always_update_field,
8055 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008056 DCHECK(kEmitCompilerReadBarrier);
8057 DCHECK(kUseBakerReadBarrier);
8058
8059 // In slow path based read barriers, the read barrier call is
8060 // inserted after the original load. However, in fast path based
8061 // Baker's read barriers, we need to perform the load of
8062 // mirror::Object::monitor_ *before* the original reference load.
8063 // This load-load ordering is required by the read barrier.
8064 // The fast path/slow path (for Baker's algorithm) should look like:
8065 //
8066 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8067 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8068 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008069 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008070 // if (is_gray) {
8071 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8072 // }
8073 //
8074 // Note: the original implementation in ReadBarrier::Barrier is
8075 // slightly more complex as:
8076 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008077 // the high-bits of rb_state, which are expected to be all zeroes
8078 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8079 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008080 // - it performs additional checks that we do not do here for
8081 // performance reasons.
8082
8083 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008084 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8085
Vladimir Marko953437b2016-08-24 08:30:46 +00008086 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008087 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008088 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008089 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8090 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8091 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8092
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008093 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008094 // ref = ReadBarrier::Mark(ref);
8095 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8096 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008097 if (needs_null_check) {
8098 MaybeRecordImplicitNullCheck(instruction);
8099 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008100
8101 // Load fence to prevent load-load reordering.
8102 // Note that this is a no-op, thanks to the x86 memory model.
8103 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8104
8105 // The actual reference load.
8106 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008107 __ movl(ref_reg, src); // Flags are unaffected.
8108
8109 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8110 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008111 SlowPathCode* slow_path;
8112 if (always_update_field) {
8113 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008114 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008115 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008116 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008117 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008118 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008119 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008120 AddSlowPath(slow_path);
8121
8122 // We have done the "if" of the gray bit check above, now branch based on the flags.
8123 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008124
8125 // Object* ref = ref_addr->AsMirrorPtr()
8126 __ MaybeUnpoisonHeapReference(ref_reg);
8127
Roland Levillain7c1559a2015-12-15 10:55:36 +00008128 __ Bind(slow_path->GetExitLabel());
8129}
8130
8131void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8132 Location out,
8133 Location ref,
8134 Location obj,
8135 uint32_t offset,
8136 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008137 DCHECK(kEmitCompilerReadBarrier);
8138
Roland Levillain7c1559a2015-12-15 10:55:36 +00008139 // Insert a slow path based read barrier *after* the reference load.
8140 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008141 // If heap poisoning is enabled, the unpoisoning of the loaded
8142 // reference will be carried out by the runtime within the slow
8143 // path.
8144 //
8145 // Note that `ref` currently does not get unpoisoned (when heap
8146 // poisoning is enabled), which is alright as the `ref` argument is
8147 // not used by the artReadBarrierSlow entry point.
8148 //
8149 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008150 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008151 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8152 AddSlowPath(slow_path);
8153
Roland Levillain0d5a2812015-11-13 10:07:31 +00008154 __ jmp(slow_path->GetEntryLabel());
8155 __ Bind(slow_path->GetExitLabel());
8156}
8157
Roland Levillain7c1559a2015-12-15 10:55:36 +00008158void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8159 Location out,
8160 Location ref,
8161 Location obj,
8162 uint32_t offset,
8163 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008164 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008165 // Baker's read barriers shall be handled by the fast path
8166 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8167 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008168 // If heap poisoning is enabled, unpoisoning will be taken care of
8169 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008170 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008171 } else if (kPoisonHeapReferences) {
8172 __ UnpoisonHeapReference(out.AsRegister<Register>());
8173 }
8174}
8175
Roland Levillain7c1559a2015-12-15 10:55:36 +00008176void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8177 Location out,
8178 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008179 DCHECK(kEmitCompilerReadBarrier);
8180
Roland Levillain7c1559a2015-12-15 10:55:36 +00008181 // Insert a slow path based read barrier *after* the GC root load.
8182 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008183 // Note that GC roots are not affected by heap poisoning, so we do
8184 // not need to do anything special for this here.
8185 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008186 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008187 AddSlowPath(slow_path);
8188
Roland Levillain0d5a2812015-11-13 10:07:31 +00008189 __ jmp(slow_path->GetEntryLabel());
8190 __ Bind(slow_path->GetExitLabel());
8191}
8192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008193void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008194 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008195 LOG(FATAL) << "Unreachable";
8196}
8197
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008198void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008199 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008200 LOG(FATAL) << "Unreachable";
8201}
8202
Mark Mendellfe57faa2015-09-18 09:26:15 -04008203// Simple implementation of packed switch - generate cascaded compare/jumps.
8204void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8205 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008206 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008207 locations->SetInAt(0, Location::RequiresRegister());
8208}
8209
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008210void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8211 int32_t lower_bound,
8212 uint32_t num_entries,
8213 HBasicBlock* switch_block,
8214 HBasicBlock* default_block) {
8215 // Figure out the correct compare values and jump conditions.
8216 // Handle the first compare/branch as a special case because it might
8217 // jump to the default case.
8218 DCHECK_GT(num_entries, 2u);
8219 Condition first_condition;
8220 uint32_t index;
8221 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8222 if (lower_bound != 0) {
8223 first_condition = kLess;
8224 __ cmpl(value_reg, Immediate(lower_bound));
8225 __ j(first_condition, codegen_->GetLabelOf(default_block));
8226 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008227
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008228 index = 1;
8229 } else {
8230 // Handle all the compare/jumps below.
8231 first_condition = kBelow;
8232 index = 0;
8233 }
8234
8235 // Handle the rest of the compare/jumps.
8236 for (; index + 1 < num_entries; index += 2) {
8237 int32_t compare_to_value = lower_bound + index + 1;
8238 __ cmpl(value_reg, Immediate(compare_to_value));
8239 // Jump to successors[index] if value < case_value[index].
8240 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8241 // Jump to successors[index + 1] if value == case_value[index + 1].
8242 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8243 }
8244
8245 if (index != num_entries) {
8246 // There are an odd number of entries. Handle the last one.
8247 DCHECK_EQ(index + 1, num_entries);
8248 __ cmpl(value_reg, Immediate(lower_bound + index));
8249 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008250 }
8251
8252 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008253 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8254 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008255 }
8256}
8257
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008258void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8259 int32_t lower_bound = switch_instr->GetStartValue();
8260 uint32_t num_entries = switch_instr->GetNumEntries();
8261 LocationSummary* locations = switch_instr->GetLocations();
8262 Register value_reg = locations->InAt(0).AsRegister<Register>();
8263
8264 GenPackedSwitchWithCompares(value_reg,
8265 lower_bound,
8266 num_entries,
8267 switch_instr->GetBlock(),
8268 switch_instr->GetDefaultBlock());
8269}
8270
Mark Mendell805b3b52015-09-18 14:10:29 -04008271void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8272 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008273 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008274 locations->SetInAt(0, Location::RequiresRegister());
8275
8276 // Constant area pointer.
8277 locations->SetInAt(1, Location::RequiresRegister());
8278
8279 // And the temporary we need.
8280 locations->AddTemp(Location::RequiresRegister());
8281}
8282
8283void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8284 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008285 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008286 LocationSummary* locations = switch_instr->GetLocations();
8287 Register value_reg = locations->InAt(0).AsRegister<Register>();
8288 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8289
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008290 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8291 GenPackedSwitchWithCompares(value_reg,
8292 lower_bound,
8293 num_entries,
8294 switch_instr->GetBlock(),
8295 default_block);
8296 return;
8297 }
8298
Mark Mendell805b3b52015-09-18 14:10:29 -04008299 // Optimizing has a jump area.
8300 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8301 Register constant_area = locations->InAt(1).AsRegister<Register>();
8302
8303 // Remove the bias, if needed.
8304 if (lower_bound != 0) {
8305 __ leal(temp_reg, Address(value_reg, -lower_bound));
8306 value_reg = temp_reg;
8307 }
8308
8309 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008310 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008311 __ cmpl(value_reg, Immediate(num_entries - 1));
8312 __ j(kAbove, codegen_->GetLabelOf(default_block));
8313
8314 // We are in the range of the table.
8315 // Load (target-constant_area) from the jump table, indexing by the value.
8316 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8317
8318 // Compute the actual target address by adding in constant_area.
8319 __ addl(temp_reg, constant_area);
8320
8321 // And jump.
8322 __ jmp(temp_reg);
8323}
8324
Mark Mendell0616ae02015-04-17 12:49:27 -04008325void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8326 HX86ComputeBaseMethodAddress* insn) {
8327 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008328 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008329 locations->SetOut(Location::RequiresRegister());
8330}
8331
8332void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8333 HX86ComputeBaseMethodAddress* insn) {
8334 LocationSummary* locations = insn->GetLocations();
8335 Register reg = locations->Out().AsRegister<Register>();
8336
8337 // Generate call to next instruction.
8338 Label next_instruction;
8339 __ call(&next_instruction);
8340 __ Bind(&next_instruction);
8341
8342 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008343 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008344
8345 // Grab the return address off the stack.
8346 __ popl(reg);
8347}
8348
8349void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8350 HX86LoadFromConstantTable* insn) {
8351 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008352 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008353
8354 locations->SetInAt(0, Location::RequiresRegister());
8355 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8356
8357 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008358 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008359 return;
8360 }
8361
8362 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008363 case DataType::Type::kFloat32:
8364 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008365 locations->SetOut(Location::RequiresFpuRegister());
8366 break;
8367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008368 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008369 locations->SetOut(Location::RequiresRegister());
8370 break;
8371
8372 default:
8373 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8374 }
8375}
8376
8377void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008378 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008379 return;
8380 }
8381
8382 LocationSummary* locations = insn->GetLocations();
8383 Location out = locations->Out();
8384 Register const_area = locations->InAt(0).AsRegister<Register>();
8385 HConstant *value = insn->GetConstant();
8386
8387 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008388 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008389 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008390 codegen_->LiteralFloatAddress(
8391 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008392 break;
8393
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008394 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008395 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008396 codegen_->LiteralDoubleAddress(
8397 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008398 break;
8399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008400 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008401 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008402 codegen_->LiteralInt32Address(
8403 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008404 break;
8405
8406 default:
8407 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8408 }
8409}
8410
Mark Mendell0616ae02015-04-17 12:49:27 -04008411/**
8412 * Class to handle late fixup of offsets into constant area.
8413 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008414class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008415 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008416 RIPFixup(CodeGeneratorX86& codegen,
8417 HX86ComputeBaseMethodAddress* base_method_address,
8418 size_t offset)
8419 : codegen_(&codegen),
8420 base_method_address_(base_method_address),
8421 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008422
8423 protected:
8424 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8425
8426 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008427 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008428
8429 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008430 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008431 // Patch the correct offset for the instruction. The place to patch is the
8432 // last 4 bytes of the instruction.
8433 // The value to patch is the distance from the offset in the constant area
8434 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008435 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008436 int32_t relative_position =
8437 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008438
8439 // Patch in the right value.
8440 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8441 }
8442
Mark Mendell0616ae02015-04-17 12:49:27 -04008443 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008444 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008445};
8446
Mark Mendell805b3b52015-09-18 14:10:29 -04008447/**
8448 * Class to handle late fixup of offsets to a jump table that will be created in the
8449 * constant area.
8450 */
8451class JumpTableRIPFixup : public RIPFixup {
8452 public:
8453 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008454 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8455 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008456
8457 void CreateJumpTable() {
8458 X86Assembler* assembler = codegen_->GetAssembler();
8459
8460 // Ensure that the reference to the jump table has the correct offset.
8461 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8462 SetOffset(offset_in_constant_table);
8463
8464 // The label values in the jump table are computed relative to the
8465 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008466 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008467
8468 // Populate the jump table with the correct values for the jump table.
8469 int32_t num_entries = switch_instr_->GetNumEntries();
8470 HBasicBlock* block = switch_instr_->GetBlock();
8471 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8472 // The value that we want is the target offset - the position of the table.
8473 for (int32_t i = 0; i < num_entries; i++) {
8474 HBasicBlock* b = successors[i];
8475 Label* l = codegen_->GetLabelOf(b);
8476 DCHECK(l->IsBound());
8477 int32_t offset_to_block = l->Position() - relative_offset;
8478 assembler->AppendInt32(offset_to_block);
8479 }
8480 }
8481
8482 private:
8483 const HX86PackedSwitch* switch_instr_;
8484};
8485
8486void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8487 // Generate the constant area if needed.
8488 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308489
Mark Mendell805b3b52015-09-18 14:10:29 -04008490 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8491 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8492 // byte values.
8493 assembler->Align(4, 0);
8494 constant_area_start_ = assembler->CodeSize();
8495
8496 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008497 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008498 jump_table->CreateJumpTable();
8499 }
8500
8501 // And now add the constant area to the generated code.
8502 assembler->AddConstantArea();
8503 }
8504
8505 // And finish up.
8506 CodeGenerator::Finalize(allocator);
8507}
8508
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008509Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8510 HX86ComputeBaseMethodAddress* method_base,
8511 Register reg) {
8512 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008513 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008514 return Address(reg, kDummy32BitOffset, fixup);
8515}
8516
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008517Address CodeGeneratorX86::LiteralFloatAddress(float v,
8518 HX86ComputeBaseMethodAddress* method_base,
8519 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008520 AssemblerFixup* fixup =
8521 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008522 return Address(reg, kDummy32BitOffset, fixup);
8523}
8524
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008525Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8526 HX86ComputeBaseMethodAddress* method_base,
8527 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008528 AssemblerFixup* fixup =
8529 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008530 return Address(reg, kDummy32BitOffset, fixup);
8531}
8532
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008533Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8534 HX86ComputeBaseMethodAddress* method_base,
8535 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008536 AssemblerFixup* fixup =
8537 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008538 return Address(reg, kDummy32BitOffset, fixup);
8539}
8540
Aart Bika19616e2016-02-01 18:57:58 -08008541void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8542 if (value == 0) {
8543 __ xorl(dest, dest);
8544 } else {
8545 __ movl(dest, Immediate(value));
8546 }
8547}
8548
8549void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8550 if (value == 0) {
8551 __ testl(dest, dest);
8552 } else {
8553 __ cmpl(dest, Immediate(value));
8554 }
8555}
8556
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008557void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8558 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008559 GenerateIntCompare(lhs_reg, rhs);
8560}
8561
8562void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008563 if (rhs.IsConstant()) {
8564 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008565 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008566 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008567 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008568 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008569 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008570 }
8571}
8572
8573Address CodeGeneratorX86::ArrayAddress(Register obj,
8574 Location index,
8575 ScaleFactor scale,
8576 uint32_t data_offset) {
8577 return index.IsConstant() ?
8578 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8579 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8580}
8581
Mark Mendell805b3b52015-09-18 14:10:29 -04008582Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8583 Register reg,
8584 Register value) {
8585 // Create a fixup to be used to create and address the jump table.
8586 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008587 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008588
8589 // We have to populate the jump tables.
8590 fixups_to_jump_tables_.push_back(table_fixup);
8591
8592 // We want a scaled address, as we are extracting the correct offset from the table.
8593 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8594}
8595
Andreas Gampe85b62f22015-09-09 13:15:38 -07008596// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008597void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008598 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008599 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008600 return;
8601 }
8602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008603 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008604
8605 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8606 if (target.Equals(return_loc)) {
8607 return;
8608 }
8609
8610 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8611 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008612 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008613 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008614 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8615 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008616 GetMoveResolver()->EmitNativeCode(&parallel_move);
8617 } else {
8618 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008619 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008620 parallel_move.AddMove(return_loc, target, type, nullptr);
8621 GetMoveResolver()->EmitNativeCode(&parallel_move);
8622 }
8623}
8624
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008625void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8626 const uint8_t* roots_data,
8627 const PatchInfo<Label>& info,
8628 uint64_t index_in_table) const {
8629 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8630 uintptr_t address =
8631 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008632 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008633 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8634 dchecked_integral_cast<uint32_t>(address);
8635}
8636
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008637void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8638 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008639 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008640 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008641 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008642 }
8643
8644 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008645 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008646 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008647 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008648 }
8649}
8650
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008651void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8652 ATTRIBUTE_UNUSED) {
8653 LOG(FATAL) << "Unreachable";
8654}
8655
8656void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8657 ATTRIBUTE_UNUSED) {
8658 LOG(FATAL) << "Unreachable";
8659}
8660
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308661bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8662 return codegen_->GetInstructionSetFeatures().HasAVX();
8663}
8664bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8665 return codegen_->GetInstructionSetFeatures().HasAVX2();
8666}
8667bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8668 return codegen_->GetInstructionSetFeatures().HasAVX();
8669}
8670bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8671 return codegen_->GetInstructionSetFeatures().HasAVX2();
8672}
8673
Roland Levillain4d027112015-07-01 15:41:14 +01008674#undef __
8675
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008676} // namespace x86
8677} // namespace art