blob: 9f34a51d84d414adddf8b6341f19c734fbbdd7d4 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "intrinsics.h"
29#include "intrinsics_x86.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070032#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010034#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010038#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041
Roland Levillain0d5a2812015-11-13 10:07:31 +000042template<class MirrorType>
43class GcRoot;
44
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000045namespace x86 {
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050049static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Mark Mendell24f2dfa2015-01-14 19:51:45 -050051static constexpr int kC2ConditionMask = 0x400;
52
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000053static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000054
Aart Bik1f8d51b2018-02-15 10:42:37 -080055static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
56static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
57
Vladimir Marko3232dbb2018-07-25 15:42:46 +010058static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
59 InvokeRuntimeCallingConvention calling_convention;
60 RegisterSet caller_saves = RegisterSet::Empty();
61 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
62 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
63 // that the the kPrimNot result register is the same as the first argument register.
64 return caller_saves;
65}
66
Roland Levillain7cbd27f2016-08-11 23:53:33 +010067// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
68#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070069#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
Andreas Gampe85b62f22015-09-09 13:15:38 -070071class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000073 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010075 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010076 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000078 if (instruction_->CanThrowIntoCatchBlock()) {
79 // Live registers will be restored in the catch block if caught.
80 SaveLiveRegisters(codegen, instruction_->GetLocations());
81 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010082 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010083 instruction_,
84 instruction_->GetDexPc(),
85 this);
Roland Levillain888d0672015-11-23 18:53:50 +000086 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 }
88
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010089 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010092
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010094 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
95};
96
Andreas Gampe85b62f22015-09-09 13:15:38 -070097class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000098 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000099 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100101 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000103 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100104 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000105 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000106 }
107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100108 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100110 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100111
Calin Juravled0d48522014-11-04 16:40:20 +0000112 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000113 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
114};
115
Andreas Gampe85b62f22015-09-09 13:15:38 -0700116class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000117 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
119 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100121 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000122 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 if (is_div_) {
124 __ negl(reg_);
125 } else {
126 __ movl(reg_, Immediate(0));
127 }
Calin Juravled0d48522014-11-04 16:40:20 +0000128 __ jmp(GetExitLabel());
129 }
130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100132
Calin Juravled0d48522014-11-04 16:40:20 +0000133 private:
134 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 bool is_div_;
136 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000137};
138
Andreas Gampe85b62f22015-09-09 13:15:38 -0700139class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000141 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100143 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100144 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100145 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000147 // We're moving two locations to locations that could overlap, so we need a parallel
148 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000149 if (instruction_->CanThrowIntoCatchBlock()) {
150 // Live registers will be restored in the catch block if caught.
151 SaveLiveRegisters(codegen, instruction_->GetLocations());
152 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400153
154 // Are we using an array length from memory?
155 HInstruction* array_length = instruction_->InputAt(1);
156 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400158 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
159 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100160 HArrayLength* length = array_length->AsArrayLength();
161 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400162 Location array_loc = array_length->GetLocations()->InAt(0);
163 Address array_len(array_loc.AsRegister<Register>(), len_offset);
164 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
165 // Check for conflicts with index.
166 if (length_loc.Equals(locations->InAt(0))) {
167 // We know we aren't using parameter 2.
168 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
169 }
170 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100171 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100172 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700173 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400174 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000175 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100176 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100178 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400179 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100180 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100182 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
183 ? kQuickThrowStringBounds
184 : kQuickThrowArrayBounds;
185 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100186 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000187 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 }
189
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100190 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100192 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100193
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100195 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
196};
197
Andreas Gampe85b62f22015-09-09 13:15:38 -0700198class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000200 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100203 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700204 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100205 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700207 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100208 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000209 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700210 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100211 if (successor_ == nullptr) {
212 __ jmp(GetReturnLabel());
213 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100214 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000216 }
217
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100218 Label* GetReturnLabel() {
219 DCHECK(successor_ == nullptr);
220 return &return_label_;
221 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000222
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100223 HBasicBlock* GetSuccessor() const {
224 return successor_;
225 }
226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100227 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100228
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000229 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100230 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000231 Label return_label_;
232
233 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
234};
235
Vladimir Markoaad75c62016-10-03 08:46:48 +0000236class LoadStringSlowPathX86 : public SlowPathCode {
237 public:
238 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100240 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000241 LocationSummary* locations = instruction_->GetLocations();
242 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
243
244 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
245 __ Bind(GetEntryLabel());
246 SaveLiveRegisters(codegen, locations);
247
248 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
250 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000251 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
252 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
253 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
254 RestoreLiveRegisters(codegen, locations);
255
Vladimir Markoaad75c62016-10-03 08:46:48 +0000256 __ jmp(GetExitLabel());
257 }
258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100259 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000260
261 private:
262 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
263};
264
Andreas Gampe85b62f22015-09-09 13:15:38 -0700265class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100267 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
268 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100270 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 }
272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100273 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000274 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100275 Location out = locations->Out();
276 const uint32_t dex_pc = instruction_->GetDexPc();
277 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
278 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
279
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000280 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
281 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000282 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283
284 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100285 if (must_resolve_type) {
286 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
287 dex::TypeIndex type_index = cls_->GetTypeIndex();
288 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100289 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
290 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100291 // If we also must_do_clinit, the resolved type is now in the correct register.
292 } else {
293 DCHECK(must_do_clinit);
294 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
295 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
296 }
297 if (must_do_clinit) {
298 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
299 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000300 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000301
302 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000306 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000307 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000308 __ jmp(GetExitLabel());
309 }
310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100311 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100312
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000313 private:
314 // The class this slow path will load.
315 HLoadClass* const cls_;
316
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
318};
319
Andreas Gampe85b62f22015-09-09 13:15:38 -0700320class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000323 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100325 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 DCHECK(instruction_->IsCheckCast()
328 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
331 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000333 if (kPoisonHeapReferences &&
334 instruction_->IsCheckCast() &&
335 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
336 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
337 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
338 }
339
Vladimir Marko87584542017-12-12 17:47:52 +0000340 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341 SaveLiveRegisters(codegen, locations);
342 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343
344 // We're moving two locations to locations that could overlap, so we need a parallel
345 // move resolver.
346 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800347 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100349 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100354 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100355 instruction_,
356 instruction_->GetDexPc(),
357 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 } else {
360 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800361 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
362 instruction_,
363 instruction_->GetDexPc(),
364 this);
365 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000366 }
367
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 if (!is_fatal_) {
369 if (instruction_->IsInstanceOf()) {
370 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
371 }
372 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000373
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 __ jmp(GetExitLabel());
375 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000376 }
377
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100378 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
379 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100380
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000382 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000383
384 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
385};
386
Andreas Gampe85b62f22015-09-09 13:15:38 -0700387class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 public:
Aart Bik42249c32016-01-07 15:33:50 -0800389 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000390 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100392 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100393 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100395 LocationSummary* locations = instruction_->GetLocations();
396 SaveLiveRegisters(codegen, locations);
397 InvokeRuntimeCallingConvention calling_convention;
398 x86_codegen->Load32BitValue(
399 calling_convention.GetRegisterAt(0),
400 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100401 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100402 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 }
404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100405 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100406
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700407 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700408 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
409};
410
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100411class ArraySetSlowPathX86 : public SlowPathCode {
412 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000413 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100415 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 LocationSummary* locations = instruction_->GetLocations();
417 __ Bind(GetEntryLabel());
418 SaveLiveRegisters(codegen, locations);
419
420 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100421 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 parallel_move.AddMove(
423 locations->InAt(0),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100425 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100426 nullptr);
427 parallel_move.AddMove(
428 locations->InAt(1),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100430 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100431 nullptr);
432 parallel_move.AddMove(
433 locations->InAt(2),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100435 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 nullptr);
437 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
438
439 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100440 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000441 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100442 RestoreLiveRegisters(codegen, locations);
443 __ jmp(GetExitLabel());
444 }
445
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100446 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100447
448 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100449 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
450};
451
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100452// Slow path marking an object reference `ref` during a read
453// barrier. The field `obj.field` in the object `obj` holding this
454// reference does not get updated by this slow path after marking (see
455// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
456//
457// This means that after the execution of this slow path, `ref` will
458// always be up-to-date, but `obj.field` may not; i.e., after the
459// flip, `ref` will be a to-space reference, but `obj.field` will
460// probably still be a from-space reference (unless it gets updated by
461// another thread, or if another thread installed another object
462// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000463class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
464 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100465 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
466 Location ref,
467 bool unpoison_ref_before_marking)
468 : SlowPathCode(instruction),
469 ref_(ref),
470 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 DCHECK(kEmitCompilerReadBarrier);
472 }
473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100476 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100478 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000479 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 DCHECK(instruction_->IsInstanceFieldGet() ||
482 instruction_->IsStaticFieldGet() ||
483 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100484 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 instruction_->IsLoadClass() ||
486 instruction_->IsLoadString() ||
487 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100488 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100489 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
490 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000491 << "Unexpected instruction in read barrier marking slow path: "
492 << instruction_->DebugName();
493
494 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000496 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 }
Roland Levillain4359e612016-07-20 11:32:19 +0100499 // No need to save live registers; it's taken care of by the
500 // entrypoint. Also, there is no need to update the stack mask,
501 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 DCHECK_NE(ref_reg, ESP);
504 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100505 // "Compact" slow path, saving two moves.
506 //
507 // Instead of using the standard runtime calling convention (input
508 // and output in EAX):
509 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100511 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100513 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // of a dedicated entrypoint:
516 //
517 // rX <- ReadBarrierMarkRegX(rX)
518 //
Roland Levillain97c46462017-05-11 14:04:03 +0100519 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100520 // This runtime call does not require a stack map.
521 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 __ jmp(GetExitLabel());
523 }
524
525 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100526 // The location (register) of the marked object reference.
527 const Location ref_;
528 // Should the reference in `ref_` be unpoisoned prior to marking it?
529 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000530
531 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
532};
533
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100534// Slow path marking an object reference `ref` during a read barrier,
535// and if needed, atomically updating the field `obj.field` in the
536// object `obj` holding this reference after marking (contrary to
537// ReadBarrierMarkSlowPathX86 above, which never tries to update
538// `obj.field`).
539//
540// This means that after the execution of this slow path, both `ref`
541// and `obj.field` will be up-to-date; i.e., after the flip, both will
542// hold the same to-space reference (unless another thread installed
543// another object reference (different from `ref`) in `obj.field`).
544class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
545 public:
546 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
547 Location ref,
548 Register obj,
549 const Address& field_addr,
550 bool unpoison_ref_before_marking,
551 Register temp)
552 : SlowPathCode(instruction),
553 ref_(ref),
554 obj_(obj),
555 field_addr_(field_addr),
556 unpoison_ref_before_marking_(unpoison_ref_before_marking),
557 temp_(temp) {
558 DCHECK(kEmitCompilerReadBarrier);
559 }
560
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100561 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100564 LocationSummary* locations = instruction_->GetLocations();
565 Register ref_reg = ref_.AsRegister<Register>();
566 DCHECK(locations->CanCall());
567 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
568 // This slow path is only used by the UnsafeCASObject intrinsic.
569 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
570 << "Unexpected instruction in read barrier marking and field updating slow path: "
571 << instruction_->DebugName();
572 DCHECK(instruction_->GetLocations()->Intrinsified());
573 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
574
575 __ Bind(GetEntryLabel());
576 if (unpoison_ref_before_marking_) {
577 // Object* ref = ref_addr->AsMirrorPtr()
578 __ MaybeUnpoisonHeapReference(ref_reg);
579 }
580
581 // Save the old (unpoisoned) reference.
582 __ movl(temp_, ref_reg);
583
584 // No need to save live registers; it's taken care of by the
585 // entrypoint. Also, there is no need to update the stack mask,
586 // as this runtime call will not trigger a garbage collection.
587 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
588 DCHECK_NE(ref_reg, ESP);
589 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
590 // "Compact" slow path, saving two moves.
591 //
592 // Instead of using the standard runtime calling convention (input
593 // and output in EAX):
594 //
595 // EAX <- ref
596 // EAX <- ReadBarrierMark(EAX)
597 // ref <- EAX
598 //
599 // we just use rX (the register containing `ref`) as input and output
600 // of a dedicated entrypoint:
601 //
602 // rX <- ReadBarrierMarkRegX(rX)
603 //
Roland Levillain97c46462017-05-11 14:04:03 +0100604 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100605 // This runtime call does not require a stack map.
606 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
607
608 // If the new reference is different from the old reference,
609 // update the field in the holder (`*field_addr`).
610 //
611 // Note that this field could also hold a different object, if
612 // another thread had concurrently changed it. In that case, the
613 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
614 // operation below would abort the CAS, leaving the field as-is.
615 NearLabel done;
616 __ cmpl(temp_, ref_reg);
617 __ j(kEqual, &done);
618
619 // Update the the holder's field atomically. This may fail if
620 // mutator updates before us, but it's OK. This is achieved
621 // using a strong compare-and-set (CAS) operation with relaxed
622 // memory synchronization ordering, where the expected value is
623 // the old reference and the desired value is the new reference.
624 // This operation is implemented with a 32-bit LOCK CMPXLCHG
625 // instruction, which requires the expected value (the old
626 // reference) to be in EAX. Save EAX beforehand, and move the
627 // expected value (stored in `temp_`) into EAX.
628 __ pushl(EAX);
629 __ movl(EAX, temp_);
630
631 // Convenience aliases.
632 Register base = obj_;
633 Register expected = EAX;
634 Register value = ref_reg;
635
636 bool base_equals_value = (base == value);
637 if (kPoisonHeapReferences) {
638 if (base_equals_value) {
639 // If `base` and `value` are the same register location, move
640 // `value` to a temporary register. This way, poisoning
641 // `value` won't invalidate `base`.
642 value = temp_;
643 __ movl(value, base);
644 }
645
646 // Check that the register allocator did not assign the location
647 // of `expected` (EAX) to `value` nor to `base`, so that heap
648 // poisoning (when enabled) works as intended below.
649 // - If `value` were equal to `expected`, both references would
650 // be poisoned twice, meaning they would not be poisoned at
651 // all, as heap poisoning uses address negation.
652 // - If `base` were equal to `expected`, poisoning `expected`
653 // would invalidate `base`.
654 DCHECK_NE(value, expected);
655 DCHECK_NE(base, expected);
656
657 __ PoisonHeapReference(expected);
658 __ PoisonHeapReference(value);
659 }
660
661 __ LockCmpxchgl(field_addr_, value);
662
663 // If heap poisoning is enabled, we need to unpoison the values
664 // that were poisoned earlier.
665 if (kPoisonHeapReferences) {
666 if (base_equals_value) {
667 // `value` has been moved to a temporary register, no need
668 // to unpoison it.
669 } else {
670 __ UnpoisonHeapReference(value);
671 }
672 // No need to unpoison `expected` (EAX), as it is be overwritten below.
673 }
674
675 // Restore EAX.
676 __ popl(EAX);
677
678 __ Bind(&done);
679 __ jmp(GetExitLabel());
680 }
681
682 private:
683 // The location (register) of the marked object reference.
684 const Location ref_;
685 // The register containing the object holding the marked object reference field.
686 const Register obj_;
687 // The address of the marked reference field. The base of this address must be `obj_`.
688 const Address field_addr_;
689
690 // Should the reference in `ref_` be unpoisoned prior to marking it?
691 const bool unpoison_ref_before_marking_;
692
693 const Register temp_;
694
695 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
696};
697
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698// Slow path generating a read barrier for a heap reference.
699class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
700 public:
701 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
702 Location out,
703 Location ref,
704 Location obj,
705 uint32_t offset,
706 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000707 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000708 out_(out),
709 ref_(ref),
710 obj_(obj),
711 offset_(offset),
712 index_(index) {
713 DCHECK(kEmitCompilerReadBarrier);
714 // If `obj` is equal to `out` or `ref`, it means the initial object
715 // has been overwritten by (or after) the heap object reference load
716 // to be instrumented, e.g.:
717 //
718 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000719 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000720 //
721 // In that case, we have lost the information about the original
722 // object, and the emitted read barrier cannot work properly.
723 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
724 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
725 }
726
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100727 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
729 LocationSummary* locations = instruction_->GetLocations();
730 Register reg_out = out_.AsRegister<Register>();
731 DCHECK(locations->CanCall());
732 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100733 DCHECK(instruction_->IsInstanceFieldGet() ||
734 instruction_->IsStaticFieldGet() ||
735 instruction_->IsArrayGet() ||
736 instruction_->IsInstanceOf() ||
737 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700738 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000739 << "Unexpected instruction in read barrier for heap reference slow path: "
740 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000741
742 __ Bind(GetEntryLabel());
743 SaveLiveRegisters(codegen, locations);
744
745 // We may have to change the index's value, but as `index_` is a
746 // constant member (like other "inputs" of this slow path),
747 // introduce a copy of it, `index`.
748 Location index = index_;
749 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100750 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000751 if (instruction_->IsArrayGet()) {
752 // Compute the actual memory offset and store it in `index`.
753 Register index_reg = index_.AsRegister<Register>();
754 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
755 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
756 // We are about to change the value of `index_reg` (see the
757 // calls to art::x86::X86Assembler::shll and
758 // art::x86::X86Assembler::AddImmediate below), but it has
759 // not been saved by the previous call to
760 // art::SlowPathCode::SaveLiveRegisters, as it is a
761 // callee-save register --
762 // art::SlowPathCode::SaveLiveRegisters does not consider
763 // callee-save registers, as it has been designed with the
764 // assumption that callee-save registers are supposed to be
765 // handled by the called function. So, as a callee-save
766 // register, `index_reg` _would_ eventually be saved onto
767 // the stack, but it would be too late: we would have
768 // changed its value earlier. Therefore, we manually save
769 // it here into another freely available register,
770 // `free_reg`, chosen of course among the caller-save
771 // registers (as a callee-save `free_reg` register would
772 // exhibit the same problem).
773 //
774 // Note we could have requested a temporary register from
775 // the register allocator instead; but we prefer not to, as
776 // this is a slow path, and we know we can find a
777 // caller-save register that is available.
778 Register free_reg = FindAvailableCallerSaveRegister(codegen);
779 __ movl(free_reg, index_reg);
780 index_reg = free_reg;
781 index = Location::RegisterLocation(index_reg);
782 } else {
783 // The initial register stored in `index_` has already been
784 // saved in the call to art::SlowPathCode::SaveLiveRegisters
785 // (as it is not a callee-save register), so we can freely
786 // use it.
787 }
788 // Shifting the index value contained in `index_reg` by the scale
789 // factor (2) cannot overflow in practice, as the runtime is
790 // unable to allocate object arrays with a size larger than
791 // 2^26 - 1 (that is, 2^28 - 4 bytes).
792 __ shll(index_reg, Immediate(TIMES_4));
793 static_assert(
794 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
795 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
796 __ AddImmediate(index_reg, Immediate(offset_));
797 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100798 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
799 // intrinsics, `index_` is not shifted by a scale factor of 2
800 // (as in the case of ArrayGet), as it is actually an offset
801 // to an object field within an object.
802 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000803 DCHECK(instruction_->GetLocations()->Intrinsified());
804 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
805 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
806 << instruction_->AsInvoke()->GetIntrinsic();
807 DCHECK_EQ(offset_, 0U);
808 DCHECK(index_.IsRegisterPair());
809 // UnsafeGet's offset location is a register pair, the low
810 // part contains the correct offset.
811 index = index_.ToLow();
812 }
813 }
814
815 // We're moving two or three locations to locations that could
816 // overlap, so we need a parallel move resolver.
817 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100818 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000819 parallel_move.AddMove(ref_,
820 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100821 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 nullptr);
823 parallel_move.AddMove(obj_,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100825 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000826 nullptr);
827 if (index.IsValid()) {
828 parallel_move.AddMove(index,
829 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100830 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000831 nullptr);
832 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
833 } else {
834 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
835 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
836 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100837 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000838 CheckEntrypointTypes<
839 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
840 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
841
842 RestoreLiveRegisters(codegen, locations);
843 __ jmp(GetExitLabel());
844 }
845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100846 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000847
848 private:
849 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
850 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
851 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
852 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
853 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
854 return static_cast<Register>(i);
855 }
856 }
857 // We shall never fail to find a free caller-save register, as
858 // there are more than two core caller-save registers on x86
859 // (meaning it is possible to find one which is different from
860 // `ref` and `obj`).
861 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
862 LOG(FATAL) << "Could not find a free caller-save register";
863 UNREACHABLE();
864 }
865
Roland Levillain0d5a2812015-11-13 10:07:31 +0000866 const Location out_;
867 const Location ref_;
868 const Location obj_;
869 const uint32_t offset_;
870 // An additional location containing an index to an array.
871 // Only used for HArrayGet and the UnsafeGetObject &
872 // UnsafeGetObjectVolatile intrinsics.
873 const Location index_;
874
875 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
876};
877
878// Slow path generating a read barrier for a GC root.
879class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
880 public:
881 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000882 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000883 DCHECK(kEmitCompilerReadBarrier);
884 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100886 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887 LocationSummary* locations = instruction_->GetLocations();
888 Register reg_out = out_.AsRegister<Register>();
889 DCHECK(locations->CanCall());
890 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000891 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
892 << "Unexpected instruction in read barrier for GC root slow path: "
893 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000894
895 __ Bind(GetEntryLabel());
896 SaveLiveRegisters(codegen, locations);
897
898 InvokeRuntimeCallingConvention calling_convention;
899 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
900 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100901 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000902 instruction_,
903 instruction_->GetDexPc(),
904 this);
905 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
906 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
907
908 RestoreLiveRegisters(codegen, locations);
909 __ jmp(GetExitLabel());
910 }
911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100912 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000913
914 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000915 const Location out_;
916 const Location root_;
917
918 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
919};
920
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100921#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100922// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
923#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100924
Aart Bike9f37602015-10-09 11:15:55 -0700925inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700926 switch (cond) {
927 case kCondEQ: return kEqual;
928 case kCondNE: return kNotEqual;
929 case kCondLT: return kLess;
930 case kCondLE: return kLessEqual;
931 case kCondGT: return kGreater;
932 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700933 case kCondB: return kBelow;
934 case kCondBE: return kBelowEqual;
935 case kCondA: return kAbove;
936 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700937 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938 LOG(FATAL) << "Unreachable";
939 UNREACHABLE();
940}
941
Aart Bike9f37602015-10-09 11:15:55 -0700942// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100943inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
944 switch (cond) {
945 case kCondEQ: return kEqual;
946 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700947 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 case kCondLT: return kBelow;
949 case kCondLE: return kBelowEqual;
950 case kCondGT: return kAbove;
951 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700952 // Unsigned remain unchanged.
953 case kCondB: return kBelow;
954 case kCondBE: return kBelowEqual;
955 case kCondA: return kAbove;
956 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100957 }
958 LOG(FATAL) << "Unreachable";
959 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700960}
961
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100962void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100963 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100964}
965
966void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100967 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100968}
969
Vladimir Markoa0431112018-06-25 09:32:54 +0100970const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
971 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
972}
973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100974size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
975 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
976 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100977}
978
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100979size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
980 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
981 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100982}
983
Mark Mendell7c8d0092015-01-26 11:21:33 -0500984size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700985 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700986 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 } else {
988 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
989 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500990 return GetFloatingPointSpillSlotSize();
991}
992
993size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700994 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700995 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700996 } else {
997 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
998 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500999 return GetFloatingPointSpillSlotSize();
1000}
1001
Calin Juravle175dc732015-08-25 15:42:32 +01001002void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1003 HInstruction* instruction,
1004 uint32_t dex_pc,
1005 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001006 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001007 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1008 if (EntrypointRequiresStackMap(entrypoint)) {
1009 RecordPcInfo(instruction, dex_pc, slow_path);
1010 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001011}
1012
Roland Levillaindec8f632016-07-22 17:10:06 +01001013void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1014 HInstruction* instruction,
1015 SlowPathCode* slow_path) {
1016 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001017 GenerateInvokeRuntime(entry_point_offset);
1018}
1019
1020void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001021 __ fs()->call(Address::Absolute(entry_point_offset));
1022}
1023
Mark Mendellfb8d2792015-03-31 22:16:59 -04001024CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001025 const CompilerOptions& compiler_options,
1026 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001027 : CodeGenerator(graph,
1028 kNumberOfCpuRegisters,
1029 kNumberOfXmmRegisters,
1030 kNumberOfRegisterPairs,
1031 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1032 arraysize(kCoreCalleeSaves))
1033 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001034 0,
1035 compiler_options,
1036 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001037 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001038 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001039 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 move_resolver_(graph->GetAllocator(), this),
1041 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1043 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1044 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1045 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001046 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001047 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001048 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1050 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001051 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001053 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001055 // Use a fake return address register to mimic Quick.
1056 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001057}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001058
David Brazdil58282f42016-01-14 12:45:10 +00001059void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001060 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001061 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062}
1063
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001064InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001065 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001066 assembler_(codegen->GetAssembler()),
1067 codegen_(codegen) {}
1068
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001069static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001070 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071}
1072
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001073void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001074 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001075 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001076 bool skip_overflow_check =
1077 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001079
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001080 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1081 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1082 Immediate(1));
1083 }
1084
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001085 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001086 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1087 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001088 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001089 }
1090
Mark Mendell5f874182015-03-04 15:42:45 -05001091 if (HasEmptyFrame()) {
1092 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001093 }
Mark Mendell5f874182015-03-04 15:42:45 -05001094
1095 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1096 Register reg = kCoreCalleeSaves[i];
1097 if (allocated_registers_.ContainsCoreRegister(reg)) {
1098 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001099 __ cfi().AdjustCFAOffset(kX86WordSize);
1100 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001101 }
1102 }
1103
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001104 int adjust = GetFrameSize() - FrameEntrySpillSize();
1105 __ subl(ESP, Immediate(adjust));
1106 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001107 // Save the current method if we need it. Note that we do not
1108 // do this in HCurrentMethod, as the instruction might have been removed
1109 // in the SSA graph.
1110 if (RequiresCurrentMethod()) {
1111 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1112 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001113
1114 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1115 // Initialize should_deoptimize flag to 0.
1116 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1117 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001118}
1119
1120void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001121 __ cfi().RememberState();
1122 if (!HasEmptyFrame()) {
1123 int adjust = GetFrameSize() - FrameEntrySpillSize();
1124 __ addl(ESP, Immediate(adjust));
1125 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001126
David Srbeckyc34dc932015-04-12 09:27:43 +01001127 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1128 Register reg = kCoreCalleeSaves[i];
1129 if (allocated_registers_.ContainsCoreRegister(reg)) {
1130 __ popl(reg);
1131 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1132 __ cfi().Restore(DWARFReg(reg));
1133 }
Mark Mendell5f874182015-03-04 15:42:45 -05001134 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001135 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001136 __ ret();
1137 __ cfi().RestoreState();
1138 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001139}
1140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001141void CodeGeneratorX86::Bind(HBasicBlock* block) {
1142 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001143}
1144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001147 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001149 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001150 case DataType::Type::kInt8:
1151 case DataType::Type::kUint16:
1152 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001153 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001154 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001155 return Location::RegisterLocation(EAX);
1156
Aart Bik66c158e2018-01-31 12:55:04 -08001157 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001158 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001159 return Location::RegisterPairLocation(EAX, EDX);
1160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001161 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001162 return Location::NoLocation();
1163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001164 case DataType::Type::kFloat64:
1165 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001166 return Location::FpuRegisterLocation(XMM0);
1167 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001168
1169 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001170}
1171
1172Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1173 return Location::RegisterLocation(kMethodRegisterArgument);
1174}
1175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001176Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001178 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001179 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001180 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 case DataType::Type::kInt8:
1182 case DataType::Type::kUint16:
1183 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001184 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001186 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001187 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001188 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001190 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001191 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001192 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001195 uint32_t index = gp_index_;
1196 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001197 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001198 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001199 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1200 calling_convention.GetRegisterPairAt(index));
1201 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001202 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001203 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1204 }
1205 }
1206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001207 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001208 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001209 stack_index_++;
1210 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1211 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1212 } else {
1213 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1214 }
1215 }
1216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001218 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001219 stack_index_ += 2;
1220 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1221 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1222 } else {
1223 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001224 }
1225 }
1226
Aart Bik66c158e2018-01-31 12:55:04 -08001227 case DataType::Type::kUint32:
1228 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001229 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001230 LOG(FATAL) << "Unexpected parameter type " << type;
1231 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001232 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001233 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001234}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001235
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236void CodeGeneratorX86::Move32(Location destination, Location source) {
1237 if (source.Equals(destination)) {
1238 return;
1239 }
1240 if (destination.IsRegister()) {
1241 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 } else {
1246 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001249 } else if (destination.IsFpuRegister()) {
1250 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001253 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001254 } else {
1255 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001256 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001257 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001258 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001259 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001261 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001262 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001263 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001264 } else if (source.IsConstant()) {
1265 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001266 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001267 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001268 } else {
1269 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001270 __ pushl(Address(ESP, source.GetStackIndex()));
1271 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 }
1273 }
1274}
1275
1276void CodeGeneratorX86::Move64(Location destination, Location source) {
1277 if (source.Equals(destination)) {
1278 return;
1279 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001280 if (destination.IsRegisterPair()) {
1281 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001282 EmitParallelMoves(
1283 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1284 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001285 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001286 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001287 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001288 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001289 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1291 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1292 __ psrlq(src_reg, Immediate(32));
1293 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001294 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001295 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1298 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001299 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1300 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001302 if (source.IsFpuRegister()) {
1303 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1304 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001307 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001308 // Create stack space for 2 elements.
1309 __ subl(ESP, Immediate(2 * elem_size));
1310 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1311 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1312 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1313 // And remove the temporary stack space we allocated.
1314 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001315 } else {
1316 LOG(FATAL) << "Unimplemented";
1317 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001319 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001320 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001321 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001322 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001323 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001324 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001325 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001327 } else if (source.IsConstant()) {
1328 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001329 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1330 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001331 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001332 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1333 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001334 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001335 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001336 EmitParallelMoves(
1337 Location::StackSlot(source.GetStackIndex()),
1338 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001339 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001340 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001341 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001343 }
1344 }
1345}
1346
Calin Juravle175dc732015-08-25 15:42:32 +01001347void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1348 DCHECK(location.IsRegister());
1349 __ movl(location.AsRegister<Register>(), Immediate(value));
1350}
1351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001352void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001353 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001354 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1355 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1356 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001357 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001358 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001359 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001360 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001361}
1362
1363void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1364 if (location.IsRegister()) {
1365 locations->AddTemp(location);
1366 } else if (location.IsRegisterPair()) {
1367 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1368 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1369 } else {
1370 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1371 }
1372}
1373
David Brazdilfc6a86a2015-06-26 10:33:45 +00001374void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001375 if (successor->IsExitBlock()) {
1376 DCHECK(got->GetPrevious()->AlwaysThrows());
1377 return; // no code needed
1378 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001379
1380 HBasicBlock* block = got->GetBlock();
1381 HInstruction* previous = got->GetPrevious();
1382
1383 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001384 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001385 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1386 __ pushl(EAX);
1387 __ movl(EAX, Address(ESP, kX86WordSize));
1388 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1389 __ popl(EAX);
1390 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001391 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1392 return;
1393 }
1394
1395 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1396 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1397 }
1398 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001399 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001400 }
1401}
1402
David Brazdilfc6a86a2015-06-26 10:33:45 +00001403void LocationsBuilderX86::VisitGoto(HGoto* got) {
1404 got->SetLocations(nullptr);
1405}
1406
1407void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1408 HandleGoto(got, got->GetSuccessor());
1409}
1410
1411void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1412 try_boundary->SetLocations(nullptr);
1413}
1414
1415void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1416 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1417 if (!successor->IsExitBlock()) {
1418 HandleGoto(try_boundary, successor);
1419 }
1420}
1421
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001422void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001423 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001424}
1425
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001426void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001427}
1428
Mark Mendell152408f2015-12-31 12:28:50 -05001429template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001430void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001431 LabelType* true_label,
1432 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 if (cond->IsFPConditionTrueIfNaN()) {
1434 __ j(kUnordered, true_label);
1435 } else if (cond->IsFPConditionFalseIfNaN()) {
1436 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001437 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001439}
1440
Mark Mendell152408f2015-12-31 12:28:50 -05001441template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001442void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001443 LabelType* true_label,
1444 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001445 LocationSummary* locations = cond->GetLocations();
1446 Location left = locations->InAt(0);
1447 Location right = locations->InAt(1);
1448 IfCondition if_cond = cond->GetCondition();
1449
Mark Mendellc4701932015-04-10 13:18:51 -04001450 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001451 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001452 IfCondition true_high_cond = if_cond;
1453 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001454 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001455
1456 // Set the conditions for the test, remembering that == needs to be
1457 // decided using the low words.
1458 switch (if_cond) {
1459 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001460 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001461 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001462 break;
1463 case kCondLT:
1464 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001465 break;
1466 case kCondLE:
1467 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001468 break;
1469 case kCondGT:
1470 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001471 break;
1472 case kCondGE:
1473 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001474 break;
Aart Bike9f37602015-10-09 11:15:55 -07001475 case kCondB:
1476 false_high_cond = kCondA;
1477 break;
1478 case kCondBE:
1479 true_high_cond = kCondB;
1480 break;
1481 case kCondA:
1482 false_high_cond = kCondB;
1483 break;
1484 case kCondAE:
1485 true_high_cond = kCondA;
1486 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001487 }
1488
1489 if (right.IsConstant()) {
1490 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001491 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001492 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001493
Aart Bika19616e2016-02-01 18:57:58 -08001494 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001495 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001496 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001497 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001498 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001499 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001500 __ j(X86Condition(true_high_cond), true_label);
1501 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001502 }
1503 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001504 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001505 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001506 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001507 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001508
1509 __ cmpl(left_high, right_high);
1510 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001511 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001512 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001513 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001514 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001515 __ j(X86Condition(true_high_cond), true_label);
1516 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001517 }
1518 // Must be equal high, so compare the lows.
1519 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001520 } else {
1521 DCHECK(right.IsDoubleStackSlot());
1522 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1523 if (if_cond == kCondNE) {
1524 __ j(X86Condition(true_high_cond), true_label);
1525 } else if (if_cond == kCondEQ) {
1526 __ j(X86Condition(false_high_cond), false_label);
1527 } else {
1528 __ j(X86Condition(true_high_cond), true_label);
1529 __ j(X86Condition(false_high_cond), false_label);
1530 }
1531 // Must be equal high, so compare the lows.
1532 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001533 }
1534 // The last comparison might be unsigned.
1535 __ j(final_condition, true_label);
1536}
1537
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001538void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1539 Location rhs,
1540 HInstruction* insn,
1541 bool is_double) {
1542 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1543 if (is_double) {
1544 if (rhs.IsFpuRegister()) {
1545 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1546 } else if (const_area != nullptr) {
1547 DCHECK(const_area->IsEmittedAtUseSite());
1548 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1549 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001550 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1551 const_area->GetBaseMethodAddress(),
1552 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001553 } else {
1554 DCHECK(rhs.IsDoubleStackSlot());
1555 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1556 }
1557 } else {
1558 if (rhs.IsFpuRegister()) {
1559 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1560 } else if (const_area != nullptr) {
1561 DCHECK(const_area->IsEmittedAtUseSite());
1562 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1563 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001564 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1565 const_area->GetBaseMethodAddress(),
1566 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001567 } else {
1568 DCHECK(rhs.IsStackSlot());
1569 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1570 }
1571 }
1572}
1573
Mark Mendell152408f2015-12-31 12:28:50 -05001574template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001575void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001576 LabelType* true_target_in,
1577 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001578 // Generated branching requires both targets to be explicit. If either of the
1579 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001580 LabelType fallthrough_target;
1581 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1582 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001583
Mark Mendellc4701932015-04-10 13:18:51 -04001584 LocationSummary* locations = condition->GetLocations();
1585 Location left = locations->InAt(0);
1586 Location right = locations->InAt(1);
1587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001588 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001589 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001590 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001591 GenerateLongComparesAndJumps(condition, true_target, false_target);
1592 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001593 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001594 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001595 GenerateFPJumps(condition, true_target, false_target);
1596 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001597 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001598 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001599 GenerateFPJumps(condition, true_target, false_target);
1600 break;
1601 default:
1602 LOG(FATAL) << "Unexpected compare type " << type;
1603 }
1604
David Brazdil0debae72015-11-12 18:37:00 +00001605 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001606 __ jmp(false_target);
1607 }
David Brazdil0debae72015-11-12 18:37:00 +00001608
1609 if (fallthrough_target.IsLinked()) {
1610 __ Bind(&fallthrough_target);
1611 }
Mark Mendellc4701932015-04-10 13:18:51 -04001612}
1613
David Brazdil0debae72015-11-12 18:37:00 +00001614static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1615 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1616 // are set only strictly before `branch`. We can't use the eflags on long/FP
1617 // conditions if they are materialized due to the complex branching.
1618 return cond->IsCondition() &&
1619 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1621 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001622}
1623
Mark Mendell152408f2015-12-31 12:28:50 -05001624template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001625void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001626 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001627 LabelType* true_target,
1628 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001629 HInstruction* cond = instruction->InputAt(condition_input_index);
1630
1631 if (true_target == nullptr && false_target == nullptr) {
1632 // Nothing to do. The code always falls through.
1633 return;
1634 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001635 // Constant condition, statically compared against "true" (integer value 1).
1636 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001637 if (true_target != nullptr) {
1638 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001639 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001640 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001641 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001642 if (false_target != nullptr) {
1643 __ jmp(false_target);
1644 }
1645 }
1646 return;
1647 }
1648
1649 // The following code generates these patterns:
1650 // (1) true_target == nullptr && false_target != nullptr
1651 // - opposite condition true => branch to false_target
1652 // (2) true_target != nullptr && false_target == nullptr
1653 // - condition true => branch to true_target
1654 // (3) true_target != nullptr && false_target != nullptr
1655 // - condition true => branch to true_target
1656 // - branch to false_target
1657 if (IsBooleanValueOrMaterializedCondition(cond)) {
1658 if (AreEflagsSetFrom(cond, instruction)) {
1659 if (true_target == nullptr) {
1660 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1661 } else {
1662 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1663 }
1664 } else {
1665 // Materialized condition, compare against 0.
1666 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1667 if (lhs.IsRegister()) {
1668 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1669 } else {
1670 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1671 }
1672 if (true_target == nullptr) {
1673 __ j(kEqual, false_target);
1674 } else {
1675 __ j(kNotEqual, true_target);
1676 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001677 }
1678 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001679 // Condition has not been materialized, use its inputs as the comparison and
1680 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001681 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001682
1683 // If this is a long or FP comparison that has been folded into
1684 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 DataType::Type type = condition->InputAt(0)->GetType();
1686 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001687 GenerateCompareTestAndBranch(condition, true_target, false_target);
1688 return;
1689 }
1690
1691 Location lhs = condition->GetLocations()->InAt(0);
1692 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001694 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (true_target == nullptr) {
1696 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1697 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001698 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001699 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001700 }
David Brazdil0debae72015-11-12 18:37:00 +00001701
1702 // If neither branch falls through (case 3), the conditional branch to `true_target`
1703 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1704 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001705 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001706 }
1707}
1708
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001709void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001710 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001711 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001712 locations->SetInAt(0, Location::Any());
1713 }
1714}
1715
1716void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001717 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1718 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1719 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1720 nullptr : codegen_->GetLabelOf(true_successor);
1721 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1722 nullptr : codegen_->GetLabelOf(false_successor);
1723 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001724}
1725
1726void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001727 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001728 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001729 InvokeRuntimeCallingConvention calling_convention;
1730 RegisterSet caller_saves = RegisterSet::Empty();
1731 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1732 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001733 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001734 locations->SetInAt(0, Location::Any());
1735 }
1736}
1737
1738void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001739 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001740 GenerateTestAndBranch<Label>(deoptimize,
1741 /* condition_input_index */ 0,
1742 slow_path->GetEntryLabel(),
1743 /* false_target */ nullptr);
1744}
1745
Mingyao Yang063fc772016-08-02 11:02:54 -07001746void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001747 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001748 LocationSummary(flag, LocationSummary::kNoCall);
1749 locations->SetOut(Location::RequiresRegister());
1750}
1751
1752void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1753 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1754 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1755}
1756
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001757static bool SelectCanUseCMOV(HSelect* select) {
1758 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001760 return false;
1761 }
1762
1763 // A FP condition doesn't generate the single CC that we need.
1764 // In 32 bit mode, a long condition doesn't generate a single CC either.
1765 HInstruction* condition = select->GetCondition();
1766 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 DataType::Type compare_type = condition->InputAt(0)->GetType();
1768 if (compare_type == DataType::Type::kInt64 ||
1769 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001770 return false;
1771 }
1772 }
1773
1774 // We can generate a CMOV for this Select.
1775 return true;
1776}
1777
David Brazdil74eb1b22015-12-14 11:44:01 +00001778void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001779 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001780 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001781 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001782 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001783 } else {
1784 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001785 if (SelectCanUseCMOV(select)) {
1786 if (select->InputAt(1)->IsConstant()) {
1787 // Cmov can't handle a constant value.
1788 locations->SetInAt(1, Location::RequiresRegister());
1789 } else {
1790 locations->SetInAt(1, Location::Any());
1791 }
1792 } else {
1793 locations->SetInAt(1, Location::Any());
1794 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001795 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001796 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1797 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001798 }
1799 locations->SetOut(Location::SameAsFirstInput());
1800}
1801
1802void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1803 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001804 DCHECK(locations->InAt(0).Equals(locations->Out()));
1805 if (SelectCanUseCMOV(select)) {
1806 // If both the condition and the source types are integer, we can generate
1807 // a CMOV to implement Select.
1808
1809 HInstruction* select_condition = select->GetCondition();
1810 Condition cond = kNotEqual;
1811
1812 // Figure out how to test the 'condition'.
1813 if (select_condition->IsCondition()) {
1814 HCondition* condition = select_condition->AsCondition();
1815 if (!condition->IsEmittedAtUseSite()) {
1816 // This was a previously materialized condition.
1817 // Can we use the existing condition code?
1818 if (AreEflagsSetFrom(condition, select)) {
1819 // Materialization was the previous instruction. Condition codes are right.
1820 cond = X86Condition(condition->GetCondition());
1821 } else {
1822 // No, we have to recreate the condition code.
1823 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1824 __ testl(cond_reg, cond_reg);
1825 }
1826 } else {
1827 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001828 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1829 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001830 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001831 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001832 cond = X86Condition(condition->GetCondition());
1833 }
1834 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001835 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001836 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1837 __ testl(cond_reg, cond_reg);
1838 }
1839
1840 // If the condition is true, overwrite the output, which already contains false.
1841 Location false_loc = locations->InAt(0);
1842 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001843 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001844 // 64 bit conditional move.
1845 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1846 Register false_low = false_loc.AsRegisterPairLow<Register>();
1847 if (true_loc.IsRegisterPair()) {
1848 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1849 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1850 } else {
1851 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1852 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1853 }
1854 } else {
1855 // 32 bit conditional move.
1856 Register false_reg = false_loc.AsRegister<Register>();
1857 if (true_loc.IsRegister()) {
1858 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1859 } else {
1860 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1861 }
1862 }
1863 } else {
1864 NearLabel false_target;
1865 GenerateTestAndBranch<NearLabel>(
1866 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1867 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1868 __ Bind(&false_target);
1869 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001870}
1871
David Srbecky0cf44932015-12-09 14:09:59 +00001872void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001873 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001874}
1875
David Srbeckyd28f4a02016-03-14 17:14:24 +00001876void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1877 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001878}
1879
1880void CodeGeneratorX86::GenerateNop() {
1881 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001882}
1883
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001884void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001885 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001886 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001887 // Handle the long/FP comparisons made in instruction simplification.
1888 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001889 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001890 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001891 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001892 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001893 locations->SetOut(Location::RequiresRegister());
1894 }
1895 break;
1896 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001897 case DataType::Type::kFloat32:
1898 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001899 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001900 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1901 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1902 } else if (cond->InputAt(1)->IsConstant()) {
1903 locations->SetInAt(1, Location::RequiresFpuRegister());
1904 } else {
1905 locations->SetInAt(1, Location::Any());
1906 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001907 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001908 locations->SetOut(Location::RequiresRegister());
1909 }
1910 break;
1911 }
1912 default:
1913 locations->SetInAt(0, Location::RequiresRegister());
1914 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001915 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001916 // We need a byte register.
1917 locations->SetOut(Location::RegisterLocation(ECX));
1918 }
1919 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001920 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001921}
1922
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001923void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001924 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001925 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001926 }
Mark Mendellc4701932015-04-10 13:18:51 -04001927
1928 LocationSummary* locations = cond->GetLocations();
1929 Location lhs = locations->InAt(0);
1930 Location rhs = locations->InAt(1);
1931 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001932 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001933
1934 switch (cond->InputAt(0)->GetType()) {
1935 default: {
1936 // Integer case.
1937
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001938 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001939 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001940 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001941 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001942 return;
1943 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001944 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001945 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1946 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001948 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001949 GenerateFPJumps(cond, &true_label, &false_label);
1950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001952 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001953 GenerateFPJumps(cond, &true_label, &false_label);
1954 break;
1955 }
1956
1957 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001958 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001959
Roland Levillain4fa13f62015-07-06 18:11:54 +01001960 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001961 __ Bind(&false_label);
1962 __ xorl(reg, reg);
1963 __ jmp(&done_label);
1964
Roland Levillain4fa13f62015-07-06 18:11:54 +01001965 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001966 __ Bind(&true_label);
1967 __ movl(reg, Immediate(1));
1968 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001997}
1998
1999void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002001}
2002
2003void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002005}
2006
2007void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002009}
2010
2011void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002013}
2014
2015void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002017}
2018
Aart Bike9f37602015-10-09 11:15:55 -07002019void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
2031void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002033}
2034
2035void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002036 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002037}
2038
2039void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002040 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002041}
2042
2043void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002044 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002045}
2046
2047void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002049}
2050
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002051void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002052 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002053 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002054 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055}
2056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002057void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002058 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002059}
2060
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002061void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2062 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002063 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002064 locations->SetOut(Location::ConstantLocation(constant));
2065}
2066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002067void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002068 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002069}
2070
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002071void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002073 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002074 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075}
2076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002078 // Will be generated at use site.
2079}
2080
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2082 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002083 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002084 locations->SetOut(Location::ConstantLocation(constant));
2085}
2086
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002087void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002088 // Will be generated at use site.
2089}
2090
2091void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2092 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002094 locations->SetOut(Location::ConstantLocation(constant));
2095}
2096
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002097void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002098 // Will be generated at use site.
2099}
2100
Igor Murashkind01745e2017-04-05 16:40:31 -07002101void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2102 constructor_fence->SetLocations(nullptr);
2103}
2104
2105void InstructionCodeGeneratorX86::VisitConstructorFence(
2106 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2107 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2108}
2109
Calin Juravle27df7582015-04-17 19:12:31 +01002110void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2111 memory_barrier->SetLocations(nullptr);
2112}
2113
2114void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002115 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002116}
2117
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002118void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002119 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120}
2121
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002122void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002123 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002124}
2125
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002126void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002127 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002128 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002130 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002132 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 case DataType::Type::kInt8:
2134 case DataType::Type::kUint16:
2135 case DataType::Type::kInt16:
2136 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 break;
2139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002140 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002142 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 break;
2144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002145 case DataType::Type::kFloat32:
2146 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002147 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002148 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002149 break;
2150
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002151 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002152 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002153 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002154}
2155
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002156void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002157 if (kIsDebugBuild) {
2158 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002159 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002161 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 case DataType::Type::kInt8:
2163 case DataType::Type::kUint16:
2164 case DataType::Type::kInt16:
2165 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002166 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002167 break;
2168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002170 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2171 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002172 break;
2173
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 case DataType::Type::kFloat32:
2175 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002176 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002177 break;
2178
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002179 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002180 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002181 }
2182 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002183 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002184}
2185
Calin Juravle175dc732015-08-25 15:42:32 +01002186void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2187 // The trampoline uses the same calling convention as dex calling conventions,
2188 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2189 // the method_idx.
2190 HandleInvoke(invoke);
2191}
2192
2193void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2194 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2195}
2196
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002197void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002198 // Explicit clinit checks triggered by static invokes must have been pruned by
2199 // art::PrepareForRegisterAllocation.
2200 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002201
Mark Mendellfb8d2792015-03-31 22:16:59 -04002202 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002203 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002204 if (invoke->GetLocations()->CanCall() &&
2205 invoke->HasPcRelativeMethodLoadKind() &&
2206 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002207 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002208 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002209 return;
2210 }
2211
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002212 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002213
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002214 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002215 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002216 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002217 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002218}
2219
Mark Mendell09ed1a32015-03-25 08:30:06 -04002220static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2221 if (invoke->GetLocations()->Intrinsified()) {
2222 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2223 intrinsic.Dispatch(invoke);
2224 return true;
2225 }
2226 return false;
2227}
2228
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002229void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002230 // Explicit clinit checks triggered by static invokes must have been pruned by
2231 // art::PrepareForRegisterAllocation.
2232 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002233
Mark Mendell09ed1a32015-03-25 08:30:06 -04002234 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2235 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002236 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002237
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002238 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002239 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002240 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002241}
2242
2243void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002244 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2245 if (intrinsic.TryDispatch(invoke)) {
2246 return;
2247 }
2248
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002249 HandleInvoke(invoke);
2250}
2251
2252void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002253 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002254 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002255}
2256
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002257void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002258 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2259 return;
2260 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002261
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002262 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002263 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002264}
2265
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002267 // This call to HandleInvoke allocates a temporary (core) register
2268 // which is also used to transfer the hidden argument from FP to
2269 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 HandleInvoke(invoke);
2271 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002272 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273}
2274
2275void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2276 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002277 LocationSummary* locations = invoke->GetLocations();
2278 Register temp = locations->GetTemp(0).AsRegister<Register>();
2279 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 Location receiver = locations->InAt(0);
2281 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2282
Roland Levillain0d5a2812015-11-13 10:07:31 +00002283 // Set the hidden argument. This is safe to do this here, as XMM7
2284 // won't be modified thereafter, before the `call` instruction.
2285 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002286 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002287 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002288
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002289 if (receiver.IsStackSlot()) {
2290 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002291 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002292 __ movl(temp, Address(temp, class_offset));
2293 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002294 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002295 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002296 }
Roland Levillain4d027112015-07-01 15:41:14 +01002297 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002298 // Instead of simply (possibly) unpoisoning `temp` here, we should
2299 // emit a read barrier for the previous class reference load.
2300 // However this is not required in practice, as this is an
2301 // intermediate/temporary reference and because the current
2302 // concurrent copying collector keeps the from-space memory
2303 // intact/accessible until the end of the marking phase (the
2304 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002305 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002306 // temp = temp->GetAddressOfIMT()
2307 __ movl(temp,
2308 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002309 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002310 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002311 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002312 __ movl(temp, Address(temp, method_offset));
2313 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002314 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002315 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002316
2317 DCHECK(!codegen_->IsLeafMethod());
2318 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2319}
2320
Orion Hodsonac141392017-01-13 11:53:47 +00002321void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2322 HandleInvoke(invoke);
2323}
2324
2325void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2326 codegen_->GenerateInvokePolymorphicCall(invoke);
2327}
2328
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002329void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2330 HandleInvoke(invoke);
2331}
2332
2333void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2334 codegen_->GenerateInvokeCustomCall(invoke);
2335}
2336
Roland Levillain88cb1752014-10-20 16:36:47 +01002337void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2338 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002339 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002340 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kInt32:
2342 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002343 locations->SetInAt(0, Location::RequiresRegister());
2344 locations->SetOut(Location::SameAsFirstInput());
2345 break;
2346
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002348 locations->SetInAt(0, Location::RequiresFpuRegister());
2349 locations->SetOut(Location::SameAsFirstInput());
2350 locations->AddTemp(Location::RequiresRegister());
2351 locations->AddTemp(Location::RequiresFpuRegister());
2352 break;
2353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002355 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002356 locations->SetOut(Location::SameAsFirstInput());
2357 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002358 break;
2359
2360 default:
2361 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2362 }
2363}
2364
2365void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2366 LocationSummary* locations = neg->GetLocations();
2367 Location out = locations->Out();
2368 Location in = locations->InAt(0);
2369 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002371 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002372 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002373 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002374 break;
2375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002377 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002378 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002379 __ negl(out.AsRegisterPairLow<Register>());
2380 // Negation is similar to subtraction from zero. The least
2381 // significant byte triggers a borrow when it is different from
2382 // zero; to take it into account, add 1 to the most significant
2383 // byte if the carry flag (CF) is set to 1 after the first NEGL
2384 // operation.
2385 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2386 __ negl(out.AsRegisterPairHigh<Register>());
2387 break;
2388
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002389 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002390 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002391 Register constant = locations->GetTemp(0).AsRegister<Register>();
2392 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002393 // Implement float negation with an exclusive or with value
2394 // 0x80000000 (mask for bit 31, representing the sign of a
2395 // single-precision floating-point number).
2396 __ movl(constant, Immediate(INT32_C(0x80000000)));
2397 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002398 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002399 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002400 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002403 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002404 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002405 // Implement double negation with an exclusive or with value
2406 // 0x8000000000000000 (mask for bit 63, representing the sign of
2407 // a double-precision floating-point number).
2408 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002409 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002410 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002411 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002412
2413 default:
2414 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2415 }
2416}
2417
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002418void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2419 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002420 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002422 locations->SetInAt(0, Location::RequiresFpuRegister());
2423 locations->SetInAt(1, Location::RequiresRegister());
2424 locations->SetOut(Location::SameAsFirstInput());
2425 locations->AddTemp(Location::RequiresFpuRegister());
2426}
2427
2428void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2429 LocationSummary* locations = neg->GetLocations();
2430 Location out = locations->Out();
2431 DCHECK(locations->InAt(0).Equals(out));
2432
2433 Register constant_area = locations->InAt(1).AsRegister<Register>();
2434 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002435 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002436 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2437 neg->GetBaseMethodAddress(),
2438 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002439 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2440 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002441 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2442 neg->GetBaseMethodAddress(),
2443 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002444 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2445 }
2446}
2447
Roland Levillaindff1f282014-11-05 14:15:05 +00002448void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002449 DataType::Type result_type = conversion->GetResultType();
2450 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002451 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2452 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002453
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002454 // The float-to-long and double-to-long type conversions rely on a
2455 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002456 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2458 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002459 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002460 : LocationSummary::kNoCall;
2461 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002462 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002463
Roland Levillaindff1f282014-11-05 14:15:05 +00002464 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002465 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002467 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002468 case DataType::Type::kUint8:
2469 case DataType::Type::kInt8:
2470 case DataType::Type::kUint16:
2471 case DataType::Type::kInt16:
2472 case DataType::Type::kInt32:
2473 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2474 // Make the output overlap to please the register allocator. This greatly simplifies
2475 // the validation of the linear scan implementation
2476 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2477 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002478 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002479 HInstruction* input = conversion->InputAt(0);
2480 Location input_location = input->IsConstant()
2481 ? Location::ConstantLocation(input->AsConstant())
2482 : Location::RegisterPairLocation(EAX, EDX);
2483 locations->SetInAt(0, input_location);
2484 // Make the output overlap to please the register allocator. This greatly simplifies
2485 // the validation of the linear scan implementation
2486 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2487 break;
2488 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 }
2494 break;
2495
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002496 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002497 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002498 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2499 locations->SetInAt(0, Location::Any());
2500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002501 break;
2502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002503 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002504 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002506 locations->SetInAt(0, Location::Any());
2507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2508 break;
2509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002511 locations->SetInAt(0, Location::RequiresFpuRegister());
2512 locations->SetOut(Location::RequiresRegister());
2513 locations->AddTemp(Location::RequiresFpuRegister());
2514 break;
2515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002517 locations->SetInAt(0, Location::RequiresFpuRegister());
2518 locations->SetOut(Location::RequiresRegister());
2519 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
2526 break;
2527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002529 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002530 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002531 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002533 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kInt16:
2535 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002536 locations->SetInAt(0, Location::RegisterLocation(EAX));
2537 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2538 break;
2539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kFloat32:
2541 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002542 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002543 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2544 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2545
Vladimir Marko949c91f2015-01-27 10:48:44 +00002546 // The runtime helper puts the result in EAX, EDX.
2547 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002548 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002549 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002550
2551 default:
2552 LOG(FATAL) << "Unexpected type conversion from " << input_type
2553 << " to " << result_type;
2554 }
2555 break;
2556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002558 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002560 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002562 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 case DataType::Type::kInt16:
2564 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetOut(Location::RequiresFpuRegister());
2567 break;
2568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002569 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002570 locations->SetInAt(0, Location::Any());
2571 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002572 break;
2573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002574 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002575 locations->SetInAt(0, Location::RequiresFpuRegister());
2576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002577 break;
2578
2579 default:
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type
2581 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002582 }
Roland Levillaincff13742014-11-17 14:32:17 +00002583 break;
2584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002585 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002586 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002588 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002590 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 case DataType::Type::kInt16:
2592 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002593 locations->SetInAt(0, Location::RequiresRegister());
2594 locations->SetOut(Location::RequiresFpuRegister());
2595 break;
2596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002598 locations->SetInAt(0, Location::Any());
2599 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002600 break;
2601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002603 locations->SetInAt(0, Location::RequiresFpuRegister());
2604 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002605 break;
2606
2607 default:
2608 LOG(FATAL) << "Unexpected type conversion from " << input_type
2609 << " to " << result_type;
2610 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002611 break;
2612
2613 default:
2614 LOG(FATAL) << "Unexpected type conversion from " << input_type
2615 << " to " << result_type;
2616 }
2617}
2618
2619void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2620 LocationSummary* locations = conversion->GetLocations();
2621 Location out = locations->Out();
2622 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002623 DataType::Type result_type = conversion->GetResultType();
2624 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002625 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2626 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002627 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002628 case DataType::Type::kUint8:
2629 switch (input_type) {
2630 case DataType::Type::kInt8:
2631 case DataType::Type::kUint16:
2632 case DataType::Type::kInt16:
2633 case DataType::Type::kInt32:
2634 if (in.IsRegister()) {
2635 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2636 } else {
2637 DCHECK(in.GetConstant()->IsIntConstant());
2638 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2640 }
2641 break;
2642 case DataType::Type::kInt64:
2643 if (in.IsRegisterPair()) {
2644 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2645 } else {
2646 DCHECK(in.GetConstant()->IsLongConstant());
2647 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2648 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2649 }
2650 break;
2651
2652 default:
2653 LOG(FATAL) << "Unexpected type conversion from " << input_type
2654 << " to " << result_type;
2655 }
2656 break;
2657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002659 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kUint8:
2661 case DataType::Type::kUint16:
2662 case DataType::Type::kInt16:
2663 case DataType::Type::kInt32:
2664 if (in.IsRegister()) {
2665 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2666 } else {
2667 DCHECK(in.GetConstant()->IsIntConstant());
2668 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2669 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2670 }
2671 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002673 if (in.IsRegisterPair()) {
2674 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2675 } else {
2676 DCHECK(in.GetConstant()->IsLongConstant());
2677 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2678 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2679 }
2680 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002681
2682 default:
2683 LOG(FATAL) << "Unexpected type conversion from " << input_type
2684 << " to " << result_type;
2685 }
2686 break;
2687
2688 case DataType::Type::kUint16:
2689 switch (input_type) {
2690 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 case DataType::Type::kInt16:
2692 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002693 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002694 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2695 } else if (in.IsStackSlot()) {
2696 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002697 } else {
2698 DCHECK(in.GetConstant()->IsIntConstant());
2699 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002700 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2701 }
2702 break;
2703 case DataType::Type::kInt64:
2704 if (in.IsRegisterPair()) {
2705 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2706 } else if (in.IsDoubleStackSlot()) {
2707 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2708 } else {
2709 DCHECK(in.GetConstant()->IsLongConstant());
2710 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2711 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002712 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002713 break;
2714
2715 default:
2716 LOG(FATAL) << "Unexpected type conversion from " << input_type
2717 << " to " << result_type;
2718 }
2719 break;
2720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002722 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002724 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002725 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002726 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002727 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002729 } else {
2730 DCHECK(in.GetConstant()->IsIntConstant());
2731 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002733 }
2734 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002735 case DataType::Type::kInt64:
2736 if (in.IsRegisterPair()) {
2737 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2738 } else if (in.IsDoubleStackSlot()) {
2739 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2740 } else {
2741 DCHECK(in.GetConstant()->IsLongConstant());
2742 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2743 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2744 }
2745 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002746
2747 default:
2748 LOG(FATAL) << "Unexpected type conversion from " << input_type
2749 << " to " << result_type;
2750 }
2751 break;
2752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002754 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002756 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002758 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002759 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002760 } else {
2761 DCHECK(in.IsConstant());
2762 DCHECK(in.GetConstant()->IsLongConstant());
2763 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002764 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002765 }
2766 break;
2767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002769 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2770 Register output = out.AsRegister<Register>();
2771 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002772 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002773
2774 __ movl(output, Immediate(kPrimIntMax));
2775 // temp = int-to-float(output)
2776 __ cvtsi2ss(temp, output);
2777 // if input >= temp goto done
2778 __ comiss(input, temp);
2779 __ j(kAboveEqual, &done);
2780 // if input == NaN goto nan
2781 __ j(kUnordered, &nan);
2782 // output = float-to-int-truncate(input)
2783 __ cvttss2si(output, input);
2784 __ jmp(&done);
2785 __ Bind(&nan);
2786 // output = 0
2787 __ xorl(output, output);
2788 __ Bind(&done);
2789 break;
2790 }
2791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002793 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2794 Register output = out.AsRegister<Register>();
2795 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002796 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002797
2798 __ movl(output, Immediate(kPrimIntMax));
2799 // temp = int-to-double(output)
2800 __ cvtsi2sd(temp, output);
2801 // if input >= temp goto done
2802 __ comisd(input, temp);
2803 __ j(kAboveEqual, &done);
2804 // if input == NaN goto nan
2805 __ j(kUnordered, &nan);
2806 // output = double-to-int-truncate(input)
2807 __ cvttsd2si(output, input);
2808 __ jmp(&done);
2809 __ Bind(&nan);
2810 // output = 0
2811 __ xorl(output, output);
2812 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002813 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002814 }
Roland Levillain946e1432014-11-11 17:35:19 +00002815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 }
2820 break;
2821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002823 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002825 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002827 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 case DataType::Type::kInt16:
2829 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002830 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2831 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002832 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002833 __ cdq();
2834 break;
2835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002837 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002838 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002839 break;
2840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002842 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002843 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002844 break;
2845
2846 default:
2847 LOG(FATAL) << "Unexpected type conversion from " << input_type
2848 << " to " << result_type;
2849 }
2850 break;
2851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002852 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002853 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002854 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002855 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002856 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002857 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 case DataType::Type::kInt16:
2859 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002861 break;
2862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002863 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002864 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002865
Roland Levillain232ade02015-04-20 15:14:36 +01002866 // Create stack space for the call to
2867 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2868 // TODO: enhance register allocator to ask for stack temporaries.
2869 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002871 __ subl(ESP, Immediate(adjustment));
2872 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002873
Roland Levillain232ade02015-04-20 15:14:36 +01002874 // Load the value to the FP stack, using temporaries if needed.
2875 PushOntoFPStack(in, 0, adjustment, false, true);
2876
2877 if (out.IsStackSlot()) {
2878 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2879 } else {
2880 __ fstps(Address(ESP, 0));
2881 Location stack_temp = Location::StackSlot(0);
2882 codegen_->Move32(out, stack_temp);
2883 }
2884
2885 // Remove the temporary stack space we allocated.
2886 if (adjustment != 0) {
2887 __ addl(ESP, Immediate(adjustment));
2888 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002889 break;
2890 }
2891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002893 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002894 break;
2895
2896 default:
2897 LOG(FATAL) << "Unexpected type conversion from " << input_type
2898 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002899 }
Roland Levillaincff13742014-11-17 14:32:17 +00002900 break;
2901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002903 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002905 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002907 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kInt16:
2909 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002910 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002911 break;
2912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002914 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002915
Roland Levillain232ade02015-04-20 15:14:36 +01002916 // Create stack space for the call to
2917 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2918 // TODO: enhance register allocator to ask for stack temporaries.
2919 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002921 __ subl(ESP, Immediate(adjustment));
2922 }
2923
2924 // Load the value to the FP stack, using temporaries if needed.
2925 PushOntoFPStack(in, 0, adjustment, false, true);
2926
2927 if (out.IsDoubleStackSlot()) {
2928 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2929 } else {
2930 __ fstpl(Address(ESP, 0));
2931 Location stack_temp = Location::DoubleStackSlot(0);
2932 codegen_->Move64(out, stack_temp);
2933 }
2934
2935 // Remove the temporary stack space we allocated.
2936 if (adjustment != 0) {
2937 __ addl(ESP, Immediate(adjustment));
2938 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002939 break;
2940 }
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002943 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002944 break;
2945
2946 default:
2947 LOG(FATAL) << "Unexpected type conversion from " << input_type
2948 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002949 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unexpected type conversion from " << input_type
2954 << " to " << result_type;
2955 }
2956}
2957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002959 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002960 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002961 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2966 break;
2967 }
2968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002970 locations->SetInAt(0, Location::RequiresRegister());
2971 locations->SetInAt(1, Location::Any());
2972 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 break;
2974 }
2975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kFloat32:
2977 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2980 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002981 } else if (add->InputAt(1)->IsConstant()) {
2982 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002983 } else {
2984 locations->SetInAt(1, Location::Any());
2985 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002991 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2992 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994}
2995
2996void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2997 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 Location first = locations->InAt(0);
2999 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003000 Location out = locations->Out();
3001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003002 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003005 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003007 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3008 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003009 } else {
3010 __ leal(out.AsRegister<Register>(), Address(
3011 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3012 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003014 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3015 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3016 __ addl(out.AsRegister<Register>(), Immediate(value));
3017 } else {
3018 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3019 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003021 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003023 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003024 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025 }
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003028 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003029 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3030 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003032 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3033 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003034 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003035 } else {
3036 DCHECK(second.IsConstant()) << second;
3037 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3038 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3039 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003040 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003041 break;
3042 }
3043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003047 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3048 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003049 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003050 __ addss(first.AsFpuRegister<XmmRegister>(),
3051 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003052 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3053 const_area->GetBaseMethodAddress(),
3054 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003055 } else {
3056 DCHECK(second.IsStackSlot());
3057 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003060 }
3061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003063 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003065 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3066 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003067 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003068 __ addsd(first.AsFpuRegister<XmmRegister>(),
3069 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003070 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3071 const_area->GetBaseMethodAddress(),
3072 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003073 } else {
3074 DCHECK(second.IsDoubleStackSlot());
3075 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003076 }
3077 break;
3078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003080 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003081 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003082 }
3083}
3084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003086 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003087 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003088 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 case DataType::Type::kInt32:
3090 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003091 locations->SetInAt(0, Location::RequiresRegister());
3092 locations->SetInAt(1, Location::Any());
3093 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003094 break;
3095 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 case DataType::Type::kFloat32:
3097 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003098 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003101 } else if (sub->InputAt(1)->IsConstant()) {
3102 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003103 } else {
3104 locations->SetInAt(1, Location::Any());
3105 }
Calin Juravle11351682014-10-23 15:38:15 +01003106 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003107 break;
Calin Juravle11351682014-10-23 15:38:15 +01003108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 default:
Calin Juravle11351682014-10-23 15:38:15 +01003111 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003112 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003113}
3114
3115void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3116 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003117 Location first = locations->InAt(0);
3118 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003119 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003122 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003124 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003125 __ subl(first.AsRegister<Register>(),
3126 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003127 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003129 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003130 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003131 }
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003134 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003135 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3136 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003137 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003138 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003139 __ sbbl(first.AsRegisterPairHigh<Register>(),
3140 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003141 } else {
3142 DCHECK(second.IsConstant()) << second;
3143 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3144 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3145 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003146 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003147 break;
3148 }
3149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003151 if (second.IsFpuRegister()) {
3152 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3153 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3154 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003155 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003156 __ subss(first.AsFpuRegister<XmmRegister>(),
3157 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003158 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3159 const_area->GetBaseMethodAddress(),
3160 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003161 } else {
3162 DCHECK(second.IsStackSlot());
3163 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3164 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003165 break;
Calin Juravle11351682014-10-23 15:38:15 +01003166 }
3167
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003168 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003169 if (second.IsFpuRegister()) {
3170 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3171 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3172 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003173 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003174 __ subsd(first.AsFpuRegister<XmmRegister>(),
3175 codegen_->LiteralDoubleAddress(
3176 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003177 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003178 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3179 } else {
3180 DCHECK(second.IsDoubleStackSlot());
3181 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3182 }
Calin Juravle11351682014-10-23 15:38:15 +01003183 break;
3184 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003186 default:
Calin Juravle11351682014-10-23 15:38:15 +01003187 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003188 }
3189}
3190
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191void LocationsBuilderX86::VisitMul(HMul* mul) {
3192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003193 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 locations->SetInAt(0, Location::RequiresRegister());
3197 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003198 if (mul->InputAt(1)->IsIntConstant()) {
3199 // Can use 3 operand multiply.
3200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3201 } else {
3202 locations->SetOut(Location::SameAsFirstInput());
3203 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 locations->SetInAt(1, Location::Any());
3208 locations->SetOut(Location::SameAsFirstInput());
3209 // Needed for imul on 32bits with 64bits output.
3210 locations->AddTemp(Location::RegisterLocation(EAX));
3211 locations->AddTemp(Location::RegisterLocation(EDX));
3212 break;
3213 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 case DataType::Type::kFloat32:
3215 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003216 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003217 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3218 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003219 } else if (mul->InputAt(1)->IsConstant()) {
3220 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003221 } else {
3222 locations->SetInAt(1, Location::Any());
3223 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003224 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003226 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227
3228 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003229 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 }
3231}
3232
3233void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3234 LocationSummary* locations = mul->GetLocations();
3235 Location first = locations->InAt(0);
3236 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003237 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238
3239 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003241 // The constant may have ended up in a register, so test explicitly to avoid
3242 // problems where the output may not be the same as the first operand.
3243 if (mul->InputAt(1)->IsIntConstant()) {
3244 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3245 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3246 } else if (second.IsRegister()) {
3247 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249 } else {
3250 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003251 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253 }
3254 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003255
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003256 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257 Register in1_hi = first.AsRegisterPairHigh<Register>();
3258 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 Register eax = locations->GetTemp(0).AsRegister<Register>();
3260 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003261
3262 DCHECK_EQ(EAX, eax);
3263 DCHECK_EQ(EDX, edx);
3264
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003265 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003266 // output: in1
3267 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3268 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3269 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003270 if (second.IsConstant()) {
3271 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003272
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003273 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3274 int32_t low_value = Low32Bits(value);
3275 int32_t high_value = High32Bits(value);
3276 Immediate low(low_value);
3277 Immediate high(high_value);
3278
3279 __ movl(eax, high);
3280 // eax <- in1.lo * in2.hi
3281 __ imull(eax, in1_lo);
3282 // in1.hi <- in1.hi * in2.lo
3283 __ imull(in1_hi, low);
3284 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3285 __ addl(in1_hi, eax);
3286 // move in2_lo to eax to prepare for double precision
3287 __ movl(eax, low);
3288 // edx:eax <- in1.lo * in2.lo
3289 __ mull(in1_lo);
3290 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3291 __ addl(in1_hi, edx);
3292 // in1.lo <- (in1.lo * in2.lo)[31:0];
3293 __ movl(in1_lo, eax);
3294 } else if (second.IsRegisterPair()) {
3295 Register in2_hi = second.AsRegisterPairHigh<Register>();
3296 Register in2_lo = second.AsRegisterPairLow<Register>();
3297
3298 __ movl(eax, in2_hi);
3299 // eax <- in1.lo * in2.hi
3300 __ imull(eax, in1_lo);
3301 // in1.hi <- in1.hi * in2.lo
3302 __ imull(in1_hi, in2_lo);
3303 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3304 __ addl(in1_hi, eax);
3305 // move in1_lo to eax to prepare for double precision
3306 __ movl(eax, in1_lo);
3307 // edx:eax <- in1.lo * in2.lo
3308 __ mull(in2_lo);
3309 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3310 __ addl(in1_hi, edx);
3311 // in1.lo <- (in1.lo * in2.lo)[31:0];
3312 __ movl(in1_lo, eax);
3313 } else {
3314 DCHECK(second.IsDoubleStackSlot()) << second;
3315 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3316 Address in2_lo(ESP, second.GetStackIndex());
3317
3318 __ movl(eax, in2_hi);
3319 // eax <- in1.lo * in2.hi
3320 __ imull(eax, in1_lo);
3321 // in1.hi <- in1.hi * in2.lo
3322 __ imull(in1_hi, in2_lo);
3323 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3324 __ addl(in1_hi, eax);
3325 // move in1_lo to eax to prepare for double precision
3326 __ movl(eax, in1_lo);
3327 // edx:eax <- in1.lo * in2.lo
3328 __ mull(in2_lo);
3329 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3330 __ addl(in1_hi, edx);
3331 // in1.lo <- (in1.lo * in2.lo)[31:0];
3332 __ movl(in1_lo, eax);
3333 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334
3335 break;
3336 }
3337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003339 DCHECK(first.Equals(locations->Out()));
3340 if (second.IsFpuRegister()) {
3341 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3342 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3343 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003344 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 __ mulss(first.AsFpuRegister<XmmRegister>(),
3346 codegen_->LiteralFloatAddress(
3347 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003348 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3350 } else {
3351 DCHECK(second.IsStackSlot());
3352 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3353 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003355 }
3356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003357 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003358 DCHECK(first.Equals(locations->Out()));
3359 if (second.IsFpuRegister()) {
3360 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3361 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3362 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003363 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003364 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3365 codegen_->LiteralDoubleAddress(
3366 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003367 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003368 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3369 } else {
3370 DCHECK(second.IsDoubleStackSlot());
3371 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3372 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003373 break;
3374 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375
3376 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003377 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003378 }
3379}
3380
Roland Levillain232ade02015-04-20 15:14:36 +01003381void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3382 uint32_t temp_offset,
3383 uint32_t stack_adjustment,
3384 bool is_fp,
3385 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003386 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003387 DCHECK(!is_wide);
3388 if (is_fp) {
3389 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 } else {
3391 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3392 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003393 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003394 DCHECK(is_wide);
3395 if (is_fp) {
3396 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3397 } else {
3398 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3399 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003400 } else {
3401 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003402 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403 Location stack_temp = Location::StackSlot(temp_offset);
3404 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003405 if (is_fp) {
3406 __ flds(Address(ESP, temp_offset));
3407 } else {
3408 __ filds(Address(ESP, temp_offset));
3409 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003410 } else {
3411 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3412 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003413 if (is_fp) {
3414 __ fldl(Address(ESP, temp_offset));
3415 } else {
3416 __ fildl(Address(ESP, temp_offset));
3417 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003418 }
3419 }
3420}
3421
3422void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003423 DataType::Type type = rem->GetResultType();
3424 bool is_float = type == DataType::Type::kFloat32;
3425 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003426 LocationSummary* locations = rem->GetLocations();
3427 Location first = locations->InAt(0);
3428 Location second = locations->InAt(1);
3429 Location out = locations->Out();
3430
3431 // Create stack space for 2 elements.
3432 // TODO: enhance register allocator to ask for stack temporaries.
3433 __ subl(ESP, Immediate(2 * elem_size));
3434
3435 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003436 const bool is_wide = !is_float;
3437 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3438 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003439
3440 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003441 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003442 __ Bind(&retry);
3443 __ fprem();
3444
3445 // Move FP status to AX.
3446 __ fstsw();
3447
3448 // And see if the argument reduction is complete. This is signaled by the
3449 // C2 FPU flag bit set to 0.
3450 __ andl(EAX, Immediate(kC2ConditionMask));
3451 __ j(kNotEqual, &retry);
3452
3453 // We have settled on the final value. Retrieve it into an XMM register.
3454 // Store FP top of stack to real stack.
3455 if (is_float) {
3456 __ fsts(Address(ESP, 0));
3457 } else {
3458 __ fstl(Address(ESP, 0));
3459 }
3460
3461 // Pop the 2 items from the FP stack.
3462 __ fucompp();
3463
3464 // Load the value from the stack into an XMM register.
3465 DCHECK(out.IsFpuRegister()) << out;
3466 if (is_float) {
3467 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3468 } else {
3469 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3470 }
3471
3472 // And remove the temporary stack space we allocated.
3473 __ addl(ESP, Immediate(2 * elem_size));
3474}
3475
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476
3477void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3478 DCHECK(instruction->IsDiv() || instruction->IsRem());
3479
3480 LocationSummary* locations = instruction->GetLocations();
3481 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003482 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483
3484 Register out_register = locations->Out().AsRegister<Register>();
3485 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487
3488 DCHECK(imm == 1 || imm == -1);
3489
3490 if (instruction->IsRem()) {
3491 __ xorl(out_register, out_register);
3492 } else {
3493 __ movl(out_register, input_register);
3494 if (imm == -1) {
3495 __ negl(out_register);
3496 }
3497 }
3498}
3499
3500
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003501void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 LocationSummary* locations = instruction->GetLocations();
3503
3504 Register out_register = locations->Out().AsRegister<Register>();
3505 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003506 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003507 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3508 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 Register num = locations->GetTemp(0).AsRegister<Register>();
3511
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003512 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513 __ testl(input_register, input_register);
3514 __ cmovl(kGreaterEqual, num, input_register);
3515 int shift = CTZ(imm);
3516 __ sarl(num, Immediate(shift));
3517
3518 if (imm < 0) {
3519 __ negl(num);
3520 }
3521
3522 __ movl(out_register, num);
3523}
3524
3525void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3526 DCHECK(instruction->IsDiv() || instruction->IsRem());
3527
3528 LocationSummary* locations = instruction->GetLocations();
3529 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3530
3531 Register eax = locations->InAt(0).AsRegister<Register>();
3532 Register out = locations->Out().AsRegister<Register>();
3533 Register num;
3534 Register edx;
3535
3536 if (instruction->IsDiv()) {
3537 edx = locations->GetTemp(0).AsRegister<Register>();
3538 num = locations->GetTemp(1).AsRegister<Register>();
3539 } else {
3540 edx = locations->Out().AsRegister<Register>();
3541 num = locations->GetTemp(0).AsRegister<Register>();
3542 }
3543
3544 DCHECK_EQ(EAX, eax);
3545 DCHECK_EQ(EDX, edx);
3546 if (instruction->IsDiv()) {
3547 DCHECK_EQ(EAX, out);
3548 } else {
3549 DCHECK_EQ(EDX, out);
3550 }
3551
3552 int64_t magic;
3553 int shift;
3554 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 // Save the numerator.
3557 __ movl(num, eax);
3558
3559 // EAX = magic
3560 __ movl(eax, Immediate(magic));
3561
3562 // EDX:EAX = magic * numerator
3563 __ imull(num);
3564
3565 if (imm > 0 && magic < 0) {
3566 // EDX += num
3567 __ addl(edx, num);
3568 } else if (imm < 0 && magic > 0) {
3569 __ subl(edx, num);
3570 }
3571
3572 // Shift if needed.
3573 if (shift != 0) {
3574 __ sarl(edx, Immediate(shift));
3575 }
3576
3577 // EDX += 1 if EDX < 0
3578 __ movl(eax, edx);
3579 __ shrl(edx, Immediate(31));
3580 __ addl(edx, eax);
3581
3582 if (instruction->IsRem()) {
3583 __ movl(eax, num);
3584 __ imull(edx, Immediate(imm));
3585 __ subl(eax, edx);
3586 __ movl(edx, eax);
3587 } else {
3588 __ movl(eax, edx);
3589 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590}
3591
Calin Juravlebacfec32014-11-14 15:54:36 +00003592void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3593 DCHECK(instruction->IsDiv() || instruction->IsRem());
3594
3595 LocationSummary* locations = instruction->GetLocations();
3596 Location out = locations->Out();
3597 Location first = locations->InAt(0);
3598 Location second = locations->InAt(1);
3599 bool is_div = instruction->IsDiv();
3600
3601 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003602 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 DCHECK_EQ(EAX, first.AsRegister<Register>());
3604 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003606 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003607 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003608
3609 if (imm == 0) {
3610 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3611 } else if (imm == 1 || imm == -1) {
3612 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003613 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003614 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 } else {
3616 DCHECK(imm <= -2 || imm >= 2);
3617 GenerateDivRemWithAnyConstant(instruction);
3618 }
3619 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003620 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003621 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003623
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 Register second_reg = second.AsRegister<Register>();
3625 // 0x80000000/-1 triggers an arithmetic exception!
3626 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3627 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003628
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 __ cmpl(second_reg, Immediate(-1));
3630 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003631
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632 // edx:eax <- sign-extended of eax
3633 __ cdq();
3634 // eax = quotient, edx = remainder
3635 __ idivl(second_reg);
3636 __ Bind(slow_path->GetExitLabel());
3637 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 break;
3639 }
3640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003642 InvokeRuntimeCallingConvention calling_convention;
3643 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3645 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3646 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3647 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3648 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3649
3650 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003651 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003652 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003654 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003655 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 break;
3658 }
3659
3660 default:
3661 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3662 }
3663}
3664
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003666 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003667 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003668 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003670
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003672 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003673 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003674 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003675 locations->SetOut(Location::SameAsFirstInput());
3676 // Intel uses edx:eax as the dividend.
3677 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003678 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3679 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3680 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003681 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003682 locations->AddTemp(Location::RequiresRegister());
3683 }
Calin Juravled0d48522014-11-04 16:40:20 +00003684 break;
3685 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003687 InvokeRuntimeCallingConvention calling_convention;
3688 locations->SetInAt(0, Location::RegisterPairLocation(
3689 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3690 locations->SetInAt(1, Location::RegisterPairLocation(
3691 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3692 // Runtime helper puts the result in EAX, EDX.
3693 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 break;
3695 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kFloat32:
3697 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003698 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003699 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3700 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003701 } else if (div->InputAt(1)->IsConstant()) {
3702 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003703 } else {
3704 locations->SetInAt(1, Location::Any());
3705 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003706 locations->SetOut(Location::SameAsFirstInput());
3707 break;
3708 }
3709
3710 default:
3711 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3712 }
3713}
3714
3715void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3716 LocationSummary* locations = div->GetLocations();
3717 Location first = locations->InAt(0);
3718 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719
3720 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003721 case DataType::Type::kInt32:
3722 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003723 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003724 break;
3725 }
3726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 if (second.IsFpuRegister()) {
3729 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3730 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3731 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003732 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 __ divss(first.AsFpuRegister<XmmRegister>(),
3734 codegen_->LiteralFloatAddress(
3735 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003736 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3738 } else {
3739 DCHECK(second.IsStackSlot());
3740 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3741 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003742 break;
3743 }
3744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003746 if (second.IsFpuRegister()) {
3747 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3748 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3749 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003750 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003751 __ divsd(first.AsFpuRegister<XmmRegister>(),
3752 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003753 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3754 const_area->GetBaseMethodAddress(),
3755 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003756 } else {
3757 DCHECK(second.IsDoubleStackSlot());
3758 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3759 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003760 break;
3761 }
3762
3763 default:
3764 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3765 }
3766}
3767
Calin Juravlebacfec32014-11-14 15:54:36 +00003768void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003769 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003772 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003773 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003774 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003775
Calin Juravled2ec87d2014-12-08 14:24:46 +00003776 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003781 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3782 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3783 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003784 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003785 locations->AddTemp(Location::RequiresRegister());
3786 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003787 break;
3788 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003789 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003790 InvokeRuntimeCallingConvention calling_convention;
3791 locations->SetInAt(0, Location::RegisterPairLocation(
3792 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3793 locations->SetInAt(1, Location::RegisterPairLocation(
3794 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3795 // Runtime helper puts the result in EAX, EDX.
3796 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3797 break;
3798 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003799 case DataType::Type::kFloat64:
3800 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003801 locations->SetInAt(0, Location::Any());
3802 locations->SetInAt(1, Location::Any());
3803 locations->SetOut(Location::RequiresFpuRegister());
3804 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003805 break;
3806 }
3807
3808 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003809 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 }
3811}
3812
3813void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003815 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003816 case DataType::Type::kInt32:
3817 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 GenerateDivRemIntegral(rem);
3819 break;
3820 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kFloat32:
3822 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003823 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003824 break;
3825 }
3826 default:
3827 LOG(FATAL) << "Unexpected rem type " << type;
3828 }
3829}
3830
Aart Bik1f8d51b2018-02-15 10:42:37 -08003831static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3832 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3833 switch (minmax->GetResultType()) {
3834 case DataType::Type::kInt32:
3835 locations->SetInAt(0, Location::RequiresRegister());
3836 locations->SetInAt(1, Location::RequiresRegister());
3837 locations->SetOut(Location::SameAsFirstInput());
3838 break;
3839 case DataType::Type::kInt64:
3840 locations->SetInAt(0, Location::RequiresRegister());
3841 locations->SetInAt(1, Location::RequiresRegister());
3842 locations->SetOut(Location::SameAsFirstInput());
3843 // Register to use to perform a long subtract to set cc.
3844 locations->AddTemp(Location::RequiresRegister());
3845 break;
3846 case DataType::Type::kFloat32:
3847 locations->SetInAt(0, Location::RequiresFpuRegister());
3848 locations->SetInAt(1, Location::RequiresFpuRegister());
3849 locations->SetOut(Location::SameAsFirstInput());
3850 locations->AddTemp(Location::RequiresRegister());
3851 break;
3852 case DataType::Type::kFloat64:
3853 locations->SetInAt(0, Location::RequiresFpuRegister());
3854 locations->SetInAt(1, Location::RequiresFpuRegister());
3855 locations->SetOut(Location::SameAsFirstInput());
3856 break;
3857 default:
3858 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3859 }
3860}
3861
Aart Bik351df3e2018-03-07 11:54:57 -08003862void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3863 bool is_min,
3864 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003865 Location op1_loc = locations->InAt(0);
3866 Location op2_loc = locations->InAt(1);
3867
3868 // Shortcut for same input locations.
3869 if (op1_loc.Equals(op2_loc)) {
3870 // Can return immediately, as op1_loc == out_loc.
3871 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3872 // a copy here.
3873 DCHECK(locations->Out().Equals(op1_loc));
3874 return;
3875 }
3876
3877 if (type == DataType::Type::kInt64) {
3878 // Need to perform a subtract to get the sign right.
3879 // op1 is already in the same location as the output.
3880 Location output = locations->Out();
3881 Register output_lo = output.AsRegisterPairLow<Register>();
3882 Register output_hi = output.AsRegisterPairHigh<Register>();
3883
3884 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3885 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3886
3887 // The comparison is performed by subtracting the second operand from
3888 // the first operand and then setting the status flags in the same
3889 // manner as the SUB instruction."
3890 __ cmpl(output_lo, op2_lo);
3891
3892 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3893 Register temp = locations->GetTemp(0).AsRegister<Register>();
3894 __ movl(temp, output_hi);
3895 __ sbbl(temp, op2_hi);
3896
3897 // Now the condition code is correct.
3898 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3899 __ cmovl(cond, output_lo, op2_lo);
3900 __ cmovl(cond, output_hi, op2_hi);
3901 } else {
3902 DCHECK_EQ(type, DataType::Type::kInt32);
3903 Register out = locations->Out().AsRegister<Register>();
3904 Register op2 = op2_loc.AsRegister<Register>();
3905
3906 // (out := op1)
3907 // out <=? op2
3908 // if out is min jmp done
3909 // out := op2
3910 // done:
3911
3912 __ cmpl(out, op2);
3913 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3914 __ cmovl(cond, out, op2);
3915 }
3916}
3917
3918void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3919 bool is_min,
3920 DataType::Type type) {
3921 Location op1_loc = locations->InAt(0);
3922 Location op2_loc = locations->InAt(1);
3923 Location out_loc = locations->Out();
3924 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3925
3926 // Shortcut for same input locations.
3927 if (op1_loc.Equals(op2_loc)) {
3928 DCHECK(out_loc.Equals(op1_loc));
3929 return;
3930 }
3931
3932 // (out := op1)
3933 // out <=? op2
3934 // if Nan jmp Nan_label
3935 // if out is min jmp done
3936 // if op2 is min jmp op2_label
3937 // handle -0/+0
3938 // jmp done
3939 // Nan_label:
3940 // out := NaN
3941 // op2_label:
3942 // out := op2
3943 // done:
3944 //
3945 // This removes one jmp, but needs to copy one input (op1) to out.
3946 //
3947 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3948
3949 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3950
3951 NearLabel nan, done, op2_label;
3952 if (type == DataType::Type::kFloat64) {
3953 __ ucomisd(out, op2);
3954 } else {
3955 DCHECK_EQ(type, DataType::Type::kFloat32);
3956 __ ucomiss(out, op2);
3957 }
3958
3959 __ j(Condition::kParityEven, &nan);
3960
3961 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3962 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3963
3964 // Handle 0.0/-0.0.
3965 if (is_min) {
3966 if (type == DataType::Type::kFloat64) {
3967 __ orpd(out, op2);
3968 } else {
3969 __ orps(out, op2);
3970 }
3971 } else {
3972 if (type == DataType::Type::kFloat64) {
3973 __ andpd(out, op2);
3974 } else {
3975 __ andps(out, op2);
3976 }
3977 }
3978 __ jmp(&done);
3979
3980 // NaN handling.
3981 __ Bind(&nan);
3982 if (type == DataType::Type::kFloat64) {
3983 // TODO: Use a constant from the constant table (requires extra input).
3984 __ LoadLongConstant(out, kDoubleNaN);
3985 } else {
3986 Register constant = locations->GetTemp(0).AsRegister<Register>();
3987 __ movl(constant, Immediate(kFloatNaN));
3988 __ movd(out, constant);
3989 }
3990 __ jmp(&done);
3991
3992 // out := op2;
3993 __ Bind(&op2_label);
3994 if (type == DataType::Type::kFloat64) {
3995 __ movsd(out, op2);
3996 } else {
3997 __ movss(out, op2);
3998 }
3999
4000 // Done.
4001 __ Bind(&done);
4002}
4003
Aart Bik351df3e2018-03-07 11:54:57 -08004004void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4005 DataType::Type type = minmax->GetResultType();
4006 switch (type) {
4007 case DataType::Type::kInt32:
4008 case DataType::Type::kInt64:
4009 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4010 break;
4011 case DataType::Type::kFloat32:
4012 case DataType::Type::kFloat64:
4013 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4014 break;
4015 default:
4016 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4017 }
4018}
4019
Aart Bik1f8d51b2018-02-15 10:42:37 -08004020void LocationsBuilderX86::VisitMin(HMin* min) {
4021 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4022}
4023
4024void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004025 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004026}
4027
4028void LocationsBuilderX86::VisitMax(HMax* max) {
4029 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4030}
4031
4032void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004033 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004034}
4035
Aart Bik3dad3412018-02-28 12:01:46 -08004036void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4037 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4038 switch (abs->GetResultType()) {
4039 case DataType::Type::kInt32:
4040 locations->SetInAt(0, Location::RegisterLocation(EAX));
4041 locations->SetOut(Location::SameAsFirstInput());
4042 locations->AddTemp(Location::RegisterLocation(EDX));
4043 break;
4044 case DataType::Type::kInt64:
4045 locations->SetInAt(0, Location::RequiresRegister());
4046 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4047 locations->AddTemp(Location::RequiresRegister());
4048 break;
4049 case DataType::Type::kFloat32:
4050 locations->SetInAt(0, Location::RequiresFpuRegister());
4051 locations->SetOut(Location::SameAsFirstInput());
4052 locations->AddTemp(Location::RequiresFpuRegister());
4053 locations->AddTemp(Location::RequiresRegister());
4054 break;
4055 case DataType::Type::kFloat64:
4056 locations->SetInAt(0, Location::RequiresFpuRegister());
4057 locations->SetOut(Location::SameAsFirstInput());
4058 locations->AddTemp(Location::RequiresFpuRegister());
4059 break;
4060 default:
4061 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4062 }
4063}
4064
4065void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4066 LocationSummary* locations = abs->GetLocations();
4067 switch (abs->GetResultType()) {
4068 case DataType::Type::kInt32: {
4069 Register out = locations->Out().AsRegister<Register>();
4070 DCHECK_EQ(out, EAX);
4071 Register temp = locations->GetTemp(0).AsRegister<Register>();
4072 DCHECK_EQ(temp, EDX);
4073 // Sign extend EAX into EDX.
4074 __ cdq();
4075 // XOR EAX with sign.
4076 __ xorl(EAX, EDX);
4077 // Subtract out sign to correct.
4078 __ subl(EAX, EDX);
4079 // The result is in EAX.
4080 break;
4081 }
4082 case DataType::Type::kInt64: {
4083 Location input = locations->InAt(0);
4084 Register input_lo = input.AsRegisterPairLow<Register>();
4085 Register input_hi = input.AsRegisterPairHigh<Register>();
4086 Location output = locations->Out();
4087 Register output_lo = output.AsRegisterPairLow<Register>();
4088 Register output_hi = output.AsRegisterPairHigh<Register>();
4089 Register temp = locations->GetTemp(0).AsRegister<Register>();
4090 // Compute the sign into the temporary.
4091 __ movl(temp, input_hi);
4092 __ sarl(temp, Immediate(31));
4093 // Store the sign into the output.
4094 __ movl(output_lo, temp);
4095 __ movl(output_hi, temp);
4096 // XOR the input to the output.
4097 __ xorl(output_lo, input_lo);
4098 __ xorl(output_hi, input_hi);
4099 // Subtract the sign.
4100 __ subl(output_lo, temp);
4101 __ sbbl(output_hi, temp);
4102 break;
4103 }
4104 case DataType::Type::kFloat32: {
4105 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4106 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4107 Register constant = locations->GetTemp(1).AsRegister<Register>();
4108 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4109 __ movd(temp, constant);
4110 __ andps(out, temp);
4111 break;
4112 }
4113 case DataType::Type::kFloat64: {
4114 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4115 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4116 // TODO: Use a constant from the constant table (requires extra input).
4117 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4118 __ andpd(out, temp);
4119 break;
4120 }
4121 default:
4122 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4123 }
4124}
4125
Calin Juravled0d48522014-11-04 16:40:20 +00004126void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004127 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004128 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004129 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004130 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004131 case DataType::Type::kInt8:
4132 case DataType::Type::kUint16:
4133 case DataType::Type::kInt16:
4134 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004135 locations->SetInAt(0, Location::Any());
4136 break;
4137 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004138 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004139 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4140 if (!instruction->IsConstant()) {
4141 locations->AddTemp(Location::RequiresRegister());
4142 }
4143 break;
4144 }
4145 default:
4146 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4147 }
Calin Juravled0d48522014-11-04 16:40:20 +00004148}
4149
4150void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004151 SlowPathCode* slow_path =
4152 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004153 codegen_->AddSlowPath(slow_path);
4154
4155 LocationSummary* locations = instruction->GetLocations();
4156 Location value = locations->InAt(0);
4157
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004158 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004159 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004160 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 case DataType::Type::kInt8:
4162 case DataType::Type::kUint16:
4163 case DataType::Type::kInt16:
4164 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004165 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004166 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004167 __ j(kEqual, slow_path->GetEntryLabel());
4168 } else if (value.IsStackSlot()) {
4169 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4170 __ j(kEqual, slow_path->GetEntryLabel());
4171 } else {
4172 DCHECK(value.IsConstant()) << value;
4173 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004174 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004175 }
4176 }
4177 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004179 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004180 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004182 __ movl(temp, value.AsRegisterPairLow<Register>());
4183 __ orl(temp, value.AsRegisterPairHigh<Register>());
4184 __ j(kEqual, slow_path->GetEntryLabel());
4185 } else {
4186 DCHECK(value.IsConstant()) << value;
4187 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4188 __ jmp(slow_path->GetEntryLabel());
4189 }
4190 }
4191 break;
4192 }
4193 default:
4194 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004195 }
Calin Juravled0d48522014-11-04 16:40:20 +00004196}
4197
Calin Juravle9aec02f2014-11-18 23:06:35 +00004198void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4199 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4200
4201 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004202 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004203
4204 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 case DataType::Type::kInt32:
4206 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004207 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004208 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004209 // The shift count needs to be in CL or a constant.
4210 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004211 locations->SetOut(Location::SameAsFirstInput());
4212 break;
4213 }
4214 default:
4215 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4216 }
4217}
4218
4219void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4220 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4221
4222 LocationSummary* locations = op->GetLocations();
4223 Location first = locations->InAt(0);
4224 Location second = locations->InAt(1);
4225 DCHECK(first.Equals(locations->Out()));
4226
4227 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004228 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004229 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004230 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004231 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004232 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004233 DCHECK_EQ(ECX, second_reg);
4234 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004235 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004236 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004237 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004238 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004239 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004240 }
4241 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004242 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004243 if (shift == 0) {
4244 return;
4245 }
4246 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004247 if (op->IsShl()) {
4248 __ shll(first_reg, imm);
4249 } else if (op->IsShr()) {
4250 __ sarl(first_reg, imm);
4251 } else {
4252 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004253 }
4254 }
4255 break;
4256 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004257 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004258 if (second.IsRegister()) {
4259 Register second_reg = second.AsRegister<Register>();
4260 DCHECK_EQ(ECX, second_reg);
4261 if (op->IsShl()) {
4262 GenerateShlLong(first, second_reg);
4263 } else if (op->IsShr()) {
4264 GenerateShrLong(first, second_reg);
4265 } else {
4266 GenerateUShrLong(first, second_reg);
4267 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004268 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004269 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004270 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004271 // Nothing to do if the shift is 0, as the input is already the output.
4272 if (shift != 0) {
4273 if (op->IsShl()) {
4274 GenerateShlLong(first, shift);
4275 } else if (op->IsShr()) {
4276 GenerateShrLong(first, shift);
4277 } else {
4278 GenerateUShrLong(first, shift);
4279 }
4280 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004281 }
4282 break;
4283 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004284 default:
4285 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4286 }
4287}
4288
Mark P Mendell73945692015-04-29 14:56:17 +00004289void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4290 Register low = loc.AsRegisterPairLow<Register>();
4291 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004292 if (shift == 1) {
4293 // This is just an addition.
4294 __ addl(low, low);
4295 __ adcl(high, high);
4296 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004297 // Shift by 32 is easy. High gets low, and low gets 0.
4298 codegen_->EmitParallelMoves(
4299 loc.ToLow(),
4300 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004301 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004302 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4303 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004304 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004305 } else if (shift > 32) {
4306 // Low part becomes 0. High part is low part << (shift-32).
4307 __ movl(high, low);
4308 __ shll(high, Immediate(shift - 32));
4309 __ xorl(low, low);
4310 } else {
4311 // Between 1 and 31.
4312 __ shld(high, low, Immediate(shift));
4313 __ shll(low, Immediate(shift));
4314 }
4315}
4316
Calin Juravle9aec02f2014-11-18 23:06:35 +00004317void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004318 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004319 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4320 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4321 __ testl(shifter, Immediate(32));
4322 __ j(kEqual, &done);
4323 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4324 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4325 __ Bind(&done);
4326}
4327
Mark P Mendell73945692015-04-29 14:56:17 +00004328void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4329 Register low = loc.AsRegisterPairLow<Register>();
4330 Register high = loc.AsRegisterPairHigh<Register>();
4331 if (shift == 32) {
4332 // Need to copy the sign.
4333 DCHECK_NE(low, high);
4334 __ movl(low, high);
4335 __ sarl(high, Immediate(31));
4336 } else if (shift > 32) {
4337 DCHECK_NE(low, high);
4338 // High part becomes sign. Low part is shifted by shift - 32.
4339 __ movl(low, high);
4340 __ sarl(high, Immediate(31));
4341 __ sarl(low, Immediate(shift - 32));
4342 } else {
4343 // Between 1 and 31.
4344 __ shrd(low, high, Immediate(shift));
4345 __ sarl(high, Immediate(shift));
4346 }
4347}
4348
Calin Juravle9aec02f2014-11-18 23:06:35 +00004349void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004350 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004351 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4352 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4353 __ testl(shifter, Immediate(32));
4354 __ j(kEqual, &done);
4355 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4356 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4357 __ Bind(&done);
4358}
4359
Mark P Mendell73945692015-04-29 14:56:17 +00004360void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4361 Register low = loc.AsRegisterPairLow<Register>();
4362 Register high = loc.AsRegisterPairHigh<Register>();
4363 if (shift == 32) {
4364 // Shift by 32 is easy. Low gets high, and high gets 0.
4365 codegen_->EmitParallelMoves(
4366 loc.ToHigh(),
4367 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004368 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004369 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4370 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004371 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004372 } else if (shift > 32) {
4373 // Low part is high >> (shift - 32). High part becomes 0.
4374 __ movl(low, high);
4375 __ shrl(low, Immediate(shift - 32));
4376 __ xorl(high, high);
4377 } else {
4378 // Between 1 and 31.
4379 __ shrd(low, high, Immediate(shift));
4380 __ shrl(high, Immediate(shift));
4381 }
4382}
4383
Calin Juravle9aec02f2014-11-18 23:06:35 +00004384void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004385 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004386 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4387 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4388 __ testl(shifter, Immediate(32));
4389 __ j(kEqual, &done);
4390 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4391 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4392 __ Bind(&done);
4393}
4394
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004395void LocationsBuilderX86::VisitRor(HRor* ror) {
4396 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004397 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004398
4399 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004400 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004401 // Add the temporary needed.
4402 locations->AddTemp(Location::RequiresRegister());
4403 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004404 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004405 locations->SetInAt(0, Location::RequiresRegister());
4406 // The shift count needs to be in CL (unless it is a constant).
4407 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4408 locations->SetOut(Location::SameAsFirstInput());
4409 break;
4410 default:
4411 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4412 UNREACHABLE();
4413 }
4414}
4415
4416void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4417 LocationSummary* locations = ror->GetLocations();
4418 Location first = locations->InAt(0);
4419 Location second = locations->InAt(1);
4420
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004421 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004422 Register first_reg = first.AsRegister<Register>();
4423 if (second.IsRegister()) {
4424 Register second_reg = second.AsRegister<Register>();
4425 __ rorl(first_reg, second_reg);
4426 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004427 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004428 __ rorl(first_reg, imm);
4429 }
4430 return;
4431 }
4432
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004433 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004434 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4435 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4436 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4437 if (second.IsRegister()) {
4438 Register second_reg = second.AsRegister<Register>();
4439 DCHECK_EQ(second_reg, ECX);
4440 __ movl(temp_reg, first_reg_hi);
4441 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4442 __ shrd(first_reg_lo, temp_reg, second_reg);
4443 __ movl(temp_reg, first_reg_hi);
4444 __ testl(second_reg, Immediate(32));
4445 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4446 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4447 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004448 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004449 if (shift_amt == 0) {
4450 // Already fine.
4451 return;
4452 }
4453 if (shift_amt == 32) {
4454 // Just swap.
4455 __ movl(temp_reg, first_reg_lo);
4456 __ movl(first_reg_lo, first_reg_hi);
4457 __ movl(first_reg_hi, temp_reg);
4458 return;
4459 }
4460
4461 Immediate imm(shift_amt);
4462 // Save the constents of the low value.
4463 __ movl(temp_reg, first_reg_lo);
4464
4465 // Shift right into low, feeding bits from high.
4466 __ shrd(first_reg_lo, first_reg_hi, imm);
4467
4468 // Shift right into high, feeding bits from the original low.
4469 __ shrd(first_reg_hi, temp_reg, imm);
4470
4471 // Swap if needed.
4472 if (shift_amt > 32) {
4473 __ movl(temp_reg, first_reg_lo);
4474 __ movl(first_reg_lo, first_reg_hi);
4475 __ movl(first_reg_hi, temp_reg);
4476 }
4477 }
4478}
4479
Calin Juravle9aec02f2014-11-18 23:06:35 +00004480void LocationsBuilderX86::VisitShl(HShl* shl) {
4481 HandleShift(shl);
4482}
4483
4484void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4485 HandleShift(shl);
4486}
4487
4488void LocationsBuilderX86::VisitShr(HShr* shr) {
4489 HandleShift(shr);
4490}
4491
4492void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4493 HandleShift(shr);
4494}
4495
4496void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4497 HandleShift(ushr);
4498}
4499
4500void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4501 HandleShift(ushr);
4502}
4503
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004504void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004505 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4506 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004507 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004508 InvokeRuntimeCallingConvention calling_convention;
4509 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004510}
4511
4512void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004513 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4514 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4515 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004516}
4517
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004518void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004519 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4520 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004521 locations->SetOut(Location::RegisterLocation(EAX));
4522 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004523 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4524 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525}
4526
4527void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004528 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4529 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004530 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004531 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004532 DCHECK(!codegen_->IsLeafMethod());
4533}
4534
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004535void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004536 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004537 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004538 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4539 if (location.IsStackSlot()) {
4540 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4541 } else if (location.IsDoubleStackSlot()) {
4542 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004543 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004544 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004545}
4546
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004547void InstructionCodeGeneratorX86::VisitParameterValue(
4548 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4549}
4550
4551void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4552 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004553 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004554 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4555}
4556
4557void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004558}
4559
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004560void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4561 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004562 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004563 locations->SetInAt(0, Location::RequiresRegister());
4564 locations->SetOut(Location::RequiresRegister());
4565}
4566
4567void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4568 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004569 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004570 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004571 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004572 __ movl(locations->Out().AsRegister<Register>(),
4573 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004574 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004575 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004576 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004577 __ movl(locations->Out().AsRegister<Register>(),
4578 Address(locations->InAt(0).AsRegister<Register>(),
4579 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4580 // temp = temp->GetImtEntryAt(method_offset);
4581 __ movl(locations->Out().AsRegister<Register>(),
4582 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004583 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004584}
4585
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004586void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004587 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004588 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004589 locations->SetInAt(0, Location::RequiresRegister());
4590 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004591}
4592
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004593void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4594 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004595 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004596 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004597 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004598 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004599 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004600 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004601 break;
4602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004603 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004604 __ notl(out.AsRegisterPairLow<Register>());
4605 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004606 break;
4607
4608 default:
4609 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4610 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004611}
4612
David Brazdil66d126e2015-04-03 16:02:44 +01004613void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4614 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004615 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004616 locations->SetInAt(0, Location::RequiresRegister());
4617 locations->SetOut(Location::SameAsFirstInput());
4618}
4619
4620void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004621 LocationSummary* locations = bool_not->GetLocations();
4622 Location in = locations->InAt(0);
4623 Location out = locations->Out();
4624 DCHECK(in.Equals(out));
4625 __ xorl(out.AsRegister<Register>(), Immediate(1));
4626}
4627
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004628void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004629 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004630 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004631 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004632 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004633 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004634 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004635 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004636 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kInt32:
4638 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004639 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004640 locations->SetInAt(1, Location::Any());
4641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4642 break;
4643 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004644 case DataType::Type::kFloat32:
4645 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004646 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004647 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4648 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4649 } else if (compare->InputAt(1)->IsConstant()) {
4650 locations->SetInAt(1, Location::RequiresFpuRegister());
4651 } else {
4652 locations->SetInAt(1, Location::Any());
4653 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004654 locations->SetOut(Location::RequiresRegister());
4655 break;
4656 }
4657 default:
4658 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4659 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004660}
4661
4662void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004663 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004664 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004665 Location left = locations->InAt(0);
4666 Location right = locations->InAt(1);
4667
Mark Mendell0c9497d2015-08-21 09:30:05 -04004668 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004669 Condition less_cond = kLess;
4670
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004671 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004675 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004676 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004678 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004679 break;
4680 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004681 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004682 Register left_low = left.AsRegisterPairLow<Register>();
4683 Register left_high = left.AsRegisterPairHigh<Register>();
4684 int32_t val_low = 0;
4685 int32_t val_high = 0;
4686 bool right_is_const = false;
4687
4688 if (right.IsConstant()) {
4689 DCHECK(right.GetConstant()->IsLongConstant());
4690 right_is_const = true;
4691 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4692 val_low = Low32Bits(val);
4693 val_high = High32Bits(val);
4694 }
4695
Calin Juravleddb7df22014-11-25 20:56:51 +00004696 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004697 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004698 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004699 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004700 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004701 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004702 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004703 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004704 __ j(kLess, &less); // Signed compare.
4705 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004706 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004707 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004708 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004709 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004710 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004711 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004712 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004713 }
Aart Bika19616e2016-02-01 18:57:58 -08004714 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004715 break;
4716 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004718 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004719 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004720 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004721 break;
4722 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004723 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004724 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004725 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004726 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004727 break;
4728 }
4729 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004730 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004731 }
Aart Bika19616e2016-02-01 18:57:58 -08004732
Calin Juravleddb7df22014-11-25 20:56:51 +00004733 __ movl(out, Immediate(0));
4734 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004735 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004736
4737 __ Bind(&greater);
4738 __ movl(out, Immediate(1));
4739 __ jmp(&done);
4740
4741 __ Bind(&less);
4742 __ movl(out, Immediate(-1));
4743
4744 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004745}
4746
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004747void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004748 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004749 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004750 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004751 locations->SetInAt(i, Location::Any());
4752 }
4753 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004754}
4755
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004756void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004757 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004758}
4759
Roland Levillain7c1559a2015-12-15 10:55:36 +00004760void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004761 /*
4762 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4763 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4764 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4765 */
4766 switch (kind) {
4767 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004768 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004769 break;
4770 }
4771 case MemBarrierKind::kAnyStore:
4772 case MemBarrierKind::kLoadAny:
4773 case MemBarrierKind::kStoreStore: {
4774 // nop
4775 break;
4776 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004777 case MemBarrierKind::kNTStoreStore:
4778 // Non-Temporal Store/Store needs an explicit fence.
4779 MemoryFence(/* non-temporal */ true);
4780 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004781 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782}
4783
Vladimir Markodc151b22015-10-15 18:02:30 +01004784HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4785 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004786 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004787 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004788}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004790Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4791 Register temp) {
4792 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004793 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004794 if (!invoke->GetLocations()->Intrinsified()) {
4795 return location.AsRegister<Register>();
4796 }
4797 // For intrinsics we allow any location, so it may be on the stack.
4798 if (!location.IsRegister()) {
4799 __ movl(temp, Address(ESP, location.GetStackIndex()));
4800 return temp;
4801 }
4802 // For register locations, check if the register was saved. If so, get it from the stack.
4803 // Note: There is a chance that the register was saved but not overwritten, so we could
4804 // save one load. However, since this is just an intrinsic slow path we prefer this
4805 // simple and more robust approach rather that trying to determine if that's the case.
4806 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004807 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4808 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4809 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4810 __ movl(temp, Address(ESP, stack_offset));
4811 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004812 }
4813 return location.AsRegister<Register>();
4814}
4815
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004816void CodeGeneratorX86::GenerateStaticOrDirectCall(
4817 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004818 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4819 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004820 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004821 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004822 uint32_t offset =
4823 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4824 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004825 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004826 }
Vladimir Marko58155012015-08-19 12:49:41 +00004827 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004828 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004829 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004830 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4831 DCHECK(GetCompilerOptions().IsBootImage());
4832 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4833 temp.AsRegister<Register>());
4834 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004835 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004836 break;
4837 }
Vladimir Markob066d432018-01-03 13:14:37 +00004838 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4839 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4840 temp.AsRegister<Register>());
4841 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4842 RecordBootImageRelRoPatch(
4843 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004844 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004845 break;
4846 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004847 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004848 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4849 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004850 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004851 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004852 break;
4853 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004854 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4855 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4856 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004857 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4858 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4859 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004860 }
Vladimir Marko58155012015-08-19 12:49:41 +00004861 }
4862
4863 switch (invoke->GetCodePtrLocation()) {
4864 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4865 __ call(GetFrameEntryLabel());
4866 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004867 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4868 // (callee_method + offset_of_quick_compiled_code)()
4869 __ call(Address(callee_method.AsRegister<Register>(),
4870 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004871 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004872 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004873 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004874 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004875
4876 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004877}
4878
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004879void CodeGeneratorX86::GenerateVirtualCall(
4880 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004881 Register temp = temp_in.AsRegister<Register>();
4882 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4883 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004884
4885 // Use the calling convention instead of the location of the receiver, as
4886 // intrinsics may have put the receiver in a different register. In the intrinsics
4887 // slow path, the arguments have been moved to the right place, so here we are
4888 // guaranteed that the receiver is the first register of the calling convention.
4889 InvokeDexCallingConvention calling_convention;
4890 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004891 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004892 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004893 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004894 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004895 // Instead of simply (possibly) unpoisoning `temp` here, we should
4896 // emit a read barrier for the previous class reference load.
4897 // However this is not required in practice, as this is an
4898 // intermediate/temporary reference and because the current
4899 // concurrent copying collector keeps the from-space memory
4900 // intact/accessible until the end of the marking phase (the
4901 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004902 __ MaybeUnpoisonHeapReference(temp);
4903 // temp = temp->GetMethodAt(method_offset);
4904 __ movl(temp, Address(temp, method_offset));
4905 // call temp->GetEntryPoint();
4906 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004907 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004908 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004909}
4910
Vladimir Marko6fd16062018-06-26 11:02:04 +01004911void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
4912 uint32_t intrinsic_data) {
4913 boot_image_intrinsic_patches_.emplace_back(
4914 method_address, /* target_dex_file */ nullptr, intrinsic_data);
4915 __ Bind(&boot_image_intrinsic_patches_.back().label);
4916}
4917
Vladimir Markob066d432018-01-03 13:14:37 +00004918void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4919 uint32_t boot_image_offset) {
4920 boot_image_method_patches_.emplace_back(
4921 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4922 __ Bind(&boot_image_method_patches_.back().label);
4923}
4924
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004925void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004926 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004927 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004928 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004929 boot_image_method_patches_.emplace_back(
4930 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004931 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004932}
4933
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004934void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4935 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4936 HX86ComputeBaseMethodAddress* method_address =
4937 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004938 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004939 method_bss_entry_patches_.emplace_back(
4940 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4941 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004942}
4943
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004944void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4945 HX86ComputeBaseMethodAddress* method_address =
4946 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4947 boot_image_type_patches_.emplace_back(
4948 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004949 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950}
4951
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004952Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004953 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004954 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4955 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004956 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004957 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004958}
4959
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004960void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4961 HX86ComputeBaseMethodAddress* method_address =
4962 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4963 boot_image_string_patches_.emplace_back(
4964 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4965 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004966}
4967
Vladimir Markoaad75c62016-10-03 08:46:48 +00004968Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4969 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004970 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004971 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004972 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004973 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004974 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004975}
4976
Vladimir Markoeebb8212018-06-05 14:57:24 +01004977void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004978 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01004979 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004980 if (GetCompilerOptions().IsBootImage()) {
4981 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4982 HX86ComputeBaseMethodAddress* method_address =
4983 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4984 DCHECK(method_address != nullptr);
4985 Register method_address_reg =
4986 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4987 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
4988 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004989 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004990 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4991 HX86ComputeBaseMethodAddress* method_address =
4992 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4993 DCHECK(method_address != nullptr);
4994 Register method_address_reg =
4995 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4996 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01004997 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004998 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004999 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005000 gc::Heap* heap = Runtime::Current()->GetHeap();
5001 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005002 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005003 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5004 }
5005}
5006
Vladimir Marko6fd16062018-06-26 11:02:04 +01005007void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5008 uint32_t boot_image_offset) {
5009 DCHECK(invoke->IsStatic());
5010 InvokeRuntimeCallingConvention calling_convention;
5011 Register argument = calling_convention.GetRegisterAt(0);
5012 if (GetCompilerOptions().IsBootImage()) {
5013 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5014 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5015 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5016 HX86ComputeBaseMethodAddress* method_address =
5017 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5018 DCHECK(method_address != nullptr);
5019 Register method_address_reg =
5020 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5021 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5022 MethodReference target_method = invoke->GetTargetMethod();
5023 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5024 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5025 __ Bind(&boot_image_type_patches_.back().label);
5026 } else {
5027 LoadBootImageAddress(argument, boot_image_offset, invoke);
5028 }
5029 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5030 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5031}
5032
Vladimir Markoaad75c62016-10-03 08:46:48 +00005033// The label points to the end of the "movl" or another instruction but the literal offset
5034// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5035constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5036
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005037template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005038inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005039 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005040 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005041 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005042 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005043 linker_patches->push_back(Factory(literal_offset,
5044 info.target_dex_file,
5045 GetMethodAddressOffset(info.method_address),
5046 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005047 }
5048}
5049
Vladimir Marko6fd16062018-06-26 11:02:04 +01005050template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5051linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5052 const DexFile* target_dex_file,
5053 uint32_t pc_insn_offset,
5054 uint32_t boot_image_offset) {
5055 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5056 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005057}
5058
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005059void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005060 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005061 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005062 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005063 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005064 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005065 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005066 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005067 string_bss_entry_patches_.size() +
5068 boot_image_intrinsic_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005069 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005070 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005071 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5072 boot_image_method_patches_, linker_patches);
5073 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5074 boot_image_type_patches_, linker_patches);
5075 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005076 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005077 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5078 boot_image_intrinsic_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005079 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005080 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00005081 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005082 DCHECK(boot_image_type_patches_.empty());
5083 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005084 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005085 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005086 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5087 method_bss_entry_patches_, linker_patches);
5088 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5089 type_bss_entry_patches_, linker_patches);
5090 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5091 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005092 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005093}
5094
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005095void CodeGeneratorX86::MarkGCCard(Register temp,
5096 Register card,
5097 Register object,
5098 Register value,
5099 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005100 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005101 if (value_can_be_null) {
5102 __ testl(value, value);
5103 __ j(kEqual, &is_null);
5104 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005105 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005106 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005107 // Calculate the offset (in the card table) of the card corresponding to
5108 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 __ movl(temp, object);
5110 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005111 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5112 // `object`'s card.
5113 //
5114 // Register `card` contains the address of the card table. Note that the card
5115 // table's base is biased during its creation so that it always starts at an
5116 // address whose least-significant byte is equal to `kCardDirty` (see
5117 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5118 // below writes the `kCardDirty` (byte) value into the `object`'s card
5119 // (located at `card + object >> kCardShift`).
5120 //
5121 // This dual use of the value in register `card` (1. to calculate the location
5122 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5123 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005124 __ movb(Address(temp, card, TIMES_1, 0),
5125 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005126 if (value_can_be_null) {
5127 __ Bind(&is_null);
5128 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129}
5130
Calin Juravle52c48962014-12-16 17:02:57 +00005131void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5132 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005133
5134 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005136 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005137 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5138 kEmitCompilerReadBarrier
5139 ? LocationSummary::kCallOnSlowPath
5140 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005141 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005142 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005143 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005144 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005146 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005147 locations->SetOut(Location::RequiresFpuRegister());
5148 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005149 // The output overlaps in case of long: we don't want the low move
5150 // to overwrite the object's location. Likewise, in the case of
5151 // an object field get with read barriers enabled, we do not want
5152 // the move to overwrite the object's location, as we need it to emit
5153 // the read barrier.
5154 locations->SetOut(
5155 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005156 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005157 Location::kOutputOverlap :
5158 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005159 }
Calin Juravle52c48962014-12-16 17:02:57 +00005160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005161 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005162 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005163 // So we use an XMM register as a temp to achieve atomicity (first
5164 // load the temp into the XMM and then copy the XMM into the
5165 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005166 locations->AddTemp(Location::RequiresFpuRegister());
5167 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005168}
5169
Calin Juravle52c48962014-12-16 17:02:57 +00005170void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5171 const FieldInfo& field_info) {
5172 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005173
Calin Juravle52c48962014-12-16 17:02:57 +00005174 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005175 Location base_loc = locations->InAt(0);
5176 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005177 Location out = locations->Out();
5178 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005179 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5180 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005181 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5182
Vladimir Marko61b92282017-10-11 13:23:17 +01005183 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005184 case DataType::Type::kBool:
5185 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005186 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187 break;
5188 }
5189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005190 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005191 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005192 break;
5193 }
5194
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005195 case DataType::Type::kUint16: {
5196 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005197 break;
5198 }
5199
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005200 case DataType::Type::kInt16: {
5201 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 break;
5203 }
5204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005206 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005210 // /* HeapReference<Object> */ out = *(base + offset)
5211 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 // Note that a potential implicit null check is handled in this
5213 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5214 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005215 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 if (is_volatile) {
5217 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5218 }
5219 } else {
5220 __ movl(out.AsRegister<Register>(), Address(base, offset));
5221 codegen_->MaybeRecordImplicitNullCheck(instruction);
5222 if (is_volatile) {
5223 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5224 }
5225 // If read barriers are enabled, emit read barriers other than
5226 // Baker's using a slow path (and also unpoison the loaded
5227 // reference, if heap poisoning is enabled).
5228 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5229 }
5230 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005231 }
5232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005233 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005234 if (is_volatile) {
5235 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5236 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005238 __ movd(out.AsRegisterPairLow<Register>(), temp);
5239 __ psrlq(temp, Immediate(32));
5240 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5241 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005242 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005243 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005244 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005245 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5246 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005247 break;
5248 }
5249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005250 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005251 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005252 break;
5253 }
5254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005255 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005256 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005257 break;
5258 }
5259
Aart Bik66c158e2018-01-31 12:55:04 -08005260 case DataType::Type::kUint32:
5261 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005263 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005264 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005265 }
Calin Juravle52c48962014-12-16 17:02:57 +00005266
Vladimir Marko61b92282017-10-11 13:23:17 +01005267 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 // Potential implicit null checks, in the case of reference or
5269 // long fields, are handled in the previous switch statement.
5270 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005271 codegen_->MaybeRecordImplicitNullCheck(instruction);
5272 }
5273
Calin Juravle52c48962014-12-16 17:02:57 +00005274 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005275 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005276 // Memory barriers, in the case of references, are also handled
5277 // in the previous switch statement.
5278 } else {
5279 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5280 }
Roland Levillain4d027112015-07-01 15:41:14 +01005281 }
Calin Juravle52c48962014-12-16 17:02:57 +00005282}
5283
5284void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5285 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5286
5287 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005288 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005289 locations->SetInAt(0, Location::RequiresRegister());
5290 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005292 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005293
5294 // The register allocator does not support multiple
5295 // inputs that die at entry with one in a specific register.
5296 if (is_byte_type) {
5297 // Ensure the value is in a byte register.
5298 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005299 } else if (DataType::IsFloatingPointType(field_type)) {
5300 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005301 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5302 locations->SetInAt(1, Location::RequiresFpuRegister());
5303 } else {
5304 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5305 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005307 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005308 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005309
Calin Juravle52c48962014-12-16 17:02:57 +00005310 // 64bits value can be atomically written to an address with movsd and an XMM register.
5311 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5312 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5313 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5314 // isolated cases when we need this it isn't worth adding the extra complexity.
5315 locations->AddTemp(Location::RequiresFpuRegister());
5316 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005317 } else {
5318 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5319
5320 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5321 // Temporary registers for the write barrier.
5322 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5323 // Ensure the card is in a byte register.
5324 locations->AddTemp(Location::RegisterLocation(ECX));
5325 }
Calin Juravle52c48962014-12-16 17:02:57 +00005326 }
5327}
5328
5329void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005330 const FieldInfo& field_info,
5331 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005332 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5333
5334 LocationSummary* locations = instruction->GetLocations();
5335 Register base = locations->InAt(0).AsRegister<Register>();
5336 Location value = locations->InAt(1);
5337 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005338 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005339 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005340 bool needs_write_barrier =
5341 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005342
5343 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005344 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005345 }
5346
Mark Mendell81489372015-11-04 11:30:41 -05005347 bool maybe_record_implicit_null_check_done = false;
5348
Calin Juravle52c48962014-12-16 17:02:57 +00005349 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005351 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005352 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005353 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5354 break;
5355 }
5356
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005357 case DataType::Type::kUint16:
5358 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005359 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005360 __ movw(Address(base, offset),
5361 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005362 } else {
5363 __ movw(Address(base, offset), value.AsRegister<Register>());
5364 }
Calin Juravle52c48962014-12-16 17:02:57 +00005365 break;
5366 }
5367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005368 case DataType::Type::kInt32:
5369 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005370 if (kPoisonHeapReferences && needs_write_barrier) {
5371 // Note that in the case where `value` is a null reference,
5372 // we do not enter this block, as the reference does not
5373 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005374 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005375 Register temp = locations->GetTemp(0).AsRegister<Register>();
5376 __ movl(temp, value.AsRegister<Register>());
5377 __ PoisonHeapReference(temp);
5378 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005379 } else if (value.IsConstant()) {
5380 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5381 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005382 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005383 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005384 __ movl(Address(base, offset), value.AsRegister<Register>());
5385 }
Calin Juravle52c48962014-12-16 17:02:57 +00005386 break;
5387 }
5388
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005389 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005390 if (is_volatile) {
5391 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5392 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5393 __ movd(temp1, value.AsRegisterPairLow<Register>());
5394 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5395 __ punpckldq(temp1, temp2);
5396 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005397 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005398 } else if (value.IsConstant()) {
5399 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5400 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5401 codegen_->MaybeRecordImplicitNullCheck(instruction);
5402 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005403 } else {
5404 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005406 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5407 }
Mark Mendell81489372015-11-04 11:30:41 -05005408 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005409 break;
5410 }
5411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005412 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005413 if (value.IsConstant()) {
5414 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5415 __ movl(Address(base, offset), Immediate(v));
5416 } else {
5417 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5418 }
Calin Juravle52c48962014-12-16 17:02:57 +00005419 break;
5420 }
5421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005422 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005423 if (value.IsConstant()) {
5424 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5425 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5426 codegen_->MaybeRecordImplicitNullCheck(instruction);
5427 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5428 maybe_record_implicit_null_check_done = true;
5429 } else {
5430 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5431 }
Calin Juravle52c48962014-12-16 17:02:57 +00005432 break;
5433 }
5434
Aart Bik66c158e2018-01-31 12:55:04 -08005435 case DataType::Type::kUint32:
5436 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005438 LOG(FATAL) << "Unreachable type " << field_type;
5439 UNREACHABLE();
5440 }
5441
Mark Mendell81489372015-11-04 11:30:41 -05005442 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005443 codegen_->MaybeRecordImplicitNullCheck(instruction);
5444 }
5445
Roland Levillain4d027112015-07-01 15:41:14 +01005446 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005447 Register temp = locations->GetTemp(0).AsRegister<Register>();
5448 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005449 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005450 }
5451
Calin Juravle52c48962014-12-16 17:02:57 +00005452 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005453 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005454 }
5455}
5456
5457void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5458 HandleFieldGet(instruction, instruction->GetFieldInfo());
5459}
5460
5461void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5462 HandleFieldGet(instruction, instruction->GetFieldInfo());
5463}
5464
5465void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5466 HandleFieldSet(instruction, instruction->GetFieldInfo());
5467}
5468
5469void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005470 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005471}
5472
5473void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5474 HandleFieldSet(instruction, instruction->GetFieldInfo());
5475}
5476
5477void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005478 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005479}
5480
5481void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5482 HandleFieldGet(instruction, instruction->GetFieldInfo());
5483}
5484
5485void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5486 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005487}
5488
Calin Juravlee460d1d2015-09-29 04:52:17 +01005489void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5490 HUnresolvedInstanceFieldGet* instruction) {
5491 FieldAccessCallingConventionX86 calling_convention;
5492 codegen_->CreateUnresolvedFieldLocationSummary(
5493 instruction, instruction->GetFieldType(), calling_convention);
5494}
5495
5496void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5497 HUnresolvedInstanceFieldGet* instruction) {
5498 FieldAccessCallingConventionX86 calling_convention;
5499 codegen_->GenerateUnresolvedFieldAccess(instruction,
5500 instruction->GetFieldType(),
5501 instruction->GetFieldIndex(),
5502 instruction->GetDexPc(),
5503 calling_convention);
5504}
5505
5506void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5507 HUnresolvedInstanceFieldSet* instruction) {
5508 FieldAccessCallingConventionX86 calling_convention;
5509 codegen_->CreateUnresolvedFieldLocationSummary(
5510 instruction, instruction->GetFieldType(), calling_convention);
5511}
5512
5513void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5514 HUnresolvedInstanceFieldSet* instruction) {
5515 FieldAccessCallingConventionX86 calling_convention;
5516 codegen_->GenerateUnresolvedFieldAccess(instruction,
5517 instruction->GetFieldType(),
5518 instruction->GetFieldIndex(),
5519 instruction->GetDexPc(),
5520 calling_convention);
5521}
5522
5523void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5524 HUnresolvedStaticFieldGet* instruction) {
5525 FieldAccessCallingConventionX86 calling_convention;
5526 codegen_->CreateUnresolvedFieldLocationSummary(
5527 instruction, instruction->GetFieldType(), calling_convention);
5528}
5529
5530void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5531 HUnresolvedStaticFieldGet* instruction) {
5532 FieldAccessCallingConventionX86 calling_convention;
5533 codegen_->GenerateUnresolvedFieldAccess(instruction,
5534 instruction->GetFieldType(),
5535 instruction->GetFieldIndex(),
5536 instruction->GetDexPc(),
5537 calling_convention);
5538}
5539
5540void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5541 HUnresolvedStaticFieldSet* instruction) {
5542 FieldAccessCallingConventionX86 calling_convention;
5543 codegen_->CreateUnresolvedFieldLocationSummary(
5544 instruction, instruction->GetFieldType(), calling_convention);
5545}
5546
5547void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5548 HUnresolvedStaticFieldSet* instruction) {
5549 FieldAccessCallingConventionX86 calling_convention;
5550 codegen_->GenerateUnresolvedFieldAccess(instruction,
5551 instruction->GetFieldType(),
5552 instruction->GetFieldIndex(),
5553 instruction->GetDexPc(),
5554 calling_convention);
5555}
5556
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005557void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005558 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5559 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5560 ? Location::RequiresRegister()
5561 : Location::Any();
5562 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005563}
5564
Calin Juravle2ae48182016-03-16 14:05:09 +00005565void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5566 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005567 return;
5568 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005569 LocationSummary* locations = instruction->GetLocations();
5570 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005571
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005572 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005573 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005574}
5575
Calin Juravle2ae48182016-03-16 14:05:09 +00005576void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005577 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005578 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005579
5580 LocationSummary* locations = instruction->GetLocations();
5581 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005582
5583 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005584 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005585 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005586 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005587 } else {
5588 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005589 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005590 __ jmp(slow_path->GetEntryLabel());
5591 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005592 }
5593 __ j(kEqual, slow_path->GetEntryLabel());
5594}
5595
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005596void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005597 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005598}
5599
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005600void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005601 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005603 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005604 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5605 object_array_get_with_read_barrier
5606 ? LocationSummary::kCallOnSlowPath
5607 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005608 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005609 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005610 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005611 locations->SetInAt(0, Location::RequiresRegister());
5612 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005613 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005614 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5615 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005616 // The output overlaps in case of long: we don't want the low move
5617 // to overwrite the array's location. Likewise, in the case of an
5618 // object array get with read barriers enabled, we do not want the
5619 // move to overwrite the array's location, as we need it to emit
5620 // the read barrier.
5621 locations->SetOut(
5622 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005623 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5624 ? Location::kOutputOverlap
5625 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005626 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005627}
5628
5629void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005631 Location obj_loc = locations->InAt(0);
5632 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005633 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005634 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005635 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005638 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005639 case DataType::Type::kBool:
5640 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005641 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005642 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643 break;
5644 }
5645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005647 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005648 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005649 break;
5650 }
5651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005653 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005654 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5655 // Branch cases into compressed and uncompressed for each index's type.
5656 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5657 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005658 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005659 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005660 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5661 "Expecting 0=compressed, 1=uncompressed");
5662 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005663 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5664 __ jmp(&done);
5665 __ Bind(&not_compressed);
5666 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5667 __ Bind(&done);
5668 } else {
5669 // Common case for charAt of array of char or when string compression's
5670 // feature is turned off.
5671 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5672 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005673 break;
5674 }
5675
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005676 case DataType::Type::kInt16: {
5677 Register out = out_loc.AsRegister<Register>();
5678 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5679 break;
5680 }
5681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005682 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005683 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005684 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005685 break;
5686 }
5687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005688 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005689 static_assert(
5690 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5691 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005692 // /* HeapReference<Object> */ out =
5693 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5694 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005695 // Note that a potential implicit null check is handled in this
5696 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5697 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005698 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005699 } else {
5700 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005701 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5702 codegen_->MaybeRecordImplicitNullCheck(instruction);
5703 // If read barriers are enabled, emit read barriers other than
5704 // Baker's using a slow path (and also unpoison the loaded
5705 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005706 if (index.IsConstant()) {
5707 uint32_t offset =
5708 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005709 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5710 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005711 codegen_->MaybeGenerateReadBarrierSlow(
5712 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5713 }
5714 }
5715 break;
5716 }
5717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005718 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005719 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005720 __ movl(out_loc.AsRegisterPairLow<Register>(),
5721 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5722 codegen_->MaybeRecordImplicitNullCheck(instruction);
5723 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5724 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005725 break;
5726 }
5727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005729 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005730 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005731 break;
5732 }
5733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005735 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005736 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005737 break;
5738 }
5739
Aart Bik66c158e2018-01-31 12:55:04 -08005740 case DataType::Type::kUint32:
5741 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005743 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005744 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005745 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005748 // Potential implicit null checks, in the case of reference or
5749 // long arrays, are handled in the previous switch statement.
5750 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005751 codegen_->MaybeRecordImplicitNullCheck(instruction);
5752 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005753}
5754
5755void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005757
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005758 bool needs_write_barrier =
5759 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005760 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005761
Vladimir Markoca6fff82017-10-03 14:49:14 +01005762 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005763 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005764 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005765 LocationSummary::kCallOnSlowPath :
5766 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005767
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005768 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005769 // We need the inputs to be different than the output in case of long operation.
5770 // In case of a byte operation, the register allocator does not support multiple
5771 // inputs that die at entry with one in a specific register.
5772 locations->SetInAt(0, Location::RequiresRegister());
5773 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5774 if (is_byte_type) {
5775 // Ensure the value is in a byte register.
5776 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005778 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005779 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005780 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5781 }
5782 if (needs_write_barrier) {
5783 // Temporary registers for the write barrier.
5784 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5785 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005786 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005787 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005788}
5789
5790void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5791 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 Location array_loc = locations->InAt(0);
5793 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005794 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005795 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005797 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5798 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5799 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005800 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005801 bool needs_write_barrier =
5802 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005803
5804 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005805 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005806 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005808 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005809 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005810 if (value.IsRegister()) {
5811 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005812 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005813 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005814 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005816 break;
5817 }
5818
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005819 case DataType::Type::kUint16:
5820 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005821 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005822 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005823 if (value.IsRegister()) {
5824 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005825 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005826 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005827 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005828 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005829 break;
5830 }
5831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005833 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005834 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005836 if (!value.IsRegister()) {
5837 // Just setting null.
5838 DCHECK(instruction->InputAt(2)->IsNullConstant());
5839 DCHECK(value.IsConstant()) << value;
5840 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005841 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005842 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005844 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005845 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005846
5847 DCHECK(needs_write_barrier);
5848 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005849 // We cannot use a NearLabel for `done`, as its range may be too
5850 // short when Baker read barriers are enabled.
5851 Label done;
5852 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005853 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005854 Location temp_loc = locations->GetTemp(0);
5855 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005857 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005858 codegen_->AddSlowPath(slow_path);
5859 if (instruction->GetValueCanBeNull()) {
5860 __ testl(register_value, register_value);
5861 __ j(kNotEqual, &not_null);
5862 __ movl(address, Immediate(0));
5863 codegen_->MaybeRecordImplicitNullCheck(instruction);
5864 __ jmp(&done);
5865 __ Bind(&not_null);
5866 }
5867
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005868 // Note that when Baker read barriers are enabled, the type
5869 // checks are performed without read barriers. This is fine,
5870 // even in the case where a class object is in the from-space
5871 // after the flip, as a comparison involving such a type would
5872 // not produce a false positive; it may of course produce a
5873 // false negative, in which case we would take the ArraySet
5874 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005875
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005876 // /* HeapReference<Class> */ temp = array->klass_
5877 __ movl(temp, Address(array, class_offset));
5878 codegen_->MaybeRecordImplicitNullCheck(instruction);
5879 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005880
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005881 // /* HeapReference<Class> */ temp = temp->component_type_
5882 __ movl(temp, Address(temp, component_offset));
5883 // If heap poisoning is enabled, no need to unpoison `temp`
5884 // nor the object reference in `register_value->klass`, as
5885 // we are comparing two poisoned references.
5886 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005887
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005888 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5889 __ j(kEqual, &do_put);
5890 // If heap poisoning is enabled, the `temp` reference has
5891 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005892 __ MaybeUnpoisonHeapReference(temp);
5893
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005894 // If heap poisoning is enabled, no need to unpoison the
5895 // heap reference loaded below, as it is only used for a
5896 // comparison with null.
5897 __ cmpl(Address(temp, super_offset), Immediate(0));
5898 __ j(kNotEqual, slow_path->GetEntryLabel());
5899 __ Bind(&do_put);
5900 } else {
5901 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005902 }
5903 }
5904
5905 if (kPoisonHeapReferences) {
5906 __ movl(temp, register_value);
5907 __ PoisonHeapReference(temp);
5908 __ movl(address, temp);
5909 } else {
5910 __ movl(address, register_value);
5911 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005913 codegen_->MaybeRecordImplicitNullCheck(instruction);
5914 }
5915
5916 Register card = locations->GetTemp(1).AsRegister<Register>();
5917 codegen_->MarkGCCard(
5918 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5919 __ Bind(&done);
5920
5921 if (slow_path != nullptr) {
5922 __ Bind(slow_path->GetExitLabel());
5923 }
5924
5925 break;
5926 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005929 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005930 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005931 if (value.IsRegister()) {
5932 __ movl(address, value.AsRegister<Register>());
5933 } else {
5934 DCHECK(value.IsConstant()) << value;
5935 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5936 __ movl(address, Immediate(v));
5937 }
5938 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005939 break;
5940 }
5941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005942 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005944 if (value.IsRegisterPair()) {
5945 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5946 value.AsRegisterPairLow<Register>());
5947 codegen_->MaybeRecordImplicitNullCheck(instruction);
5948 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5949 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005950 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005951 DCHECK(value.IsConstant());
5952 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5953 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5954 Immediate(Low32Bits(val)));
5955 codegen_->MaybeRecordImplicitNullCheck(instruction);
5956 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5957 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005958 }
5959 break;
5960 }
5961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005963 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005964 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005965 if (value.IsFpuRegister()) {
5966 __ movss(address, value.AsFpuRegister<XmmRegister>());
5967 } else {
5968 DCHECK(value.IsConstant());
5969 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5970 __ movl(address, Immediate(v));
5971 }
5972 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005973 break;
5974 }
5975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005977 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005978 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005979 if (value.IsFpuRegister()) {
5980 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5981 } else {
5982 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005983 Address address_hi =
5984 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005985 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5986 __ movl(address, Immediate(Low32Bits(v)));
5987 codegen_->MaybeRecordImplicitNullCheck(instruction);
5988 __ movl(address_hi, Immediate(High32Bits(v)));
5989 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005990 break;
5991 }
5992
Aart Bik66c158e2018-01-31 12:55:04 -08005993 case DataType::Type::kUint32:
5994 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005996 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005997 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005998 }
5999}
6000
6001void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006002 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006003 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006004 if (!instruction->IsEmittedAtUseSite()) {
6005 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6006 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006007}
6008
6009void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006010 if (instruction->IsEmittedAtUseSite()) {
6011 return;
6012 }
6013
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006015 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006016 Register obj = locations->InAt(0).AsRegister<Register>();
6017 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006018 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006019 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006020 // Mask out most significant bit in case the array is String's array of char.
6021 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006022 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006023 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024}
6025
6026void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006027 RegisterSet caller_saves = RegisterSet::Empty();
6028 InvokeRuntimeCallingConvention calling_convention;
6029 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6030 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6031 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006032 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006033 HInstruction* length = instruction->InputAt(1);
6034 if (!length->IsEmittedAtUseSite()) {
6035 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6036 }
jessicahandojo4877b792016-09-08 19:49:13 -07006037 // Need register to see array's length.
6038 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6039 locations->AddTemp(Location::RequiresRegister());
6040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041}
6042
6043void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006044 const bool is_string_compressed_char_at =
6045 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006047 Location index_loc = locations->InAt(0);
6048 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006049 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006050 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051
Mark Mendell99dbd682015-04-22 16:18:52 -04006052 if (length_loc.IsConstant()) {
6053 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6054 if (index_loc.IsConstant()) {
6055 // BCE will remove the bounds check if we are guarenteed to pass.
6056 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6057 if (index < 0 || index >= length) {
6058 codegen_->AddSlowPath(slow_path);
6059 __ jmp(slow_path->GetEntryLabel());
6060 } else {
6061 // Some optimization after BCE may have generated this, and we should not
6062 // generate a bounds check if it is a valid range.
6063 }
6064 return;
6065 }
6066
6067 // We have to reverse the jump condition because the length is the constant.
6068 Register index_reg = index_loc.AsRegister<Register>();
6069 __ cmpl(index_reg, Immediate(length));
6070 codegen_->AddSlowPath(slow_path);
6071 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006072 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006073 HInstruction* array_length = instruction->InputAt(1);
6074 if (array_length->IsEmittedAtUseSite()) {
6075 // Address the length field in the array.
6076 DCHECK(array_length->IsArrayLength());
6077 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6078 Location array_loc = array_length->GetLocations()->InAt(0);
6079 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006080 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006081 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6082 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006083 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6084 __ movl(length_reg, array_len);
6085 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006086 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006087 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006088 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006089 // Checking bounds for general case:
6090 // Array of char or string's array with feature compression off.
6091 if (index_loc.IsConstant()) {
6092 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6093 __ cmpl(array_len, Immediate(value));
6094 } else {
6095 __ cmpl(array_len, index_loc.AsRegister<Register>());
6096 }
6097 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006098 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006099 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006100 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006101 }
6102 codegen_->AddSlowPath(slow_path);
6103 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006104 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105}
6106
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006107void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006108 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006109}
6110
6111void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006112 if (instruction->GetNext()->IsSuspendCheck() &&
6113 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6114 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6115 // The back edge will generate the suspend check.
6116 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6117 }
6118
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006119 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6120}
6121
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006122void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006123 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6124 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006125 // In suspend check slow path, usually there are no caller-save registers at all.
6126 // If SIMD instructions are present, however, we force spilling all live SIMD
6127 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006128 locations->SetCustomSlowPathCallerSaves(
6129 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006130}
6131
6132void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006133 HBasicBlock* block = instruction->GetBlock();
6134 if (block->GetLoopInformation() != nullptr) {
6135 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6136 // The back edge will generate the suspend check.
6137 return;
6138 }
6139 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6140 // The goto will generate the suspend check.
6141 return;
6142 }
6143 GenerateSuspendCheck(instruction, nullptr);
6144}
6145
6146void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6147 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006148 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006149 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6150 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006151 slow_path =
6152 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006153 instruction->SetSlowPath(slow_path);
6154 codegen_->AddSlowPath(slow_path);
6155 if (successor != nullptr) {
6156 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006157 }
6158 } else {
6159 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6160 }
6161
Andreas Gampe542451c2016-07-26 09:02:02 -07006162 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006163 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006164 if (successor == nullptr) {
6165 __ j(kNotEqual, slow_path->GetEntryLabel());
6166 __ Bind(slow_path->GetReturnLabel());
6167 } else {
6168 __ j(kEqual, codegen_->GetLabelOf(successor));
6169 __ jmp(slow_path->GetEntryLabel());
6170 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006171}
6172
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006173X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6174 return codegen_->GetAssembler();
6175}
6176
Aart Bikcfe50bb2017-12-12 14:54:12 -08006177void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006178 ScratchRegisterScope ensure_scratch(
6179 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6180 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6181 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006182
Aart Bikcfe50bb2017-12-12 14:54:12 -08006183 // Now that temp register is available (possibly spilled), move blocks of memory.
6184 for (int i = 0; i < number_of_words; i++) {
6185 __ movl(temp_reg, Address(ESP, src + stack_offset));
6186 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6187 stack_offset += kX86WordSize;
6188 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006189}
6190
6191void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006192 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006193 Location source = move->GetSource();
6194 Location destination = move->GetDestination();
6195
6196 if (source.IsRegister()) {
6197 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006198 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006199 } else if (destination.IsFpuRegister()) {
6200 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006201 } else {
6202 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006203 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006204 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006205 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006207 // Create stack space for 2 elements.
6208 __ subl(ESP, Immediate(2 * elem_size));
6209 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6210 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6211 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6212 // And remove the temporary stack space we allocated.
6213 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006214 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006215 if (destination.IsRegister()) {
6216 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6217 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006218 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006219 } else if (destination.IsRegisterPair()) {
6220 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6221 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6222 __ psrlq(src_reg, Immediate(32));
6223 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006224 } else if (destination.IsStackSlot()) {
6225 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006226 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006227 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006228 } else {
6229 DCHECK(destination.IsSIMDStackSlot());
6230 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006231 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006232 } else if (source.IsStackSlot()) {
6233 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006234 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006235 } else if (destination.IsFpuRegister()) {
6236 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006237 } else {
6238 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006239 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006240 }
6241 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006242 if (destination.IsRegisterPair()) {
6243 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6244 __ movl(destination.AsRegisterPairHigh<Register>(),
6245 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6246 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006247 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6248 } else {
6249 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006250 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006251 }
Aart Bik5576f372017-03-23 16:17:37 -07006252 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006253 if (destination.IsFpuRegister()) {
6254 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6255 } else {
6256 DCHECK(destination.IsSIMDStackSlot());
6257 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6258 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006259 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006260 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006261 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006262 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006263 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006264 if (value == 0) {
6265 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6266 } else {
6267 __ movl(destination.AsRegister<Register>(), Immediate(value));
6268 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006269 } else {
6270 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006271 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006272 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006273 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006274 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006275 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006276 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006277 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006278 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6279 if (value == 0) {
6280 // Easy handling of 0.0.
6281 __ xorps(dest, dest);
6282 } else {
6283 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006284 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6285 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6286 __ movl(temp, Immediate(value));
6287 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006288 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006289 } else {
6290 DCHECK(destination.IsStackSlot()) << destination;
6291 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6292 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006293 } else if (constant->IsLongConstant()) {
6294 int64_t value = constant->AsLongConstant()->GetValue();
6295 int32_t low_value = Low32Bits(value);
6296 int32_t high_value = High32Bits(value);
6297 Immediate low(low_value);
6298 Immediate high(high_value);
6299 if (destination.IsDoubleStackSlot()) {
6300 __ movl(Address(ESP, destination.GetStackIndex()), low);
6301 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6302 } else {
6303 __ movl(destination.AsRegisterPairLow<Register>(), low);
6304 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6305 }
6306 } else {
6307 DCHECK(constant->IsDoubleConstant());
6308 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006309 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006310 int32_t low_value = Low32Bits(value);
6311 int32_t high_value = High32Bits(value);
6312 Immediate low(low_value);
6313 Immediate high(high_value);
6314 if (destination.IsFpuRegister()) {
6315 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6316 if (value == 0) {
6317 // Easy handling of 0.0.
6318 __ xorpd(dest, dest);
6319 } else {
6320 __ pushl(high);
6321 __ pushl(low);
6322 __ movsd(dest, Address(ESP, 0));
6323 __ addl(ESP, Immediate(8));
6324 }
6325 } else {
6326 DCHECK(destination.IsDoubleStackSlot()) << destination;
6327 __ movl(Address(ESP, destination.GetStackIndex()), low);
6328 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6329 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006330 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006331 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006332 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006333 }
6334}
6335
Mark Mendella5c19ce2015-04-01 12:51:05 -04006336void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006337 Register suggested_scratch = reg == EAX ? EBX : EAX;
6338 ScratchRegisterScope ensure_scratch(
6339 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6340
6341 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6342 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6343 __ movl(Address(ESP, mem + stack_offset), reg);
6344 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006345}
6346
Mark Mendell7c8d0092015-01-26 11:21:33 -05006347void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006348 ScratchRegisterScope ensure_scratch(
6349 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6350
6351 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6352 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6353 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6354 __ movss(Address(ESP, mem + stack_offset), reg);
6355 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006356}
6357
Aart Bikcfe50bb2017-12-12 14:54:12 -08006358void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6359 size_t extra_slot = 4 * kX86WordSize;
6360 __ subl(ESP, Immediate(extra_slot));
6361 __ movups(Address(ESP, 0), XmmRegister(reg));
6362 ExchangeMemory(0, mem + extra_slot, 4);
6363 __ movups(XmmRegister(reg), Address(ESP, 0));
6364 __ addl(ESP, Immediate(extra_slot));
6365}
6366
6367void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006368 ScratchRegisterScope ensure_scratch1(
6369 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006370
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006371 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6372 ScratchRegisterScope ensure_scratch2(
6373 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006374
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006375 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6376 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006377
6378 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6379 for (int i = 0; i < number_of_words; i++) {
6380 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6381 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6382 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6383 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6384 stack_offset += kX86WordSize;
6385 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006386}
6387
6388void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006389 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006390 Location source = move->GetSource();
6391 Location destination = move->GetDestination();
6392
6393 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006394 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6395 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6396 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6397 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6398 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006399 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006400 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006401 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006402 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006403 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006404 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006405 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6406 // Use XOR Swap algorithm to avoid a temporary.
6407 DCHECK_NE(source.reg(), destination.reg());
6408 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6409 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6410 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6411 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6412 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6413 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6414 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006415 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6416 // Take advantage of the 16 bytes in the XMM register.
6417 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6418 Address stack(ESP, destination.GetStackIndex());
6419 // Load the double into the high doubleword.
6420 __ movhpd(reg, stack);
6421
6422 // Store the low double into the destination.
6423 __ movsd(stack, reg);
6424
6425 // Move the high double to the low double.
6426 __ psrldq(reg, Immediate(8));
6427 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6428 // Take advantage of the 16 bytes in the XMM register.
6429 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6430 Address stack(ESP, source.GetStackIndex());
6431 // Load the double into the high doubleword.
6432 __ movhpd(reg, stack);
6433
6434 // Store the low double into the destination.
6435 __ movsd(stack, reg);
6436
6437 // Move the high double to the low double.
6438 __ psrldq(reg, Immediate(8));
6439 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006440 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6441 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6442 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6443 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6444 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6445 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6446 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006447 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006448 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006449 }
6450}
6451
6452void ParallelMoveResolverX86::SpillScratch(int reg) {
6453 __ pushl(static_cast<Register>(reg));
6454}
6455
6456void ParallelMoveResolverX86::RestoreScratch(int reg) {
6457 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006458}
6459
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006460HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6461 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006462 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006463 case HLoadClass::LoadKind::kInvalid:
6464 LOG(FATAL) << "UNREACHABLE";
6465 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006466 case HLoadClass::LoadKind::kReferrersClass:
6467 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006468 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006469 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006470 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006471 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006472 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006473 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006474 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006475 DCHECK(Runtime::Current()->UseJitCompilation());
6476 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006477 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006478 break;
6479 }
6480 return desired_class_load_kind;
6481}
6482
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006483void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006484 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006485 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006486 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006487 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006488 cls,
6489 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006490 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006491 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006492 return;
6493 }
Vladimir Marko41559982017-01-06 14:04:23 +00006494 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006495
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006496 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6497 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006498 ? LocationSummary::kCallOnSlowPath
6499 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006500 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006501 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006502 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006503 }
6504
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006505 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006506 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006507 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006508 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006509 locations->SetInAt(0, Location::RequiresRegister());
6510 }
6511 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006512 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6513 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6514 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006515 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006516 } else {
6517 // For non-Baker read barrier we have a temp-clobbering call.
6518 }
6519 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006520}
6521
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006522Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006523 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006524 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006525 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006526 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006527 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006528 PatchInfo<Label>* info = &jit_class_patches_.back();
6529 return &info->label;
6530}
6531
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006532// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6533// move.
6534void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006535 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006536 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006537 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006538 return;
6539 }
Vladimir Marko41559982017-01-06 14:04:23 +00006540 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006541
Vladimir Marko41559982017-01-06 14:04:23 +00006542 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 Location out_loc = locations->Out();
6544 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006545
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006546 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006547 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6548 ? kWithoutReadBarrier
6549 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006550 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006551 case HLoadClass::LoadKind::kReferrersClass: {
6552 DCHECK(!cls->CanCallRuntime());
6553 DCHECK(!cls->MustGenerateClinitCheck());
6554 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6555 Register current_method = locations->InAt(0).AsRegister<Register>();
6556 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006557 cls,
6558 out_loc,
6559 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006560 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006561 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006562 break;
6563 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006564 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006565 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006566 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006567 Register method_address = locations->InAt(0).AsRegister<Register>();
6568 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006569 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006570 break;
6571 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006572 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006573 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6574 Register method_address = locations->InAt(0).AsRegister<Register>();
6575 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006576 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6577 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006578 break;
6579 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006580 case HLoadClass::LoadKind::kBssEntry: {
6581 Register method_address = locations->InAt(0).AsRegister<Register>();
6582 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6583 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6584 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6585 generate_null_check = true;
6586 break;
6587 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006588 case HLoadClass::LoadKind::kJitBootImageAddress: {
6589 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6590 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6591 DCHECK_NE(address, 0u);
6592 __ movl(out, Immediate(address));
6593 break;
6594 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006595 case HLoadClass::LoadKind::kJitTableAddress: {
6596 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6597 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006598 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006599 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006600 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006601 break;
6602 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006603 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006604 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006605 LOG(FATAL) << "UNREACHABLE";
6606 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006607 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006608
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006609 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6610 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006611 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006612 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006613
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006614 if (generate_null_check) {
6615 __ testl(out, out);
6616 __ j(kEqual, slow_path->GetEntryLabel());
6617 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006618
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006619 if (cls->MustGenerateClinitCheck()) {
6620 GenerateClassInitializationCheck(slow_path, out);
6621 } else {
6622 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006623 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006624 }
6625}
6626
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006627void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6628 InvokeRuntimeCallingConvention calling_convention;
6629 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6630 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6631}
6632
6633void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6634 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6635}
6636
Orion Hodson18259d72018-04-12 11:18:23 +01006637void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6638 InvokeRuntimeCallingConvention calling_convention;
6639 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6640 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6641}
6642
6643void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6644 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6645}
6646
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006647void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6648 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006649 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006650 locations->SetInAt(0, Location::RequiresRegister());
6651 if (check->HasUses()) {
6652 locations->SetOut(Location::SameAsFirstInput());
6653 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006654 // Rely on the type initialization to save everything we need.
6655 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006656}
6657
6658void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006659 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006660 SlowPathCode* slow_path =
6661 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006662 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006663 GenerateClassInitializationCheck(slow_path,
6664 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006665}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006666
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006667void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006668 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006669 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6670 const size_t status_byte_offset =
6671 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6672 constexpr uint32_t shifted_initialized_value =
6673 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6674
6675 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006676 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006677 __ Bind(slow_path->GetExitLabel());
6678 // No need for memory fence, thanks to the X86 memory model.
6679}
6680
Vladimir Marko175e7862018-03-27 09:03:13 +00006681void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6682 Register temp) {
6683 uint32_t path_to_root = check->GetBitstringPathToRoot();
6684 uint32_t mask = check->GetBitstringMask();
6685 DCHECK(IsPowerOfTwo(mask + 1));
6686 size_t mask_bits = WhichPowerOf2(mask + 1);
6687
6688 if (mask_bits == 16u) {
6689 // Compare the bitstring in memory.
6690 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6691 } else {
6692 // /* uint32_t */ temp = temp->status_
6693 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6694 // Compare the bitstring bits using SUB.
6695 __ subl(temp, Immediate(path_to_root));
6696 // Shift out bits that do not contribute to the comparison.
6697 __ shll(temp, Immediate(32u - mask_bits));
6698 }
6699}
6700
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006701HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6702 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006703 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006704 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006705 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006706 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006707 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006708 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006709 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006710 case HLoadString::LoadKind::kJitTableAddress:
6711 DCHECK(Runtime::Current()->UseJitCompilation());
6712 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006713 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006714 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006715 }
6716 return desired_string_load_kind;
6717}
6718
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006719void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006720 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006721 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006722 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006723 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006724 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006725 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006726 locations->SetInAt(0, Location::RequiresRegister());
6727 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006728 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006729 locations->SetOut(Location::RegisterLocation(EAX));
6730 } else {
6731 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006732 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6733 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006734 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006735 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006736 } else {
6737 // For non-Baker read barrier we have a temp-clobbering call.
6738 }
6739 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006740 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006741}
6742
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006743Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006744 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006745 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006746 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006747 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006748 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006749 PatchInfo<Label>* info = &jit_string_patches_.back();
6750 return &info->label;
6751}
6752
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006753// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6754// move.
6755void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006756 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757 Location out_loc = locations->Out();
6758 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006759
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006760 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006761 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006762 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006763 Register method_address = locations->InAt(0).AsRegister<Register>();
6764 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006765 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006766 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006767 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006768 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006769 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6770 Register method_address = locations->InAt(0).AsRegister<Register>();
6771 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006772 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6773 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006774 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006775 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006776 case HLoadString::LoadKind::kBssEntry: {
6777 Register method_address = locations->InAt(0).AsRegister<Register>();
6778 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6779 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006780 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006781 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006782 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006783 codegen_->AddSlowPath(slow_path);
6784 __ testl(out, out);
6785 __ j(kEqual, slow_path->GetEntryLabel());
6786 __ Bind(slow_path->GetExitLabel());
6787 return;
6788 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006789 case HLoadString::LoadKind::kJitBootImageAddress: {
6790 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6791 DCHECK_NE(address, 0u);
6792 __ movl(out, Immediate(address));
6793 return;
6794 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006795 case HLoadString::LoadKind::kJitTableAddress: {
6796 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6797 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006798 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006799 // /* GcRoot<mirror::String> */ out = *address
6800 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6801 return;
6802 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006803 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006804 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006805 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006806
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006807 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006808 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006809 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006810 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006811 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6812 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006813}
6814
David Brazdilcb1c0552015-08-04 16:22:25 +01006815static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006816 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006817}
6818
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006819void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6820 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006821 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006822 locations->SetOut(Location::RequiresRegister());
6823}
6824
6825void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006826 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6827}
6828
6829void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006830 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006831}
6832
6833void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6834 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006835}
6836
6837void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006838 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6839 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006840 InvokeRuntimeCallingConvention calling_convention;
6841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6842}
6843
6844void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006845 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006846 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006847}
6848
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006849// Temp is used for read barrier.
6850static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6851 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006852 !kUseBakerReadBarrier &&
6853 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006854 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006855 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6856 return 1;
6857 }
6858 return 0;
6859}
6860
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006861// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6862// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006863// The other checks have one temp for loading the object's class.
6864static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006865 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006866 return 2;
6867 }
6868 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006869}
6870
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006871void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006872 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006873 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006874 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006875 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006876 case TypeCheckKind::kExactCheck:
6877 case TypeCheckKind::kAbstractClassCheck:
6878 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006879 case TypeCheckKind::kArrayObjectCheck: {
6880 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6881 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6882 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006883 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006884 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006885 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006886 case TypeCheckKind::kUnresolvedCheck:
6887 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006888 call_kind = LocationSummary::kCallOnSlowPath;
6889 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006890 case TypeCheckKind::kBitstringCheck:
6891 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006892 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006893
Vladimir Markoca6fff82017-10-03 14:49:14 +01006894 LocationSummary* locations =
6895 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006896 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006897 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006898 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006899 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006900 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6901 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6902 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6903 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6904 } else {
6905 locations->SetInAt(1, Location::Any());
6906 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006907 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6908 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006909 // When read barriers are enabled, we need a temporary register for some cases.
6910 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006911}
6912
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006913void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006914 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006915 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006916 Location obj_loc = locations->InAt(0);
6917 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006918 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006919 Location out_loc = locations->Out();
6920 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006921 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6922 DCHECK_LE(num_temps, 1u);
6923 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006924 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006925 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6926 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6927 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006928 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006929 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006930
6931 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006932 // Avoid null check if we know obj is not null.
6933 if (instruction->MustDoNullCheck()) {
6934 __ testl(obj, obj);
6935 __ j(kEqual, &zero);
6936 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006937
Roland Levillain7c1559a2015-12-15 10:55:36 +00006938 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006939 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006940 ReadBarrierOption read_barrier_option =
6941 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006942 // /* HeapReference<Class> */ out = obj->klass_
6943 GenerateReferenceLoadTwoRegisters(instruction,
6944 out_loc,
6945 obj_loc,
6946 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006947 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006948 if (cls.IsRegister()) {
6949 __ cmpl(out, cls.AsRegister<Register>());
6950 } else {
6951 DCHECK(cls.IsStackSlot()) << cls;
6952 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6953 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006954
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006955 // Classes must be equal for the instanceof to succeed.
6956 __ j(kNotEqual, &zero);
6957 __ movl(out, Immediate(1));
6958 __ jmp(&done);
6959 break;
6960 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006961
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006962 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006963 ReadBarrierOption read_barrier_option =
6964 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006965 // /* HeapReference<Class> */ out = obj->klass_
6966 GenerateReferenceLoadTwoRegisters(instruction,
6967 out_loc,
6968 obj_loc,
6969 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006970 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006971 // If the class is abstract, we eagerly fetch the super class of the
6972 // object to avoid doing a comparison we know will fail.
6973 NearLabel loop;
6974 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006975 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006976 GenerateReferenceLoadOneRegister(instruction,
6977 out_loc,
6978 super_offset,
6979 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006980 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006981 __ testl(out, out);
6982 // If `out` is null, we use it for the result, and jump to `done`.
6983 __ j(kEqual, &done);
6984 if (cls.IsRegister()) {
6985 __ cmpl(out, cls.AsRegister<Register>());
6986 } else {
6987 DCHECK(cls.IsStackSlot()) << cls;
6988 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6989 }
6990 __ j(kNotEqual, &loop);
6991 __ movl(out, Immediate(1));
6992 if (zero.IsLinked()) {
6993 __ jmp(&done);
6994 }
6995 break;
6996 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006997
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006998 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006999 ReadBarrierOption read_barrier_option =
7000 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007001 // /* HeapReference<Class> */ out = obj->klass_
7002 GenerateReferenceLoadTwoRegisters(instruction,
7003 out_loc,
7004 obj_loc,
7005 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007006 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007007 // Walk over the class hierarchy to find a match.
7008 NearLabel loop, success;
7009 __ Bind(&loop);
7010 if (cls.IsRegister()) {
7011 __ cmpl(out, cls.AsRegister<Register>());
7012 } else {
7013 DCHECK(cls.IsStackSlot()) << cls;
7014 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7015 }
7016 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007017 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007018 GenerateReferenceLoadOneRegister(instruction,
7019 out_loc,
7020 super_offset,
7021 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007022 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007023 __ testl(out, out);
7024 __ j(kNotEqual, &loop);
7025 // If `out` is null, we use it for the result, and jump to `done`.
7026 __ jmp(&done);
7027 __ Bind(&success);
7028 __ movl(out, Immediate(1));
7029 if (zero.IsLinked()) {
7030 __ jmp(&done);
7031 }
7032 break;
7033 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007034
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007035 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007036 ReadBarrierOption read_barrier_option =
7037 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007038 // /* HeapReference<Class> */ out = obj->klass_
7039 GenerateReferenceLoadTwoRegisters(instruction,
7040 out_loc,
7041 obj_loc,
7042 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007043 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007044 // Do an exact check.
7045 NearLabel exact_check;
7046 if (cls.IsRegister()) {
7047 __ cmpl(out, cls.AsRegister<Register>());
7048 } else {
7049 DCHECK(cls.IsStackSlot()) << cls;
7050 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7051 }
7052 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007053 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007054 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007055 GenerateReferenceLoadOneRegister(instruction,
7056 out_loc,
7057 component_offset,
7058 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007059 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007060 __ testl(out, out);
7061 // If `out` is null, we use it for the result, and jump to `done`.
7062 __ j(kEqual, &done);
7063 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7064 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007065 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007066 __ movl(out, Immediate(1));
7067 __ jmp(&done);
7068 break;
7069 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007070
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007071 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007072 // No read barrier since the slow path will retry upon failure.
7073 // /* HeapReference<Class> */ out = obj->klass_
7074 GenerateReferenceLoadTwoRegisters(instruction,
7075 out_loc,
7076 obj_loc,
7077 class_offset,
7078 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007079 if (cls.IsRegister()) {
7080 __ cmpl(out, cls.AsRegister<Register>());
7081 } else {
7082 DCHECK(cls.IsStackSlot()) << cls;
7083 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7084 }
7085 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007086 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7087 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007088 codegen_->AddSlowPath(slow_path);
7089 __ j(kNotEqual, slow_path->GetEntryLabel());
7090 __ movl(out, Immediate(1));
7091 if (zero.IsLinked()) {
7092 __ jmp(&done);
7093 }
7094 break;
7095 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007096
Calin Juravle98893e12015-10-02 21:05:03 +01007097 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007098 case TypeCheckKind::kInterfaceCheck: {
7099 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007100 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007101 // cases.
7102 //
7103 // We cannot directly call the InstanceofNonTrivial runtime
7104 // entry point without resorting to a type checking slow path
7105 // here (i.e. by calling InvokeRuntime directly), as it would
7106 // require to assign fixed registers for the inputs of this
7107 // HInstanceOf instruction (following the runtime calling
7108 // convention), which might be cluttered by the potential first
7109 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110 //
7111 // TODO: Introduce a new runtime entry point taking the object
7112 // to test (instead of its class) as argument, and let it deal
7113 // with the read barrier issues. This will let us refactor this
7114 // case of the `switch` code as it was previously (with a direct
7115 // call to the runtime not using a type checking slow path).
7116 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007117 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007118 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7119 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007120 codegen_->AddSlowPath(slow_path);
7121 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007122 if (zero.IsLinked()) {
7123 __ jmp(&done);
7124 }
7125 break;
7126 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007127
7128 case TypeCheckKind::kBitstringCheck: {
7129 // /* HeapReference<Class> */ temp = obj->klass_
7130 GenerateReferenceLoadTwoRegisters(instruction,
7131 out_loc,
7132 obj_loc,
7133 class_offset,
7134 kWithoutReadBarrier);
7135
7136 GenerateBitstringTypeCheckCompare(instruction, out);
7137 __ j(kNotEqual, &zero);
7138 __ movl(out, Immediate(1));
7139 __ jmp(&done);
7140 break;
7141 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007142 }
7143
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007144 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007145 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 __ xorl(out, out);
7147 }
7148
7149 if (done.IsLinked()) {
7150 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007151 }
7152
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007153 if (slow_path != nullptr) {
7154 __ Bind(slow_path->GetExitLabel());
7155 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007156}
7157
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007158void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007159 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007160 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007161 LocationSummary* locations =
7162 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007163 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007164 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7165 // Require a register for the interface check since there is a loop that compares the class to
7166 // a memory address.
7167 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007168 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7169 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7170 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7171 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007172 } else {
7173 locations->SetInAt(1, Location::Any());
7174 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007175 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007176 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7177}
7178
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007179void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007181 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007182 Location obj_loc = locations->InAt(0);
7183 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007184 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007185 Location temp_loc = locations->GetTemp(0);
7186 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007187 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7188 DCHECK_GE(num_temps, 1u);
7189 DCHECK_LE(num_temps, 2u);
7190 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7191 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7192 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7193 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7194 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7195 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7196 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7197 const uint32_t object_array_data_offset =
7198 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007199
Vladimir Marko87584542017-12-12 17:47:52 +00007200 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007201 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007202 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7203 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007204 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205
Roland Levillain0d5a2812015-11-13 10:07:31 +00007206 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007207 // Avoid null check if we know obj is not null.
7208 if (instruction->MustDoNullCheck()) {
7209 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007210 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007211 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007212
Roland Levillain0d5a2812015-11-13 10:07:31 +00007213 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007214 case TypeCheckKind::kExactCheck:
7215 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007216 // /* HeapReference<Class> */ temp = obj->klass_
7217 GenerateReferenceLoadTwoRegisters(instruction,
7218 temp_loc,
7219 obj_loc,
7220 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007221 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007222
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007223 if (cls.IsRegister()) {
7224 __ cmpl(temp, cls.AsRegister<Register>());
7225 } else {
7226 DCHECK(cls.IsStackSlot()) << cls;
7227 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7228 }
7229 // Jump to slow path for throwing the exception or doing a
7230 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007231 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 break;
7233 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007234
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007235 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007236 // /* HeapReference<Class> */ temp = obj->klass_
7237 GenerateReferenceLoadTwoRegisters(instruction,
7238 temp_loc,
7239 obj_loc,
7240 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007241 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007243 // If the class is abstract, we eagerly fetch the super class of the
7244 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007245 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007246 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007247 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007248 GenerateReferenceLoadOneRegister(instruction,
7249 temp_loc,
7250 super_offset,
7251 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007252 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007253
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007254 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7255 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007256 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007257 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007258
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007259 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007260 if (cls.IsRegister()) {
7261 __ cmpl(temp, cls.AsRegister<Register>());
7262 } else {
7263 DCHECK(cls.IsStackSlot()) << cls;
7264 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7265 }
7266 __ j(kNotEqual, &loop);
7267 break;
7268 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007269
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007270 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007271 // /* HeapReference<Class> */ temp = obj->klass_
7272 GenerateReferenceLoadTwoRegisters(instruction,
7273 temp_loc,
7274 obj_loc,
7275 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007276 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007277
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007278 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007279 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007280 __ Bind(&loop);
7281 if (cls.IsRegister()) {
7282 __ cmpl(temp, cls.AsRegister<Register>());
7283 } else {
7284 DCHECK(cls.IsStackSlot()) << cls;
7285 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7286 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007287 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007288
Roland Levillain0d5a2812015-11-13 10:07:31 +00007289 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007290 GenerateReferenceLoadOneRegister(instruction,
7291 temp_loc,
7292 super_offset,
7293 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007294 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007295
7296 // If the class reference currently in `temp` is not null, jump
7297 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007298 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007299 __ j(kNotZero, &loop);
7300 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007301 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007302 break;
7303 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007304
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007305 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007306 // /* HeapReference<Class> */ temp = obj->klass_
7307 GenerateReferenceLoadTwoRegisters(instruction,
7308 temp_loc,
7309 obj_loc,
7310 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007311 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007312
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007313 // Do an exact check.
7314 if (cls.IsRegister()) {
7315 __ cmpl(temp, cls.AsRegister<Register>());
7316 } else {
7317 DCHECK(cls.IsStackSlot()) << cls;
7318 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7319 }
7320 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007321
7322 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007323 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007324 GenerateReferenceLoadOneRegister(instruction,
7325 temp_loc,
7326 component_offset,
7327 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007328 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007330 // If the component type is null (i.e. the object not an array), jump to the slow path to
7331 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007332 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007333 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007335 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007336 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007337 break;
7338 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007339
Calin Juravle98893e12015-10-02 21:05:03 +01007340 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007341 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 // We cannot directly call the CheckCast runtime entry point
7343 // without resorting to a type checking slow path here (i.e. by
7344 // calling InvokeRuntime directly), as it would require to
7345 // assign fixed registers for the inputs of this HInstanceOf
7346 // instruction (following the runtime calling convention), which
7347 // might be cluttered by the potential first read barrier
7348 // emission at the beginning of this method.
7349 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007350 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007351
7352 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007353 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7354 // We can not get false positives by doing this.
7355 // /* HeapReference<Class> */ temp = obj->klass_
7356 GenerateReferenceLoadTwoRegisters(instruction,
7357 temp_loc,
7358 obj_loc,
7359 class_offset,
7360 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007361
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007362 // /* HeapReference<Class> */ temp = temp->iftable_
7363 GenerateReferenceLoadTwoRegisters(instruction,
7364 temp_loc,
7365 temp_loc,
7366 iftable_offset,
7367 kWithoutReadBarrier);
7368 // Iftable is never null.
7369 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7370 // Maybe poison the `cls` for direct comparison with memory.
7371 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7372 // Loop through the iftable and check if any class matches.
7373 NearLabel start_loop;
7374 __ Bind(&start_loop);
7375 // Need to subtract first to handle the empty array case.
7376 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7377 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7378 // Go to next interface if the classes do not match.
7379 __ cmpl(cls.AsRegister<Register>(),
7380 CodeGeneratorX86::ArrayAddress(temp,
7381 maybe_temp2_loc,
7382 TIMES_4,
7383 object_array_data_offset));
7384 __ j(kNotEqual, &start_loop);
7385 // If `cls` was poisoned above, unpoison it.
7386 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007387 break;
7388 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007389
7390 case TypeCheckKind::kBitstringCheck: {
7391 // /* HeapReference<Class> */ temp = obj->klass_
7392 GenerateReferenceLoadTwoRegisters(instruction,
7393 temp_loc,
7394 obj_loc,
7395 class_offset,
7396 kWithoutReadBarrier);
7397
7398 GenerateBitstringTypeCheckCompare(instruction, temp);
7399 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7400 break;
7401 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007402 }
7403 __ Bind(&done);
7404
Roland Levillain0d5a2812015-11-13 10:07:31 +00007405 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007406}
7407
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007408void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007409 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7410 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007411 InvokeRuntimeCallingConvention calling_convention;
7412 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7413}
7414
7415void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007416 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7417 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007418 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007419 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007420 if (instruction->IsEnter()) {
7421 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7422 } else {
7423 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7424 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007425}
7426
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007427void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7428void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7429void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7430
7431void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7432 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007433 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007434 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7435 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007436 locations->SetInAt(0, Location::RequiresRegister());
7437 locations->SetInAt(1, Location::Any());
7438 locations->SetOut(Location::SameAsFirstInput());
7439}
7440
7441void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7442 HandleBitwiseOperation(instruction);
7443}
7444
7445void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7446 HandleBitwiseOperation(instruction);
7447}
7448
7449void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7450 HandleBitwiseOperation(instruction);
7451}
7452
7453void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7454 LocationSummary* locations = instruction->GetLocations();
7455 Location first = locations->InAt(0);
7456 Location second = locations->InAt(1);
7457 DCHECK(first.Equals(locations->Out()));
7458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007459 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007460 if (second.IsRegister()) {
7461 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007462 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007463 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007464 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007465 } else {
7466 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007467 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007468 }
7469 } else if (second.IsConstant()) {
7470 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007471 __ andl(first.AsRegister<Register>(),
7472 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007473 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007474 __ orl(first.AsRegister<Register>(),
7475 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007476 } else {
7477 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007478 __ xorl(first.AsRegister<Register>(),
7479 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007480 }
7481 } else {
7482 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007483 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007484 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007485 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007486 } else {
7487 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007488 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007489 }
7490 }
7491 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007492 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007493 if (second.IsRegisterPair()) {
7494 if (instruction->IsAnd()) {
7495 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7496 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7497 } else if (instruction->IsOr()) {
7498 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7499 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7500 } else {
7501 DCHECK(instruction->IsXor());
7502 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7503 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7504 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007505 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007506 if (instruction->IsAnd()) {
7507 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7508 __ andl(first.AsRegisterPairHigh<Register>(),
7509 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7510 } else if (instruction->IsOr()) {
7511 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7512 __ orl(first.AsRegisterPairHigh<Register>(),
7513 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7514 } else {
7515 DCHECK(instruction->IsXor());
7516 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7517 __ xorl(first.AsRegisterPairHigh<Register>(),
7518 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7519 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007520 } else {
7521 DCHECK(second.IsConstant()) << second;
7522 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007523 int32_t low_value = Low32Bits(value);
7524 int32_t high_value = High32Bits(value);
7525 Immediate low(low_value);
7526 Immediate high(high_value);
7527 Register first_low = first.AsRegisterPairLow<Register>();
7528 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007529 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007530 if (low_value == 0) {
7531 __ xorl(first_low, first_low);
7532 } else if (low_value != -1) {
7533 __ andl(first_low, low);
7534 }
7535 if (high_value == 0) {
7536 __ xorl(first_high, first_high);
7537 } else if (high_value != -1) {
7538 __ andl(first_high, high);
7539 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007540 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007541 if (low_value != 0) {
7542 __ orl(first_low, low);
7543 }
7544 if (high_value != 0) {
7545 __ orl(first_high, high);
7546 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007547 } else {
7548 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007549 if (low_value != 0) {
7550 __ xorl(first_low, low);
7551 }
7552 if (high_value != 0) {
7553 __ xorl(first_high, high);
7554 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007555 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007556 }
7557 }
7558}
7559
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007560void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7561 HInstruction* instruction,
7562 Location out,
7563 uint32_t offset,
7564 Location maybe_temp,
7565 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007566 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007567 if (read_barrier_option == kWithReadBarrier) {
7568 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007569 if (kUseBakerReadBarrier) {
7570 // Load with fast path based Baker's read barrier.
7571 // /* HeapReference<Object> */ out = *(out + offset)
7572 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007573 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007574 } else {
7575 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007576 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007577 // in the following move operation, as we will need it for the
7578 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007579 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007580 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007581 // /* HeapReference<Object> */ out = *(out + offset)
7582 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007583 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007584 }
7585 } else {
7586 // Plain load with no read barrier.
7587 // /* HeapReference<Object> */ out = *(out + offset)
7588 __ movl(out_reg, Address(out_reg, offset));
7589 __ MaybeUnpoisonHeapReference(out_reg);
7590 }
7591}
7592
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007593void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7594 HInstruction* instruction,
7595 Location out,
7596 Location obj,
7597 uint32_t offset,
7598 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007599 Register out_reg = out.AsRegister<Register>();
7600 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007601 if (read_barrier_option == kWithReadBarrier) {
7602 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007603 if (kUseBakerReadBarrier) {
7604 // Load with fast path based Baker's read barrier.
7605 // /* HeapReference<Object> */ out = *(obj + offset)
7606 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007607 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007608 } else {
7609 // Load with slow path based read barrier.
7610 // /* HeapReference<Object> */ out = *(obj + offset)
7611 __ movl(out_reg, Address(obj_reg, offset));
7612 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7613 }
7614 } else {
7615 // Plain load with no read barrier.
7616 // /* HeapReference<Object> */ out = *(obj + offset)
7617 __ movl(out_reg, Address(obj_reg, offset));
7618 __ MaybeUnpoisonHeapReference(out_reg);
7619 }
7620}
7621
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007622void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7623 HInstruction* instruction,
7624 Location root,
7625 const Address& address,
7626 Label* fixup_label,
7627 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007628 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007629 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007630 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007631 if (kUseBakerReadBarrier) {
7632 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7633 // Baker's read barrier are used:
7634 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007635 // root = obj.field;
7636 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7637 // if (temp != null) {
7638 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007639 // }
7640
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007641 // /* GcRoot<mirror::Object> */ root = *address
7642 __ movl(root_reg, address);
7643 if (fixup_label != nullptr) {
7644 __ Bind(fixup_label);
7645 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007646 static_assert(
7647 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7648 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7649 "have different sizes.");
7650 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7651 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7652 "have different sizes.");
7653
Vladimir Marko953437b2016-08-24 08:30:46 +00007654 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007655 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007656 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007657 codegen_->AddSlowPath(slow_path);
7658
Roland Levillaind966ce72017-02-09 16:20:14 +00007659 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7660 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007661 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007662 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7663 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007664 __ j(kNotEqual, slow_path->GetEntryLabel());
7665 __ Bind(slow_path->GetExitLabel());
7666 } else {
7667 // GC root loaded through a slow path for read barriers other
7668 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007669 // /* GcRoot<mirror::Object>* */ root = address
7670 __ leal(root_reg, address);
7671 if (fixup_label != nullptr) {
7672 __ Bind(fixup_label);
7673 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007674 // /* mirror::Object* */ root = root->Read()
7675 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7676 }
7677 } else {
7678 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007679 // /* GcRoot<mirror::Object> */ root = *address
7680 __ movl(root_reg, address);
7681 if (fixup_label != nullptr) {
7682 __ Bind(fixup_label);
7683 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007684 // Note that GC roots are not affected by heap poisoning, thus we
7685 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007686 }
7687}
7688
7689void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7690 Location ref,
7691 Register obj,
7692 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007693 bool needs_null_check) {
7694 DCHECK(kEmitCompilerReadBarrier);
7695 DCHECK(kUseBakerReadBarrier);
7696
7697 // /* HeapReference<Object> */ ref = *(obj + offset)
7698 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007699 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007700}
7701
7702void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7703 Location ref,
7704 Register obj,
7705 uint32_t data_offset,
7706 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007707 bool needs_null_check) {
7708 DCHECK(kEmitCompilerReadBarrier);
7709 DCHECK(kUseBakerReadBarrier);
7710
Roland Levillain3d312422016-06-23 13:53:42 +01007711 static_assert(
7712 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7713 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007714 // /* HeapReference<Object> */ ref =
7715 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007716 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007717 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007718}
7719
7720void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7721 Location ref,
7722 Register obj,
7723 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007724 bool needs_null_check,
7725 bool always_update_field,
7726 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007727 DCHECK(kEmitCompilerReadBarrier);
7728 DCHECK(kUseBakerReadBarrier);
7729
7730 // In slow path based read barriers, the read barrier call is
7731 // inserted after the original load. However, in fast path based
7732 // Baker's read barriers, we need to perform the load of
7733 // mirror::Object::monitor_ *before* the original reference load.
7734 // This load-load ordering is required by the read barrier.
7735 // The fast path/slow path (for Baker's algorithm) should look like:
7736 //
7737 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7738 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7739 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007740 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007741 // if (is_gray) {
7742 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7743 // }
7744 //
7745 // Note: the original implementation in ReadBarrier::Barrier is
7746 // slightly more complex as:
7747 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007748 // the high-bits of rb_state, which are expected to be all zeroes
7749 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7750 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007751 // - it performs additional checks that we do not do here for
7752 // performance reasons.
7753
7754 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007755 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7756
Vladimir Marko953437b2016-08-24 08:30:46 +00007757 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007758 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007759 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007760 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7761 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7762 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7763
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007764 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007765 // ref = ReadBarrier::Mark(ref);
7766 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7767 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007768 if (needs_null_check) {
7769 MaybeRecordImplicitNullCheck(instruction);
7770 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007771
7772 // Load fence to prevent load-load reordering.
7773 // Note that this is a no-op, thanks to the x86 memory model.
7774 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7775
7776 // The actual reference load.
7777 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007778 __ movl(ref_reg, src); // Flags are unaffected.
7779
7780 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7781 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007782 SlowPathCode* slow_path;
7783 if (always_update_field) {
7784 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007785 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007786 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7787 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007788 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007789 instruction, ref, /* unpoison_ref_before_marking */ true);
7790 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007791 AddSlowPath(slow_path);
7792
7793 // We have done the "if" of the gray bit check above, now branch based on the flags.
7794 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007795
7796 // Object* ref = ref_addr->AsMirrorPtr()
7797 __ MaybeUnpoisonHeapReference(ref_reg);
7798
Roland Levillain7c1559a2015-12-15 10:55:36 +00007799 __ Bind(slow_path->GetExitLabel());
7800}
7801
7802void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7803 Location out,
7804 Location ref,
7805 Location obj,
7806 uint32_t offset,
7807 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007808 DCHECK(kEmitCompilerReadBarrier);
7809
Roland Levillain7c1559a2015-12-15 10:55:36 +00007810 // Insert a slow path based read barrier *after* the reference load.
7811 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007812 // If heap poisoning is enabled, the unpoisoning of the loaded
7813 // reference will be carried out by the runtime within the slow
7814 // path.
7815 //
7816 // Note that `ref` currently does not get unpoisoned (when heap
7817 // poisoning is enabled), which is alright as the `ref` argument is
7818 // not used by the artReadBarrierSlow entry point.
7819 //
7820 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007821 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007822 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7823 AddSlowPath(slow_path);
7824
Roland Levillain0d5a2812015-11-13 10:07:31 +00007825 __ jmp(slow_path->GetEntryLabel());
7826 __ Bind(slow_path->GetExitLabel());
7827}
7828
Roland Levillain7c1559a2015-12-15 10:55:36 +00007829void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7830 Location out,
7831 Location ref,
7832 Location obj,
7833 uint32_t offset,
7834 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007835 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007836 // Baker's read barriers shall be handled by the fast path
7837 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7838 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007839 // If heap poisoning is enabled, unpoisoning will be taken care of
7840 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007841 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007842 } else if (kPoisonHeapReferences) {
7843 __ UnpoisonHeapReference(out.AsRegister<Register>());
7844 }
7845}
7846
Roland Levillain7c1559a2015-12-15 10:55:36 +00007847void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7848 Location out,
7849 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007850 DCHECK(kEmitCompilerReadBarrier);
7851
Roland Levillain7c1559a2015-12-15 10:55:36 +00007852 // Insert a slow path based read barrier *after* the GC root load.
7853 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007854 // Note that GC roots are not affected by heap poisoning, so we do
7855 // not need to do anything special for this here.
7856 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007857 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007858 AddSlowPath(slow_path);
7859
Roland Levillain0d5a2812015-11-13 10:07:31 +00007860 __ jmp(slow_path->GetEntryLabel());
7861 __ Bind(slow_path->GetExitLabel());
7862}
7863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007864void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007865 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007866 LOG(FATAL) << "Unreachable";
7867}
7868
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007869void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007870 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007871 LOG(FATAL) << "Unreachable";
7872}
7873
Mark Mendellfe57faa2015-09-18 09:26:15 -04007874// Simple implementation of packed switch - generate cascaded compare/jumps.
7875void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7876 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007877 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007878 locations->SetInAt(0, Location::RequiresRegister());
7879}
7880
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007881void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7882 int32_t lower_bound,
7883 uint32_t num_entries,
7884 HBasicBlock* switch_block,
7885 HBasicBlock* default_block) {
7886 // Figure out the correct compare values and jump conditions.
7887 // Handle the first compare/branch as a special case because it might
7888 // jump to the default case.
7889 DCHECK_GT(num_entries, 2u);
7890 Condition first_condition;
7891 uint32_t index;
7892 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7893 if (lower_bound != 0) {
7894 first_condition = kLess;
7895 __ cmpl(value_reg, Immediate(lower_bound));
7896 __ j(first_condition, codegen_->GetLabelOf(default_block));
7897 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007898
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007899 index = 1;
7900 } else {
7901 // Handle all the compare/jumps below.
7902 first_condition = kBelow;
7903 index = 0;
7904 }
7905
7906 // Handle the rest of the compare/jumps.
7907 for (; index + 1 < num_entries; index += 2) {
7908 int32_t compare_to_value = lower_bound + index + 1;
7909 __ cmpl(value_reg, Immediate(compare_to_value));
7910 // Jump to successors[index] if value < case_value[index].
7911 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7912 // Jump to successors[index + 1] if value == case_value[index + 1].
7913 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7914 }
7915
7916 if (index != num_entries) {
7917 // There are an odd number of entries. Handle the last one.
7918 DCHECK_EQ(index + 1, num_entries);
7919 __ cmpl(value_reg, Immediate(lower_bound + index));
7920 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007921 }
7922
7923 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007924 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7925 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007926 }
7927}
7928
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007929void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7930 int32_t lower_bound = switch_instr->GetStartValue();
7931 uint32_t num_entries = switch_instr->GetNumEntries();
7932 LocationSummary* locations = switch_instr->GetLocations();
7933 Register value_reg = locations->InAt(0).AsRegister<Register>();
7934
7935 GenPackedSwitchWithCompares(value_reg,
7936 lower_bound,
7937 num_entries,
7938 switch_instr->GetBlock(),
7939 switch_instr->GetDefaultBlock());
7940}
7941
Mark Mendell805b3b52015-09-18 14:10:29 -04007942void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7943 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007944 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007945 locations->SetInAt(0, Location::RequiresRegister());
7946
7947 // Constant area pointer.
7948 locations->SetInAt(1, Location::RequiresRegister());
7949
7950 // And the temporary we need.
7951 locations->AddTemp(Location::RequiresRegister());
7952}
7953
7954void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7955 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007956 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007957 LocationSummary* locations = switch_instr->GetLocations();
7958 Register value_reg = locations->InAt(0).AsRegister<Register>();
7959 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7960
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007961 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7962 GenPackedSwitchWithCompares(value_reg,
7963 lower_bound,
7964 num_entries,
7965 switch_instr->GetBlock(),
7966 default_block);
7967 return;
7968 }
7969
Mark Mendell805b3b52015-09-18 14:10:29 -04007970 // Optimizing has a jump area.
7971 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7972 Register constant_area = locations->InAt(1).AsRegister<Register>();
7973
7974 // Remove the bias, if needed.
7975 if (lower_bound != 0) {
7976 __ leal(temp_reg, Address(value_reg, -lower_bound));
7977 value_reg = temp_reg;
7978 }
7979
7980 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007981 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007982 __ cmpl(value_reg, Immediate(num_entries - 1));
7983 __ j(kAbove, codegen_->GetLabelOf(default_block));
7984
7985 // We are in the range of the table.
7986 // Load (target-constant_area) from the jump table, indexing by the value.
7987 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7988
7989 // Compute the actual target address by adding in constant_area.
7990 __ addl(temp_reg, constant_area);
7991
7992 // And jump.
7993 __ jmp(temp_reg);
7994}
7995
Mark Mendell0616ae02015-04-17 12:49:27 -04007996void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7997 HX86ComputeBaseMethodAddress* insn) {
7998 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007999 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008000 locations->SetOut(Location::RequiresRegister());
8001}
8002
8003void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8004 HX86ComputeBaseMethodAddress* insn) {
8005 LocationSummary* locations = insn->GetLocations();
8006 Register reg = locations->Out().AsRegister<Register>();
8007
8008 // Generate call to next instruction.
8009 Label next_instruction;
8010 __ call(&next_instruction);
8011 __ Bind(&next_instruction);
8012
8013 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008014 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008015
8016 // Grab the return address off the stack.
8017 __ popl(reg);
8018}
8019
8020void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8021 HX86LoadFromConstantTable* insn) {
8022 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008023 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008024
8025 locations->SetInAt(0, Location::RequiresRegister());
8026 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8027
8028 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008029 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008030 return;
8031 }
8032
8033 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008034 case DataType::Type::kFloat32:
8035 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008036 locations->SetOut(Location::RequiresFpuRegister());
8037 break;
8038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008039 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008040 locations->SetOut(Location::RequiresRegister());
8041 break;
8042
8043 default:
8044 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8045 }
8046}
8047
8048void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008049 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008050 return;
8051 }
8052
8053 LocationSummary* locations = insn->GetLocations();
8054 Location out = locations->Out();
8055 Register const_area = locations->InAt(0).AsRegister<Register>();
8056 HConstant *value = insn->GetConstant();
8057
8058 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008059 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008060 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008061 codegen_->LiteralFloatAddress(
8062 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008063 break;
8064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008065 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008066 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008067 codegen_->LiteralDoubleAddress(
8068 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008069 break;
8070
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008071 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008072 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008073 codegen_->LiteralInt32Address(
8074 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008075 break;
8076
8077 default:
8078 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8079 }
8080}
8081
Mark Mendell0616ae02015-04-17 12:49:27 -04008082/**
8083 * Class to handle late fixup of offsets into constant area.
8084 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008085class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008086 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008087 RIPFixup(CodeGeneratorX86& codegen,
8088 HX86ComputeBaseMethodAddress* base_method_address,
8089 size_t offset)
8090 : codegen_(&codegen),
8091 base_method_address_(base_method_address),
8092 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008093
8094 protected:
8095 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8096
8097 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008098 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008099
8100 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008101 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008102 // Patch the correct offset for the instruction. The place to patch is the
8103 // last 4 bytes of the instruction.
8104 // The value to patch is the distance from the offset in the constant area
8105 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008106 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008107 int32_t relative_position =
8108 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008109
8110 // Patch in the right value.
8111 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8112 }
8113
Mark Mendell0616ae02015-04-17 12:49:27 -04008114 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008115 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008116};
8117
Mark Mendell805b3b52015-09-18 14:10:29 -04008118/**
8119 * Class to handle late fixup of offsets to a jump table that will be created in the
8120 * constant area.
8121 */
8122class JumpTableRIPFixup : public RIPFixup {
8123 public:
8124 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008125 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8126 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008127
8128 void CreateJumpTable() {
8129 X86Assembler* assembler = codegen_->GetAssembler();
8130
8131 // Ensure that the reference to the jump table has the correct offset.
8132 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8133 SetOffset(offset_in_constant_table);
8134
8135 // The label values in the jump table are computed relative to the
8136 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008137 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008138
8139 // Populate the jump table with the correct values for the jump table.
8140 int32_t num_entries = switch_instr_->GetNumEntries();
8141 HBasicBlock* block = switch_instr_->GetBlock();
8142 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8143 // The value that we want is the target offset - the position of the table.
8144 for (int32_t i = 0; i < num_entries; i++) {
8145 HBasicBlock* b = successors[i];
8146 Label* l = codegen_->GetLabelOf(b);
8147 DCHECK(l->IsBound());
8148 int32_t offset_to_block = l->Position() - relative_offset;
8149 assembler->AppendInt32(offset_to_block);
8150 }
8151 }
8152
8153 private:
8154 const HX86PackedSwitch* switch_instr_;
8155};
8156
8157void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8158 // Generate the constant area if needed.
8159 X86Assembler* assembler = GetAssembler();
8160 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8161 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8162 // byte values.
8163 assembler->Align(4, 0);
8164 constant_area_start_ = assembler->CodeSize();
8165
8166 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008167 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008168 jump_table->CreateJumpTable();
8169 }
8170
8171 // And now add the constant area to the generated code.
8172 assembler->AddConstantArea();
8173 }
8174
8175 // And finish up.
8176 CodeGenerator::Finalize(allocator);
8177}
8178
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008179Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8180 HX86ComputeBaseMethodAddress* method_base,
8181 Register reg) {
8182 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008183 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008184 return Address(reg, kDummy32BitOffset, fixup);
8185}
8186
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008187Address CodeGeneratorX86::LiteralFloatAddress(float v,
8188 HX86ComputeBaseMethodAddress* method_base,
8189 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008190 AssemblerFixup* fixup =
8191 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008192 return Address(reg, kDummy32BitOffset, fixup);
8193}
8194
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008195Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8196 HX86ComputeBaseMethodAddress* method_base,
8197 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008198 AssemblerFixup* fixup =
8199 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008200 return Address(reg, kDummy32BitOffset, fixup);
8201}
8202
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008203Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8204 HX86ComputeBaseMethodAddress* method_base,
8205 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008206 AssemblerFixup* fixup =
8207 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008208 return Address(reg, kDummy32BitOffset, fixup);
8209}
8210
Aart Bika19616e2016-02-01 18:57:58 -08008211void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8212 if (value == 0) {
8213 __ xorl(dest, dest);
8214 } else {
8215 __ movl(dest, Immediate(value));
8216 }
8217}
8218
8219void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8220 if (value == 0) {
8221 __ testl(dest, dest);
8222 } else {
8223 __ cmpl(dest, Immediate(value));
8224 }
8225}
8226
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008227void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8228 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008229 GenerateIntCompare(lhs_reg, rhs);
8230}
8231
8232void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008233 if (rhs.IsConstant()) {
8234 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008235 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008236 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008237 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008238 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008239 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008240 }
8241}
8242
8243Address CodeGeneratorX86::ArrayAddress(Register obj,
8244 Location index,
8245 ScaleFactor scale,
8246 uint32_t data_offset) {
8247 return index.IsConstant() ?
8248 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8249 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8250}
8251
Mark Mendell805b3b52015-09-18 14:10:29 -04008252Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8253 Register reg,
8254 Register value) {
8255 // Create a fixup to be used to create and address the jump table.
8256 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008257 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008258
8259 // We have to populate the jump tables.
8260 fixups_to_jump_tables_.push_back(table_fixup);
8261
8262 // We want a scaled address, as we are extracting the correct offset from the table.
8263 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8264}
8265
Andreas Gampe85b62f22015-09-09 13:15:38 -07008266// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008267void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008268 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008269 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008270 return;
8271 }
8272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008273 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008274
8275 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8276 if (target.Equals(return_loc)) {
8277 return;
8278 }
8279
8280 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8281 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008282 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008283 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008284 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8285 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008286 GetMoveResolver()->EmitNativeCode(&parallel_move);
8287 } else {
8288 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008289 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008290 parallel_move.AddMove(return_loc, target, type, nullptr);
8291 GetMoveResolver()->EmitNativeCode(&parallel_move);
8292 }
8293}
8294
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008295void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8296 const uint8_t* roots_data,
8297 const PatchInfo<Label>& info,
8298 uint64_t index_in_table) const {
8299 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8300 uintptr_t address =
8301 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008302 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008303 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8304 dchecked_integral_cast<uint32_t>(address);
8305}
8306
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008307void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8308 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008309 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008310 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008311 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008312 }
8313
8314 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008315 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008316 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008317 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008318 }
8319}
8320
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008321void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8322 ATTRIBUTE_UNUSED) {
8323 LOG(FATAL) << "Unreachable";
8324}
8325
8326void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8327 ATTRIBUTE_UNUSED) {
8328 LOG(FATAL) << "Unreachable";
8329}
8330
Roland Levillain4d027112015-07-01 15:41:14 +01008331#undef __
8332
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008333} // namespace x86
8334} // namespace art