blob: 595b31ec26bac5b9339fa584ecae71252ecebeae [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);
1083 __ movl(EAX, Address(ESP, kX86WordSize));
1084 }
1085 NearLabel overflow;
1086 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1087 Immediate(ArtMethod::MaxCounter()));
1088 __ j(kEqual, &overflow);
1089 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1090 Immediate(1));
1091 __ Bind(&overflow);
1092 if (!is_frame_entry) {
1093 __ popl(EAX);
1094 }
1095 }
1096
1097 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1098 ScopedObjectAccess soa(Thread::Current());
1099 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001100 if (info != nullptr) {
1101 uint32_t address = reinterpret_cast32<uint32_t>(info);
1102 NearLabel done;
1103 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001104 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001105 // Alignment
1106 __ subl(ESP, Immediate(8));
1107 __ cfi().AdjustCFAOffset(8);
1108 // We need a temporary. The stub also expects the method at bottom of stack.
1109 __ pushl(EAX);
1110 __ cfi().AdjustCFAOffset(4);
1111 __ movl(EAX, Immediate(address));
1112 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1113 Immediate(1));
1114 __ j(kCarryClear, &done);
1115 GenerateInvokeRuntime(
1116 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1117 __ Bind(&done);
1118 // We don't strictly require to restore EAX, but this makes the generated
1119 // code easier to reason about.
1120 __ popl(EAX);
1121 __ cfi().AdjustCFAOffset(-4);
1122 __ addl(ESP, Immediate(8));
1123 __ cfi().AdjustCFAOffset(-8);
1124 } 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();
1170 __ subl(ESP, Immediate(adjust));
1171 __ cfi().AdjustCFAOffset(adjust);
1172 // Save the current method if we need it. Note that we do not
1173 // do this in HCurrentMethod, as the instruction might have been removed
1174 // in the SSA graph.
1175 if (RequiresCurrentMethod()) {
1176 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1177 }
1178
1179 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1180 // Initialize should_deoptimize flag to 0.
1181 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001182 }
1183 }
1184
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001185 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186}
1187
1188void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001189 __ cfi().RememberState();
1190 if (!HasEmptyFrame()) {
1191 int adjust = GetFrameSize() - FrameEntrySpillSize();
1192 __ addl(ESP, Immediate(adjust));
1193 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001194
David Srbeckyc34dc932015-04-12 09:27:43 +01001195 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1196 Register reg = kCoreCalleeSaves[i];
1197 if (allocated_registers_.ContainsCoreRegister(reg)) {
1198 __ popl(reg);
1199 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1200 __ cfi().Restore(DWARFReg(reg));
1201 }
Mark Mendell5f874182015-03-04 15:42:45 -05001202 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001203 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001204 __ ret();
1205 __ cfi().RestoreState();
1206 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001207}
1208
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001209void CodeGeneratorX86::Bind(HBasicBlock* block) {
1210 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001211}
1212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001214 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001215 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001217 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001218 case DataType::Type::kInt8:
1219 case DataType::Type::kUint16:
1220 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001221 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001222 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001223 return Location::RegisterLocation(EAX);
1224
Aart Bik66c158e2018-01-31 12:55:04 -08001225 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001226 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001227 return Location::RegisterPairLocation(EAX, EDX);
1228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001229 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001230 return Location::NoLocation();
1231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232 case DataType::Type::kFloat64:
1233 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001234 return Location::FpuRegisterLocation(XMM0);
1235 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001236
1237 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001238}
1239
1240Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1241 return Location::RegisterLocation(kMethodRegisterArgument);
1242}
1243
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001244Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001245 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001246 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001248 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001249 case DataType::Type::kInt8:
1250 case DataType::Type::kUint16:
1251 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001252 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001253 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001254 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001255 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001256 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001257 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001258 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001259 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001260 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001262 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001263 uint32_t index = gp_index_;
1264 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001265 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001266 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1268 calling_convention.GetRegisterPairAt(index));
1269 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001270 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001271 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1272 }
1273 }
1274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001275 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001276 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001277 stack_index_++;
1278 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1279 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1280 } else {
1281 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1282 }
1283 }
1284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001285 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001286 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001287 stack_index_ += 2;
1288 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1289 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1290 } else {
1291 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001292 }
1293 }
1294
Aart Bik66c158e2018-01-31 12:55:04 -08001295 case DataType::Type::kUint32:
1296 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001297 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001298 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001299 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001300 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001301 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001302}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001303
Vladimir Marko86c87522020-05-11 16:55:55 +01001304Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1305 DCHECK_NE(type, DataType::Type::kReference);
1306
1307 Location location;
1308 if (DataType::Is64BitType(type)) {
1309 location = Location::DoubleStackSlot(stack_offset_);
1310 stack_offset_ += 2 * kFramePointerSize;
1311 } else {
1312 location = Location::StackSlot(stack_offset_);
1313 stack_offset_ += kFramePointerSize;
1314 }
1315 if (for_register_allocation_) {
1316 location = Location::Any();
1317 }
1318 return location;
1319}
1320
1321Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1322 // We perform conversion to the managed ABI return register after the call if needed.
1323 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1324 return dex_calling_convention.GetReturnLocation(type);
1325}
1326
1327Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1328 // Pass the method in the hidden argument EAX.
1329 return Location::RegisterLocation(EAX);
1330}
1331
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001332void CodeGeneratorX86::Move32(Location destination, Location source) {
1333 if (source.Equals(destination)) {
1334 return;
1335 }
1336 if (destination.IsRegister()) {
1337 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001339 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001340 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001341 } else {
1342 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001343 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001344 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001345 } else if (destination.IsFpuRegister()) {
1346 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001347 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001349 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001350 } else {
1351 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001353 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001355 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001356 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001358 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001360 } else if (source.IsConstant()) {
1361 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001362 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001363 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001364 } else {
1365 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001366 __ pushl(Address(ESP, source.GetStackIndex()));
1367 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001368 }
1369 }
1370}
1371
1372void CodeGeneratorX86::Move64(Location destination, Location source) {
1373 if (source.Equals(destination)) {
1374 return;
1375 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001376 if (destination.IsRegisterPair()) {
1377 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001378 EmitParallelMoves(
1379 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1380 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001381 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001382 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001383 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001384 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001385 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001386 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1387 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1388 __ psrlq(src_reg, Immediate(32));
1389 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001391 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001392 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001393 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1394 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1396 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001397 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001398 if (source.IsFpuRegister()) {
1399 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1400 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001401 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001402 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001403 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001404 // Create stack space for 2 elements.
1405 __ subl(ESP, Immediate(2 * elem_size));
Vladimir Marko86c87522020-05-11 16:55:55 +01001406 __ cfi().AdjustCFAOffset(2 * elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001407 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1408 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1409 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1410 // And remove the temporary stack space we allocated.
1411 __ addl(ESP, Immediate(2 * elem_size));
Vladimir Marko86c87522020-05-11 16:55:55 +01001412 __ cfi().AdjustCFAOffset(-(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001413 } else {
1414 LOG(FATAL) << "Unimplemented";
1415 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001416 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001417 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001418 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001419 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001420 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001421 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001422 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001424 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001425 } else if (source.IsConstant()) {
1426 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001427 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1428 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001429 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001430 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1431 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001432 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001433 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001434 EmitParallelMoves(
1435 Location::StackSlot(source.GetStackIndex()),
1436 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001437 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001438 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001439 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001440 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001441 }
1442 }
1443}
1444
Calin Juravle175dc732015-08-25 15:42:32 +01001445void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1446 DCHECK(location.IsRegister());
1447 __ movl(location.AsRegister<Register>(), Immediate(value));
1448}
1449
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001451 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001452 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1453 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1454 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001455 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001456 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001457 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001458 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001459}
1460
1461void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1462 if (location.IsRegister()) {
1463 locations->AddTemp(location);
1464 } else if (location.IsRegisterPair()) {
1465 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1466 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1467 } else {
1468 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1469 }
1470}
1471
David Brazdilfc6a86a2015-06-26 10:33:45 +00001472void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001473 if (successor->IsExitBlock()) {
1474 DCHECK(got->GetPrevious()->AlwaysThrows());
1475 return; // no code needed
1476 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001477
1478 HBasicBlock* block = got->GetBlock();
1479 HInstruction* previous = got->GetPrevious();
1480
1481 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001482 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001483 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001484 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1485 return;
1486 }
1487
1488 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1489 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1490 }
1491 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001492 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001493 }
1494}
1495
David Brazdilfc6a86a2015-06-26 10:33:45 +00001496void LocationsBuilderX86::VisitGoto(HGoto* got) {
1497 got->SetLocations(nullptr);
1498}
1499
1500void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1501 HandleGoto(got, got->GetSuccessor());
1502}
1503
1504void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1505 try_boundary->SetLocations(nullptr);
1506}
1507
1508void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1509 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1510 if (!successor->IsExitBlock()) {
1511 HandleGoto(try_boundary, successor);
1512 }
1513}
1514
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001515void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001516 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001517}
1518
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001519void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001520}
1521
Mark Mendell152408f2015-12-31 12:28:50 -05001522template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001523void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001524 LabelType* true_label,
1525 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001526 if (cond->IsFPConditionTrueIfNaN()) {
1527 __ j(kUnordered, true_label);
1528 } else if (cond->IsFPConditionFalseIfNaN()) {
1529 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001530 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001531 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001532}
1533
Mark Mendell152408f2015-12-31 12:28:50 -05001534template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001535void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001536 LabelType* true_label,
1537 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001538 LocationSummary* locations = cond->GetLocations();
1539 Location left = locations->InAt(0);
1540 Location right = locations->InAt(1);
1541 IfCondition if_cond = cond->GetCondition();
1542
Mark Mendellc4701932015-04-10 13:18:51 -04001543 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001544 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001545 IfCondition true_high_cond = if_cond;
1546 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001547 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001548
1549 // Set the conditions for the test, remembering that == needs to be
1550 // decided using the low words.
1551 switch (if_cond) {
1552 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001553 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001554 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001555 break;
1556 case kCondLT:
1557 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001558 break;
1559 case kCondLE:
1560 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001561 break;
1562 case kCondGT:
1563 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001564 break;
1565 case kCondGE:
1566 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001567 break;
Aart Bike9f37602015-10-09 11:15:55 -07001568 case kCondB:
1569 false_high_cond = kCondA;
1570 break;
1571 case kCondBE:
1572 true_high_cond = kCondB;
1573 break;
1574 case kCondA:
1575 false_high_cond = kCondB;
1576 break;
1577 case kCondAE:
1578 true_high_cond = kCondA;
1579 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001580 }
1581
1582 if (right.IsConstant()) {
1583 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001584 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001585 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001586
Aart Bika19616e2016-02-01 18:57:58 -08001587 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001588 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001589 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001590 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001591 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001592 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001593 __ j(X86Condition(true_high_cond), true_label);
1594 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001595 }
1596 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001597 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001598 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001599 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001600 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001601
1602 __ cmpl(left_high, right_high);
1603 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001604 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001605 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001606 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001607 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001608 __ j(X86Condition(true_high_cond), true_label);
1609 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001610 }
1611 // Must be equal high, so compare the lows.
1612 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001613 } else {
1614 DCHECK(right.IsDoubleStackSlot());
1615 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1616 if (if_cond == kCondNE) {
1617 __ j(X86Condition(true_high_cond), true_label);
1618 } else if (if_cond == kCondEQ) {
1619 __ j(X86Condition(false_high_cond), false_label);
1620 } else {
1621 __ j(X86Condition(true_high_cond), true_label);
1622 __ j(X86Condition(false_high_cond), false_label);
1623 }
1624 // Must be equal high, so compare the lows.
1625 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001626 }
1627 // The last comparison might be unsigned.
1628 __ j(final_condition, true_label);
1629}
1630
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001631void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1632 Location rhs,
1633 HInstruction* insn,
1634 bool is_double) {
1635 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1636 if (is_double) {
1637 if (rhs.IsFpuRegister()) {
1638 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1639 } else if (const_area != nullptr) {
1640 DCHECK(const_area->IsEmittedAtUseSite());
1641 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1642 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001643 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1644 const_area->GetBaseMethodAddress(),
1645 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001646 } else {
1647 DCHECK(rhs.IsDoubleStackSlot());
1648 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1649 }
1650 } else {
1651 if (rhs.IsFpuRegister()) {
1652 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1653 } else if (const_area != nullptr) {
1654 DCHECK(const_area->IsEmittedAtUseSite());
1655 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1656 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001657 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1658 const_area->GetBaseMethodAddress(),
1659 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001660 } else {
1661 DCHECK(rhs.IsStackSlot());
1662 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1663 }
1664 }
1665}
1666
Mark Mendell152408f2015-12-31 12:28:50 -05001667template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001668void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001669 LabelType* true_target_in,
1670 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001671 // Generated branching requires both targets to be explicit. If either of the
1672 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001673 LabelType fallthrough_target;
1674 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1675 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001676
Mark Mendellc4701932015-04-10 13:18:51 -04001677 LocationSummary* locations = condition->GetLocations();
1678 Location left = locations->InAt(0);
1679 Location right = locations->InAt(1);
1680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001682 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001684 GenerateLongComparesAndJumps(condition, true_target, false_target);
1685 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001687 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001688 GenerateFPJumps(condition, true_target, false_target);
1689 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001691 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001692 GenerateFPJumps(condition, true_target, false_target);
1693 break;
1694 default:
1695 LOG(FATAL) << "Unexpected compare type " << type;
1696 }
1697
David Brazdil0debae72015-11-12 18:37:00 +00001698 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001699 __ jmp(false_target);
1700 }
David Brazdil0debae72015-11-12 18:37:00 +00001701
1702 if (fallthrough_target.IsLinked()) {
1703 __ Bind(&fallthrough_target);
1704 }
Mark Mendellc4701932015-04-10 13:18:51 -04001705}
1706
David Brazdil0debae72015-11-12 18:37:00 +00001707static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1708 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1709 // are set only strictly before `branch`. We can't use the eflags on long/FP
1710 // conditions if they are materialized due to the complex branching.
1711 return cond->IsCondition() &&
1712 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001713 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1714 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001715}
1716
Mark Mendell152408f2015-12-31 12:28:50 -05001717template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001718void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001719 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001720 LabelType* true_target,
1721 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001722 HInstruction* cond = instruction->InputAt(condition_input_index);
1723
1724 if (true_target == nullptr && false_target == nullptr) {
1725 // Nothing to do. The code always falls through.
1726 return;
1727 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001728 // Constant condition, statically compared against "true" (integer value 1).
1729 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001730 if (true_target != nullptr) {
1731 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001732 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001733 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001734 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001735 if (false_target != nullptr) {
1736 __ jmp(false_target);
1737 }
1738 }
1739 return;
1740 }
1741
1742 // The following code generates these patterns:
1743 // (1) true_target == nullptr && false_target != nullptr
1744 // - opposite condition true => branch to false_target
1745 // (2) true_target != nullptr && false_target == nullptr
1746 // - condition true => branch to true_target
1747 // (3) true_target != nullptr && false_target != nullptr
1748 // - condition true => branch to true_target
1749 // - branch to false_target
1750 if (IsBooleanValueOrMaterializedCondition(cond)) {
1751 if (AreEflagsSetFrom(cond, instruction)) {
1752 if (true_target == nullptr) {
1753 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1754 } else {
1755 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1756 }
1757 } else {
1758 // Materialized condition, compare against 0.
1759 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1760 if (lhs.IsRegister()) {
1761 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1762 } else {
1763 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1764 }
1765 if (true_target == nullptr) {
1766 __ j(kEqual, false_target);
1767 } else {
1768 __ j(kNotEqual, true_target);
1769 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001770 }
1771 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001772 // Condition has not been materialized, use its inputs as the comparison and
1773 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001774 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001775
1776 // If this is a long or FP comparison that has been folded into
1777 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 DataType::Type type = condition->InputAt(0)->GetType();
1779 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001780 GenerateCompareTestAndBranch(condition, true_target, false_target);
1781 return;
1782 }
1783
1784 Location lhs = condition->GetLocations()->InAt(0);
1785 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001787 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001788 if (true_target == nullptr) {
1789 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1790 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001791 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001792 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001793 }
David Brazdil0debae72015-11-12 18:37:00 +00001794
1795 // If neither branch falls through (case 3), the conditional branch to `true_target`
1796 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1797 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001798 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799 }
1800}
1801
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001802void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001803 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001804 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001805 locations->SetInAt(0, Location::Any());
1806 }
1807}
1808
1809void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001810 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1811 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1812 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1813 nullptr : codegen_->GetLabelOf(true_successor);
1814 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1815 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001816 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001817}
1818
1819void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001820 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001821 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001822 InvokeRuntimeCallingConvention calling_convention;
1823 RegisterSet caller_saves = RegisterSet::Empty();
1824 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1825 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001826 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001827 locations->SetInAt(0, Location::Any());
1828 }
1829}
1830
1831void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001832 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001833 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001834 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001835 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001836 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001837}
1838
Mingyao Yang063fc772016-08-02 11:02:54 -07001839void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001840 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001841 LocationSummary(flag, LocationSummary::kNoCall);
1842 locations->SetOut(Location::RequiresRegister());
1843}
1844
1845void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1846 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1847 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1848}
1849
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001850static bool SelectCanUseCMOV(HSelect* select) {
1851 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001852 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001853 return false;
1854 }
1855
1856 // A FP condition doesn't generate the single CC that we need.
1857 // In 32 bit mode, a long condition doesn't generate a single CC either.
1858 HInstruction* condition = select->GetCondition();
1859 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 DataType::Type compare_type = condition->InputAt(0)->GetType();
1861 if (compare_type == DataType::Type::kInt64 ||
1862 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001863 return false;
1864 }
1865 }
1866
1867 // We can generate a CMOV for this Select.
1868 return true;
1869}
1870
David Brazdil74eb1b22015-12-14 11:44:01 +00001871void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001872 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001874 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001875 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001876 } else {
1877 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001878 if (SelectCanUseCMOV(select)) {
1879 if (select->InputAt(1)->IsConstant()) {
1880 // Cmov can't handle a constant value.
1881 locations->SetInAt(1, Location::RequiresRegister());
1882 } else {
1883 locations->SetInAt(1, Location::Any());
1884 }
1885 } else {
1886 locations->SetInAt(1, Location::Any());
1887 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001888 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001889 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1890 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001891 }
1892 locations->SetOut(Location::SameAsFirstInput());
1893}
1894
1895void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1896 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001897 DCHECK(locations->InAt(0).Equals(locations->Out()));
1898 if (SelectCanUseCMOV(select)) {
1899 // If both the condition and the source types are integer, we can generate
1900 // a CMOV to implement Select.
1901
1902 HInstruction* select_condition = select->GetCondition();
1903 Condition cond = kNotEqual;
1904
1905 // Figure out how to test the 'condition'.
1906 if (select_condition->IsCondition()) {
1907 HCondition* condition = select_condition->AsCondition();
1908 if (!condition->IsEmittedAtUseSite()) {
1909 // This was a previously materialized condition.
1910 // Can we use the existing condition code?
1911 if (AreEflagsSetFrom(condition, select)) {
1912 // Materialization was the previous instruction. Condition codes are right.
1913 cond = X86Condition(condition->GetCondition());
1914 } else {
1915 // No, we have to recreate the condition code.
1916 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1917 __ testl(cond_reg, cond_reg);
1918 }
1919 } else {
1920 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1922 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001923 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001924 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001925 cond = X86Condition(condition->GetCondition());
1926 }
1927 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001928 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001929 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1930 __ testl(cond_reg, cond_reg);
1931 }
1932
1933 // If the condition is true, overwrite the output, which already contains false.
1934 Location false_loc = locations->InAt(0);
1935 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001936 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001937 // 64 bit conditional move.
1938 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1939 Register false_low = false_loc.AsRegisterPairLow<Register>();
1940 if (true_loc.IsRegisterPair()) {
1941 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1942 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1943 } else {
1944 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1945 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1946 }
1947 } else {
1948 // 32 bit conditional move.
1949 Register false_reg = false_loc.AsRegister<Register>();
1950 if (true_loc.IsRegister()) {
1951 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1952 } else {
1953 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1954 }
1955 }
1956 } else {
1957 NearLabel false_target;
1958 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001959 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001960 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1961 __ Bind(&false_target);
1962 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001963}
1964
David Srbecky0cf44932015-12-09 14:09:59 +00001965void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001966 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001967}
1968
David Srbeckyd28f4a02016-03-14 17:14:24 +00001969void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1970 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001971}
1972
1973void CodeGeneratorX86::GenerateNop() {
1974 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001975}
1976
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001979 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001980 // Handle the long/FP comparisons made in instruction simplification.
1981 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001983 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001984 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001985 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001986 locations->SetOut(Location::RequiresRegister());
1987 }
1988 break;
1989 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 case DataType::Type::kFloat32:
1991 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001992 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001993 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1994 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1995 } else if (cond->InputAt(1)->IsConstant()) {
1996 locations->SetInAt(1, Location::RequiresFpuRegister());
1997 } else {
1998 locations->SetInAt(1, Location::Any());
1999 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002000 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002001 locations->SetOut(Location::RequiresRegister());
2002 }
2003 break;
2004 }
2005 default:
2006 locations->SetInAt(0, Location::RequiresRegister());
2007 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002008 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002009 // We need a byte register.
2010 locations->SetOut(Location::RegisterLocation(ECX));
2011 }
2012 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002013 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002014}
2015
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002017 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002018 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002019 }
Mark Mendellc4701932015-04-10 13:18:51 -04002020
2021 LocationSummary* locations = cond->GetLocations();
2022 Location lhs = locations->InAt(0);
2023 Location rhs = locations->InAt(1);
2024 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002025 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002026
2027 switch (cond->InputAt(0)->GetType()) {
2028 default: {
2029 // Integer case.
2030
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002031 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002032 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002033 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002034 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002035 return;
2036 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002038 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2039 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002041 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002042 GenerateFPJumps(cond, &true_label, &false_label);
2043 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002045 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002046 GenerateFPJumps(cond, &true_label, &false_label);
2047 break;
2048 }
2049
2050 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002051 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002052
Roland Levillain4fa13f62015-07-06 18:11:54 +01002053 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002054 __ Bind(&false_label);
2055 __ xorl(reg, reg);
2056 __ jmp(&done_label);
2057
Roland Levillain4fa13f62015-07-06 18:11:54 +01002058 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002059 __ Bind(&true_label);
2060 __ movl(reg, Immediate(1));
2061 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002062}
2063
2064void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002066}
2067
2068void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002069 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002070}
2071
2072void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002073 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002074}
2075
2076void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002077 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002078}
2079
2080void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002081 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002082}
2083
2084void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002085 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002086}
2087
2088void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002089 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002090}
2091
2092void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002094}
2095
2096void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002097 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002098}
2099
2100void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002101 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002102}
2103
2104void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002105 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002106}
2107
2108void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002109 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002110}
2111
Aart Bike9f37602015-10-09 11:15:55 -07002112void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002114}
2115
2116void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002118}
2119
2120void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002122}
2123
2124void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002125 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002126}
2127
2128void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002129 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002130}
2131
2132void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002133 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002134}
2135
2136void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002137 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002138}
2139
2140void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002141 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002142}
2143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002145 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002146 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002147 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148}
2149
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002150void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002151 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152}
2153
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002154void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2155 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002156 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002157 locations->SetOut(Location::ConstantLocation(constant));
2158}
2159
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002160void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002162}
2163
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002164void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002165 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002166 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002167 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168}
2169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002170void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171 // Will be generated at use site.
2172}
2173
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002174void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2175 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002176 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002177 locations->SetOut(Location::ConstantLocation(constant));
2178}
2179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002180void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181 // Will be generated at use site.
2182}
2183
2184void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002186 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002187 locations->SetOut(Location::ConstantLocation(constant));
2188}
2189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002190void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002191 // Will be generated at use site.
2192}
2193
Igor Murashkind01745e2017-04-05 16:40:31 -07002194void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2195 constructor_fence->SetLocations(nullptr);
2196}
2197
2198void InstructionCodeGeneratorX86::VisitConstructorFence(
2199 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2200 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2201}
2202
Calin Juravle27df7582015-04-17 19:12:31 +01002203void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2204 memory_barrier->SetLocations(nullptr);
2205}
2206
2207void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002208 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002209}
2210
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002211void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002212 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002213}
2214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002215void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002216 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002217}
2218
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002219void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002220 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002221 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002222 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002223 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002225 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002226 case DataType::Type::kInt8:
2227 case DataType::Type::kUint16:
2228 case DataType::Type::kInt16:
2229 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002230 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002231 break;
2232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002235 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002236 break;
2237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 case DataType::Type::kFloat32:
2239 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002240 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002241 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002242 break;
2243
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002244 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002246 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002247}
2248
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002249void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002250 switch (ret->InputAt(0)->GetType()) {
2251 case DataType::Type::kReference:
2252 case DataType::Type::kBool:
2253 case DataType::Type::kUint8:
2254 case DataType::Type::kInt8:
2255 case DataType::Type::kUint16:
2256 case DataType::Type::kInt16:
2257 case DataType::Type::kInt32:
2258 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2259 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002260
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002261 case DataType::Type::kInt64:
2262 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2263 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2264 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002265
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002266 case DataType::Type::kFloat32:
2267 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2268 if (GetGraph()->IsCompilingOsr()) {
2269 // To simplify callers of an OSR method, we put the return value in both
2270 // floating point and core registers.
2271 __ movd(EAX, XMM0);
2272 }
2273 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002274
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002275 case DataType::Type::kFloat64:
2276 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2277 if (GetGraph()->IsCompilingOsr()) {
2278 // To simplify callers of an OSR method, we put the return value in both
2279 // floating point and core registers.
2280 __ movd(EAX, XMM0);
2281 // Use XMM1 as temporary register to not clobber XMM0.
2282 __ movaps(XMM1, XMM0);
2283 __ psrlq(XMM1, Immediate(32));
2284 __ movd(EDX, XMM1);
2285 }
2286 break;
2287
2288 default:
2289 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002291 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002292}
2293
Calin Juravle175dc732015-08-25 15:42:32 +01002294void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2295 // The trampoline uses the same calling convention as dex calling conventions,
2296 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2297 // the method_idx.
2298 HandleInvoke(invoke);
2299}
2300
2301void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2302 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2303}
2304
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002305void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002306 // Explicit clinit checks triggered by static invokes must have been pruned by
2307 // art::PrepareForRegisterAllocation.
2308 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002309
Mark Mendellfb8d2792015-03-31 22:16:59 -04002310 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002311 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002312 if (invoke->GetLocations()->CanCall() &&
2313 invoke->HasPcRelativeMethodLoadKind() &&
2314 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002315 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002316 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002317 return;
2318 }
2319
Vladimir Marko86c87522020-05-11 16:55:55 +01002320 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2321 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2322 /*for_register_allocation=*/ true);
2323 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2324 } else {
2325 HandleInvoke(invoke);
2326 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002327
Vladimir Marko86c87522020-05-11 16:55:55 +01002328 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002329 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002330 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002331 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002332}
2333
Mark Mendell09ed1a32015-03-25 08:30:06 -04002334static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2335 if (invoke->GetLocations()->Intrinsified()) {
2336 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2337 intrinsic.Dispatch(invoke);
2338 return true;
2339 }
2340 return false;
2341}
2342
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002343void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002344 // Explicit clinit checks triggered by static invokes must have been pruned by
2345 // art::PrepareForRegisterAllocation.
2346 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002347
Mark Mendell09ed1a32015-03-25 08:30:06 -04002348 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2349 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002350 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002351
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002352 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002353 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002354 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002355}
2356
2357void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002358 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2359 if (intrinsic.TryDispatch(invoke)) {
2360 return;
2361 }
2362
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002363 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002364
2365 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002366 // Add one temporary for inline cache update.
2367 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2368 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002369}
2370
2371void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002372 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002373 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002374}
2375
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002377 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2378 return;
2379 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002380
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002381 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002382 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002383}
2384
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002386 // This call to HandleInvoke allocates a temporary (core) register
2387 // which is also used to transfer the hidden argument from FP to
2388 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002389 HandleInvoke(invoke);
2390 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002391 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002392
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002393 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002394 // Add one temporary for inline cache update.
2395 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2396 }
2397}
2398
2399void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2400 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002401 // We know the destination of an intrinsic, so no need to record inline
2402 // caches (also the intrinsic location builder doesn't request an additional
2403 // temporary).
2404 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002405 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002406 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002407 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2408 ScopedObjectAccess soa(Thread::Current());
2409 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002410 if (info != nullptr) {
2411 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2412 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2413 if (kIsDebugBuild) {
2414 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2415 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2416 }
2417 Register temp = EBP;
2418 NearLabel done;
2419 __ movl(temp, Immediate(address));
2420 // Fast path for a monomorphic cache.
2421 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2422 __ j(kEqual, &done);
2423 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2424 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002425 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002426 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002427}
2428
2429void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2430 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002431 LocationSummary* locations = invoke->GetLocations();
2432 Register temp = locations->GetTemp(0).AsRegister<Register>();
2433 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434 Location receiver = locations->InAt(0);
2435 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2436
Roland Levillain0d5a2812015-11-13 10:07:31 +00002437 // Set the hidden argument. This is safe to do this here, as XMM7
2438 // won't be modified thereafter, before the `call` instruction.
2439 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002440 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002441 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002442
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002443 if (receiver.IsStackSlot()) {
2444 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002445 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002446 __ movl(temp, Address(temp, class_offset));
2447 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002448 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450 }
Roland Levillain4d027112015-07-01 15:41:14 +01002451 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002452 // Instead of simply (possibly) unpoisoning `temp` here, we should
2453 // emit a read barrier for the previous class reference load.
2454 // However this is not required in practice, as this is an
2455 // intermediate/temporary reference and because the current
2456 // concurrent copying collector keeps the from-space memory
2457 // intact/accessible until the end of the marking phase (the
2458 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002459 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002460
2461 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2462
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002463 // temp = temp->GetAddressOfIMT()
2464 __ movl(temp,
2465 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002466 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002467 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002468 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002469 __ movl(temp, Address(temp, method_offset));
2470 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002471 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002472 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002473
2474 DCHECK(!codegen_->IsLeafMethod());
2475 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2476}
2477
Orion Hodsonac141392017-01-13 11:53:47 +00002478void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2479 HandleInvoke(invoke);
2480}
2481
2482void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2483 codegen_->GenerateInvokePolymorphicCall(invoke);
2484}
2485
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002486void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2487 HandleInvoke(invoke);
2488}
2489
2490void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2491 codegen_->GenerateInvokeCustomCall(invoke);
2492}
2493
Roland Levillain88cb1752014-10-20 16:36:47 +01002494void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2495 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002496 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002497 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 case DataType::Type::kInt32:
2499 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002500 locations->SetInAt(0, Location::RequiresRegister());
2501 locations->SetOut(Location::SameAsFirstInput());
2502 break;
2503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002505 locations->SetInAt(0, Location::RequiresFpuRegister());
2506 locations->SetOut(Location::SameAsFirstInput());
2507 locations->AddTemp(Location::RequiresRegister());
2508 locations->AddTemp(Location::RequiresFpuRegister());
2509 break;
2510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002512 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002513 locations->SetOut(Location::SameAsFirstInput());
2514 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002515 break;
2516
2517 default:
2518 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2519 }
2520}
2521
2522void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2523 LocationSummary* locations = neg->GetLocations();
2524 Location out = locations->Out();
2525 Location in = locations->InAt(0);
2526 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002528 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002529 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002531 break;
2532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002534 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002535 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002536 __ negl(out.AsRegisterPairLow<Register>());
2537 // Negation is similar to subtraction from zero. The least
2538 // significant byte triggers a borrow when it is different from
2539 // zero; to take it into account, add 1 to the most significant
2540 // byte if the carry flag (CF) is set to 1 after the first NEGL
2541 // operation.
2542 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2543 __ negl(out.AsRegisterPairHigh<Register>());
2544 break;
2545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002547 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002548 Register constant = locations->GetTemp(0).AsRegister<Register>();
2549 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002550 // Implement float negation with an exclusive or with value
2551 // 0x80000000 (mask for bit 31, representing the sign of a
2552 // single-precision floating-point number).
2553 __ movl(constant, Immediate(INT32_C(0x80000000)));
2554 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002555 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002556 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002557 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002560 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002561 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002562 // Implement double negation with an exclusive or with value
2563 // 0x8000000000000000 (mask for bit 63, representing the sign of
2564 // a double-precision floating-point number).
2565 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002566 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002567 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002568 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002569
2570 default:
2571 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2572 }
2573}
2574
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002575void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2576 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002577 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002579 locations->SetInAt(0, Location::RequiresFpuRegister());
2580 locations->SetInAt(1, Location::RequiresRegister());
2581 locations->SetOut(Location::SameAsFirstInput());
2582 locations->AddTemp(Location::RequiresFpuRegister());
2583}
2584
2585void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2586 LocationSummary* locations = neg->GetLocations();
2587 Location out = locations->Out();
2588 DCHECK(locations->InAt(0).Equals(out));
2589
2590 Register constant_area = locations->InAt(1).AsRegister<Register>();
2591 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002593 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2594 neg->GetBaseMethodAddress(),
2595 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002596 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2597 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002598 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2599 neg->GetBaseMethodAddress(),
2600 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002601 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2602 }
2603}
2604
Roland Levillaindff1f282014-11-05 14:15:05 +00002605void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002606 DataType::Type result_type = conversion->GetResultType();
2607 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002608 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2609 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002610
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002611 // The float-to-long and double-to-long type conversions rely on a
2612 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002613 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2615 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002616 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002617 : LocationSummary::kNoCall;
2618 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002619 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002620
Roland Levillaindff1f282014-11-05 14:15:05 +00002621 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002622 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002623 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002624 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002625 case DataType::Type::kUint8:
2626 case DataType::Type::kInt8:
2627 case DataType::Type::kUint16:
2628 case DataType::Type::kInt16:
2629 case DataType::Type::kInt32:
2630 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2631 // Make the output overlap to please the register allocator. This greatly simplifies
2632 // the validation of the linear scan implementation
2633 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2634 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002636 HInstruction* input = conversion->InputAt(0);
2637 Location input_location = input->IsConstant()
2638 ? Location::ConstantLocation(input->AsConstant())
2639 : Location::RegisterPairLocation(EAX, EDX);
2640 locations->SetInAt(0, input_location);
2641 // Make the output overlap to please the register allocator. This greatly simplifies
2642 // the validation of the linear scan implementation
2643 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2644 break;
2645 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002646
2647 default:
2648 LOG(FATAL) << "Unexpected type conversion from " << input_type
2649 << " to " << result_type;
2650 }
2651 break;
2652
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002655 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2656 locations->SetInAt(0, Location::Any());
2657 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002658 break;
2659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002661 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002663 locations->SetInAt(0, Location::Any());
2664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002668 locations->SetInAt(0, Location::RequiresFpuRegister());
2669 locations->SetOut(Location::RequiresRegister());
2670 locations->AddTemp(Location::RequiresFpuRegister());
2671 break;
2672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002674 locations->SetInAt(0, Location::RequiresFpuRegister());
2675 locations->SetOut(Location::RequiresRegister());
2676 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002677 break;
2678
2679 default:
2680 LOG(FATAL) << "Unexpected type conversion from " << input_type
2681 << " to " << result_type;
2682 }
2683 break;
2684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002686 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002688 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002690 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 case DataType::Type::kInt16:
2692 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 locations->SetInAt(0, Location::RegisterLocation(EAX));
2694 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2695 break;
2696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 case DataType::Type::kFloat32:
2698 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002699 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002700 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2701 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2702
Vladimir Marko949c91f2015-01-27 10:48:44 +00002703 // The runtime helper puts the result in EAX, EDX.
2704 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002705 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002706 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002707
2708 default:
2709 LOG(FATAL) << "Unexpected type conversion from " << input_type
2710 << " to " << result_type;
2711 }
2712 break;
2713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002715 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002717 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002719 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 case DataType::Type::kInt16:
2721 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002722 locations->SetInAt(0, Location::RequiresRegister());
2723 locations->SetOut(Location::RequiresFpuRegister());
2724 break;
2725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002727 locations->SetInAt(0, Location::Any());
2728 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002729 break;
2730
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002732 locations->SetInAt(0, Location::RequiresFpuRegister());
2733 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002734 break;
2735
2736 default:
2737 LOG(FATAL) << "Unexpected type conversion from " << input_type
2738 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002739 }
Roland Levillaincff13742014-11-17 14:32:17 +00002740 break;
2741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002743 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002745 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002747 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kInt16:
2749 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002750 locations->SetInAt(0, Location::RequiresRegister());
2751 locations->SetOut(Location::RequiresFpuRegister());
2752 break;
2753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002755 locations->SetInAt(0, Location::Any());
2756 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002757 break;
2758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002760 locations->SetInAt(0, Location::RequiresFpuRegister());
2761 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002768 break;
2769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774}
2775
2776void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2777 LocationSummary* locations = conversion->GetLocations();
2778 Location out = locations->Out();
2779 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 DataType::Type result_type = conversion->GetResultType();
2781 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002782 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2783 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002785 case DataType::Type::kUint8:
2786 switch (input_type) {
2787 case DataType::Type::kInt8:
2788 case DataType::Type::kUint16:
2789 case DataType::Type::kInt16:
2790 case DataType::Type::kInt32:
2791 if (in.IsRegister()) {
2792 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2793 } else {
2794 DCHECK(in.GetConstant()->IsIntConstant());
2795 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2796 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2797 }
2798 break;
2799 case DataType::Type::kInt64:
2800 if (in.IsRegisterPair()) {
2801 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2802 } else {
2803 DCHECK(in.GetConstant()->IsLongConstant());
2804 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2805 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2806 }
2807 break;
2808
2809 default:
2810 LOG(FATAL) << "Unexpected type conversion from " << input_type
2811 << " to " << result_type;
2812 }
2813 break;
2814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002816 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002817 case DataType::Type::kUint8:
2818 case DataType::Type::kUint16:
2819 case DataType::Type::kInt16:
2820 case DataType::Type::kInt32:
2821 if (in.IsRegister()) {
2822 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2823 } else {
2824 DCHECK(in.GetConstant()->IsIntConstant());
2825 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2826 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2827 }
2828 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002830 if (in.IsRegisterPair()) {
2831 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2832 } else {
2833 DCHECK(in.GetConstant()->IsLongConstant());
2834 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2835 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2836 }
2837 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002838
2839 default:
2840 LOG(FATAL) << "Unexpected type conversion from " << input_type
2841 << " to " << result_type;
2842 }
2843 break;
2844
2845 case DataType::Type::kUint16:
2846 switch (input_type) {
2847 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 case DataType::Type::kInt16:
2849 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002850 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002851 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2852 } else if (in.IsStackSlot()) {
2853 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002854 } else {
2855 DCHECK(in.GetConstant()->IsIntConstant());
2856 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002857 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2858 }
2859 break;
2860 case DataType::Type::kInt64:
2861 if (in.IsRegisterPair()) {
2862 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2863 } else if (in.IsDoubleStackSlot()) {
2864 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2865 } else {
2866 DCHECK(in.GetConstant()->IsLongConstant());
2867 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2868 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002869 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002870 break;
2871
2872 default:
2873 LOG(FATAL) << "Unexpected type conversion from " << input_type
2874 << " to " << result_type;
2875 }
2876 break;
2877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002879 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002881 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002882 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002884 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002885 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002886 } else {
2887 DCHECK(in.GetConstant()->IsIntConstant());
2888 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002889 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002890 }
2891 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002892 case DataType::Type::kInt64:
2893 if (in.IsRegisterPair()) {
2894 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2895 } else if (in.IsDoubleStackSlot()) {
2896 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2897 } else {
2898 DCHECK(in.GetConstant()->IsLongConstant());
2899 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2900 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2901 }
2902 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002903
2904 default:
2905 LOG(FATAL) << "Unexpected type conversion from " << input_type
2906 << " to " << result_type;
2907 }
2908 break;
2909
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002910 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002911 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002913 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002914 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002915 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002916 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002917 } else {
2918 DCHECK(in.IsConstant());
2919 DCHECK(in.GetConstant()->IsLongConstant());
2920 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002922 }
2923 break;
2924
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002926 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2927 Register output = out.AsRegister<Register>();
2928 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002929 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002930
2931 __ movl(output, Immediate(kPrimIntMax));
2932 // temp = int-to-float(output)
2933 __ cvtsi2ss(temp, output);
2934 // if input >= temp goto done
2935 __ comiss(input, temp);
2936 __ j(kAboveEqual, &done);
2937 // if input == NaN goto nan
2938 __ j(kUnordered, &nan);
2939 // output = float-to-int-truncate(input)
2940 __ cvttss2si(output, input);
2941 __ jmp(&done);
2942 __ Bind(&nan);
2943 // output = 0
2944 __ xorl(output, output);
2945 __ Bind(&done);
2946 break;
2947 }
2948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002949 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002950 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2951 Register output = out.AsRegister<Register>();
2952 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002953 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002954
2955 __ movl(output, Immediate(kPrimIntMax));
2956 // temp = int-to-double(output)
2957 __ cvtsi2sd(temp, output);
2958 // if input >= temp goto done
2959 __ comisd(input, temp);
2960 __ j(kAboveEqual, &done);
2961 // if input == NaN goto nan
2962 __ j(kUnordered, &nan);
2963 // output = double-to-int-truncate(input)
2964 __ cvttsd2si(output, input);
2965 __ jmp(&done);
2966 __ Bind(&nan);
2967 // output = 0
2968 __ xorl(output, output);
2969 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002970 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002971 }
Roland Levillain946e1432014-11-11 17:35:19 +00002972
2973 default:
2974 LOG(FATAL) << "Unexpected type conversion from " << input_type
2975 << " to " << result_type;
2976 }
2977 break;
2978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002979 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002980 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002981 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002982 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002984 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 case DataType::Type::kInt16:
2986 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002987 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2988 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002989 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002990 __ cdq();
2991 break;
2992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002994 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002995 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002996 break;
2997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002999 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003000 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003001 break;
3002
3003 default:
3004 LOG(FATAL) << "Unexpected type conversion from " << input_type
3005 << " to " << result_type;
3006 }
3007 break;
3008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003010 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003011 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003012 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003013 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003014 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 case DataType::Type::kInt16:
3016 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003018 break;
3019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003021 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003022
Roland Levillain232ade02015-04-20 15:14:36 +01003023 // Create stack space for the call to
3024 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3025 // TODO: enhance register allocator to ask for stack temporaries.
3026 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01003028 __ subl(ESP, Immediate(adjustment));
Vladimir Marko86c87522020-05-11 16:55:55 +01003029 __ cfi().AdjustCFAOffset(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003030 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003031
Roland Levillain232ade02015-04-20 15:14:36 +01003032 // Load the value to the FP stack, using temporaries if needed.
3033 PushOntoFPStack(in, 0, adjustment, false, true);
3034
3035 if (out.IsStackSlot()) {
3036 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3037 } else {
3038 __ fstps(Address(ESP, 0));
3039 Location stack_temp = Location::StackSlot(0);
3040 codegen_->Move32(out, stack_temp);
3041 }
3042
3043 // Remove the temporary stack space we allocated.
3044 if (adjustment != 0) {
3045 __ addl(ESP, Immediate(adjustment));
Vladimir Marko86c87522020-05-11 16:55:55 +01003046 __ cfi().AdjustCFAOffset(-adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003047 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003048 break;
3049 }
3050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003052 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003053 break;
3054
3055 default:
3056 LOG(FATAL) << "Unexpected type conversion from " << input_type
3057 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003058 }
Roland Levillaincff13742014-11-17 14:32:17 +00003059 break;
3060
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003062 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003063 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003064 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003065 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003066 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 case DataType::Type::kInt16:
3068 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003070 break;
3071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003073 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003074
Roland Levillain232ade02015-04-20 15:14:36 +01003075 // Create stack space for the call to
3076 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3077 // TODO: enhance register allocator to ask for stack temporaries.
3078 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01003080 __ subl(ESP, Immediate(adjustment));
Vladimir Marko86c87522020-05-11 16:55:55 +01003081 __ cfi().AdjustCFAOffset(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003082 }
3083
3084 // Load the value to the FP stack, using temporaries if needed.
3085 PushOntoFPStack(in, 0, adjustment, false, true);
3086
3087 if (out.IsDoubleStackSlot()) {
3088 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3089 } else {
3090 __ fstpl(Address(ESP, 0));
3091 Location stack_temp = Location::DoubleStackSlot(0);
3092 codegen_->Move64(out, stack_temp);
3093 }
3094
3095 // Remove the temporary stack space we allocated.
3096 if (adjustment != 0) {
3097 __ addl(ESP, Immediate(adjustment));
Vladimir Marko86c87522020-05-11 16:55:55 +01003098 __ cfi().AdjustCFAOffset(-adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003099 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003100 break;
3101 }
3102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003103 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003104 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003105 break;
3106
3107 default:
3108 LOG(FATAL) << "Unexpected type conversion from " << input_type
3109 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003110 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003111 break;
3112
3113 default:
3114 LOG(FATAL) << "Unexpected type conversion from " << input_type
3115 << " to " << result_type;
3116 }
3117}
3118
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003119void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003121 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003122 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003124 locations->SetInAt(0, Location::RequiresRegister());
3125 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3126 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3127 break;
3128 }
3129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003130 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003131 locations->SetInAt(0, Location::RequiresRegister());
3132 locations->SetInAt(1, Location::Any());
3133 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003134 break;
3135 }
3136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003137 case DataType::Type::kFloat32:
3138 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003139 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003140 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3141 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003142 } else if (add->InputAt(1)->IsConstant()) {
3143 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003144 } else {
3145 locations->SetInAt(1, Location::Any());
3146 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003147 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003148 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003149 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003150
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003151 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003152 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003153 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003154 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003155}
3156
3157void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3158 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003159 Location first = locations->InAt(0);
3160 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003161 Location out = locations->Out();
3162
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003163 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003165 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003166 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3167 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003168 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3169 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003170 } else {
3171 __ leal(out.AsRegister<Register>(), Address(
3172 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3173 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003174 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003175 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3176 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3177 __ addl(out.AsRegister<Register>(), Immediate(value));
3178 } else {
3179 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3180 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003181 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003182 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003183 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003184 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003185 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003186 }
3187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003189 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003190 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3191 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003192 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003193 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3194 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003195 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003196 } else {
3197 DCHECK(second.IsConstant()) << second;
3198 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3199 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3200 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003201 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003202 break;
3203 }
3204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003206 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003208 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3209 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003210 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003211 __ addss(first.AsFpuRegister<XmmRegister>(),
3212 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003213 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3214 const_area->GetBaseMethodAddress(),
3215 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003216 } else {
3217 DCHECK(second.IsStackSlot());
3218 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003219 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003220 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003221 }
3222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003223 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003224 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003226 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3227 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003228 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003229 __ addsd(first.AsFpuRegister<XmmRegister>(),
3230 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003231 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3232 const_area->GetBaseMethodAddress(),
3233 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003234 } else {
3235 DCHECK(second.IsDoubleStackSlot());
3236 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003237 }
3238 break;
3239 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003240
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003241 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003242 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003243 }
3244}
3245
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003246void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003247 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003248 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003249 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003250 case DataType::Type::kInt32:
3251 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003252 locations->SetInAt(0, Location::RequiresRegister());
3253 locations->SetInAt(1, Location::Any());
3254 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003255 break;
3256 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003257 case DataType::Type::kFloat32:
3258 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003259 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003260 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3261 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003262 } else if (sub->InputAt(1)->IsConstant()) {
3263 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003264 } else {
3265 locations->SetInAt(1, Location::Any());
3266 }
Calin Juravle11351682014-10-23 15:38:15 +01003267 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003268 break;
Calin Juravle11351682014-10-23 15:38:15 +01003269 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003270
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003271 default:
Calin Juravle11351682014-10-23 15:38:15 +01003272 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003273 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003274}
3275
3276void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3277 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003278 Location first = locations->InAt(0);
3279 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003280 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003281 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003283 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003284 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003285 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003286 __ subl(first.AsRegister<Register>(),
3287 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003289 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003290 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003291 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003292 }
3293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003295 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003296 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3297 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003298 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003299 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003300 __ sbbl(first.AsRegisterPairHigh<Register>(),
3301 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003302 } else {
3303 DCHECK(second.IsConstant()) << second;
3304 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3305 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3306 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003307 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003308 break;
3309 }
3310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003311 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003312 if (second.IsFpuRegister()) {
3313 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3314 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3315 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003316 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003317 __ subss(first.AsFpuRegister<XmmRegister>(),
3318 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003319 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3320 const_area->GetBaseMethodAddress(),
3321 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003322 } else {
3323 DCHECK(second.IsStackSlot());
3324 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3325 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003326 break;
Calin Juravle11351682014-10-23 15:38:15 +01003327 }
3328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003330 if (second.IsFpuRegister()) {
3331 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3332 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3333 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003334 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003335 __ subsd(first.AsFpuRegister<XmmRegister>(),
3336 codegen_->LiteralDoubleAddress(
3337 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003338 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003339 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3340 } else {
3341 DCHECK(second.IsDoubleStackSlot());
3342 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3343 }
Calin Juravle11351682014-10-23 15:38:15 +01003344 break;
3345 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003346
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003347 default:
Calin Juravle11351682014-10-23 15:38:15 +01003348 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003349 }
3350}
3351
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352void LocationsBuilderX86::VisitMul(HMul* mul) {
3353 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003354 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003356 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003357 locations->SetInAt(0, Location::RequiresRegister());
3358 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003359 if (mul->InputAt(1)->IsIntConstant()) {
3360 // Can use 3 operand multiply.
3361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3362 } else {
3363 locations->SetOut(Location::SameAsFirstInput());
3364 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003365 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003366 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003367 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003368 locations->SetInAt(1, Location::Any());
3369 locations->SetOut(Location::SameAsFirstInput());
3370 // Needed for imul on 32bits with 64bits output.
3371 locations->AddTemp(Location::RegisterLocation(EAX));
3372 locations->AddTemp(Location::RegisterLocation(EDX));
3373 break;
3374 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003375 case DataType::Type::kFloat32:
3376 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003377 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003378 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3379 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003380 } else if (mul->InputAt(1)->IsConstant()) {
3381 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003382 } else {
3383 locations->SetInAt(1, Location::Any());
3384 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003385 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003386 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003387 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003388
3389 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003390 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003391 }
3392}
3393
3394void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3395 LocationSummary* locations = mul->GetLocations();
3396 Location first = locations->InAt(0);
3397 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003398 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003399
3400 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003402 // The constant may have ended up in a register, so test explicitly to avoid
3403 // problems where the output may not be the same as the first operand.
3404 if (mul->InputAt(1)->IsIntConstant()) {
3405 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3406 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3407 } else if (second.IsRegister()) {
3408 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003409 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003410 } else {
3411 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003412 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003413 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003414 }
3415 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003418 Register in1_hi = first.AsRegisterPairHigh<Register>();
3419 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003420 Register eax = locations->GetTemp(0).AsRegister<Register>();
3421 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003422
3423 DCHECK_EQ(EAX, eax);
3424 DCHECK_EQ(EDX, edx);
3425
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003426 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003427 // output: in1
3428 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3429 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3430 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003431 if (second.IsConstant()) {
3432 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003433
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003434 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3435 int32_t low_value = Low32Bits(value);
3436 int32_t high_value = High32Bits(value);
3437 Immediate low(low_value);
3438 Immediate high(high_value);
3439
3440 __ movl(eax, high);
3441 // eax <- in1.lo * in2.hi
3442 __ imull(eax, in1_lo);
3443 // in1.hi <- in1.hi * in2.lo
3444 __ imull(in1_hi, low);
3445 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3446 __ addl(in1_hi, eax);
3447 // move in2_lo to eax to prepare for double precision
3448 __ movl(eax, low);
3449 // edx:eax <- in1.lo * in2.lo
3450 __ mull(in1_lo);
3451 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3452 __ addl(in1_hi, edx);
3453 // in1.lo <- (in1.lo * in2.lo)[31:0];
3454 __ movl(in1_lo, eax);
3455 } else if (second.IsRegisterPair()) {
3456 Register in2_hi = second.AsRegisterPairHigh<Register>();
3457 Register in2_lo = second.AsRegisterPairLow<Register>();
3458
3459 __ movl(eax, in2_hi);
3460 // eax <- in1.lo * in2.hi
3461 __ imull(eax, in1_lo);
3462 // in1.hi <- in1.hi * in2.lo
3463 __ imull(in1_hi, in2_lo);
3464 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3465 __ addl(in1_hi, eax);
3466 // move in1_lo to eax to prepare for double precision
3467 __ movl(eax, in1_lo);
3468 // edx:eax <- in1.lo * in2.lo
3469 __ mull(in2_lo);
3470 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3471 __ addl(in1_hi, edx);
3472 // in1.lo <- (in1.lo * in2.lo)[31:0];
3473 __ movl(in1_lo, eax);
3474 } else {
3475 DCHECK(second.IsDoubleStackSlot()) << second;
3476 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3477 Address in2_lo(ESP, second.GetStackIndex());
3478
3479 __ movl(eax, in2_hi);
3480 // eax <- in1.lo * in2.hi
3481 __ imull(eax, in1_lo);
3482 // in1.hi <- in1.hi * in2.lo
3483 __ imull(in1_hi, in2_lo);
3484 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3485 __ addl(in1_hi, eax);
3486 // move in1_lo to eax to prepare for double precision
3487 __ movl(eax, in1_lo);
3488 // edx:eax <- in1.lo * in2.lo
3489 __ mull(in2_lo);
3490 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3491 __ addl(in1_hi, edx);
3492 // in1.lo <- (in1.lo * in2.lo)[31:0];
3493 __ movl(in1_lo, eax);
3494 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003495
3496 break;
3497 }
3498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003499 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003500 DCHECK(first.Equals(locations->Out()));
3501 if (second.IsFpuRegister()) {
3502 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3503 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3504 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003505 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 __ mulss(first.AsFpuRegister<XmmRegister>(),
3507 codegen_->LiteralFloatAddress(
3508 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003509 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003510 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3511 } else {
3512 DCHECK(second.IsStackSlot());
3513 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3514 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003515 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003516 }
3517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003518 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003519 DCHECK(first.Equals(locations->Out()));
3520 if (second.IsFpuRegister()) {
3521 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3522 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3523 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003524 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003525 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3526 codegen_->LiteralDoubleAddress(
3527 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003528 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003529 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3530 } else {
3531 DCHECK(second.IsDoubleStackSlot());
3532 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3533 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003534 break;
3535 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003536
3537 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003538 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003539 }
3540}
3541
Roland Levillain232ade02015-04-20 15:14:36 +01003542void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3543 uint32_t temp_offset,
3544 uint32_t stack_adjustment,
3545 bool is_fp,
3546 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003547 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003548 DCHECK(!is_wide);
3549 if (is_fp) {
3550 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3551 } else {
3552 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3553 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003554 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003555 DCHECK(is_wide);
3556 if (is_fp) {
3557 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3558 } else {
3559 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3560 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003561 } else {
3562 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003563 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 Location stack_temp = Location::StackSlot(temp_offset);
3565 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003566 if (is_fp) {
3567 __ flds(Address(ESP, temp_offset));
3568 } else {
3569 __ filds(Address(ESP, temp_offset));
3570 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003571 } else {
3572 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3573 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003574 if (is_fp) {
3575 __ fldl(Address(ESP, temp_offset));
3576 } else {
3577 __ fildl(Address(ESP, temp_offset));
3578 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003579 }
3580 }
3581}
3582
3583void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003584 DataType::Type type = rem->GetResultType();
3585 bool is_float = type == DataType::Type::kFloat32;
3586 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003587 LocationSummary* locations = rem->GetLocations();
3588 Location first = locations->InAt(0);
3589 Location second = locations->InAt(1);
3590 Location out = locations->Out();
3591
3592 // Create stack space for 2 elements.
3593 // TODO: enhance register allocator to ask for stack temporaries.
3594 __ subl(ESP, Immediate(2 * elem_size));
Vladimir Marko86c87522020-05-11 16:55:55 +01003595 __ cfi().AdjustCFAOffset(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003596
3597 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003598 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003599 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3600 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003601
3602 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003603 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003604 __ Bind(&retry);
3605 __ fprem();
3606
3607 // Move FP status to AX.
3608 __ fstsw();
3609
3610 // And see if the argument reduction is complete. This is signaled by the
3611 // C2 FPU flag bit set to 0.
3612 __ andl(EAX, Immediate(kC2ConditionMask));
3613 __ j(kNotEqual, &retry);
3614
3615 // We have settled on the final value. Retrieve it into an XMM register.
3616 // Store FP top of stack to real stack.
3617 if (is_float) {
3618 __ fsts(Address(ESP, 0));
3619 } else {
3620 __ fstl(Address(ESP, 0));
3621 }
3622
3623 // Pop the 2 items from the FP stack.
3624 __ fucompp();
3625
3626 // Load the value from the stack into an XMM register.
3627 DCHECK(out.IsFpuRegister()) << out;
3628 if (is_float) {
3629 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3630 } else {
3631 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3632 }
3633
3634 // And remove the temporary stack space we allocated.
3635 __ addl(ESP, Immediate(2 * elem_size));
Vladimir Marko86c87522020-05-11 16:55:55 +01003636 __ cfi().AdjustCFAOffset(-(2 * elem_size));
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003637}
3638
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003639
3640void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3641 DCHECK(instruction->IsDiv() || instruction->IsRem());
3642
3643 LocationSummary* locations = instruction->GetLocations();
3644 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003645 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003646
3647 Register out_register = locations->Out().AsRegister<Register>();
3648 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003649 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003650
3651 DCHECK(imm == 1 || imm == -1);
3652
3653 if (instruction->IsRem()) {
3654 __ xorl(out_register, out_register);
3655 } else {
3656 __ movl(out_register, input_register);
3657 if (imm == -1) {
3658 __ negl(out_register);
3659 }
3660 }
3661}
3662
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303663void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3664 LocationSummary* locations = instruction->GetLocations();
3665 Location second = locations->InAt(1);
3666
3667 Register out = locations->Out().AsRegister<Register>();
3668 Register numerator = locations->InAt(0).AsRegister<Register>();
3669
3670 int32_t imm = Int64FromConstant(second.GetConstant());
3671 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3672 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3673
3674 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3675 NearLabel done;
3676 __ movl(out, numerator);
3677 __ andl(out, Immediate(abs_imm-1));
3678 __ j(Condition::kZero, &done);
3679 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3680 __ testl(numerator, numerator);
3681 __ cmovl(Condition::kLess, out, tmp);
3682 __ Bind(&done);
3683}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003684
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003685void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003686 LocationSummary* locations = instruction->GetLocations();
3687
3688 Register out_register = locations->Out().AsRegister<Register>();
3689 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003690 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003691 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3692 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003693
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003694 Register num = locations->GetTemp(0).AsRegister<Register>();
3695
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003696 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003697 __ testl(input_register, input_register);
3698 __ cmovl(kGreaterEqual, num, input_register);
3699 int shift = CTZ(imm);
3700 __ sarl(num, Immediate(shift));
3701
3702 if (imm < 0) {
3703 __ negl(num);
3704 }
3705
3706 __ movl(out_register, num);
3707}
3708
3709void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3710 DCHECK(instruction->IsDiv() || instruction->IsRem());
3711
3712 LocationSummary* locations = instruction->GetLocations();
3713 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3714
3715 Register eax = locations->InAt(0).AsRegister<Register>();
3716 Register out = locations->Out().AsRegister<Register>();
3717 Register num;
3718 Register edx;
3719
3720 if (instruction->IsDiv()) {
3721 edx = locations->GetTemp(0).AsRegister<Register>();
3722 num = locations->GetTemp(1).AsRegister<Register>();
3723 } else {
3724 edx = locations->Out().AsRegister<Register>();
3725 num = locations->GetTemp(0).AsRegister<Register>();
3726 }
3727
3728 DCHECK_EQ(EAX, eax);
3729 DCHECK_EQ(EDX, edx);
3730 if (instruction->IsDiv()) {
3731 DCHECK_EQ(EAX, out);
3732 } else {
3733 DCHECK_EQ(EDX, out);
3734 }
3735
3736 int64_t magic;
3737 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003738 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003739
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003740 // Save the numerator.
3741 __ movl(num, eax);
3742
3743 // EAX = magic
3744 __ movl(eax, Immediate(magic));
3745
3746 // EDX:EAX = magic * numerator
3747 __ imull(num);
3748
3749 if (imm > 0 && magic < 0) {
3750 // EDX += num
3751 __ addl(edx, num);
3752 } else if (imm < 0 && magic > 0) {
3753 __ subl(edx, num);
3754 }
3755
3756 // Shift if needed.
3757 if (shift != 0) {
3758 __ sarl(edx, Immediate(shift));
3759 }
3760
3761 // EDX += 1 if EDX < 0
3762 __ movl(eax, edx);
3763 __ shrl(edx, Immediate(31));
3764 __ addl(edx, eax);
3765
3766 if (instruction->IsRem()) {
3767 __ movl(eax, num);
3768 __ imull(edx, Immediate(imm));
3769 __ subl(eax, edx);
3770 __ movl(edx, eax);
3771 } else {
3772 __ movl(eax, edx);
3773 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003774}
3775
Calin Juravlebacfec32014-11-14 15:54:36 +00003776void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3777 DCHECK(instruction->IsDiv() || instruction->IsRem());
3778
3779 LocationSummary* locations = instruction->GetLocations();
3780 Location out = locations->Out();
3781 Location first = locations->InAt(0);
3782 Location second = locations->InAt(1);
3783 bool is_div = instruction->IsDiv();
3784
3785 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 DCHECK_EQ(EAX, first.AsRegister<Register>());
3788 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003789
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003790 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003791 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003792
3793 if (imm == 0) {
3794 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3795 } else if (imm == 1 || imm == -1) {
3796 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303797 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3798 if (is_div) {
3799 DivByPowerOfTwo(instruction->AsDiv());
3800 } else {
3801 RemByPowerOfTwo(instruction->AsRem());
3802 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003803 } else {
3804 DCHECK(imm <= -2 || imm >= 2);
3805 GenerateDivRemWithAnyConstant(instruction);
3806 }
3807 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003808 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003809 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003810 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003811
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003812 Register second_reg = second.AsRegister<Register>();
3813 // 0x80000000/-1 triggers an arithmetic exception!
3814 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3815 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003816
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003817 __ cmpl(second_reg, Immediate(-1));
3818 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003819
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003820 // edx:eax <- sign-extended of eax
3821 __ cdq();
3822 // eax = quotient, edx = remainder
3823 __ idivl(second_reg);
3824 __ Bind(slow_path->GetExitLabel());
3825 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003826 break;
3827 }
3828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003830 InvokeRuntimeCallingConvention calling_convention;
3831 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3832 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3833 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3834 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3835 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3836 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3837
3838 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003839 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003840 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003841 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003842 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003843 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003844 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003845 break;
3846 }
3847
3848 default:
3849 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3850 }
3851}
3852
Calin Juravle7c4954d2014-10-28 16:57:40 +00003853void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003855 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003856 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003857 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003858
Calin Juravle7c4954d2014-10-28 16:57:40 +00003859 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003860 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003861 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003862 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003863 locations->SetOut(Location::SameAsFirstInput());
3864 // Intel uses edx:eax as the dividend.
3865 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003866 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3867 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3868 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003869 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003870 locations->AddTemp(Location::RequiresRegister());
3871 }
Calin Juravled0d48522014-11-04 16:40:20 +00003872 break;
3873 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003875 InvokeRuntimeCallingConvention calling_convention;
3876 locations->SetInAt(0, Location::RegisterPairLocation(
3877 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3878 locations->SetInAt(1, Location::RegisterPairLocation(
3879 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3880 // Runtime helper puts the result in EAX, EDX.
3881 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003882 break;
3883 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003884 case DataType::Type::kFloat32:
3885 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003886 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003887 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3888 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003889 } else if (div->InputAt(1)->IsConstant()) {
3890 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003891 } else {
3892 locations->SetInAt(1, Location::Any());
3893 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003894 locations->SetOut(Location::SameAsFirstInput());
3895 break;
3896 }
3897
3898 default:
3899 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3900 }
3901}
3902
3903void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3904 LocationSummary* locations = div->GetLocations();
3905 Location first = locations->InAt(0);
3906 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003907
3908 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 case DataType::Type::kInt32:
3910 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003911 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003912 break;
3913 }
3914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003915 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003916 if (second.IsFpuRegister()) {
3917 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3918 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3919 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003920 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003921 __ divss(first.AsFpuRegister<XmmRegister>(),
3922 codegen_->LiteralFloatAddress(
3923 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003924 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003925 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3926 } else {
3927 DCHECK(second.IsStackSlot());
3928 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3929 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003930 break;
3931 }
3932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003933 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003934 if (second.IsFpuRegister()) {
3935 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3936 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3937 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003938 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003939 __ divsd(first.AsFpuRegister<XmmRegister>(),
3940 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003941 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3942 const_area->GetBaseMethodAddress(),
3943 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003944 } else {
3945 DCHECK(second.IsDoubleStackSlot());
3946 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3947 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003948 break;
3949 }
3950
3951 default:
3952 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3953 }
3954}
3955
Calin Juravlebacfec32014-11-14 15:54:36 +00003956void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003957 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003958
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003959 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003960 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003961 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003962 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003963
Calin Juravled2ec87d2014-12-08 14:24:46 +00003964 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003965 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003966 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003967 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003968 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003969 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3970 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3971 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003972 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003973 locations->AddTemp(Location::RequiresRegister());
3974 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003975 break;
3976 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003977 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003978 InvokeRuntimeCallingConvention calling_convention;
3979 locations->SetInAt(0, Location::RegisterPairLocation(
3980 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3981 locations->SetInAt(1, Location::RegisterPairLocation(
3982 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3983 // Runtime helper puts the result in EAX, EDX.
3984 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3985 break;
3986 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003987 case DataType::Type::kFloat64:
3988 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003989 locations->SetInAt(0, Location::Any());
3990 locations->SetInAt(1, Location::Any());
3991 locations->SetOut(Location::RequiresFpuRegister());
3992 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003993 break;
3994 }
3995
3996 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003997 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003998 }
3999}
4000
4001void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004003 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004004 case DataType::Type::kInt32:
4005 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004006 GenerateDivRemIntegral(rem);
4007 break;
4008 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004009 case DataType::Type::kFloat32:
4010 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004011 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004012 break;
4013 }
4014 default:
4015 LOG(FATAL) << "Unexpected rem type " << type;
4016 }
4017}
4018
Aart Bik1f8d51b2018-02-15 10:42:37 -08004019static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4020 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4021 switch (minmax->GetResultType()) {
4022 case DataType::Type::kInt32:
4023 locations->SetInAt(0, Location::RequiresRegister());
4024 locations->SetInAt(1, Location::RequiresRegister());
4025 locations->SetOut(Location::SameAsFirstInput());
4026 break;
4027 case DataType::Type::kInt64:
4028 locations->SetInAt(0, Location::RequiresRegister());
4029 locations->SetInAt(1, Location::RequiresRegister());
4030 locations->SetOut(Location::SameAsFirstInput());
4031 // Register to use to perform a long subtract to set cc.
4032 locations->AddTemp(Location::RequiresRegister());
4033 break;
4034 case DataType::Type::kFloat32:
4035 locations->SetInAt(0, Location::RequiresFpuRegister());
4036 locations->SetInAt(1, Location::RequiresFpuRegister());
4037 locations->SetOut(Location::SameAsFirstInput());
4038 locations->AddTemp(Location::RequiresRegister());
4039 break;
4040 case DataType::Type::kFloat64:
4041 locations->SetInAt(0, Location::RequiresFpuRegister());
4042 locations->SetInAt(1, Location::RequiresFpuRegister());
4043 locations->SetOut(Location::SameAsFirstInput());
4044 break;
4045 default:
4046 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4047 }
4048}
4049
Aart Bik351df3e2018-03-07 11:54:57 -08004050void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4051 bool is_min,
4052 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004053 Location op1_loc = locations->InAt(0);
4054 Location op2_loc = locations->InAt(1);
4055
4056 // Shortcut for same input locations.
4057 if (op1_loc.Equals(op2_loc)) {
4058 // Can return immediately, as op1_loc == out_loc.
4059 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4060 // a copy here.
4061 DCHECK(locations->Out().Equals(op1_loc));
4062 return;
4063 }
4064
4065 if (type == DataType::Type::kInt64) {
4066 // Need to perform a subtract to get the sign right.
4067 // op1 is already in the same location as the output.
4068 Location output = locations->Out();
4069 Register output_lo = output.AsRegisterPairLow<Register>();
4070 Register output_hi = output.AsRegisterPairHigh<Register>();
4071
4072 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4073 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4074
4075 // The comparison is performed by subtracting the second operand from
4076 // the first operand and then setting the status flags in the same
4077 // manner as the SUB instruction."
4078 __ cmpl(output_lo, op2_lo);
4079
4080 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4081 Register temp = locations->GetTemp(0).AsRegister<Register>();
4082 __ movl(temp, output_hi);
4083 __ sbbl(temp, op2_hi);
4084
4085 // Now the condition code is correct.
4086 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4087 __ cmovl(cond, output_lo, op2_lo);
4088 __ cmovl(cond, output_hi, op2_hi);
4089 } else {
4090 DCHECK_EQ(type, DataType::Type::kInt32);
4091 Register out = locations->Out().AsRegister<Register>();
4092 Register op2 = op2_loc.AsRegister<Register>();
4093
4094 // (out := op1)
4095 // out <=? op2
4096 // if out is min jmp done
4097 // out := op2
4098 // done:
4099
4100 __ cmpl(out, op2);
4101 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4102 __ cmovl(cond, out, op2);
4103 }
4104}
4105
4106void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4107 bool is_min,
4108 DataType::Type type) {
4109 Location op1_loc = locations->InAt(0);
4110 Location op2_loc = locations->InAt(1);
4111 Location out_loc = locations->Out();
4112 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4113
4114 // Shortcut for same input locations.
4115 if (op1_loc.Equals(op2_loc)) {
4116 DCHECK(out_loc.Equals(op1_loc));
4117 return;
4118 }
4119
4120 // (out := op1)
4121 // out <=? op2
4122 // if Nan jmp Nan_label
4123 // if out is min jmp done
4124 // if op2 is min jmp op2_label
4125 // handle -0/+0
4126 // jmp done
4127 // Nan_label:
4128 // out := NaN
4129 // op2_label:
4130 // out := op2
4131 // done:
4132 //
4133 // This removes one jmp, but needs to copy one input (op1) to out.
4134 //
4135 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4136
4137 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4138
4139 NearLabel nan, done, op2_label;
4140 if (type == DataType::Type::kFloat64) {
4141 __ ucomisd(out, op2);
4142 } else {
4143 DCHECK_EQ(type, DataType::Type::kFloat32);
4144 __ ucomiss(out, op2);
4145 }
4146
4147 __ j(Condition::kParityEven, &nan);
4148
4149 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4150 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4151
4152 // Handle 0.0/-0.0.
4153 if (is_min) {
4154 if (type == DataType::Type::kFloat64) {
4155 __ orpd(out, op2);
4156 } else {
4157 __ orps(out, op2);
4158 }
4159 } else {
4160 if (type == DataType::Type::kFloat64) {
4161 __ andpd(out, op2);
4162 } else {
4163 __ andps(out, op2);
4164 }
4165 }
4166 __ jmp(&done);
4167
4168 // NaN handling.
4169 __ Bind(&nan);
4170 if (type == DataType::Type::kFloat64) {
4171 // TODO: Use a constant from the constant table (requires extra input).
4172 __ LoadLongConstant(out, kDoubleNaN);
4173 } else {
4174 Register constant = locations->GetTemp(0).AsRegister<Register>();
4175 __ movl(constant, Immediate(kFloatNaN));
4176 __ movd(out, constant);
4177 }
4178 __ jmp(&done);
4179
4180 // out := op2;
4181 __ Bind(&op2_label);
4182 if (type == DataType::Type::kFloat64) {
4183 __ movsd(out, op2);
4184 } else {
4185 __ movss(out, op2);
4186 }
4187
4188 // Done.
4189 __ Bind(&done);
4190}
4191
Aart Bik351df3e2018-03-07 11:54:57 -08004192void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4193 DataType::Type type = minmax->GetResultType();
4194 switch (type) {
4195 case DataType::Type::kInt32:
4196 case DataType::Type::kInt64:
4197 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4198 break;
4199 case DataType::Type::kFloat32:
4200 case DataType::Type::kFloat64:
4201 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4202 break;
4203 default:
4204 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4205 }
4206}
4207
Aart Bik1f8d51b2018-02-15 10:42:37 -08004208void LocationsBuilderX86::VisitMin(HMin* min) {
4209 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4210}
4211
4212void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004213 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004214}
4215
4216void LocationsBuilderX86::VisitMax(HMax* max) {
4217 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4218}
4219
4220void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004221 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004222}
4223
Aart Bik3dad3412018-02-28 12:01:46 -08004224void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4225 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4226 switch (abs->GetResultType()) {
4227 case DataType::Type::kInt32:
4228 locations->SetInAt(0, Location::RegisterLocation(EAX));
4229 locations->SetOut(Location::SameAsFirstInput());
4230 locations->AddTemp(Location::RegisterLocation(EDX));
4231 break;
4232 case DataType::Type::kInt64:
4233 locations->SetInAt(0, Location::RequiresRegister());
4234 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4235 locations->AddTemp(Location::RequiresRegister());
4236 break;
4237 case DataType::Type::kFloat32:
4238 locations->SetInAt(0, Location::RequiresFpuRegister());
4239 locations->SetOut(Location::SameAsFirstInput());
4240 locations->AddTemp(Location::RequiresFpuRegister());
4241 locations->AddTemp(Location::RequiresRegister());
4242 break;
4243 case DataType::Type::kFloat64:
4244 locations->SetInAt(0, Location::RequiresFpuRegister());
4245 locations->SetOut(Location::SameAsFirstInput());
4246 locations->AddTemp(Location::RequiresFpuRegister());
4247 break;
4248 default:
4249 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4250 }
4251}
4252
4253void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4254 LocationSummary* locations = abs->GetLocations();
4255 switch (abs->GetResultType()) {
4256 case DataType::Type::kInt32: {
4257 Register out = locations->Out().AsRegister<Register>();
4258 DCHECK_EQ(out, EAX);
4259 Register temp = locations->GetTemp(0).AsRegister<Register>();
4260 DCHECK_EQ(temp, EDX);
4261 // Sign extend EAX into EDX.
4262 __ cdq();
4263 // XOR EAX with sign.
4264 __ xorl(EAX, EDX);
4265 // Subtract out sign to correct.
4266 __ subl(EAX, EDX);
4267 // The result is in EAX.
4268 break;
4269 }
4270 case DataType::Type::kInt64: {
4271 Location input = locations->InAt(0);
4272 Register input_lo = input.AsRegisterPairLow<Register>();
4273 Register input_hi = input.AsRegisterPairHigh<Register>();
4274 Location output = locations->Out();
4275 Register output_lo = output.AsRegisterPairLow<Register>();
4276 Register output_hi = output.AsRegisterPairHigh<Register>();
4277 Register temp = locations->GetTemp(0).AsRegister<Register>();
4278 // Compute the sign into the temporary.
4279 __ movl(temp, input_hi);
4280 __ sarl(temp, Immediate(31));
4281 // Store the sign into the output.
4282 __ movl(output_lo, temp);
4283 __ movl(output_hi, temp);
4284 // XOR the input to the output.
4285 __ xorl(output_lo, input_lo);
4286 __ xorl(output_hi, input_hi);
4287 // Subtract the sign.
4288 __ subl(output_lo, temp);
4289 __ sbbl(output_hi, temp);
4290 break;
4291 }
4292 case DataType::Type::kFloat32: {
4293 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4294 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4295 Register constant = locations->GetTemp(1).AsRegister<Register>();
4296 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4297 __ movd(temp, constant);
4298 __ andps(out, temp);
4299 break;
4300 }
4301 case DataType::Type::kFloat64: {
4302 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4303 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4304 // TODO: Use a constant from the constant table (requires extra input).
4305 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4306 __ andpd(out, temp);
4307 break;
4308 }
4309 default:
4310 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4311 }
4312}
4313
Calin Juravled0d48522014-11-04 16:40:20 +00004314void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004315 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004316 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004317 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004318 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004319 case DataType::Type::kInt8:
4320 case DataType::Type::kUint16:
4321 case DataType::Type::kInt16:
4322 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004323 locations->SetInAt(0, Location::Any());
4324 break;
4325 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004326 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004327 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4328 if (!instruction->IsConstant()) {
4329 locations->AddTemp(Location::RequiresRegister());
4330 }
4331 break;
4332 }
4333 default:
4334 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4335 }
Calin Juravled0d48522014-11-04 16:40:20 +00004336}
4337
4338void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004339 SlowPathCode* slow_path =
4340 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004341 codegen_->AddSlowPath(slow_path);
4342
4343 LocationSummary* locations = instruction->GetLocations();
4344 Location value = locations->InAt(0);
4345
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004346 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004347 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004348 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004349 case DataType::Type::kInt8:
4350 case DataType::Type::kUint16:
4351 case DataType::Type::kInt16:
4352 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004353 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004354 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004355 __ j(kEqual, slow_path->GetEntryLabel());
4356 } else if (value.IsStackSlot()) {
4357 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4358 __ j(kEqual, slow_path->GetEntryLabel());
4359 } else {
4360 DCHECK(value.IsConstant()) << value;
4361 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004362 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004363 }
4364 }
4365 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004366 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004368 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004369 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004370 __ movl(temp, value.AsRegisterPairLow<Register>());
4371 __ orl(temp, value.AsRegisterPairHigh<Register>());
4372 __ j(kEqual, slow_path->GetEntryLabel());
4373 } else {
4374 DCHECK(value.IsConstant()) << value;
4375 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4376 __ jmp(slow_path->GetEntryLabel());
4377 }
4378 }
4379 break;
4380 }
4381 default:
4382 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004383 }
Calin Juravled0d48522014-11-04 16:40:20 +00004384}
4385
Calin Juravle9aec02f2014-11-18 23:06:35 +00004386void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4387 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4388
4389 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004390 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004391
4392 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004393 case DataType::Type::kInt32:
4394 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004395 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004396 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004397 // The shift count needs to be in CL or a constant.
4398 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004399 locations->SetOut(Location::SameAsFirstInput());
4400 break;
4401 }
4402 default:
4403 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4404 }
4405}
4406
4407void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4408 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4409
4410 LocationSummary* locations = op->GetLocations();
4411 Location first = locations->InAt(0);
4412 Location second = locations->InAt(1);
4413 DCHECK(first.Equals(locations->Out()));
4414
4415 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004416 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004417 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004418 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004419 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004420 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004421 DCHECK_EQ(ECX, second_reg);
4422 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004423 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004424 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004425 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004426 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004427 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004428 }
4429 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004430 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004431 if (shift == 0) {
4432 return;
4433 }
4434 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004435 if (op->IsShl()) {
4436 __ shll(first_reg, imm);
4437 } else if (op->IsShr()) {
4438 __ sarl(first_reg, imm);
4439 } else {
4440 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004441 }
4442 }
4443 break;
4444 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004445 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004446 if (second.IsRegister()) {
4447 Register second_reg = second.AsRegister<Register>();
4448 DCHECK_EQ(ECX, second_reg);
4449 if (op->IsShl()) {
4450 GenerateShlLong(first, second_reg);
4451 } else if (op->IsShr()) {
4452 GenerateShrLong(first, second_reg);
4453 } else {
4454 GenerateUShrLong(first, second_reg);
4455 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004456 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004457 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004458 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004459 // Nothing to do if the shift is 0, as the input is already the output.
4460 if (shift != 0) {
4461 if (op->IsShl()) {
4462 GenerateShlLong(first, shift);
4463 } else if (op->IsShr()) {
4464 GenerateShrLong(first, shift);
4465 } else {
4466 GenerateUShrLong(first, shift);
4467 }
4468 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004469 }
4470 break;
4471 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004472 default:
4473 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4474 }
4475}
4476
Mark P Mendell73945692015-04-29 14:56:17 +00004477void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4478 Register low = loc.AsRegisterPairLow<Register>();
4479 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004480 if (shift == 1) {
4481 // This is just an addition.
4482 __ addl(low, low);
4483 __ adcl(high, high);
4484 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004485 // Shift by 32 is easy. High gets low, and low gets 0.
4486 codegen_->EmitParallelMoves(
4487 loc.ToLow(),
4488 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004489 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004490 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4491 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004492 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004493 } else if (shift > 32) {
4494 // Low part becomes 0. High part is low part << (shift-32).
4495 __ movl(high, low);
4496 __ shll(high, Immediate(shift - 32));
4497 __ xorl(low, low);
4498 } else {
4499 // Between 1 and 31.
4500 __ shld(high, low, Immediate(shift));
4501 __ shll(low, Immediate(shift));
4502 }
4503}
4504
Calin Juravle9aec02f2014-11-18 23:06:35 +00004505void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004506 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004507 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4508 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4509 __ testl(shifter, Immediate(32));
4510 __ j(kEqual, &done);
4511 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4512 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4513 __ Bind(&done);
4514}
4515
Mark P Mendell73945692015-04-29 14:56:17 +00004516void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4517 Register low = loc.AsRegisterPairLow<Register>();
4518 Register high = loc.AsRegisterPairHigh<Register>();
4519 if (shift == 32) {
4520 // Need to copy the sign.
4521 DCHECK_NE(low, high);
4522 __ movl(low, high);
4523 __ sarl(high, Immediate(31));
4524 } else if (shift > 32) {
4525 DCHECK_NE(low, high);
4526 // High part becomes sign. Low part is shifted by shift - 32.
4527 __ movl(low, high);
4528 __ sarl(high, Immediate(31));
4529 __ sarl(low, Immediate(shift - 32));
4530 } else {
4531 // Between 1 and 31.
4532 __ shrd(low, high, Immediate(shift));
4533 __ sarl(high, Immediate(shift));
4534 }
4535}
4536
Calin Juravle9aec02f2014-11-18 23:06:35 +00004537void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004538 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004539 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4540 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4541 __ testl(shifter, Immediate(32));
4542 __ j(kEqual, &done);
4543 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4544 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4545 __ Bind(&done);
4546}
4547
Mark P Mendell73945692015-04-29 14:56:17 +00004548void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4549 Register low = loc.AsRegisterPairLow<Register>();
4550 Register high = loc.AsRegisterPairHigh<Register>();
4551 if (shift == 32) {
4552 // Shift by 32 is easy. Low gets high, and high gets 0.
4553 codegen_->EmitParallelMoves(
4554 loc.ToHigh(),
4555 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004556 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004557 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4558 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004559 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004560 } else if (shift > 32) {
4561 // Low part is high >> (shift - 32). High part becomes 0.
4562 __ movl(low, high);
4563 __ shrl(low, Immediate(shift - 32));
4564 __ xorl(high, high);
4565 } else {
4566 // Between 1 and 31.
4567 __ shrd(low, high, Immediate(shift));
4568 __ shrl(high, Immediate(shift));
4569 }
4570}
4571
Calin Juravle9aec02f2014-11-18 23:06:35 +00004572void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004573 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004574 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4575 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4576 __ testl(shifter, Immediate(32));
4577 __ j(kEqual, &done);
4578 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4579 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4580 __ Bind(&done);
4581}
4582
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004583void LocationsBuilderX86::VisitRor(HRor* ror) {
4584 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004585 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004586
4587 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004588 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004589 // Add the temporary needed.
4590 locations->AddTemp(Location::RequiresRegister());
4591 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004592 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004593 locations->SetInAt(0, Location::RequiresRegister());
4594 // The shift count needs to be in CL (unless it is a constant).
4595 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4596 locations->SetOut(Location::SameAsFirstInput());
4597 break;
4598 default:
4599 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4600 UNREACHABLE();
4601 }
4602}
4603
4604void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4605 LocationSummary* locations = ror->GetLocations();
4606 Location first = locations->InAt(0);
4607 Location second = locations->InAt(1);
4608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004609 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004610 Register first_reg = first.AsRegister<Register>();
4611 if (second.IsRegister()) {
4612 Register second_reg = second.AsRegister<Register>();
4613 __ rorl(first_reg, second_reg);
4614 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004616 __ rorl(first_reg, imm);
4617 }
4618 return;
4619 }
4620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004621 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004622 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4623 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4624 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4625 if (second.IsRegister()) {
4626 Register second_reg = second.AsRegister<Register>();
4627 DCHECK_EQ(second_reg, ECX);
4628 __ movl(temp_reg, first_reg_hi);
4629 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4630 __ shrd(first_reg_lo, temp_reg, second_reg);
4631 __ movl(temp_reg, first_reg_hi);
4632 __ testl(second_reg, Immediate(32));
4633 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4634 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4635 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004636 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004637 if (shift_amt == 0) {
4638 // Already fine.
4639 return;
4640 }
4641 if (shift_amt == 32) {
4642 // Just swap.
4643 __ movl(temp_reg, first_reg_lo);
4644 __ movl(first_reg_lo, first_reg_hi);
4645 __ movl(first_reg_hi, temp_reg);
4646 return;
4647 }
4648
4649 Immediate imm(shift_amt);
4650 // Save the constents of the low value.
4651 __ movl(temp_reg, first_reg_lo);
4652
4653 // Shift right into low, feeding bits from high.
4654 __ shrd(first_reg_lo, first_reg_hi, imm);
4655
4656 // Shift right into high, feeding bits from the original low.
4657 __ shrd(first_reg_hi, temp_reg, imm);
4658
4659 // Swap if needed.
4660 if (shift_amt > 32) {
4661 __ movl(temp_reg, first_reg_lo);
4662 __ movl(first_reg_lo, first_reg_hi);
4663 __ movl(first_reg_hi, temp_reg);
4664 }
4665 }
4666}
4667
Calin Juravle9aec02f2014-11-18 23:06:35 +00004668void LocationsBuilderX86::VisitShl(HShl* shl) {
4669 HandleShift(shl);
4670}
4671
4672void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4673 HandleShift(shl);
4674}
4675
4676void LocationsBuilderX86::VisitShr(HShr* shr) {
4677 HandleShift(shr);
4678}
4679
4680void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4681 HandleShift(shr);
4682}
4683
4684void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4685 HandleShift(ushr);
4686}
4687
4688void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4689 HandleShift(ushr);
4690}
4691
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004692void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004693 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4694 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004695 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004696 InvokeRuntimeCallingConvention calling_convention;
4697 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004698}
4699
4700void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004701 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4702 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4703 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004704}
4705
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004706void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4708 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004709 locations->SetOut(Location::RegisterLocation(EAX));
4710 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004711 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4712 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004713}
4714
4715void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004716 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4717 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004718 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004719 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004720 DCHECK(!codegen_->IsLeafMethod());
4721}
4722
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004723void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004724 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004725 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004726 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4727 if (location.IsStackSlot()) {
4728 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4729 } else if (location.IsDoubleStackSlot()) {
4730 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004731 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004732 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004733}
4734
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004735void InstructionCodeGeneratorX86::VisitParameterValue(
4736 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4737}
4738
4739void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4740 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004741 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004742 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4743}
4744
4745void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004746}
4747
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004748void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4749 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004750 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004751 locations->SetInAt(0, Location::RequiresRegister());
4752 locations->SetOut(Location::RequiresRegister());
4753}
4754
4755void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4756 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004757 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004758 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004759 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004760 __ movl(locations->Out().AsRegister<Register>(),
4761 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004762 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004763 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004764 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004765 __ movl(locations->Out().AsRegister<Register>(),
4766 Address(locations->InAt(0).AsRegister<Register>(),
4767 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4768 // temp = temp->GetImtEntryAt(method_offset);
4769 __ movl(locations->Out().AsRegister<Register>(),
4770 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004771 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004772}
4773
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004774void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004775 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004776 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004777 locations->SetInAt(0, Location::RequiresRegister());
4778 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004779}
4780
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004781void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4782 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004783 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004784 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004785 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004786 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004787 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004788 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004789 break;
4790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004791 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004792 __ notl(out.AsRegisterPairLow<Register>());
4793 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004794 break;
4795
4796 default:
4797 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4798 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004799}
4800
David Brazdil66d126e2015-04-03 16:02:44 +01004801void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4802 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004803 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004804 locations->SetInAt(0, Location::RequiresRegister());
4805 locations->SetOut(Location::SameAsFirstInput());
4806}
4807
4808void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004809 LocationSummary* locations = bool_not->GetLocations();
4810 Location in = locations->InAt(0);
4811 Location out = locations->Out();
4812 DCHECK(in.Equals(out));
4813 __ xorl(out.AsRegister<Register>(), Immediate(1));
4814}
4815
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004816void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004817 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004818 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004819 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004821 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004824 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004825 case DataType::Type::kInt32:
4826 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004827 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004828 locations->SetInAt(1, Location::Any());
4829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4830 break;
4831 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 case DataType::Type::kFloat32:
4833 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004834 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004835 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4836 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4837 } else if (compare->InputAt(1)->IsConstant()) {
4838 locations->SetInAt(1, Location::RequiresFpuRegister());
4839 } else {
4840 locations->SetInAt(1, Location::Any());
4841 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004842 locations->SetOut(Location::RequiresRegister());
4843 break;
4844 }
4845 default:
4846 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4847 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004848}
4849
4850void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004851 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004852 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004853 Location left = locations->InAt(0);
4854 Location right = locations->InAt(1);
4855
Mark Mendell0c9497d2015-08-21 09:30:05 -04004856 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004857 Condition less_cond = kLess;
4858
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004859 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004861 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004864 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004866 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004867 break;
4868 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004870 Register left_low = left.AsRegisterPairLow<Register>();
4871 Register left_high = left.AsRegisterPairHigh<Register>();
4872 int32_t val_low = 0;
4873 int32_t val_high = 0;
4874 bool right_is_const = false;
4875
4876 if (right.IsConstant()) {
4877 DCHECK(right.GetConstant()->IsLongConstant());
4878 right_is_const = true;
4879 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4880 val_low = Low32Bits(val);
4881 val_high = High32Bits(val);
4882 }
4883
Calin Juravleddb7df22014-11-25 20:56:51 +00004884 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004885 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004886 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004887 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004888 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004889 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004890 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004891 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004892 __ j(kLess, &less); // Signed compare.
4893 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004894 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004895 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004896 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004897 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004898 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004899 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004900 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004901 }
Aart Bika19616e2016-02-01 18:57:58 -08004902 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004903 break;
4904 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004905 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004906 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004907 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004908 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004909 break;
4910 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004911 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004912 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004913 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004914 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004915 break;
4916 }
4917 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004918 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004919 }
Aart Bika19616e2016-02-01 18:57:58 -08004920
Calin Juravleddb7df22014-11-25 20:56:51 +00004921 __ movl(out, Immediate(0));
4922 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004923 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004924
4925 __ Bind(&greater);
4926 __ movl(out, Immediate(1));
4927 __ jmp(&done);
4928
4929 __ Bind(&less);
4930 __ movl(out, Immediate(-1));
4931
4932 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004933}
4934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004935void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004936 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004937 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004938 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004939 locations->SetInAt(i, Location::Any());
4940 }
4941 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004942}
4943
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004944void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004945 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004946}
4947
Roland Levillain7c1559a2015-12-15 10:55:36 +00004948void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004949 /*
4950 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4951 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4952 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4953 */
4954 switch (kind) {
4955 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004956 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004957 break;
4958 }
4959 case MemBarrierKind::kAnyStore:
4960 case MemBarrierKind::kLoadAny:
4961 case MemBarrierKind::kStoreStore: {
4962 // nop
4963 break;
4964 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004965 case MemBarrierKind::kNTStoreStore:
4966 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004967 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004968 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004969 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970}
4971
Vladimir Markodc151b22015-10-15 18:02:30 +01004972HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4973 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004974 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004975 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004976}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004978Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4979 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004980 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004981 if (!invoke->GetLocations()->Intrinsified()) {
4982 return location.AsRegister<Register>();
4983 }
4984 // For intrinsics we allow any location, so it may be on the stack.
4985 if (!location.IsRegister()) {
4986 __ movl(temp, Address(ESP, location.GetStackIndex()));
4987 return temp;
4988 }
4989 // For register locations, check if the register was saved. If so, get it from the stack.
4990 // Note: There is a chance that the register was saved but not overwritten, so we could
4991 // save one load. However, since this is just an intrinsic slow path we prefer this
4992 // simple and more robust approach rather that trying to determine if that's the case.
4993 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004994 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4995 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4996 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4997 __ movl(temp, Address(ESP, stack_offset));
4998 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004999 }
5000 return location.AsRegister<Register>();
5001}
5002
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005003void CodeGeneratorX86::GenerateStaticOrDirectCall(
5004 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005005 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5006 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005007 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005008 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005009 uint32_t offset =
5010 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5011 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005012 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005013 }
Vladimir Marko58155012015-08-19 12:49:41 +00005014 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005015 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005016 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005017 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005018 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005019 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5020 temp.AsRegister<Register>());
5021 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005022 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005023 break;
5024 }
Vladimir Markob066d432018-01-03 13:14:37 +00005025 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5026 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5027 temp.AsRegister<Register>());
5028 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
5029 RecordBootImageRelRoPatch(
5030 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005031 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005032 break;
5033 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005034 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005035 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5036 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005037 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005038 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005039 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005040 break;
5041 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005042 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5043 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5044 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005045 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5046 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5047 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005048 }
Vladimir Marko58155012015-08-19 12:49:41 +00005049 }
5050
5051 switch (invoke->GetCodePtrLocation()) {
5052 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5053 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005054 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005055 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005056 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
5057 HParallelMove parallel_move(GetGraph()->GetAllocator());
5058 size_t out_frame_size =
5059 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5060 kNativeStackAlignment,
5061 GetCriticalNativeDirectCallFrameSize>(invoke, &parallel_move);
5062 if (out_frame_size != 0u) {
5063 __ subl(ESP, Immediate(out_frame_size));
5064 __ cfi().AdjustCFAOffset(out_frame_size);
5065 GetMoveResolver()->EmitNativeCode(&parallel_move);
5066 }
5067 // (callee_method + offset_of_jni_entry_point)()
5068 __ call(Address(callee_method.AsRegister<Register>(),
5069 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5070 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5071 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5072 // Create space for conversion.
5073 out_frame_size = 8u;
5074 __ subl(ESP, Immediate(out_frame_size));
5075 __ cfi().AdjustCFAOffset(out_frame_size);
5076 }
5077 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5078 switch (invoke->GetType()) {
5079 case DataType::Type::kBool:
5080 __ movzxb(EAX, AL);
5081 break;
5082 case DataType::Type::kInt8:
5083 __ movsxb(EAX, AL);
5084 break;
5085 case DataType::Type::kUint16:
5086 __ movzxw(EAX, EAX);
5087 break;
5088 case DataType::Type::kInt16:
5089 __ movsxw(EAX, EAX);
5090 break;
5091 case DataType::Type::kFloat32:
5092 __ fstps(Address(ESP, 0));
5093 __ movss(XMM0, Address(ESP, 0));
5094 break;
5095 case DataType::Type::kFloat64:
5096 __ fstpl(Address(ESP, 0));
5097 __ movsd(XMM0, Address(ESP, 0));
5098 break;
5099 case DataType::Type::kInt32:
5100 case DataType::Type::kInt64:
5101 case DataType::Type::kVoid:
5102 break;
5103 default:
5104 DCHECK(false) << invoke->GetType();
5105 break;
5106 }
5107 if (out_frame_size != 0u) {
5108 __ addl(ESP, Immediate(out_frame_size));
5109 __ cfi().AdjustCFAOffset(-out_frame_size);
5110 }
5111 break;
5112 }
Vladimir Marko58155012015-08-19 12:49:41 +00005113 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5114 // (callee_method + offset_of_quick_compiled_code)()
5115 __ call(Address(callee_method.AsRegister<Register>(),
5116 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005117 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005118 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005119 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005120 }
5121
5122 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005123}
5124
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005125void CodeGeneratorX86::GenerateVirtualCall(
5126 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005127 Register temp = temp_in.AsRegister<Register>();
5128 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5129 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005130
5131 // Use the calling convention instead of the location of the receiver, as
5132 // intrinsics may have put the receiver in a different register. In the intrinsics
5133 // slow path, the arguments have been moved to the right place, so here we are
5134 // guaranteed that the receiver is the first register of the calling convention.
5135 InvokeDexCallingConvention calling_convention;
5136 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005137 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005138 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005139 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005140 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005141 // Instead of simply (possibly) unpoisoning `temp` here, we should
5142 // emit a read barrier for the previous class reference load.
5143 // However this is not required in practice, as this is an
5144 // intermediate/temporary reference and because the current
5145 // concurrent copying collector keeps the from-space memory
5146 // intact/accessible until the end of the marking phase (the
5147 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005148 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005149
5150 MaybeGenerateInlineCacheCheck(invoke, temp);
5151
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005152 // temp = temp->GetMethodAt(method_offset);
5153 __ movl(temp, Address(temp, method_offset));
5154 // call temp->GetEntryPoint();
5155 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005156 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005157 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005158}
5159
Vladimir Marko6fd16062018-06-26 11:02:04 +01005160void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5161 uint32_t intrinsic_data) {
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, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005164 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005165}
5166
Vladimir Markob066d432018-01-03 13:14:37 +00005167void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5168 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005169 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005170 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005171 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005172}
5173
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005174void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005175 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005176 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005177 boot_image_method_patches_.emplace_back(
5178 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005179 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005180}
5181
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005182void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005183 HX86ComputeBaseMethodAddress* method_address =
5184 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005185 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005186 method_bss_entry_patches_.emplace_back(
5187 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5188 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005189}
5190
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005191void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5192 HX86ComputeBaseMethodAddress* method_address =
5193 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5194 boot_image_type_patches_.emplace_back(
5195 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005196 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005197}
5198
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005199Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005200 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005201 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5202 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005203 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005204 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005205}
5206
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005207void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5208 HX86ComputeBaseMethodAddress* method_address =
5209 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5210 boot_image_string_patches_.emplace_back(
5211 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5212 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005213}
5214
Vladimir Markoaad75c62016-10-03 08:46:48 +00005215Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005216 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005217 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005218 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005219 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005220 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005221}
5222
Vladimir Markoeebb8212018-06-05 14:57:24 +01005223void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005224 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005225 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005226 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005227 HX86ComputeBaseMethodAddress* method_address =
5228 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5229 DCHECK(method_address != nullptr);
5230 Register method_address_reg =
5231 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5232 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5233 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005234 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005235 HX86ComputeBaseMethodAddress* method_address =
5236 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5237 DCHECK(method_address != nullptr);
5238 Register method_address_reg =
5239 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5240 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005241 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005242 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005243 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005244 gc::Heap* heap = Runtime::Current()->GetHeap();
5245 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005246 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005247 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5248 }
5249}
5250
Vladimir Marko6fd16062018-06-26 11:02:04 +01005251void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5252 uint32_t boot_image_offset) {
5253 DCHECK(invoke->IsStatic());
5254 InvokeRuntimeCallingConvention calling_convention;
5255 Register argument = calling_convention.GetRegisterAt(0);
5256 if (GetCompilerOptions().IsBootImage()) {
5257 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5258 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005259 HX86ComputeBaseMethodAddress* method_address =
5260 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5261 DCHECK(method_address != nullptr);
5262 Register method_address_reg =
5263 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5264 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5265 MethodReference target_method = invoke->GetTargetMethod();
5266 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5267 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5268 __ Bind(&boot_image_type_patches_.back().label);
5269 } else {
5270 LoadBootImageAddress(argument, boot_image_offset, invoke);
5271 }
5272 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5273 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5274}
5275
Vladimir Markoaad75c62016-10-03 08:46:48 +00005276// The label points to the end of the "movl" or another instruction but the literal offset
5277// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5278constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5279
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005280template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005281inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005282 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005283 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005284 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005285 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005286 linker_patches->push_back(Factory(literal_offset,
5287 info.target_dex_file,
5288 GetMethodAddressOffset(info.method_address),
5289 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005290 }
5291}
5292
Vladimir Marko6fd16062018-06-26 11:02:04 +01005293template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5294linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5295 const DexFile* target_dex_file,
5296 uint32_t pc_insn_offset,
5297 uint32_t boot_image_offset) {
5298 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5299 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005300}
5301
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005302void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005303 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005304 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005305 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005306 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005307 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005308 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005309 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005310 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005311 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005312 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005313 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005314 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5315 boot_image_method_patches_, linker_patches);
5316 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5317 boot_image_type_patches_, linker_patches);
5318 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005319 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005320 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005321 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005322 DCHECK(boot_image_type_patches_.empty());
5323 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005324 }
5325 if (GetCompilerOptions().IsBootImage()) {
5326 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5327 boot_image_other_patches_, linker_patches);
5328 } else {
5329 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5330 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005331 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005332 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5333 method_bss_entry_patches_, linker_patches);
5334 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5335 type_bss_entry_patches_, linker_patches);
5336 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5337 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005338 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005339}
5340
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005341void CodeGeneratorX86::MarkGCCard(Register temp,
5342 Register card,
5343 Register object,
5344 Register value,
5345 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005346 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005347 if (value_can_be_null) {
5348 __ testl(value, value);
5349 __ j(kEqual, &is_null);
5350 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005351 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005352 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005353 // Calculate the offset (in the card table) of the card corresponding to
5354 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355 __ movl(temp, object);
5356 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005357 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5358 // `object`'s card.
5359 //
5360 // Register `card` contains the address of the card table. Note that the card
5361 // table's base is biased during its creation so that it always starts at an
5362 // address whose least-significant byte is equal to `kCardDirty` (see
5363 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5364 // below writes the `kCardDirty` (byte) value into the `object`'s card
5365 // (located at `card + object >> kCardShift`).
5366 //
5367 // This dual use of the value in register `card` (1. to calculate the location
5368 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5369 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005370 __ movb(Address(temp, card, TIMES_1, 0),
5371 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005372 if (value_can_be_null) {
5373 __ Bind(&is_null);
5374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375}
5376
Calin Juravle52c48962014-12-16 17:02:57 +00005377void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5378 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005379
5380 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005383 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5384 kEmitCompilerReadBarrier
5385 ? LocationSummary::kCallOnSlowPath
5386 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005387 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005388 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005389 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005390 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005393 locations->SetOut(Location::RequiresFpuRegister());
5394 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005395 // The output overlaps in case of long: we don't want the low move
5396 // to overwrite the object's location. Likewise, in the case of
5397 // an object field get with read barriers enabled, we do not want
5398 // the move to overwrite the object's location, as we need it to emit
5399 // the read barrier.
5400 locations->SetOut(
5401 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005403 Location::kOutputOverlap :
5404 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005405 }
Calin Juravle52c48962014-12-16 17:02:57 +00005406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005407 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005408 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005409 // So we use an XMM register as a temp to achieve atomicity (first
5410 // load the temp into the XMM and then copy the XMM into the
5411 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005412 locations->AddTemp(Location::RequiresFpuRegister());
5413 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005414}
5415
Calin Juravle52c48962014-12-16 17:02:57 +00005416void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5417 const FieldInfo& field_info) {
5418 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005419
Calin Juravle52c48962014-12-16 17:02:57 +00005420 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005421 Location base_loc = locations->InAt(0);
5422 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005423 Location out = locations->Out();
5424 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005425 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5426 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005427 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5428
Vladimir Marko61b92282017-10-11 13:23:17 +01005429 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005430 case DataType::Type::kBool:
5431 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005432 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005433 break;
5434 }
5435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005436 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005437 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005438 break;
5439 }
5440
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005441 case DataType::Type::kUint16: {
5442 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005443 break;
5444 }
5445
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005446 case DataType::Type::kInt16: {
5447 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005448 break;
5449 }
5450
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005452 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005453 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005454
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005455 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005456 // /* HeapReference<Object> */ out = *(base + offset)
5457 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005458 // Note that a potential implicit null check is handled in this
5459 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5460 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005461 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005462 if (is_volatile) {
5463 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5464 }
5465 } else {
5466 __ movl(out.AsRegister<Register>(), Address(base, offset));
5467 codegen_->MaybeRecordImplicitNullCheck(instruction);
5468 if (is_volatile) {
5469 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5470 }
5471 // If read barriers are enabled, emit read barriers other than
5472 // Baker's using a slow path (and also unpoison the loaded
5473 // reference, if heap poisoning is enabled).
5474 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5475 }
5476 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005477 }
5478
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005479 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005480 if (is_volatile) {
5481 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5482 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005484 __ movd(out.AsRegisterPairLow<Register>(), temp);
5485 __ psrlq(temp, Immediate(32));
5486 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5487 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005488 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005489 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005490 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005491 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5492 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005493 break;
5494 }
5495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005496 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005497 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005498 break;
5499 }
5500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005502 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005503 break;
5504 }
5505
Aart Bik66c158e2018-01-31 12:55:04 -08005506 case DataType::Type::kUint32:
5507 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005508 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005509 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005510 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005511 }
Calin Juravle52c48962014-12-16 17:02:57 +00005512
Vladimir Marko61b92282017-10-11 13:23:17 +01005513 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005514 // Potential implicit null checks, in the case of reference or
5515 // long fields, are handled in the previous switch statement.
5516 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005517 codegen_->MaybeRecordImplicitNullCheck(instruction);
5518 }
5519
Calin Juravle52c48962014-12-16 17:02:57 +00005520 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005521 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005522 // Memory barriers, in the case of references, are also handled
5523 // in the previous switch statement.
5524 } else {
5525 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5526 }
Roland Levillain4d027112015-07-01 15:41:14 +01005527 }
Calin Juravle52c48962014-12-16 17:02:57 +00005528}
5529
5530void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5531 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5532
5533 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005534 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005535 locations->SetInAt(0, Location::RequiresRegister());
5536 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005537 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005538 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005539
5540 // The register allocator does not support multiple
5541 // inputs that die at entry with one in a specific register.
5542 if (is_byte_type) {
5543 // Ensure the value is in a byte register.
5544 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 } else if (DataType::IsFloatingPointType(field_type)) {
5546 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005547 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5548 locations->SetInAt(1, Location::RequiresFpuRegister());
5549 } else {
5550 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5551 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005552 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005553 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005554 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005555
Calin Juravle52c48962014-12-16 17:02:57 +00005556 // 64bits value can be atomically written to an address with movsd and an XMM register.
5557 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5558 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5559 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5560 // isolated cases when we need this it isn't worth adding the extra complexity.
5561 locations->AddTemp(Location::RequiresFpuRegister());
5562 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005563 } else {
5564 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5565
5566 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5567 // Temporary registers for the write barrier.
5568 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5569 // Ensure the card is in a byte register.
5570 locations->AddTemp(Location::RegisterLocation(ECX));
5571 }
Calin Juravle52c48962014-12-16 17:02:57 +00005572 }
5573}
5574
5575void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005576 const FieldInfo& field_info,
5577 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005578 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5579
5580 LocationSummary* locations = instruction->GetLocations();
5581 Register base = locations->InAt(0).AsRegister<Register>();
5582 Location value = locations->InAt(1);
5583 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005584 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005585 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005586 bool needs_write_barrier =
5587 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005588
5589 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005590 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005591 }
5592
Mark Mendell81489372015-11-04 11:30:41 -05005593 bool maybe_record_implicit_null_check_done = false;
5594
Calin Juravle52c48962014-12-16 17:02:57 +00005595 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005597 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005599 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5600 break;
5601 }
5602
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005603 case DataType::Type::kUint16:
5604 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005605 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005606 __ movw(Address(base, offset),
5607 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005608 } else {
5609 __ movw(Address(base, offset), value.AsRegister<Register>());
5610 }
Calin Juravle52c48962014-12-16 17:02:57 +00005611 break;
5612 }
5613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005614 case DataType::Type::kInt32:
5615 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005616 if (kPoisonHeapReferences && needs_write_barrier) {
5617 // Note that in the case where `value` is a null reference,
5618 // we do not enter this block, as the reference does not
5619 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005620 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005621 Register temp = locations->GetTemp(0).AsRegister<Register>();
5622 __ movl(temp, value.AsRegister<Register>());
5623 __ PoisonHeapReference(temp);
5624 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005625 } else if (value.IsConstant()) {
5626 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5627 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005628 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005629 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005630 __ movl(Address(base, offset), value.AsRegister<Register>());
5631 }
Calin Juravle52c48962014-12-16 17:02:57 +00005632 break;
5633 }
5634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005635 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005636 if (is_volatile) {
5637 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5638 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5639 __ movd(temp1, value.AsRegisterPairLow<Register>());
5640 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5641 __ punpckldq(temp1, temp2);
5642 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005643 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005644 } else if (value.IsConstant()) {
5645 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5646 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5647 codegen_->MaybeRecordImplicitNullCheck(instruction);
5648 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005649 } else {
5650 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005651 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005652 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5653 }
Mark Mendell81489372015-11-04 11:30:41 -05005654 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005655 break;
5656 }
5657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005658 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005659 if (value.IsConstant()) {
5660 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5661 __ movl(Address(base, offset), Immediate(v));
5662 } else {
5663 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5664 }
Calin Juravle52c48962014-12-16 17:02:57 +00005665 break;
5666 }
5667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005668 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005669 if (value.IsConstant()) {
5670 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5671 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5672 codegen_->MaybeRecordImplicitNullCheck(instruction);
5673 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5674 maybe_record_implicit_null_check_done = true;
5675 } else {
5676 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5677 }
Calin Juravle52c48962014-12-16 17:02:57 +00005678 break;
5679 }
5680
Aart Bik66c158e2018-01-31 12:55:04 -08005681 case DataType::Type::kUint32:
5682 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005684 LOG(FATAL) << "Unreachable type " << field_type;
5685 UNREACHABLE();
5686 }
5687
Mark Mendell81489372015-11-04 11:30:41 -05005688 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005689 codegen_->MaybeRecordImplicitNullCheck(instruction);
5690 }
5691
Roland Levillain4d027112015-07-01 15:41:14 +01005692 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005693 Register temp = locations->GetTemp(0).AsRegister<Register>();
5694 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005695 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005696 }
5697
Calin Juravle52c48962014-12-16 17:02:57 +00005698 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005699 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005700 }
5701}
5702
5703void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5704 HandleFieldGet(instruction, instruction->GetFieldInfo());
5705}
5706
5707void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5708 HandleFieldGet(instruction, instruction->GetFieldInfo());
5709}
5710
5711void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5712 HandleFieldSet(instruction, instruction->GetFieldInfo());
5713}
5714
5715void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005716 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005717}
5718
5719void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5720 HandleFieldSet(instruction, instruction->GetFieldInfo());
5721}
5722
5723void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005724 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005725}
5726
5727void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5728 HandleFieldGet(instruction, instruction->GetFieldInfo());
5729}
5730
5731void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5732 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733}
5734
Vladimir Marko552a1342017-10-31 10:56:47 +00005735void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5736 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5737}
5738
5739void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5740 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5741 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5742}
5743
Calin Juravlee460d1d2015-09-29 04:52:17 +01005744void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5745 HUnresolvedInstanceFieldGet* instruction) {
5746 FieldAccessCallingConventionX86 calling_convention;
5747 codegen_->CreateUnresolvedFieldLocationSummary(
5748 instruction, instruction->GetFieldType(), calling_convention);
5749}
5750
5751void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5752 HUnresolvedInstanceFieldGet* instruction) {
5753 FieldAccessCallingConventionX86 calling_convention;
5754 codegen_->GenerateUnresolvedFieldAccess(instruction,
5755 instruction->GetFieldType(),
5756 instruction->GetFieldIndex(),
5757 instruction->GetDexPc(),
5758 calling_convention);
5759}
5760
5761void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5762 HUnresolvedInstanceFieldSet* instruction) {
5763 FieldAccessCallingConventionX86 calling_convention;
5764 codegen_->CreateUnresolvedFieldLocationSummary(
5765 instruction, instruction->GetFieldType(), calling_convention);
5766}
5767
5768void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5769 HUnresolvedInstanceFieldSet* instruction) {
5770 FieldAccessCallingConventionX86 calling_convention;
5771 codegen_->GenerateUnresolvedFieldAccess(instruction,
5772 instruction->GetFieldType(),
5773 instruction->GetFieldIndex(),
5774 instruction->GetDexPc(),
5775 calling_convention);
5776}
5777
5778void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5779 HUnresolvedStaticFieldGet* instruction) {
5780 FieldAccessCallingConventionX86 calling_convention;
5781 codegen_->CreateUnresolvedFieldLocationSummary(
5782 instruction, instruction->GetFieldType(), calling_convention);
5783}
5784
5785void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5786 HUnresolvedStaticFieldGet* instruction) {
5787 FieldAccessCallingConventionX86 calling_convention;
5788 codegen_->GenerateUnresolvedFieldAccess(instruction,
5789 instruction->GetFieldType(),
5790 instruction->GetFieldIndex(),
5791 instruction->GetDexPc(),
5792 calling_convention);
5793}
5794
5795void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5796 HUnresolvedStaticFieldSet* instruction) {
5797 FieldAccessCallingConventionX86 calling_convention;
5798 codegen_->CreateUnresolvedFieldLocationSummary(
5799 instruction, instruction->GetFieldType(), calling_convention);
5800}
5801
5802void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5803 HUnresolvedStaticFieldSet* instruction) {
5804 FieldAccessCallingConventionX86 calling_convention;
5805 codegen_->GenerateUnresolvedFieldAccess(instruction,
5806 instruction->GetFieldType(),
5807 instruction->GetFieldIndex(),
5808 instruction->GetDexPc(),
5809 calling_convention);
5810}
5811
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005813 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5814 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5815 ? Location::RequiresRegister()
5816 : Location::Any();
5817 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005818}
5819
Calin Juravle2ae48182016-03-16 14:05:09 +00005820void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5821 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005822 return;
5823 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005824 LocationSummary* locations = instruction->GetLocations();
5825 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005826
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005827 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005828 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005829}
5830
Calin Juravle2ae48182016-03-16 14:05:09 +00005831void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005832 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005833 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834
5835 LocationSummary* locations = instruction->GetLocations();
5836 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005837
5838 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005839 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005840 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005841 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005842 } else {
5843 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005844 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005845 __ jmp(slow_path->GetEntryLabel());
5846 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005847 }
5848 __ j(kEqual, slow_path->GetEntryLabel());
5849}
5850
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005851void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005852 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005853}
5854
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005855void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005858 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005859 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5860 object_array_get_with_read_barrier
5861 ? LocationSummary::kCallOnSlowPath
5862 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005863 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005864 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005865 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005866 locations->SetInAt(0, Location::RequiresRegister());
5867 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005868 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005869 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5870 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005871 // The output overlaps in case of long: we don't want the low move
5872 // to overwrite the array's location. Likewise, in the case of an
5873 // object array get with read barriers enabled, we do not want the
5874 // move to overwrite the array's location, as we need it to emit
5875 // the read barrier.
5876 locations->SetOut(
5877 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005878 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5879 ? Location::kOutputOverlap
5880 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005881 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005882}
5883
5884void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5885 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005886 Location obj_loc = locations->InAt(0);
5887 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005888 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005889 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005890 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005892 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005893 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005894 case DataType::Type::kBool:
5895 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005896 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005897 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005898 break;
5899 }
5900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005902 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005903 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005904 break;
5905 }
5906
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005908 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005909 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5910 // Branch cases into compressed and uncompressed for each index's type.
5911 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5912 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005913 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005914 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005915 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5916 "Expecting 0=compressed, 1=uncompressed");
5917 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005918 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5919 __ jmp(&done);
5920 __ Bind(&not_compressed);
5921 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5922 __ Bind(&done);
5923 } else {
5924 // Common case for charAt of array of char or when string compression's
5925 // feature is turned off.
5926 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5927 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005928 break;
5929 }
5930
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005931 case DataType::Type::kInt16: {
5932 Register out = out_loc.AsRegister<Register>();
5933 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5934 break;
5935 }
5936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005938 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005939 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005940 break;
5941 }
5942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005944 static_assert(
5945 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5946 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005947 // /* HeapReference<Object> */ out =
5948 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5949 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005950 // Note that a potential implicit null check is handled in this
5951 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5952 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005953 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005954 } else {
5955 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005956 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5957 codegen_->MaybeRecordImplicitNullCheck(instruction);
5958 // If read barriers are enabled, emit read barriers other than
5959 // Baker's using a slow path (and also unpoison the loaded
5960 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005961 if (index.IsConstant()) {
5962 uint32_t offset =
5963 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005964 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5965 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005966 codegen_->MaybeGenerateReadBarrierSlow(
5967 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5968 }
5969 }
5970 break;
5971 }
5972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005973 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005974 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005975 __ movl(out_loc.AsRegisterPairLow<Register>(),
5976 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5977 codegen_->MaybeRecordImplicitNullCheck(instruction);
5978 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5979 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005980 break;
5981 }
5982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005984 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005985 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005986 break;
5987 }
5988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005989 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005990 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005991 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005992 break;
5993 }
5994
Aart Bik66c158e2018-01-31 12:55:04 -08005995 case DataType::Type::kUint32:
5996 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005997 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005998 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005999 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006000 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006002 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006003 // Potential implicit null checks, in the case of reference or
6004 // long arrays, are handled in the previous switch statement.
6005 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006006 codegen_->MaybeRecordImplicitNullCheck(instruction);
6007 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006008}
6009
6010void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006013 bool needs_write_barrier =
6014 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006015 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006016
Vladimir Markoca6fff82017-10-03 14:49:14 +01006017 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006018 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006019 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006020
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006021 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006022 // We need the inputs to be different than the output in case of long operation.
6023 // In case of a byte operation, the register allocator does not support multiple
6024 // inputs that die at entry with one in a specific register.
6025 locations->SetInAt(0, Location::RequiresRegister());
6026 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6027 if (is_byte_type) {
6028 // Ensure the value is in a byte register.
6029 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006031 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006032 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006033 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6034 }
6035 if (needs_write_barrier) {
6036 // Temporary registers for the write barrier.
6037 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6038 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006039 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041}
6042
6043void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6044 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045 Location array_loc = locations->InAt(0);
6046 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006047 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006048 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006050 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006051 bool needs_write_barrier =
6052 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006053
6054 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006055 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006056 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006057 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006058 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006059 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006060 if (value.IsRegister()) {
6061 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006062 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006063 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006064 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006065 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006066 break;
6067 }
6068
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006069 case DataType::Type::kUint16:
6070 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006071 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006072 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006073 if (value.IsRegister()) {
6074 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006075 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006076 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006077 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006078 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006079 break;
6080 }
6081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006083 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006084 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006085
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006086 if (!value.IsRegister()) {
6087 // Just setting null.
6088 DCHECK(instruction->InputAt(2)->IsNullConstant());
6089 DCHECK(value.IsConstant()) << value;
6090 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006091 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006092 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006093 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006094 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006095 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006096
6097 DCHECK(needs_write_barrier);
6098 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006099 Location temp_loc = locations->GetTemp(0);
6100 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006101
6102 bool can_value_be_null = instruction->GetValueCanBeNull();
6103 NearLabel do_store;
6104 if (can_value_be_null) {
6105 __ testl(register_value, register_value);
6106 __ j(kEqual, &do_store);
6107 }
6108
6109 SlowPathCode* slow_path = nullptr;
6110 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006111 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006112 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006113
6114 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6115 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6116 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006117
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006118 // Note that when Baker read barriers are enabled, the type
6119 // checks are performed without read barriers. This is fine,
6120 // even in the case where a class object is in the from-space
6121 // after the flip, as a comparison involving such a type would
6122 // not produce a false positive; it may of course produce a
6123 // false negative, in which case we would take the ArraySet
6124 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006125
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006126 // /* HeapReference<Class> */ temp = array->klass_
6127 __ movl(temp, Address(array, class_offset));
6128 codegen_->MaybeRecordImplicitNullCheck(instruction);
6129 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006130
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006131 // /* HeapReference<Class> */ temp = temp->component_type_
6132 __ movl(temp, Address(temp, component_offset));
6133 // If heap poisoning is enabled, no need to unpoison `temp`
6134 // nor the object reference in `register_value->klass`, as
6135 // we are comparing two poisoned references.
6136 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006137
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006138 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006139 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006140 __ j(kEqual, &do_put);
6141 // If heap poisoning is enabled, the `temp` reference has
6142 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006143 __ MaybeUnpoisonHeapReference(temp);
6144
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006145 // If heap poisoning is enabled, no need to unpoison the
6146 // heap reference loaded below, as it is only used for a
6147 // comparison with null.
6148 __ cmpl(Address(temp, super_offset), Immediate(0));
6149 __ j(kNotEqual, slow_path->GetEntryLabel());
6150 __ Bind(&do_put);
6151 } else {
6152 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006153 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006154 }
6155
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006156 Register card = locations->GetTemp(1).AsRegister<Register>();
6157 codegen_->MarkGCCard(
6158 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6159
6160 if (can_value_be_null) {
6161 DCHECK(do_store.IsLinked());
6162 __ Bind(&do_store);
6163 }
6164
6165 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006166 if (kPoisonHeapReferences) {
6167 __ movl(temp, register_value);
6168 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006169 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006170 }
6171
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006172 __ movl(address, source);
6173
6174 if (can_value_be_null || !needs_type_check) {
6175 codegen_->MaybeRecordImplicitNullCheck(instruction);
6176 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006177
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006178 if (slow_path != nullptr) {
6179 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006180 }
6181
6182 break;
6183 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006185 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006186 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006187 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006188 if (value.IsRegister()) {
6189 __ movl(address, value.AsRegister<Register>());
6190 } else {
6191 DCHECK(value.IsConstant()) << value;
6192 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6193 __ movl(address, Immediate(v));
6194 }
6195 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006196 break;
6197 }
6198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006199 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006200 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006201 if (value.IsRegisterPair()) {
6202 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6203 value.AsRegisterPairLow<Register>());
6204 codegen_->MaybeRecordImplicitNullCheck(instruction);
6205 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6206 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006207 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006208 DCHECK(value.IsConstant());
6209 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6210 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6211 Immediate(Low32Bits(val)));
6212 codegen_->MaybeRecordImplicitNullCheck(instruction);
6213 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6214 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006215 }
6216 break;
6217 }
6218
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006219 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006220 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006221 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006222 if (value.IsFpuRegister()) {
6223 __ movss(address, value.AsFpuRegister<XmmRegister>());
6224 } else {
6225 DCHECK(value.IsConstant());
6226 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6227 __ movl(address, Immediate(v));
6228 }
6229 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006230 break;
6231 }
6232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006234 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006235 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006236 if (value.IsFpuRegister()) {
6237 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6238 } else {
6239 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006240 Address address_hi =
6241 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006242 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6243 __ movl(address, Immediate(Low32Bits(v)));
6244 codegen_->MaybeRecordImplicitNullCheck(instruction);
6245 __ movl(address_hi, Immediate(High32Bits(v)));
6246 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006247 break;
6248 }
6249
Aart Bik66c158e2018-01-31 12:55:04 -08006250 case DataType::Type::kUint32:
6251 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006253 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006254 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006255 }
6256}
6257
6258void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006259 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006260 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006261 if (!instruction->IsEmittedAtUseSite()) {
6262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264}
6265
6266void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006267 if (instruction->IsEmittedAtUseSite()) {
6268 return;
6269 }
6270
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006271 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006272 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006273 Register obj = locations->InAt(0).AsRegister<Register>();
6274 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006275 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006276 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006277 // Mask out most significant bit in case the array is String's array of char.
6278 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006279 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006280 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006281}
6282
6283void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006284 RegisterSet caller_saves = RegisterSet::Empty();
6285 InvokeRuntimeCallingConvention calling_convention;
6286 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6287 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6288 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006289 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006290 HInstruction* length = instruction->InputAt(1);
6291 if (!length->IsEmittedAtUseSite()) {
6292 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6293 }
jessicahandojo4877b792016-09-08 19:49:13 -07006294 // Need register to see array's length.
6295 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6296 locations->AddTemp(Location::RequiresRegister());
6297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006298}
6299
6300void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006301 const bool is_string_compressed_char_at =
6302 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006303 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006304 Location index_loc = locations->InAt(0);
6305 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006306 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006307 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006308
Mark Mendell99dbd682015-04-22 16:18:52 -04006309 if (length_loc.IsConstant()) {
6310 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6311 if (index_loc.IsConstant()) {
6312 // BCE will remove the bounds check if we are guarenteed to pass.
6313 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6314 if (index < 0 || index >= length) {
6315 codegen_->AddSlowPath(slow_path);
6316 __ jmp(slow_path->GetEntryLabel());
6317 } else {
6318 // Some optimization after BCE may have generated this, and we should not
6319 // generate a bounds check if it is a valid range.
6320 }
6321 return;
6322 }
6323
6324 // We have to reverse the jump condition because the length is the constant.
6325 Register index_reg = index_loc.AsRegister<Register>();
6326 __ cmpl(index_reg, Immediate(length));
6327 codegen_->AddSlowPath(slow_path);
6328 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006329 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006330 HInstruction* array_length = instruction->InputAt(1);
6331 if (array_length->IsEmittedAtUseSite()) {
6332 // Address the length field in the array.
6333 DCHECK(array_length->IsArrayLength());
6334 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6335 Location array_loc = array_length->GetLocations()->InAt(0);
6336 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006337 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006338 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6339 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006340 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6341 __ movl(length_reg, array_len);
6342 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006343 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006344 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006345 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006346 // Checking bounds for general case:
6347 // Array of char or string's array with feature compression off.
6348 if (index_loc.IsConstant()) {
6349 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6350 __ cmpl(array_len, Immediate(value));
6351 } else {
6352 __ cmpl(array_len, index_loc.AsRegister<Register>());
6353 }
6354 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006355 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006356 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006357 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006358 }
6359 codegen_->AddSlowPath(slow_path);
6360 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006361 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006362}
6363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006364void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006365 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006366}
6367
6368void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006369 if (instruction->GetNext()->IsSuspendCheck() &&
6370 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6371 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6372 // The back edge will generate the suspend check.
6373 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6374 }
6375
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006376 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6377}
6378
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006379void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006380 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6381 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006382 // In suspend check slow path, usually there are no caller-save registers at all.
6383 // If SIMD instructions are present, however, we force spilling all live SIMD
6384 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006385 locations->SetCustomSlowPathCallerSaves(
6386 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006387}
6388
6389void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006390 HBasicBlock* block = instruction->GetBlock();
6391 if (block->GetLoopInformation() != nullptr) {
6392 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6393 // The back edge will generate the suspend check.
6394 return;
6395 }
6396 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6397 // The goto will generate the suspend check.
6398 return;
6399 }
6400 GenerateSuspendCheck(instruction, nullptr);
6401}
6402
6403void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6404 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006405 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006406 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6407 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006408 slow_path =
6409 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006410 instruction->SetSlowPath(slow_path);
6411 codegen_->AddSlowPath(slow_path);
6412 if (successor != nullptr) {
6413 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006414 }
6415 } else {
6416 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6417 }
6418
Andreas Gampe542451c2016-07-26 09:02:02 -07006419 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006420 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006421 if (successor == nullptr) {
6422 __ j(kNotEqual, slow_path->GetEntryLabel());
6423 __ Bind(slow_path->GetReturnLabel());
6424 } else {
6425 __ j(kEqual, codegen_->GetLabelOf(successor));
6426 __ jmp(slow_path->GetEntryLabel());
6427 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006428}
6429
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006430X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6431 return codegen_->GetAssembler();
6432}
6433
Aart Bikcfe50bb2017-12-12 14:54:12 -08006434void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006435 ScratchRegisterScope ensure_scratch(
6436 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6437 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6438 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006439
Aart Bikcfe50bb2017-12-12 14:54:12 -08006440 // Now that temp register is available (possibly spilled), move blocks of memory.
6441 for (int i = 0; i < number_of_words; i++) {
6442 __ movl(temp_reg, Address(ESP, src + stack_offset));
6443 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6444 stack_offset += kX86WordSize;
6445 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006446}
6447
6448void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006449 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006450 Location source = move->GetSource();
6451 Location destination = move->GetDestination();
6452
6453 if (source.IsRegister()) {
6454 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006455 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006456 } else if (destination.IsFpuRegister()) {
6457 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006458 } else {
6459 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006460 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006461 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006462 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006463 if (destination.IsRegisterPair()) {
6464 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6465 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6466 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6467 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006468 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Marko86c87522020-05-11 16:55:55 +01006469 // Push the 2 source registers to stack.
6470 __ pushl(source.AsRegisterPairHigh<Register>());
6471 __ cfi().AdjustCFAOffset(elem_size);
6472 __ pushl(source.AsRegisterPairLow<Register>());
6473 __ cfi().AdjustCFAOffset(elem_size);
6474 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006475 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6476 // And remove the temporary stack space we allocated.
6477 __ addl(ESP, Immediate(2 * elem_size));
Vladimir Marko86c87522020-05-11 16:55:55 +01006478 __ cfi().AdjustCFAOffset(-(2 * elem_size));
6479 } else {
6480 DCHECK(destination.IsDoubleStackSlot());
6481 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6482 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6483 source.AsRegisterPairHigh<Register>());
6484 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006485 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006486 if (destination.IsRegister()) {
6487 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6488 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006489 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006490 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006491 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6492 // Create stack space for 2 elements.
6493 __ subl(ESP, Immediate(2 * elem_size));
6494 __ cfi().AdjustCFAOffset(2 * elem_size);
6495 // Store the source register.
6496 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6497 // And pop the values into destination registers.
6498 __ popl(destination.AsRegisterPairLow<Register>());
6499 __ cfi().AdjustCFAOffset(-elem_size);
6500 __ popl(destination.AsRegisterPairHigh<Register>());
6501 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006502 } else if (destination.IsStackSlot()) {
6503 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006504 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006505 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006506 } else {
6507 DCHECK(destination.IsSIMDStackSlot());
6508 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006509 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006510 } else if (source.IsStackSlot()) {
6511 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006512 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006513 } else if (destination.IsFpuRegister()) {
6514 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006515 } else {
6516 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006517 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006518 }
6519 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006520 if (destination.IsRegisterPair()) {
6521 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6522 __ movl(destination.AsRegisterPairHigh<Register>(),
6523 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6524 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006525 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6526 } else {
6527 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006528 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006529 }
Aart Bik5576f372017-03-23 16:17:37 -07006530 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006531 if (destination.IsFpuRegister()) {
6532 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6533 } else {
6534 DCHECK(destination.IsSIMDStackSlot());
6535 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6536 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006537 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006538 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006539 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006540 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006541 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006542 if (value == 0) {
6543 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6544 } else {
6545 __ movl(destination.AsRegister<Register>(), Immediate(value));
6546 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006547 } else {
6548 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006549 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006550 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006551 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006552 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006553 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006554 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006555 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006556 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6557 if (value == 0) {
6558 // Easy handling of 0.0.
6559 __ xorps(dest, dest);
6560 } else {
6561 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006562 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6563 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6564 __ movl(temp, Immediate(value));
6565 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006566 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006567 } else {
6568 DCHECK(destination.IsStackSlot()) << destination;
6569 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6570 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006571 } else if (constant->IsLongConstant()) {
6572 int64_t value = constant->AsLongConstant()->GetValue();
6573 int32_t low_value = Low32Bits(value);
6574 int32_t high_value = High32Bits(value);
6575 Immediate low(low_value);
6576 Immediate high(high_value);
6577 if (destination.IsDoubleStackSlot()) {
6578 __ movl(Address(ESP, destination.GetStackIndex()), low);
6579 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6580 } else {
6581 __ movl(destination.AsRegisterPairLow<Register>(), low);
6582 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6583 }
6584 } else {
6585 DCHECK(constant->IsDoubleConstant());
6586 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006587 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006588 int32_t low_value = Low32Bits(value);
6589 int32_t high_value = High32Bits(value);
6590 Immediate low(low_value);
6591 Immediate high(high_value);
6592 if (destination.IsFpuRegister()) {
6593 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6594 if (value == 0) {
6595 // Easy handling of 0.0.
6596 __ xorpd(dest, dest);
6597 } else {
6598 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006599 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006600 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006601 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006602 __ movsd(dest, Address(ESP, 0));
6603 __ addl(ESP, Immediate(8));
Vladimir Marko86c87522020-05-11 16:55:55 +01006604 __ cfi().AdjustCFAOffset(-8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006605 }
6606 } else {
6607 DCHECK(destination.IsDoubleStackSlot()) << destination;
6608 __ movl(Address(ESP, destination.GetStackIndex()), low);
6609 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6610 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006611 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006612 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006613 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006614 }
6615}
6616
Mark Mendella5c19ce2015-04-01 12:51:05 -04006617void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006618 Register suggested_scratch = reg == EAX ? EBX : EAX;
6619 ScratchRegisterScope ensure_scratch(
6620 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6621
6622 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6623 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6624 __ movl(Address(ESP, mem + stack_offset), reg);
6625 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006626}
6627
Mark Mendell7c8d0092015-01-26 11:21:33 -05006628void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006629 ScratchRegisterScope ensure_scratch(
6630 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6631
6632 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6633 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6634 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6635 __ movss(Address(ESP, mem + stack_offset), reg);
6636 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006637}
6638
Aart Bikcfe50bb2017-12-12 14:54:12 -08006639void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6640 size_t extra_slot = 4 * kX86WordSize;
6641 __ subl(ESP, Immediate(extra_slot));
Vladimir Marko86c87522020-05-11 16:55:55 +01006642 __ cfi().AdjustCFAOffset(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006643 __ movups(Address(ESP, 0), XmmRegister(reg));
6644 ExchangeMemory(0, mem + extra_slot, 4);
6645 __ movups(XmmRegister(reg), Address(ESP, 0));
6646 __ addl(ESP, Immediate(extra_slot));
Vladimir Marko86c87522020-05-11 16:55:55 +01006647 __ cfi().AdjustCFAOffset(-extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006648}
6649
6650void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006651 ScratchRegisterScope ensure_scratch1(
6652 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006653
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006654 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6655 ScratchRegisterScope ensure_scratch2(
6656 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006657
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006658 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6659 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006660
6661 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6662 for (int i = 0; i < number_of_words; i++) {
6663 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6664 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6665 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6666 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6667 stack_offset += kX86WordSize;
6668 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006669}
6670
6671void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006672 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006673 Location source = move->GetSource();
6674 Location destination = move->GetDestination();
6675
6676 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006677 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6678 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6679 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6680 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6681 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006682 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006683 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006684 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006685 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006686 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006687 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006688 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6689 // Use XOR Swap algorithm to avoid a temporary.
6690 DCHECK_NE(source.reg(), destination.reg());
6691 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6692 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6693 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6694 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6695 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6696 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6697 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006698 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6699 // Take advantage of the 16 bytes in the XMM register.
6700 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6701 Address stack(ESP, destination.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.IsFpuRegister() && source.IsDoubleStackSlot()) {
6711 // Take advantage of the 16 bytes in the XMM register.
6712 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6713 Address stack(ESP, source.GetStackIndex());
6714 // Load the double into the high doubleword.
6715 __ movhpd(reg, stack);
6716
6717 // Store the low double into the destination.
6718 __ movsd(stack, reg);
6719
6720 // Move the high double to the low double.
6721 __ psrldq(reg, Immediate(8));
6722 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006723 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6724 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6725 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6726 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6727 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6728 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6729 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006730 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006731 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006732 }
6733}
6734
6735void ParallelMoveResolverX86::SpillScratch(int reg) {
6736 __ pushl(static_cast<Register>(reg));
6737}
6738
6739void ParallelMoveResolverX86::RestoreScratch(int reg) {
6740 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006741}
6742
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006743HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6744 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006745 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006746 case HLoadClass::LoadKind::kInvalid:
6747 LOG(FATAL) << "UNREACHABLE";
6748 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006749 case HLoadClass::LoadKind::kReferrersClass:
6750 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006751 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006752 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006753 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006754 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006755 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006756 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006757 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006758 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006759 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006760 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006761 break;
6762 }
6763 return desired_class_load_kind;
6764}
6765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006767 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006768 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006769 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006770 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006771 cls,
6772 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006773 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006774 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006775 return;
6776 }
Vladimir Marko41559982017-01-06 14:04:23 +00006777 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006778
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006779 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6780 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006781 ? LocationSummary::kCallOnSlowPath
6782 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006783 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006784 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006785 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006786 }
6787
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006788 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006789 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006790 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006791 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006792 locations->SetInAt(0, Location::RequiresRegister());
6793 }
6794 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006795 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6796 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6797 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006798 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006799 } else {
6800 // For non-Baker read barrier we have a temp-clobbering call.
6801 }
6802 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006803}
6804
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006805Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006806 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006807 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006808 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006809 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006810 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006811 PatchInfo<Label>* info = &jit_class_patches_.back();
6812 return &info->label;
6813}
6814
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006815// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6816// move.
6817void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006818 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006819 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006820 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006821 return;
6822 }
Vladimir Marko41559982017-01-06 14:04:23 +00006823 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006824
Vladimir Marko41559982017-01-06 14:04:23 +00006825 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006826 Location out_loc = locations->Out();
6827 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006828
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006829 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006830 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6831 ? kWithoutReadBarrier
6832 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006833 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006834 case HLoadClass::LoadKind::kReferrersClass: {
6835 DCHECK(!cls->CanCallRuntime());
6836 DCHECK(!cls->MustGenerateClinitCheck());
6837 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6838 Register current_method = locations->InAt(0).AsRegister<Register>();
6839 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006840 cls,
6841 out_loc,
6842 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006843 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006844 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006845 break;
6846 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006847 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006848 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6849 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006850 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006851 Register method_address = locations->InAt(0).AsRegister<Register>();
6852 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006853 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006854 break;
6855 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006856 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006857 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6858 Register method_address = locations->InAt(0).AsRegister<Register>();
6859 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006860 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6861 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006862 break;
6863 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006864 case HLoadClass::LoadKind::kBssEntry: {
6865 Register method_address = locations->InAt(0).AsRegister<Register>();
6866 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6867 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6868 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006869 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006870 generate_null_check = true;
6871 break;
6872 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006873 case HLoadClass::LoadKind::kJitBootImageAddress: {
6874 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6875 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6876 DCHECK_NE(address, 0u);
6877 __ movl(out, Immediate(address));
6878 break;
6879 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006880 case HLoadClass::LoadKind::kJitTableAddress: {
6881 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6882 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006883 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006884 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006885 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006886 break;
6887 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006888 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006889 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006890 LOG(FATAL) << "UNREACHABLE";
6891 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006892 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006893
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006894 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6895 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006896 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006897 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006898
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006899 if (generate_null_check) {
6900 __ testl(out, out);
6901 __ j(kEqual, slow_path->GetEntryLabel());
6902 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006903
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006904 if (cls->MustGenerateClinitCheck()) {
6905 GenerateClassInitializationCheck(slow_path, out);
6906 } else {
6907 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006908 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006909 }
6910}
6911
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006912void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6913 InvokeRuntimeCallingConvention calling_convention;
6914 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6915 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6916}
6917
6918void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6919 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6920}
6921
Orion Hodson18259d72018-04-12 11:18:23 +01006922void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6923 InvokeRuntimeCallingConvention calling_convention;
6924 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6925 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6926}
6927
6928void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6929 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6930}
6931
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006932void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6933 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006934 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006935 locations->SetInAt(0, Location::RequiresRegister());
6936 if (check->HasUses()) {
6937 locations->SetOut(Location::SameAsFirstInput());
6938 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006939 // Rely on the type initialization to save everything we need.
6940 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006941}
6942
6943void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006944 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006945 SlowPathCode* slow_path =
6946 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006947 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006948 GenerateClassInitializationCheck(slow_path,
6949 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006950}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006951
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006952void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006953 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006954 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6955 const size_t status_byte_offset =
6956 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006957 constexpr uint32_t shifted_visibly_initialized_value =
6958 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006959
Vladimir Markobf121912019-06-04 13:49:05 +01006960 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006961 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006962 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006963}
6964
Vladimir Marko175e7862018-03-27 09:03:13 +00006965void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6966 Register temp) {
6967 uint32_t path_to_root = check->GetBitstringPathToRoot();
6968 uint32_t mask = check->GetBitstringMask();
6969 DCHECK(IsPowerOfTwo(mask + 1));
6970 size_t mask_bits = WhichPowerOf2(mask + 1);
6971
6972 if (mask_bits == 16u) {
6973 // Compare the bitstring in memory.
6974 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6975 } else {
6976 // /* uint32_t */ temp = temp->status_
6977 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6978 // Compare the bitstring bits using SUB.
6979 __ subl(temp, Immediate(path_to_root));
6980 // Shift out bits that do not contribute to the comparison.
6981 __ shll(temp, Immediate(32u - mask_bits));
6982 }
6983}
6984
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006985HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6986 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006987 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006988 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006989 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006990 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006991 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006992 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006993 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006994 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006995 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006996 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006997 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006998 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006999 }
7000 return desired_string_load_kind;
7001}
7002
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007003void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007004 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007005 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007006 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007007 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007008 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007009 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007010 locations->SetInAt(0, Location::RequiresRegister());
7011 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007012 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007013 locations->SetOut(Location::RegisterLocation(EAX));
7014 } else {
7015 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007016 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7017 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007018 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007019 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007020 } else {
7021 // For non-Baker read barrier we have a temp-clobbering call.
7022 }
7023 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007024 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007025}
7026
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007027Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007028 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007029 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007030 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007031 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007032 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007033 PatchInfo<Label>* info = &jit_string_patches_.back();
7034 return &info->label;
7035}
7036
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007037// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7038// move.
7039void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007040 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007041 Location out_loc = locations->Out();
7042 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007043
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007044 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007045 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007046 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7047 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007048 Register method_address = locations->InAt(0).AsRegister<Register>();
7049 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007050 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007051 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007052 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007053 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007054 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7055 Register method_address = locations->InAt(0).AsRegister<Register>();
7056 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007057 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7058 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007059 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007060 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007061 case HLoadString::LoadKind::kBssEntry: {
7062 Register method_address = locations->InAt(0).AsRegister<Register>();
7063 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
7064 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007065 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007066 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007067 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007068 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007069 codegen_->AddSlowPath(slow_path);
7070 __ testl(out, out);
7071 __ j(kEqual, slow_path->GetEntryLabel());
7072 __ Bind(slow_path->GetExitLabel());
7073 return;
7074 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007075 case HLoadString::LoadKind::kJitBootImageAddress: {
7076 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7077 DCHECK_NE(address, 0u);
7078 __ movl(out, Immediate(address));
7079 return;
7080 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007081 case HLoadString::LoadKind::kJitTableAddress: {
7082 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
7083 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007084 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007085 // /* GcRoot<mirror::String> */ out = *address
7086 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7087 return;
7088 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007089 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007090 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007091 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007092
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007093 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007094 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007095 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007096 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007097 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7098 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007099}
7100
David Brazdilcb1c0552015-08-04 16:22:25 +01007101static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007102 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007103}
7104
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007105void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7106 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007107 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007108 locations->SetOut(Location::RequiresRegister());
7109}
7110
7111void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007112 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7113}
7114
7115void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007116 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007117}
7118
7119void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7120 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007121}
7122
7123void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007124 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7125 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007126 InvokeRuntimeCallingConvention calling_convention;
7127 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7128}
7129
7130void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007131 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007132 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007133}
7134
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007135// Temp is used for read barrier.
7136static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7137 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007138 !kUseBakerReadBarrier &&
7139 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007141 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7142 return 1;
7143 }
7144 return 0;
7145}
7146
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007147// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7148// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007149// The other checks have one temp for loading the object's class.
7150static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007151 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007152 return 2;
7153 }
7154 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007155}
7156
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007157void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007158 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007159 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007160 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007161 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007162 case TypeCheckKind::kExactCheck:
7163 case TypeCheckKind::kAbstractClassCheck:
7164 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007165 case TypeCheckKind::kArrayObjectCheck: {
7166 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7167 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7168 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007169 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007170 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007171 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007172 case TypeCheckKind::kUnresolvedCheck:
7173 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007174 call_kind = LocationSummary::kCallOnSlowPath;
7175 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007176 case TypeCheckKind::kBitstringCheck:
7177 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007178 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007179
Vladimir Markoca6fff82017-10-03 14:49:14 +01007180 LocationSummary* locations =
7181 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007182 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007183 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007184 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007185 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007186 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7187 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7188 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7189 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7190 } else {
7191 locations->SetInAt(1, Location::Any());
7192 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007193 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7194 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007195 // When read barriers are enabled, we need a temporary register for some cases.
7196 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007197}
7198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007199void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007200 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007201 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007202 Location obj_loc = locations->InAt(0);
7203 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007204 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007205 Location out_loc = locations->Out();
7206 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007207 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7208 DCHECK_LE(num_temps, 1u);
7209 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007210 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007211 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7212 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7213 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007214 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007215 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007216
7217 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007218 // Avoid null check if we know obj is not null.
7219 if (instruction->MustDoNullCheck()) {
7220 __ testl(obj, obj);
7221 __ j(kEqual, &zero);
7222 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007223
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007225 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007226 ReadBarrierOption read_barrier_option =
7227 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007228 // /* HeapReference<Class> */ out = obj->klass_
7229 GenerateReferenceLoadTwoRegisters(instruction,
7230 out_loc,
7231 obj_loc,
7232 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007233 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007234 if (cls.IsRegister()) {
7235 __ cmpl(out, cls.AsRegister<Register>());
7236 } else {
7237 DCHECK(cls.IsStackSlot()) << cls;
7238 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7239 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007240
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007241 // Classes must be equal for the instanceof to succeed.
7242 __ j(kNotEqual, &zero);
7243 __ movl(out, Immediate(1));
7244 __ jmp(&done);
7245 break;
7246 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007248 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007249 ReadBarrierOption read_barrier_option =
7250 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007251 // /* HeapReference<Class> */ out = obj->klass_
7252 GenerateReferenceLoadTwoRegisters(instruction,
7253 out_loc,
7254 obj_loc,
7255 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007256 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007257 // If the class is abstract, we eagerly fetch the super class of the
7258 // object to avoid doing a comparison we know will fail.
7259 NearLabel loop;
7260 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007261 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007262 GenerateReferenceLoadOneRegister(instruction,
7263 out_loc,
7264 super_offset,
7265 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007266 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007267 __ testl(out, out);
7268 // If `out` is null, we use it for the result, and jump to `done`.
7269 __ j(kEqual, &done);
7270 if (cls.IsRegister()) {
7271 __ cmpl(out, cls.AsRegister<Register>());
7272 } else {
7273 DCHECK(cls.IsStackSlot()) << cls;
7274 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7275 }
7276 __ j(kNotEqual, &loop);
7277 __ movl(out, Immediate(1));
7278 if (zero.IsLinked()) {
7279 __ jmp(&done);
7280 }
7281 break;
7282 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007283
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007284 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007285 ReadBarrierOption read_barrier_option =
7286 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007287 // /* HeapReference<Class> */ out = obj->klass_
7288 GenerateReferenceLoadTwoRegisters(instruction,
7289 out_loc,
7290 obj_loc,
7291 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007292 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007293 // Walk over the class hierarchy to find a match.
7294 NearLabel loop, success;
7295 __ Bind(&loop);
7296 if (cls.IsRegister()) {
7297 __ cmpl(out, cls.AsRegister<Register>());
7298 } else {
7299 DCHECK(cls.IsStackSlot()) << cls;
7300 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7301 }
7302 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007303 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007304 GenerateReferenceLoadOneRegister(instruction,
7305 out_loc,
7306 super_offset,
7307 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007308 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007309 __ testl(out, out);
7310 __ j(kNotEqual, &loop);
7311 // If `out` is null, we use it for the result, and jump to `done`.
7312 __ jmp(&done);
7313 __ Bind(&success);
7314 __ movl(out, Immediate(1));
7315 if (zero.IsLinked()) {
7316 __ jmp(&done);
7317 }
7318 break;
7319 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007321 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007322 ReadBarrierOption read_barrier_option =
7323 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007324 // /* HeapReference<Class> */ out = obj->klass_
7325 GenerateReferenceLoadTwoRegisters(instruction,
7326 out_loc,
7327 obj_loc,
7328 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007329 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007330 // Do an exact check.
7331 NearLabel exact_check;
7332 if (cls.IsRegister()) {
7333 __ cmpl(out, cls.AsRegister<Register>());
7334 } else {
7335 DCHECK(cls.IsStackSlot()) << cls;
7336 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7337 }
7338 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007339 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007340 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007341 GenerateReferenceLoadOneRegister(instruction,
7342 out_loc,
7343 component_offset,
7344 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007345 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007346 __ testl(out, out);
7347 // If `out` is null, we use it for the result, and jump to `done`.
7348 __ j(kEqual, &done);
7349 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7350 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007351 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007352 __ movl(out, Immediate(1));
7353 __ jmp(&done);
7354 break;
7355 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007357 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007358 // No read barrier since the slow path will retry upon failure.
7359 // /* HeapReference<Class> */ out = obj->klass_
7360 GenerateReferenceLoadTwoRegisters(instruction,
7361 out_loc,
7362 obj_loc,
7363 class_offset,
7364 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007365 if (cls.IsRegister()) {
7366 __ cmpl(out, cls.AsRegister<Register>());
7367 } else {
7368 DCHECK(cls.IsStackSlot()) << cls;
7369 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7370 }
7371 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007372 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007373 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007374 codegen_->AddSlowPath(slow_path);
7375 __ j(kNotEqual, slow_path->GetEntryLabel());
7376 __ movl(out, Immediate(1));
7377 if (zero.IsLinked()) {
7378 __ jmp(&done);
7379 }
7380 break;
7381 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007382
Calin Juravle98893e12015-10-02 21:05:03 +01007383 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007384 case TypeCheckKind::kInterfaceCheck: {
7385 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007386 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007387 // cases.
7388 //
7389 // We cannot directly call the InstanceofNonTrivial runtime
7390 // entry point without resorting to a type checking slow path
7391 // here (i.e. by calling InvokeRuntime directly), as it would
7392 // require to assign fixed registers for the inputs of this
7393 // HInstanceOf instruction (following the runtime calling
7394 // convention), which might be cluttered by the potential first
7395 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007396 //
7397 // TODO: Introduce a new runtime entry point taking the object
7398 // to test (instead of its class) as argument, and let it deal
7399 // with the read barrier issues. This will let us refactor this
7400 // case of the `switch` code as it was previously (with a direct
7401 // call to the runtime not using a type checking slow path).
7402 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007403 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007404 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007405 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007406 codegen_->AddSlowPath(slow_path);
7407 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007408 if (zero.IsLinked()) {
7409 __ jmp(&done);
7410 }
7411 break;
7412 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007413
7414 case TypeCheckKind::kBitstringCheck: {
7415 // /* HeapReference<Class> */ temp = obj->klass_
7416 GenerateReferenceLoadTwoRegisters(instruction,
7417 out_loc,
7418 obj_loc,
7419 class_offset,
7420 kWithoutReadBarrier);
7421
7422 GenerateBitstringTypeCheckCompare(instruction, out);
7423 __ j(kNotEqual, &zero);
7424 __ movl(out, Immediate(1));
7425 __ jmp(&done);
7426 break;
7427 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007428 }
7429
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007430 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007431 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007432 __ xorl(out, out);
7433 }
7434
7435 if (done.IsLinked()) {
7436 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007437 }
7438
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007439 if (slow_path != nullptr) {
7440 __ Bind(slow_path->GetExitLabel());
7441 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007442}
7443
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007444void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007445 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007446 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007447 LocationSummary* locations =
7448 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007449 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007450 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7451 // Require a register for the interface check since there is a loop that compares the class to
7452 // a memory address.
7453 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007454 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7455 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7456 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7457 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007458 } else {
7459 locations->SetInAt(1, Location::Any());
7460 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007461 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007462 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7463}
7464
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007465void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007466 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007467 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007468 Location obj_loc = locations->InAt(0);
7469 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007470 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007471 Location temp_loc = locations->GetTemp(0);
7472 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007473 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7474 DCHECK_GE(num_temps, 1u);
7475 DCHECK_LE(num_temps, 2u);
7476 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7477 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7478 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7479 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7480 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7481 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7482 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7483 const uint32_t object_array_data_offset =
7484 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007485
Vladimir Marko87584542017-12-12 17:47:52 +00007486 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007487 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007488 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7489 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007490 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007491
Roland Levillain0d5a2812015-11-13 10:07:31 +00007492 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007493 // Avoid null check if we know obj is not null.
7494 if (instruction->MustDoNullCheck()) {
7495 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007496 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007497 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007498
Roland Levillain0d5a2812015-11-13 10:07:31 +00007499 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007500 case TypeCheckKind::kExactCheck:
7501 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007502 // /* HeapReference<Class> */ temp = obj->klass_
7503 GenerateReferenceLoadTwoRegisters(instruction,
7504 temp_loc,
7505 obj_loc,
7506 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007507 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007509 if (cls.IsRegister()) {
7510 __ cmpl(temp, cls.AsRegister<Register>());
7511 } else {
7512 DCHECK(cls.IsStackSlot()) << cls;
7513 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7514 }
7515 // Jump to slow path for throwing the exception or doing a
7516 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007517 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007518 break;
7519 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007520
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007521 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007522 // /* HeapReference<Class> */ temp = obj->klass_
7523 GenerateReferenceLoadTwoRegisters(instruction,
7524 temp_loc,
7525 obj_loc,
7526 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007527 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007529 // If the class is abstract, we eagerly fetch the super class of the
7530 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007531 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007532 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007533 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007534 GenerateReferenceLoadOneRegister(instruction,
7535 temp_loc,
7536 super_offset,
7537 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007538 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007539
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007540 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7541 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007542 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007543 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007544
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007545 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007546 if (cls.IsRegister()) {
7547 __ cmpl(temp, cls.AsRegister<Register>());
7548 } else {
7549 DCHECK(cls.IsStackSlot()) << cls;
7550 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7551 }
7552 __ j(kNotEqual, &loop);
7553 break;
7554 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007555
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007556 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007557 // /* HeapReference<Class> */ temp = obj->klass_
7558 GenerateReferenceLoadTwoRegisters(instruction,
7559 temp_loc,
7560 obj_loc,
7561 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007562 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007563
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007564 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007565 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007566 __ Bind(&loop);
7567 if (cls.IsRegister()) {
7568 __ cmpl(temp, cls.AsRegister<Register>());
7569 } else {
7570 DCHECK(cls.IsStackSlot()) << cls;
7571 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7572 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007573 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007574
Roland Levillain0d5a2812015-11-13 10:07:31 +00007575 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007576 GenerateReferenceLoadOneRegister(instruction,
7577 temp_loc,
7578 super_offset,
7579 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007580 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007581
7582 // If the class reference currently in `temp` is not null, jump
7583 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007584 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007585 __ j(kNotZero, &loop);
7586 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007587 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007588 break;
7589 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007590
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007591 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007592 // /* HeapReference<Class> */ temp = obj->klass_
7593 GenerateReferenceLoadTwoRegisters(instruction,
7594 temp_loc,
7595 obj_loc,
7596 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007597 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007598
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007599 // Do an exact check.
7600 if (cls.IsRegister()) {
7601 __ cmpl(temp, cls.AsRegister<Register>());
7602 } else {
7603 DCHECK(cls.IsStackSlot()) << cls;
7604 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7605 }
7606 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007607
7608 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007609 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007610 GenerateReferenceLoadOneRegister(instruction,
7611 temp_loc,
7612 component_offset,
7613 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007614 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007615
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007616 // If the component type is null (i.e. the object not an array), jump to the slow path to
7617 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007618 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007619 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007620
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007621 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007622 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007623 break;
7624 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007625
Calin Juravle98893e12015-10-02 21:05:03 +01007626 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007627 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007628 // We cannot directly call the CheckCast runtime entry point
7629 // without resorting to a type checking slow path here (i.e. by
7630 // calling InvokeRuntime directly), as it would require to
7631 // assign fixed registers for the inputs of this HInstanceOf
7632 // instruction (following the runtime calling convention), which
7633 // might be cluttered by the potential first read barrier
7634 // emission at the beginning of this method.
7635 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007636 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007637
7638 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007639 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7640 // We can not get false positives by doing this.
7641 // /* HeapReference<Class> */ temp = obj->klass_
7642 GenerateReferenceLoadTwoRegisters(instruction,
7643 temp_loc,
7644 obj_loc,
7645 class_offset,
7646 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007647
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007648 // /* HeapReference<Class> */ temp = temp->iftable_
7649 GenerateReferenceLoadTwoRegisters(instruction,
7650 temp_loc,
7651 temp_loc,
7652 iftable_offset,
7653 kWithoutReadBarrier);
7654 // Iftable is never null.
7655 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7656 // Maybe poison the `cls` for direct comparison with memory.
7657 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7658 // Loop through the iftable and check if any class matches.
7659 NearLabel start_loop;
7660 __ Bind(&start_loop);
7661 // Need to subtract first to handle the empty array case.
7662 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7663 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7664 // Go to next interface if the classes do not match.
7665 __ cmpl(cls.AsRegister<Register>(),
7666 CodeGeneratorX86::ArrayAddress(temp,
7667 maybe_temp2_loc,
7668 TIMES_4,
7669 object_array_data_offset));
7670 __ j(kNotEqual, &start_loop);
7671 // If `cls` was poisoned above, unpoison it.
7672 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007673 break;
7674 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007675
7676 case TypeCheckKind::kBitstringCheck: {
7677 // /* HeapReference<Class> */ temp = obj->klass_
7678 GenerateReferenceLoadTwoRegisters(instruction,
7679 temp_loc,
7680 obj_loc,
7681 class_offset,
7682 kWithoutReadBarrier);
7683
7684 GenerateBitstringTypeCheckCompare(instruction, temp);
7685 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7686 break;
7687 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007688 }
7689 __ Bind(&done);
7690
Roland Levillain0d5a2812015-11-13 10:07:31 +00007691 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007692}
7693
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007694void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007695 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7696 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007697 InvokeRuntimeCallingConvention calling_convention;
7698 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7699}
7700
7701void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007702 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7703 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007704 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007705 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007706 if (instruction->IsEnter()) {
7707 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7708 } else {
7709 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7710 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007711}
7712
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307713void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7714 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7715 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7716 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7717 locations->SetInAt(0, Location::RequiresRegister());
7718 locations->SetInAt(1, Location::RequiresRegister());
7719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7720}
7721
7722void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7723 LocationSummary* locations = instruction->GetLocations();
7724 Location first = locations->InAt(0);
7725 Location second = locations->InAt(1);
7726 Location dest = locations->Out();
7727 if (instruction->GetResultType() == DataType::Type::kInt32) {
7728 __ andn(dest.AsRegister<Register>(),
7729 first.AsRegister<Register>(),
7730 second.AsRegister<Register>());
7731 } else {
7732 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7733 __ andn(dest.AsRegisterPairLow<Register>(),
7734 first.AsRegisterPairLow<Register>(),
7735 second.AsRegisterPairLow<Register>());
7736 __ andn(dest.AsRegisterPairHigh<Register>(),
7737 first.AsRegisterPairHigh<Register>(),
7738 second.AsRegisterPairHigh<Register>());
7739 }
7740}
7741
7742void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7743 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7744 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7745 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7746 locations->SetInAt(0, Location::RequiresRegister());
7747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7748}
7749
7750void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7751 HX86MaskOrResetLeastSetBit* instruction) {
7752 LocationSummary* locations = instruction->GetLocations();
7753 Location src = locations->InAt(0);
7754 Location dest = locations->Out();
7755 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7756 switch (instruction->GetOpKind()) {
7757 case HInstruction::kAnd:
7758 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7759 break;
7760 case HInstruction::kXor:
7761 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7762 break;
7763 default:
7764 LOG(FATAL) << "Unreachable";
7765 }
7766}
7767
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007768void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7769void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7770void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7771
7772void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7773 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007774 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007775 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7776 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007777 locations->SetInAt(0, Location::RequiresRegister());
7778 locations->SetInAt(1, Location::Any());
7779 locations->SetOut(Location::SameAsFirstInput());
7780}
7781
7782void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7783 HandleBitwiseOperation(instruction);
7784}
7785
7786void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7787 HandleBitwiseOperation(instruction);
7788}
7789
7790void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7791 HandleBitwiseOperation(instruction);
7792}
7793
7794void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7795 LocationSummary* locations = instruction->GetLocations();
7796 Location first = locations->InAt(0);
7797 Location second = locations->InAt(1);
7798 DCHECK(first.Equals(locations->Out()));
7799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007800 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007801 if (second.IsRegister()) {
7802 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007803 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007804 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007805 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007806 } else {
7807 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007808 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007809 }
7810 } else if (second.IsConstant()) {
7811 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007812 __ andl(first.AsRegister<Register>(),
7813 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007814 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007815 __ orl(first.AsRegister<Register>(),
7816 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007817 } else {
7818 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007819 __ xorl(first.AsRegister<Register>(),
7820 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007821 }
7822 } else {
7823 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007824 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007825 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007826 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007827 } else {
7828 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007829 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007830 }
7831 }
7832 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007833 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007834 if (second.IsRegisterPair()) {
7835 if (instruction->IsAnd()) {
7836 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7837 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7838 } else if (instruction->IsOr()) {
7839 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7840 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7841 } else {
7842 DCHECK(instruction->IsXor());
7843 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7844 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7845 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007846 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007847 if (instruction->IsAnd()) {
7848 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7849 __ andl(first.AsRegisterPairHigh<Register>(),
7850 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7851 } else if (instruction->IsOr()) {
7852 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7853 __ orl(first.AsRegisterPairHigh<Register>(),
7854 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7855 } else {
7856 DCHECK(instruction->IsXor());
7857 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7858 __ xorl(first.AsRegisterPairHigh<Register>(),
7859 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7860 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007861 } else {
7862 DCHECK(second.IsConstant()) << second;
7863 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007864 int32_t low_value = Low32Bits(value);
7865 int32_t high_value = High32Bits(value);
7866 Immediate low(low_value);
7867 Immediate high(high_value);
7868 Register first_low = first.AsRegisterPairLow<Register>();
7869 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007870 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007871 if (low_value == 0) {
7872 __ xorl(first_low, first_low);
7873 } else if (low_value != -1) {
7874 __ andl(first_low, low);
7875 }
7876 if (high_value == 0) {
7877 __ xorl(first_high, first_high);
7878 } else if (high_value != -1) {
7879 __ andl(first_high, high);
7880 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007881 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007882 if (low_value != 0) {
7883 __ orl(first_low, low);
7884 }
7885 if (high_value != 0) {
7886 __ orl(first_high, high);
7887 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007888 } else {
7889 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007890 if (low_value != 0) {
7891 __ xorl(first_low, low);
7892 }
7893 if (high_value != 0) {
7894 __ xorl(first_high, high);
7895 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007896 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007897 }
7898 }
7899}
7900
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007901void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7902 HInstruction* instruction,
7903 Location out,
7904 uint32_t offset,
7905 Location maybe_temp,
7906 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007907 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007908 if (read_barrier_option == kWithReadBarrier) {
7909 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007910 if (kUseBakerReadBarrier) {
7911 // Load with fast path based Baker's read barrier.
7912 // /* HeapReference<Object> */ out = *(out + offset)
7913 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007914 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007915 } else {
7916 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007917 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007918 // in the following move operation, as we will need it for the
7919 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007920 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007921 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007922 // /* HeapReference<Object> */ out = *(out + offset)
7923 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007924 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007925 }
7926 } else {
7927 // Plain load with no read barrier.
7928 // /* HeapReference<Object> */ out = *(out + offset)
7929 __ movl(out_reg, Address(out_reg, offset));
7930 __ MaybeUnpoisonHeapReference(out_reg);
7931 }
7932}
7933
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007934void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7935 HInstruction* instruction,
7936 Location out,
7937 Location obj,
7938 uint32_t offset,
7939 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007940 Register out_reg = out.AsRegister<Register>();
7941 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007942 if (read_barrier_option == kWithReadBarrier) {
7943 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007944 if (kUseBakerReadBarrier) {
7945 // Load with fast path based Baker's read barrier.
7946 // /* HeapReference<Object> */ out = *(obj + offset)
7947 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007948 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007949 } else {
7950 // Load with slow path based read barrier.
7951 // /* HeapReference<Object> */ out = *(obj + offset)
7952 __ movl(out_reg, Address(obj_reg, offset));
7953 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7954 }
7955 } else {
7956 // Plain load with no read barrier.
7957 // /* HeapReference<Object> */ out = *(obj + offset)
7958 __ movl(out_reg, Address(obj_reg, offset));
7959 __ MaybeUnpoisonHeapReference(out_reg);
7960 }
7961}
7962
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007963void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7964 HInstruction* instruction,
7965 Location root,
7966 const Address& address,
7967 Label* fixup_label,
7968 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007969 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007970 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007971 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007972 if (kUseBakerReadBarrier) {
7973 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7974 // Baker's read barrier are used:
7975 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007976 // root = obj.field;
7977 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7978 // if (temp != null) {
7979 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007980 // }
7981
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007982 // /* GcRoot<mirror::Object> */ root = *address
7983 __ movl(root_reg, address);
7984 if (fixup_label != nullptr) {
7985 __ Bind(fixup_label);
7986 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007987 static_assert(
7988 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7989 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7990 "have different sizes.");
7991 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7992 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7993 "have different sizes.");
7994
Vladimir Marko953437b2016-08-24 08:30:46 +00007995 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007996 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007997 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007998 codegen_->AddSlowPath(slow_path);
7999
Roland Levillaind966ce72017-02-09 16:20:14 +00008000 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8001 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008002 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008003 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8004 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008005 __ j(kNotEqual, slow_path->GetEntryLabel());
8006 __ Bind(slow_path->GetExitLabel());
8007 } else {
8008 // GC root loaded through a slow path for read barriers other
8009 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008010 // /* GcRoot<mirror::Object>* */ root = address
8011 __ leal(root_reg, address);
8012 if (fixup_label != nullptr) {
8013 __ Bind(fixup_label);
8014 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008015 // /* mirror::Object* */ root = root->Read()
8016 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8017 }
8018 } else {
8019 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008020 // /* GcRoot<mirror::Object> */ root = *address
8021 __ movl(root_reg, address);
8022 if (fixup_label != nullptr) {
8023 __ Bind(fixup_label);
8024 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008025 // Note that GC roots are not affected by heap poisoning, thus we
8026 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008027 }
8028}
8029
8030void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8031 Location ref,
8032 Register obj,
8033 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008034 bool needs_null_check) {
8035 DCHECK(kEmitCompilerReadBarrier);
8036 DCHECK(kUseBakerReadBarrier);
8037
8038 // /* HeapReference<Object> */ ref = *(obj + offset)
8039 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008040 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008041}
8042
8043void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8044 Location ref,
8045 Register obj,
8046 uint32_t data_offset,
8047 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008048 bool needs_null_check) {
8049 DCHECK(kEmitCompilerReadBarrier);
8050 DCHECK(kUseBakerReadBarrier);
8051
Roland Levillain3d312422016-06-23 13:53:42 +01008052 static_assert(
8053 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8054 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008055 // /* HeapReference<Object> */ ref =
8056 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008057 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008058 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008059}
8060
8061void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8062 Location ref,
8063 Register obj,
8064 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008065 bool needs_null_check,
8066 bool always_update_field,
8067 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008068 DCHECK(kEmitCompilerReadBarrier);
8069 DCHECK(kUseBakerReadBarrier);
8070
8071 // In slow path based read barriers, the read barrier call is
8072 // inserted after the original load. However, in fast path based
8073 // Baker's read barriers, we need to perform the load of
8074 // mirror::Object::monitor_ *before* the original reference load.
8075 // This load-load ordering is required by the read barrier.
8076 // The fast path/slow path (for Baker's algorithm) should look like:
8077 //
8078 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8079 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8080 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008081 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008082 // if (is_gray) {
8083 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8084 // }
8085 //
8086 // Note: the original implementation in ReadBarrier::Barrier is
8087 // slightly more complex as:
8088 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008089 // the high-bits of rb_state, which are expected to be all zeroes
8090 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8091 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008092 // - it performs additional checks that we do not do here for
8093 // performance reasons.
8094
8095 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008096 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8097
Vladimir Marko953437b2016-08-24 08:30:46 +00008098 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008099 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008100 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008101 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8102 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8103 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8104
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008105 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008106 // ref = ReadBarrier::Mark(ref);
8107 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8108 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008109 if (needs_null_check) {
8110 MaybeRecordImplicitNullCheck(instruction);
8111 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008112
8113 // Load fence to prevent load-load reordering.
8114 // Note that this is a no-op, thanks to the x86 memory model.
8115 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8116
8117 // The actual reference load.
8118 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008119 __ movl(ref_reg, src); // Flags are unaffected.
8120
8121 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8122 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008123 SlowPathCode* slow_path;
8124 if (always_update_field) {
8125 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008126 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008127 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008128 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008129 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008130 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008131 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008132 AddSlowPath(slow_path);
8133
8134 // We have done the "if" of the gray bit check above, now branch based on the flags.
8135 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008136
8137 // Object* ref = ref_addr->AsMirrorPtr()
8138 __ MaybeUnpoisonHeapReference(ref_reg);
8139
Roland Levillain7c1559a2015-12-15 10:55:36 +00008140 __ Bind(slow_path->GetExitLabel());
8141}
8142
8143void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8144 Location out,
8145 Location ref,
8146 Location obj,
8147 uint32_t offset,
8148 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008149 DCHECK(kEmitCompilerReadBarrier);
8150
Roland Levillain7c1559a2015-12-15 10:55:36 +00008151 // Insert a slow path based read barrier *after* the reference load.
8152 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008153 // If heap poisoning is enabled, the unpoisoning of the loaded
8154 // reference will be carried out by the runtime within the slow
8155 // path.
8156 //
8157 // Note that `ref` currently does not get unpoisoned (when heap
8158 // poisoning is enabled), which is alright as the `ref` argument is
8159 // not used by the artReadBarrierSlow entry point.
8160 //
8161 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008162 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008163 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8164 AddSlowPath(slow_path);
8165
Roland Levillain0d5a2812015-11-13 10:07:31 +00008166 __ jmp(slow_path->GetEntryLabel());
8167 __ Bind(slow_path->GetExitLabel());
8168}
8169
Roland Levillain7c1559a2015-12-15 10:55:36 +00008170void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8171 Location out,
8172 Location ref,
8173 Location obj,
8174 uint32_t offset,
8175 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008176 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008177 // Baker's read barriers shall be handled by the fast path
8178 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8179 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008180 // If heap poisoning is enabled, unpoisoning will be taken care of
8181 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008182 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008183 } else if (kPoisonHeapReferences) {
8184 __ UnpoisonHeapReference(out.AsRegister<Register>());
8185 }
8186}
8187
Roland Levillain7c1559a2015-12-15 10:55:36 +00008188void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8189 Location out,
8190 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008191 DCHECK(kEmitCompilerReadBarrier);
8192
Roland Levillain7c1559a2015-12-15 10:55:36 +00008193 // Insert a slow path based read barrier *after* the GC root load.
8194 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008195 // Note that GC roots are not affected by heap poisoning, so we do
8196 // not need to do anything special for this here.
8197 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008198 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008199 AddSlowPath(slow_path);
8200
Roland Levillain0d5a2812015-11-13 10:07:31 +00008201 __ jmp(slow_path->GetEntryLabel());
8202 __ Bind(slow_path->GetExitLabel());
8203}
8204
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008205void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008206 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008207 LOG(FATAL) << "Unreachable";
8208}
8209
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008210void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008211 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008212 LOG(FATAL) << "Unreachable";
8213}
8214
Mark Mendellfe57faa2015-09-18 09:26:15 -04008215// Simple implementation of packed switch - generate cascaded compare/jumps.
8216void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008218 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008219 locations->SetInAt(0, Location::RequiresRegister());
8220}
8221
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008222void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8223 int32_t lower_bound,
8224 uint32_t num_entries,
8225 HBasicBlock* switch_block,
8226 HBasicBlock* default_block) {
8227 // Figure out the correct compare values and jump conditions.
8228 // Handle the first compare/branch as a special case because it might
8229 // jump to the default case.
8230 DCHECK_GT(num_entries, 2u);
8231 Condition first_condition;
8232 uint32_t index;
8233 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8234 if (lower_bound != 0) {
8235 first_condition = kLess;
8236 __ cmpl(value_reg, Immediate(lower_bound));
8237 __ j(first_condition, codegen_->GetLabelOf(default_block));
8238 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008239
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008240 index = 1;
8241 } else {
8242 // Handle all the compare/jumps below.
8243 first_condition = kBelow;
8244 index = 0;
8245 }
8246
8247 // Handle the rest of the compare/jumps.
8248 for (; index + 1 < num_entries; index += 2) {
8249 int32_t compare_to_value = lower_bound + index + 1;
8250 __ cmpl(value_reg, Immediate(compare_to_value));
8251 // Jump to successors[index] if value < case_value[index].
8252 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8253 // Jump to successors[index + 1] if value == case_value[index + 1].
8254 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8255 }
8256
8257 if (index != num_entries) {
8258 // There are an odd number of entries. Handle the last one.
8259 DCHECK_EQ(index + 1, num_entries);
8260 __ cmpl(value_reg, Immediate(lower_bound + index));
8261 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008262 }
8263
8264 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008265 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8266 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008267 }
8268}
8269
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008270void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8271 int32_t lower_bound = switch_instr->GetStartValue();
8272 uint32_t num_entries = switch_instr->GetNumEntries();
8273 LocationSummary* locations = switch_instr->GetLocations();
8274 Register value_reg = locations->InAt(0).AsRegister<Register>();
8275
8276 GenPackedSwitchWithCompares(value_reg,
8277 lower_bound,
8278 num_entries,
8279 switch_instr->GetBlock(),
8280 switch_instr->GetDefaultBlock());
8281}
8282
Mark Mendell805b3b52015-09-18 14:10:29 -04008283void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8284 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008285 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008286 locations->SetInAt(0, Location::RequiresRegister());
8287
8288 // Constant area pointer.
8289 locations->SetInAt(1, Location::RequiresRegister());
8290
8291 // And the temporary we need.
8292 locations->AddTemp(Location::RequiresRegister());
8293}
8294
8295void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8296 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008297 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008298 LocationSummary* locations = switch_instr->GetLocations();
8299 Register value_reg = locations->InAt(0).AsRegister<Register>();
8300 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8301
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008302 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8303 GenPackedSwitchWithCompares(value_reg,
8304 lower_bound,
8305 num_entries,
8306 switch_instr->GetBlock(),
8307 default_block);
8308 return;
8309 }
8310
Mark Mendell805b3b52015-09-18 14:10:29 -04008311 // Optimizing has a jump area.
8312 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8313 Register constant_area = locations->InAt(1).AsRegister<Register>();
8314
8315 // Remove the bias, if needed.
8316 if (lower_bound != 0) {
8317 __ leal(temp_reg, Address(value_reg, -lower_bound));
8318 value_reg = temp_reg;
8319 }
8320
8321 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008322 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008323 __ cmpl(value_reg, Immediate(num_entries - 1));
8324 __ j(kAbove, codegen_->GetLabelOf(default_block));
8325
8326 // We are in the range of the table.
8327 // Load (target-constant_area) from the jump table, indexing by the value.
8328 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8329
8330 // Compute the actual target address by adding in constant_area.
8331 __ addl(temp_reg, constant_area);
8332
8333 // And jump.
8334 __ jmp(temp_reg);
8335}
8336
Mark Mendell0616ae02015-04-17 12:49:27 -04008337void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8338 HX86ComputeBaseMethodAddress* insn) {
8339 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008340 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008341 locations->SetOut(Location::RequiresRegister());
8342}
8343
8344void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8345 HX86ComputeBaseMethodAddress* insn) {
8346 LocationSummary* locations = insn->GetLocations();
8347 Register reg = locations->Out().AsRegister<Register>();
8348
8349 // Generate call to next instruction.
8350 Label next_instruction;
8351 __ call(&next_instruction);
8352 __ Bind(&next_instruction);
8353
8354 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008355 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008356
8357 // Grab the return address off the stack.
8358 __ popl(reg);
8359}
8360
8361void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8362 HX86LoadFromConstantTable* insn) {
8363 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008364 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008365
8366 locations->SetInAt(0, Location::RequiresRegister());
8367 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8368
8369 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008370 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008371 return;
8372 }
8373
8374 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008375 case DataType::Type::kFloat32:
8376 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008377 locations->SetOut(Location::RequiresFpuRegister());
8378 break;
8379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008380 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008381 locations->SetOut(Location::RequiresRegister());
8382 break;
8383
8384 default:
8385 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8386 }
8387}
8388
8389void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008390 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008391 return;
8392 }
8393
8394 LocationSummary* locations = insn->GetLocations();
8395 Location out = locations->Out();
8396 Register const_area = locations->InAt(0).AsRegister<Register>();
8397 HConstant *value = insn->GetConstant();
8398
8399 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008400 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008401 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008402 codegen_->LiteralFloatAddress(
8403 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008404 break;
8405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008406 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008407 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008408 codegen_->LiteralDoubleAddress(
8409 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008410 break;
8411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008412 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008413 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008414 codegen_->LiteralInt32Address(
8415 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008416 break;
8417
8418 default:
8419 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8420 }
8421}
8422
Mark Mendell0616ae02015-04-17 12:49:27 -04008423/**
8424 * Class to handle late fixup of offsets into constant area.
8425 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008426class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008427 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008428 RIPFixup(CodeGeneratorX86& codegen,
8429 HX86ComputeBaseMethodAddress* base_method_address,
8430 size_t offset)
8431 : codegen_(&codegen),
8432 base_method_address_(base_method_address),
8433 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008434
8435 protected:
8436 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8437
8438 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008439 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008440
8441 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008442 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008443 // Patch the correct offset for the instruction. The place to patch is the
8444 // last 4 bytes of the instruction.
8445 // The value to patch is the distance from the offset in the constant area
8446 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008447 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008448 int32_t relative_position =
8449 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008450
8451 // Patch in the right value.
8452 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8453 }
8454
Mark Mendell0616ae02015-04-17 12:49:27 -04008455 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008456 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008457};
8458
Mark Mendell805b3b52015-09-18 14:10:29 -04008459/**
8460 * Class to handle late fixup of offsets to a jump table that will be created in the
8461 * constant area.
8462 */
8463class JumpTableRIPFixup : public RIPFixup {
8464 public:
8465 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008466 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8467 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008468
8469 void CreateJumpTable() {
8470 X86Assembler* assembler = codegen_->GetAssembler();
8471
8472 // Ensure that the reference to the jump table has the correct offset.
8473 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8474 SetOffset(offset_in_constant_table);
8475
8476 // The label values in the jump table are computed relative to the
8477 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008478 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008479
8480 // Populate the jump table with the correct values for the jump table.
8481 int32_t num_entries = switch_instr_->GetNumEntries();
8482 HBasicBlock* block = switch_instr_->GetBlock();
8483 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8484 // The value that we want is the target offset - the position of the table.
8485 for (int32_t i = 0; i < num_entries; i++) {
8486 HBasicBlock* b = successors[i];
8487 Label* l = codegen_->GetLabelOf(b);
8488 DCHECK(l->IsBound());
8489 int32_t offset_to_block = l->Position() - relative_offset;
8490 assembler->AppendInt32(offset_to_block);
8491 }
8492 }
8493
8494 private:
8495 const HX86PackedSwitch* switch_instr_;
8496};
8497
8498void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8499 // Generate the constant area if needed.
8500 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308501
Mark Mendell805b3b52015-09-18 14:10:29 -04008502 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8503 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8504 // byte values.
8505 assembler->Align(4, 0);
8506 constant_area_start_ = assembler->CodeSize();
8507
8508 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008509 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008510 jump_table->CreateJumpTable();
8511 }
8512
8513 // And now add the constant area to the generated code.
8514 assembler->AddConstantArea();
8515 }
8516
8517 // And finish up.
8518 CodeGenerator::Finalize(allocator);
8519}
8520
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008521Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8522 HX86ComputeBaseMethodAddress* method_base,
8523 Register reg) {
8524 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008525 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008526 return Address(reg, kDummy32BitOffset, fixup);
8527}
8528
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008529Address CodeGeneratorX86::LiteralFloatAddress(float v,
8530 HX86ComputeBaseMethodAddress* method_base,
8531 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008532 AssemblerFixup* fixup =
8533 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008534 return Address(reg, kDummy32BitOffset, fixup);
8535}
8536
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008537Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8538 HX86ComputeBaseMethodAddress* method_base,
8539 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008540 AssemblerFixup* fixup =
8541 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008542 return Address(reg, kDummy32BitOffset, fixup);
8543}
8544
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008545Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8546 HX86ComputeBaseMethodAddress* method_base,
8547 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008548 AssemblerFixup* fixup =
8549 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008550 return Address(reg, kDummy32BitOffset, fixup);
8551}
8552
Aart Bika19616e2016-02-01 18:57:58 -08008553void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8554 if (value == 0) {
8555 __ xorl(dest, dest);
8556 } else {
8557 __ movl(dest, Immediate(value));
8558 }
8559}
8560
8561void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8562 if (value == 0) {
8563 __ testl(dest, dest);
8564 } else {
8565 __ cmpl(dest, Immediate(value));
8566 }
8567}
8568
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008569void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8570 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008571 GenerateIntCompare(lhs_reg, rhs);
8572}
8573
8574void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008575 if (rhs.IsConstant()) {
8576 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008577 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008578 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008579 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008580 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008581 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008582 }
8583}
8584
8585Address CodeGeneratorX86::ArrayAddress(Register obj,
8586 Location index,
8587 ScaleFactor scale,
8588 uint32_t data_offset) {
8589 return index.IsConstant() ?
8590 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8591 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8592}
8593
Mark Mendell805b3b52015-09-18 14:10:29 -04008594Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8595 Register reg,
8596 Register value) {
8597 // Create a fixup to be used to create and address the jump table.
8598 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008599 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008600
8601 // We have to populate the jump tables.
8602 fixups_to_jump_tables_.push_back(table_fixup);
8603
8604 // We want a scaled address, as we are extracting the correct offset from the table.
8605 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8606}
8607
Andreas Gampe85b62f22015-09-09 13:15:38 -07008608// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008609void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008610 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008611 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008612 return;
8613 }
8614
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008615 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008616
8617 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8618 if (target.Equals(return_loc)) {
8619 return;
8620 }
8621
8622 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8623 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008624 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008625 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008626 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8627 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008628 GetMoveResolver()->EmitNativeCode(&parallel_move);
8629 } else {
8630 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008631 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008632 parallel_move.AddMove(return_loc, target, type, nullptr);
8633 GetMoveResolver()->EmitNativeCode(&parallel_move);
8634 }
8635}
8636
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008637void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8638 const uint8_t* roots_data,
8639 const PatchInfo<Label>& info,
8640 uint64_t index_in_table) const {
8641 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8642 uintptr_t address =
8643 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008644 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008645 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8646 dchecked_integral_cast<uint32_t>(address);
8647}
8648
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008649void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8650 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008651 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008652 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008653 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008654 }
8655
8656 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008657 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008658 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008659 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008660 }
8661}
8662
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008663void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8664 ATTRIBUTE_UNUSED) {
8665 LOG(FATAL) << "Unreachable";
8666}
8667
8668void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8669 ATTRIBUTE_UNUSED) {
8670 LOG(FATAL) << "Unreachable";
8671}
8672
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308673bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8674 return codegen_->GetInstructionSetFeatures().HasAVX();
8675}
8676bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8677 return codegen_->GetInstructionSetFeatures().HasAVX2();
8678}
8679bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8680 return codegen_->GetInstructionSetFeatures().HasAVX();
8681}
8682bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8683 return codegen_->GetInstructionSetFeatures().HasAVX2();
8684}
8685
Roland Levillain4d027112015-07-01 15:41:14 +01008686#undef __
8687
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008688} // namespace x86
8689} // namespace art