blob: 46bd479c2ce59991dce4f7bf52f78a9b4e4b9c2b [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
Alexandre Rames2ed20af2015-03-06 13:55:35 +000075 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
Alexandre Rames8158f282015-08-07 10:26:17 +010089 bool IsFatal() const OVERRIDE { return true; }
90
Alexandre Rames9931f312015-06-19 14:47:01 +010091 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
92
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
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000101 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
Alexandre Rames8158f282015-08-07 10:26:17 +0100108 bool IsFatal() const OVERRIDE { return true; }
109
Alexandre Rames9931f312015-06-19 14:47:01 +0100110 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
111
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
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000121 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100131 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
132
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
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000143 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
Alexandre Rames8158f282015-08-07 10:26:17 +0100190 bool IsFatal() const OVERRIDE { return true; }
191
Alexandre Rames9931f312015-06-19 14:47:01 +0100192 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
193
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
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000203 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100227 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
228
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
240 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
241 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
259 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
260
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
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 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_));
289 x86_codegen->InvokeRuntime(kQuickInitializeType, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000290 CheckEntrypointTypes<kQuickInitializeType, 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100311 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
312
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
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000325 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100378 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 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
392 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100405 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
406
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
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 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
446 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
447
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
474 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
475
476 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
477 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
561 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
562
563 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
564 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
727 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
728 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
846 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
847
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
886 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
887 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
912 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
913
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) {
Roland Levillain4d027112015-07-01 15:41:14 +01004528 // Note: if heap poisoning is enabled, the entry point takes cares
4529 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004530 QuickEntrypointEnum entrypoint =
4531 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4532 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004533 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004534 DCHECK(!codegen_->IsLeafMethod());
4535}
4536
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004537void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004539 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004540 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4541 if (location.IsStackSlot()) {
4542 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4543 } else if (location.IsDoubleStackSlot()) {
4544 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004545 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004546 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004547}
4548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004549void InstructionCodeGeneratorX86::VisitParameterValue(
4550 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4551}
4552
4553void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4554 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004555 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004556 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4557}
4558
4559void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004560}
4561
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004562void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4563 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004564 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004565 locations->SetInAt(0, Location::RequiresRegister());
4566 locations->SetOut(Location::RequiresRegister());
4567}
4568
4569void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4570 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004571 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004572 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004573 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004574 __ movl(locations->Out().AsRegister<Register>(),
4575 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004576 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004577 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004578 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004579 __ movl(locations->Out().AsRegister<Register>(),
4580 Address(locations->InAt(0).AsRegister<Register>(),
4581 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4582 // temp = temp->GetImtEntryAt(method_offset);
4583 __ movl(locations->Out().AsRegister<Register>(),
4584 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004585 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004586}
4587
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004588void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004589 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004590 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004591 locations->SetInAt(0, Location::RequiresRegister());
4592 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004593}
4594
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004595void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4596 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004597 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004598 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004599 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004600 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004601 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004602 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004603 break;
4604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004605 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004606 __ notl(out.AsRegisterPairLow<Register>());
4607 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004608 break;
4609
4610 default:
4611 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4612 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004613}
4614
David Brazdil66d126e2015-04-03 16:02:44 +01004615void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4616 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004617 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004618 locations->SetInAt(0, Location::RequiresRegister());
4619 locations->SetOut(Location::SameAsFirstInput());
4620}
4621
4622void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004623 LocationSummary* locations = bool_not->GetLocations();
4624 Location in = locations->InAt(0);
4625 Location out = locations->Out();
4626 DCHECK(in.Equals(out));
4627 __ xorl(out.AsRegister<Register>(), Immediate(1));
4628}
4629
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004630void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004631 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004632 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004633 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004634 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004635 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004636 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004638 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 case DataType::Type::kInt32:
4640 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004641 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004642 locations->SetInAt(1, Location::Any());
4643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4644 break;
4645 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004646 case DataType::Type::kFloat32:
4647 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004648 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004649 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4650 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4651 } else if (compare->InputAt(1)->IsConstant()) {
4652 locations->SetInAt(1, Location::RequiresFpuRegister());
4653 } else {
4654 locations->SetInAt(1, Location::Any());
4655 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004656 locations->SetOut(Location::RequiresRegister());
4657 break;
4658 }
4659 default:
4660 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4661 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004662}
4663
4664void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004665 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004667 Location left = locations->InAt(0);
4668 Location right = locations->InAt(1);
4669
Mark Mendell0c9497d2015-08-21 09:30:05 -04004670 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004671 Condition less_cond = kLess;
4672
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004673 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004675 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004676 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004678 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004679 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004680 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004681 break;
4682 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004683 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004684 Register left_low = left.AsRegisterPairLow<Register>();
4685 Register left_high = left.AsRegisterPairHigh<Register>();
4686 int32_t val_low = 0;
4687 int32_t val_high = 0;
4688 bool right_is_const = false;
4689
4690 if (right.IsConstant()) {
4691 DCHECK(right.GetConstant()->IsLongConstant());
4692 right_is_const = true;
4693 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4694 val_low = Low32Bits(val);
4695 val_high = High32Bits(val);
4696 }
4697
Calin Juravleddb7df22014-11-25 20:56:51 +00004698 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004699 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004700 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004701 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004702 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004703 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004704 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004705 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004706 __ j(kLess, &less); // Signed compare.
4707 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004708 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004709 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004710 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004711 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004712 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004713 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004714 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004715 }
Aart Bika19616e2016-02-01 18:57:58 -08004716 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004717 break;
4718 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004720 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004721 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004722 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004723 break;
4724 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004726 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004727 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004728 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004729 break;
4730 }
4731 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004732 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004733 }
Aart Bika19616e2016-02-01 18:57:58 -08004734
Calin Juravleddb7df22014-11-25 20:56:51 +00004735 __ movl(out, Immediate(0));
4736 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004737 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004738
4739 __ Bind(&greater);
4740 __ movl(out, Immediate(1));
4741 __ jmp(&done);
4742
4743 __ Bind(&less);
4744 __ movl(out, Immediate(-1));
4745
4746 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004747}
4748
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004749void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004750 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004751 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004752 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004753 locations->SetInAt(i, Location::Any());
4754 }
4755 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004756}
4757
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004758void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004759 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004760}
4761
Roland Levillain7c1559a2015-12-15 10:55:36 +00004762void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004763 /*
4764 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4765 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4766 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4767 */
4768 switch (kind) {
4769 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004770 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004771 break;
4772 }
4773 case MemBarrierKind::kAnyStore:
4774 case MemBarrierKind::kLoadAny:
4775 case MemBarrierKind::kStoreStore: {
4776 // nop
4777 break;
4778 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004779 case MemBarrierKind::kNTStoreStore:
4780 // Non-Temporal Store/Store needs an explicit fence.
4781 MemoryFence(/* non-temporal */ true);
4782 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004783 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784}
4785
Vladimir Markodc151b22015-10-15 18:02:30 +01004786HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4787 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004788 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004789 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004790}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004791
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004792Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4793 Register temp) {
4794 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004795 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004796 if (!invoke->GetLocations()->Intrinsified()) {
4797 return location.AsRegister<Register>();
4798 }
4799 // For intrinsics we allow any location, so it may be on the stack.
4800 if (!location.IsRegister()) {
4801 __ movl(temp, Address(ESP, location.GetStackIndex()));
4802 return temp;
4803 }
4804 // For register locations, check if the register was saved. If so, get it from the stack.
4805 // Note: There is a chance that the register was saved but not overwritten, so we could
4806 // save one load. However, since this is just an intrinsic slow path we prefer this
4807 // simple and more robust approach rather that trying to determine if that's the case.
4808 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004809 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4810 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4811 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4812 __ movl(temp, Address(ESP, stack_offset));
4813 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004814 }
4815 return location.AsRegister<Register>();
4816}
4817
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004818void CodeGeneratorX86::GenerateStaticOrDirectCall(
4819 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004820 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4821 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004822 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004823 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004824 uint32_t offset =
4825 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4826 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004827 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004828 }
Vladimir Marko58155012015-08-19 12:49:41 +00004829 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004830 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004831 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004832 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4833 DCHECK(GetCompilerOptions().IsBootImage());
4834 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4835 temp.AsRegister<Register>());
4836 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004837 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004838 break;
4839 }
Vladimir Markob066d432018-01-03 13:14:37 +00004840 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4841 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4842 temp.AsRegister<Register>());
4843 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4844 RecordBootImageRelRoPatch(
4845 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004846 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004847 break;
4848 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004849 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004850 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4851 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004852 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004853 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004854 break;
4855 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004856 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4857 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4858 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004859 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4860 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4861 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004862 }
Vladimir Marko58155012015-08-19 12:49:41 +00004863 }
4864
4865 switch (invoke->GetCodePtrLocation()) {
4866 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4867 __ call(GetFrameEntryLabel());
4868 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004869 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4870 // (callee_method + offset_of_quick_compiled_code)()
4871 __ call(Address(callee_method.AsRegister<Register>(),
4872 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004873 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004874 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004875 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004876 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004877
4878 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004879}
4880
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004881void CodeGeneratorX86::GenerateVirtualCall(
4882 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004883 Register temp = temp_in.AsRegister<Register>();
4884 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4885 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004886
4887 // Use the calling convention instead of the location of the receiver, as
4888 // intrinsics may have put the receiver in a different register. In the intrinsics
4889 // slow path, the arguments have been moved to the right place, so here we are
4890 // guaranteed that the receiver is the first register of the calling convention.
4891 InvokeDexCallingConvention calling_convention;
4892 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004893 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004894 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004895 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004896 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004897 // Instead of simply (possibly) unpoisoning `temp` here, we should
4898 // emit a read barrier for the previous class reference load.
4899 // However this is not required in practice, as this is an
4900 // intermediate/temporary reference and because the current
4901 // concurrent copying collector keeps the from-space memory
4902 // intact/accessible until the end of the marking phase (the
4903 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004904 __ MaybeUnpoisonHeapReference(temp);
4905 // temp = temp->GetMethodAt(method_offset);
4906 __ movl(temp, Address(temp, method_offset));
4907 // call temp->GetEntryPoint();
4908 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004909 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004910 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004911}
4912
Vladimir Marko6fd16062018-06-26 11:02:04 +01004913void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
4914 uint32_t intrinsic_data) {
4915 boot_image_intrinsic_patches_.emplace_back(
4916 method_address, /* target_dex_file */ nullptr, intrinsic_data);
4917 __ Bind(&boot_image_intrinsic_patches_.back().label);
4918}
4919
Vladimir Markob066d432018-01-03 13:14:37 +00004920void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4921 uint32_t boot_image_offset) {
4922 boot_image_method_patches_.emplace_back(
4923 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4924 __ Bind(&boot_image_method_patches_.back().label);
4925}
4926
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004927void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004928 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004929 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004930 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004931 boot_image_method_patches_.emplace_back(
4932 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004933 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004934}
4935
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004936void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4937 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4938 HX86ComputeBaseMethodAddress* method_address =
4939 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004940 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004941 method_bss_entry_patches_.emplace_back(
4942 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4943 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004944}
4945
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004946void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4947 HX86ComputeBaseMethodAddress* method_address =
4948 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4949 boot_image_type_patches_.emplace_back(
4950 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004951 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004952}
4953
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004954Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004955 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004956 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4957 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004958 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004959 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004960}
4961
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004962void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4963 HX86ComputeBaseMethodAddress* method_address =
4964 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4965 boot_image_string_patches_.emplace_back(
4966 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4967 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004968}
4969
Vladimir Markoaad75c62016-10-03 08:46:48 +00004970Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4971 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004972 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004973 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004974 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004975 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004976 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004977}
4978
Vladimir Markoeebb8212018-06-05 14:57:24 +01004979void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004980 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01004981 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004982 if (GetCompilerOptions().IsBootImage()) {
4983 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4984 HX86ComputeBaseMethodAddress* method_address =
4985 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4986 DCHECK(method_address != nullptr);
4987 Register method_address_reg =
4988 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4989 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
4990 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004991 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004992 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4993 HX86ComputeBaseMethodAddress* method_address =
4994 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4995 DCHECK(method_address != nullptr);
4996 Register method_address_reg =
4997 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4998 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01004999 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005000 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005001 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005002 gc::Heap* heap = Runtime::Current()->GetHeap();
5003 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005004 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005005 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5006 }
5007}
5008
Vladimir Marko6fd16062018-06-26 11:02:04 +01005009void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5010 uint32_t boot_image_offset) {
5011 DCHECK(invoke->IsStatic());
5012 InvokeRuntimeCallingConvention calling_convention;
5013 Register argument = calling_convention.GetRegisterAt(0);
5014 if (GetCompilerOptions().IsBootImage()) {
5015 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5016 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5017 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5018 HX86ComputeBaseMethodAddress* method_address =
5019 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5020 DCHECK(method_address != nullptr);
5021 Register method_address_reg =
5022 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5023 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5024 MethodReference target_method = invoke->GetTargetMethod();
5025 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5026 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5027 __ Bind(&boot_image_type_patches_.back().label);
5028 } else {
5029 LoadBootImageAddress(argument, boot_image_offset, invoke);
5030 }
5031 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5032 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5033}
5034
Vladimir Markoaad75c62016-10-03 08:46:48 +00005035// The label points to the end of the "movl" or another instruction but the literal offset
5036// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5037constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5038
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005039template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005040inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005041 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005042 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005043 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005044 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005045 linker_patches->push_back(Factory(literal_offset,
5046 info.target_dex_file,
5047 GetMethodAddressOffset(info.method_address),
5048 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005049 }
5050}
5051
Vladimir Marko6fd16062018-06-26 11:02:04 +01005052template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5053linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5054 const DexFile* target_dex_file,
5055 uint32_t pc_insn_offset,
5056 uint32_t boot_image_offset) {
5057 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5058 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005059}
5060
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005061void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005062 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005063 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005064 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005065 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005066 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005067 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005068 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005069 string_bss_entry_patches_.size() +
5070 boot_image_intrinsic_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005071 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005072 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005073 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5074 boot_image_method_patches_, linker_patches);
5075 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5076 boot_image_type_patches_, linker_patches);
5077 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005078 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005079 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5080 boot_image_intrinsic_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005081 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005082 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00005083 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005084 DCHECK(boot_image_type_patches_.empty());
5085 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005086 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005087 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005088 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5089 method_bss_entry_patches_, linker_patches);
5090 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5091 type_bss_entry_patches_, linker_patches);
5092 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5093 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005094 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005095}
5096
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005097void CodeGeneratorX86::MarkGCCard(Register temp,
5098 Register card,
5099 Register object,
5100 Register value,
5101 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005102 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005103 if (value_can_be_null) {
5104 __ testl(value, value);
5105 __ j(kEqual, &is_null);
5106 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005107 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 __ movl(temp, object);
5109 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005110 __ movb(Address(temp, card, TIMES_1, 0),
5111 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005112 if (value_can_be_null) {
5113 __ Bind(&is_null);
5114 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115}
5116
Calin Juravle52c48962014-12-16 17:02:57 +00005117void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5118 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005119
5120 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005122 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005123 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5124 kEmitCompilerReadBarrier
5125 ? LocationSummary::kCallOnSlowPath
5126 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005127 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005128 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005129 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005130 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005132 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005133 locations->SetOut(Location::RequiresFpuRegister());
5134 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005135 // The output overlaps in case of long: we don't want the low move
5136 // to overwrite the object's location. Likewise, in the case of
5137 // an object field get with read barriers enabled, we do not want
5138 // the move to overwrite the object's location, as we need it to emit
5139 // the read barrier.
5140 locations->SetOut(
5141 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005142 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005143 Location::kOutputOverlap :
5144 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005145 }
Calin Juravle52c48962014-12-16 17:02:57 +00005146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005147 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005148 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005149 // So we use an XMM register as a temp to achieve atomicity (first
5150 // load the temp into the XMM and then copy the XMM into the
5151 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005152 locations->AddTemp(Location::RequiresFpuRegister());
5153 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154}
5155
Calin Juravle52c48962014-12-16 17:02:57 +00005156void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5157 const FieldInfo& field_info) {
5158 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005159
Calin Juravle52c48962014-12-16 17:02:57 +00005160 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005161 Location base_loc = locations->InAt(0);
5162 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005163 Location out = locations->Out();
5164 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005165 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5166 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005167 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5168
Vladimir Marko61b92282017-10-11 13:23:17 +01005169 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005170 case DataType::Type::kBool:
5171 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005172 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005173 break;
5174 }
5175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005177 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005178 break;
5179 }
5180
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005181 case DataType::Type::kUint16: {
5182 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183 break;
5184 }
5185
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005186 case DataType::Type::kInt16: {
5187 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005188 break;
5189 }
5190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005191 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005192 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005193 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005195 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005196 // /* HeapReference<Object> */ out = *(base + offset)
5197 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005198 // Note that a potential implicit null check is handled in this
5199 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5200 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005201 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005202 if (is_volatile) {
5203 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5204 }
5205 } else {
5206 __ movl(out.AsRegister<Register>(), Address(base, offset));
5207 codegen_->MaybeRecordImplicitNullCheck(instruction);
5208 if (is_volatile) {
5209 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5210 }
5211 // If read barriers are enabled, emit read barriers other than
5212 // Baker's using a slow path (and also unpoison the loaded
5213 // reference, if heap poisoning is enabled).
5214 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5215 }
5216 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217 }
5218
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005219 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005220 if (is_volatile) {
5221 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5222 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005223 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005224 __ movd(out.AsRegisterPairLow<Register>(), temp);
5225 __ psrlq(temp, Immediate(32));
5226 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5227 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005228 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005229 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005230 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005231 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5232 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005233 break;
5234 }
5235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005237 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005238 break;
5239 }
5240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005241 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005242 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005243 break;
5244 }
5245
Aart Bik66c158e2018-01-31 12:55:04 -08005246 case DataType::Type::kUint32:
5247 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005249 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005250 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005251 }
Calin Juravle52c48962014-12-16 17:02:57 +00005252
Vladimir Marko61b92282017-10-11 13:23:17 +01005253 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005254 // Potential implicit null checks, in the case of reference or
5255 // long fields, are handled in the previous switch statement.
5256 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005257 codegen_->MaybeRecordImplicitNullCheck(instruction);
5258 }
5259
Calin Juravle52c48962014-12-16 17:02:57 +00005260 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005261 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 // Memory barriers, in the case of references, are also handled
5263 // in the previous switch statement.
5264 } else {
5265 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5266 }
Roland Levillain4d027112015-07-01 15:41:14 +01005267 }
Calin Juravle52c48962014-12-16 17:02:57 +00005268}
5269
5270void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5271 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5272
5273 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005274 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005275 locations->SetInAt(0, Location::RequiresRegister());
5276 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005278 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005279
5280 // The register allocator does not support multiple
5281 // inputs that die at entry with one in a specific register.
5282 if (is_byte_type) {
5283 // Ensure the value is in a byte register.
5284 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005285 } else if (DataType::IsFloatingPointType(field_type)) {
5286 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005287 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5288 locations->SetInAt(1, Location::RequiresFpuRegister());
5289 } else {
5290 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5291 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005292 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005293 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005294 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005295
Calin Juravle52c48962014-12-16 17:02:57 +00005296 // 64bits value can be atomically written to an address with movsd and an XMM register.
5297 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5298 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5299 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5300 // isolated cases when we need this it isn't worth adding the extra complexity.
5301 locations->AddTemp(Location::RequiresFpuRegister());
5302 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005303 } else {
5304 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5305
5306 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5307 // Temporary registers for the write barrier.
5308 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5309 // Ensure the card is in a byte register.
5310 locations->AddTemp(Location::RegisterLocation(ECX));
5311 }
Calin Juravle52c48962014-12-16 17:02:57 +00005312 }
5313}
5314
5315void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005316 const FieldInfo& field_info,
5317 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005318 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5319
5320 LocationSummary* locations = instruction->GetLocations();
5321 Register base = locations->InAt(0).AsRegister<Register>();
5322 Location value = locations->InAt(1);
5323 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005324 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005325 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005326 bool needs_write_barrier =
5327 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005328
5329 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005330 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005331 }
5332
Mark Mendell81489372015-11-04 11:30:41 -05005333 bool maybe_record_implicit_null_check_done = false;
5334
Calin Juravle52c48962014-12-16 17:02:57 +00005335 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005336 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005337 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005338 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005339 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5340 break;
5341 }
5342
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005343 case DataType::Type::kUint16:
5344 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005345 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005346 __ movw(Address(base, offset),
5347 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005348 } else {
5349 __ movw(Address(base, offset), value.AsRegister<Register>());
5350 }
Calin Juravle52c48962014-12-16 17:02:57 +00005351 break;
5352 }
5353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005354 case DataType::Type::kInt32:
5355 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005356 if (kPoisonHeapReferences && needs_write_barrier) {
5357 // Note that in the case where `value` is a null reference,
5358 // we do not enter this block, as the reference does not
5359 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005360 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005361 Register temp = locations->GetTemp(0).AsRegister<Register>();
5362 __ movl(temp, value.AsRegister<Register>());
5363 __ PoisonHeapReference(temp);
5364 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005365 } else if (value.IsConstant()) {
5366 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5367 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005368 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005369 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005370 __ movl(Address(base, offset), value.AsRegister<Register>());
5371 }
Calin Juravle52c48962014-12-16 17:02:57 +00005372 break;
5373 }
5374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005375 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005376 if (is_volatile) {
5377 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5378 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5379 __ movd(temp1, value.AsRegisterPairLow<Register>());
5380 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5381 __ punpckldq(temp1, temp2);
5382 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005384 } else if (value.IsConstant()) {
5385 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5386 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5387 codegen_->MaybeRecordImplicitNullCheck(instruction);
5388 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005389 } else {
5390 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005391 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005392 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5393 }
Mark Mendell81489372015-11-04 11:30:41 -05005394 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005395 break;
5396 }
5397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005399 if (value.IsConstant()) {
5400 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5401 __ movl(Address(base, offset), Immediate(v));
5402 } else {
5403 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5404 }
Calin Juravle52c48962014-12-16 17:02:57 +00005405 break;
5406 }
5407
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005408 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005409 if (value.IsConstant()) {
5410 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5411 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5412 codegen_->MaybeRecordImplicitNullCheck(instruction);
5413 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5414 maybe_record_implicit_null_check_done = true;
5415 } else {
5416 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5417 }
Calin Juravle52c48962014-12-16 17:02:57 +00005418 break;
5419 }
5420
Aart Bik66c158e2018-01-31 12:55:04 -08005421 case DataType::Type::kUint32:
5422 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005424 LOG(FATAL) << "Unreachable type " << field_type;
5425 UNREACHABLE();
5426 }
5427
Mark Mendell81489372015-11-04 11:30:41 -05005428 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005429 codegen_->MaybeRecordImplicitNullCheck(instruction);
5430 }
5431
Roland Levillain4d027112015-07-01 15:41:14 +01005432 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005433 Register temp = locations->GetTemp(0).AsRegister<Register>();
5434 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005435 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005436 }
5437
Calin Juravle52c48962014-12-16 17:02:57 +00005438 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005439 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005440 }
5441}
5442
5443void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5444 HandleFieldGet(instruction, instruction->GetFieldInfo());
5445}
5446
5447void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5448 HandleFieldGet(instruction, instruction->GetFieldInfo());
5449}
5450
5451void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5452 HandleFieldSet(instruction, instruction->GetFieldInfo());
5453}
5454
5455void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005456 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005457}
5458
5459void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5460 HandleFieldSet(instruction, instruction->GetFieldInfo());
5461}
5462
5463void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005464 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005465}
5466
5467void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5468 HandleFieldGet(instruction, instruction->GetFieldInfo());
5469}
5470
5471void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5472 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005473}
5474
Calin Juravlee460d1d2015-09-29 04:52:17 +01005475void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5476 HUnresolvedInstanceFieldGet* instruction) {
5477 FieldAccessCallingConventionX86 calling_convention;
5478 codegen_->CreateUnresolvedFieldLocationSummary(
5479 instruction, instruction->GetFieldType(), calling_convention);
5480}
5481
5482void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5483 HUnresolvedInstanceFieldGet* instruction) {
5484 FieldAccessCallingConventionX86 calling_convention;
5485 codegen_->GenerateUnresolvedFieldAccess(instruction,
5486 instruction->GetFieldType(),
5487 instruction->GetFieldIndex(),
5488 instruction->GetDexPc(),
5489 calling_convention);
5490}
5491
5492void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5493 HUnresolvedInstanceFieldSet* instruction) {
5494 FieldAccessCallingConventionX86 calling_convention;
5495 codegen_->CreateUnresolvedFieldLocationSummary(
5496 instruction, instruction->GetFieldType(), calling_convention);
5497}
5498
5499void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5500 HUnresolvedInstanceFieldSet* instruction) {
5501 FieldAccessCallingConventionX86 calling_convention;
5502 codegen_->GenerateUnresolvedFieldAccess(instruction,
5503 instruction->GetFieldType(),
5504 instruction->GetFieldIndex(),
5505 instruction->GetDexPc(),
5506 calling_convention);
5507}
5508
5509void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5510 HUnresolvedStaticFieldGet* instruction) {
5511 FieldAccessCallingConventionX86 calling_convention;
5512 codegen_->CreateUnresolvedFieldLocationSummary(
5513 instruction, instruction->GetFieldType(), calling_convention);
5514}
5515
5516void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5517 HUnresolvedStaticFieldGet* instruction) {
5518 FieldAccessCallingConventionX86 calling_convention;
5519 codegen_->GenerateUnresolvedFieldAccess(instruction,
5520 instruction->GetFieldType(),
5521 instruction->GetFieldIndex(),
5522 instruction->GetDexPc(),
5523 calling_convention);
5524}
5525
5526void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5527 HUnresolvedStaticFieldSet* instruction) {
5528 FieldAccessCallingConventionX86 calling_convention;
5529 codegen_->CreateUnresolvedFieldLocationSummary(
5530 instruction, instruction->GetFieldType(), calling_convention);
5531}
5532
5533void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5534 HUnresolvedStaticFieldSet* instruction) {
5535 FieldAccessCallingConventionX86 calling_convention;
5536 codegen_->GenerateUnresolvedFieldAccess(instruction,
5537 instruction->GetFieldType(),
5538 instruction->GetFieldIndex(),
5539 instruction->GetDexPc(),
5540 calling_convention);
5541}
5542
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005543void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005544 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5545 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5546 ? Location::RequiresRegister()
5547 : Location::Any();
5548 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005549}
5550
Calin Juravle2ae48182016-03-16 14:05:09 +00005551void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5552 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005553 return;
5554 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005555 LocationSummary* locations = instruction->GetLocations();
5556 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005557
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005558 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005559 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005560}
5561
Calin Juravle2ae48182016-03-16 14:05:09 +00005562void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005563 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005564 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005565
5566 LocationSummary* locations = instruction->GetLocations();
5567 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005568
5569 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005570 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005571 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005572 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005573 } else {
5574 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005575 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005576 __ jmp(slow_path->GetEntryLabel());
5577 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005578 }
5579 __ j(kEqual, slow_path->GetEntryLabel());
5580}
5581
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005582void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005583 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005584}
5585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005587 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005589 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005590 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5591 object_array_get_with_read_barrier
5592 ? LocationSummary::kCallOnSlowPath
5593 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005594 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005595 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005596 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005597 locations->SetInAt(0, Location::RequiresRegister());
5598 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005599 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005600 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5601 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005602 // The output overlaps in case of long: we don't want the low move
5603 // to overwrite the array's location. Likewise, in the case of an
5604 // object array get with read barriers enabled, we do not want the
5605 // move to overwrite the array's location, as we need it to emit
5606 // the read barrier.
5607 locations->SetOut(
5608 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005609 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5610 ? Location::kOutputOverlap
5611 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005612 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613}
5614
5615void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5616 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005617 Location obj_loc = locations->InAt(0);
5618 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005619 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005620 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005621 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005623 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005624 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005625 case DataType::Type::kBool:
5626 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005627 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005628 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005629 break;
5630 }
5631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005632 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005633 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005634 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635 break;
5636 }
5637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005639 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005640 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5641 // Branch cases into compressed and uncompressed for each index's type.
5642 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5643 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005644 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005645 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005646 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5647 "Expecting 0=compressed, 1=uncompressed");
5648 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005649 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5650 __ jmp(&done);
5651 __ Bind(&not_compressed);
5652 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5653 __ Bind(&done);
5654 } else {
5655 // Common case for charAt of array of char or when string compression's
5656 // feature is turned off.
5657 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5658 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005659 break;
5660 }
5661
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005662 case DataType::Type::kInt16: {
5663 Register out = out_loc.AsRegister<Register>();
5664 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5665 break;
5666 }
5667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005668 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005669 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005670 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005671 break;
5672 }
5673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005675 static_assert(
5676 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5677 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005678 // /* HeapReference<Object> */ out =
5679 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5680 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005681 // Note that a potential implicit null check is handled in this
5682 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5683 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005684 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005685 } else {
5686 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005687 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5688 codegen_->MaybeRecordImplicitNullCheck(instruction);
5689 // If read barriers are enabled, emit read barriers other than
5690 // Baker's using a slow path (and also unpoison the loaded
5691 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005692 if (index.IsConstant()) {
5693 uint32_t offset =
5694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005695 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5696 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005697 codegen_->MaybeGenerateReadBarrierSlow(
5698 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5699 }
5700 }
5701 break;
5702 }
5703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005704 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005705 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005706 __ movl(out_loc.AsRegisterPairLow<Register>(),
5707 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5708 codegen_->MaybeRecordImplicitNullCheck(instruction);
5709 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5710 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005711 break;
5712 }
5713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005714 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005715 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005716 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005717 break;
5718 }
5719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005721 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005722 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005723 break;
5724 }
5725
Aart Bik66c158e2018-01-31 12:55:04 -08005726 case DataType::Type::kUint32:
5727 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005729 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005730 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005731 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005733 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005734 // Potential implicit null checks, in the case of reference or
5735 // long arrays, are handled in the previous switch statement.
5736 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005737 codegen_->MaybeRecordImplicitNullCheck(instruction);
5738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005739}
5740
5741void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005744 bool needs_write_barrier =
5745 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005747
Vladimir Markoca6fff82017-10-03 14:49:14 +01005748 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005749 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005750 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005751 LocationSummary::kCallOnSlowPath :
5752 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005753
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005754 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005755 // We need the inputs to be different than the output in case of long operation.
5756 // In case of a byte operation, the register allocator does not support multiple
5757 // inputs that die at entry with one in a specific register.
5758 locations->SetInAt(0, Location::RequiresRegister());
5759 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5760 if (is_byte_type) {
5761 // Ensure the value is in a byte register.
5762 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005763 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005764 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005765 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005766 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5767 }
5768 if (needs_write_barrier) {
5769 // Temporary registers for the write barrier.
5770 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5771 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005772 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005773 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005774}
5775
5776void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5777 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778 Location array_loc = locations->InAt(0);
5779 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005780 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005781 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005782 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005783 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5784 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5785 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005787 bool needs_write_barrier =
5788 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005789
5790 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005792 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005793 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005794 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005795 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005796 if (value.IsRegister()) {
5797 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005798 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005799 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005800 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005801 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005802 break;
5803 }
5804
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005805 case DataType::Type::kUint16:
5806 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005807 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005808 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005809 if (value.IsRegister()) {
5810 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005811 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005812 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005813 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005814 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005815 break;
5816 }
5817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005818 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005819 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005820 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005822 if (!value.IsRegister()) {
5823 // Just setting null.
5824 DCHECK(instruction->InputAt(2)->IsNullConstant());
5825 DCHECK(value.IsConstant()) << value;
5826 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005827 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005828 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005829 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005830 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005831 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005832
5833 DCHECK(needs_write_barrier);
5834 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005835 // We cannot use a NearLabel for `done`, as its range may be too
5836 // short when Baker read barriers are enabled.
5837 Label done;
5838 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005839 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005840 Location temp_loc = locations->GetTemp(0);
5841 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005842 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005843 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005844 codegen_->AddSlowPath(slow_path);
5845 if (instruction->GetValueCanBeNull()) {
5846 __ testl(register_value, register_value);
5847 __ j(kNotEqual, &not_null);
5848 __ movl(address, Immediate(0));
5849 codegen_->MaybeRecordImplicitNullCheck(instruction);
5850 __ jmp(&done);
5851 __ Bind(&not_null);
5852 }
5853
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005854 // Note that when Baker read barriers are enabled, the type
5855 // checks are performed without read barriers. This is fine,
5856 // even in the case where a class object is in the from-space
5857 // after the flip, as a comparison involving such a type would
5858 // not produce a false positive; it may of course produce a
5859 // false negative, in which case we would take the ArraySet
5860 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005861
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005862 // /* HeapReference<Class> */ temp = array->klass_
5863 __ movl(temp, Address(array, class_offset));
5864 codegen_->MaybeRecordImplicitNullCheck(instruction);
5865 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005866
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005867 // /* HeapReference<Class> */ temp = temp->component_type_
5868 __ movl(temp, Address(temp, component_offset));
5869 // If heap poisoning is enabled, no need to unpoison `temp`
5870 // nor the object reference in `register_value->klass`, as
5871 // we are comparing two poisoned references.
5872 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005873
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005874 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5875 __ j(kEqual, &do_put);
5876 // If heap poisoning is enabled, the `temp` reference has
5877 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878 __ MaybeUnpoisonHeapReference(temp);
5879
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005880 // If heap poisoning is enabled, no need to unpoison the
5881 // heap reference loaded below, as it is only used for a
5882 // comparison with null.
5883 __ cmpl(Address(temp, super_offset), Immediate(0));
5884 __ j(kNotEqual, slow_path->GetEntryLabel());
5885 __ Bind(&do_put);
5886 } else {
5887 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005888 }
5889 }
5890
5891 if (kPoisonHeapReferences) {
5892 __ movl(temp, register_value);
5893 __ PoisonHeapReference(temp);
5894 __ movl(address, temp);
5895 } else {
5896 __ movl(address, register_value);
5897 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005898 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005899 codegen_->MaybeRecordImplicitNullCheck(instruction);
5900 }
5901
5902 Register card = locations->GetTemp(1).AsRegister<Register>();
5903 codegen_->MarkGCCard(
5904 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5905 __ Bind(&done);
5906
5907 if (slow_path != nullptr) {
5908 __ Bind(slow_path->GetExitLabel());
5909 }
5910
5911 break;
5912 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005915 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005916 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005917 if (value.IsRegister()) {
5918 __ movl(address, value.AsRegister<Register>());
5919 } else {
5920 DCHECK(value.IsConstant()) << value;
5921 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5922 __ movl(address, Immediate(v));
5923 }
5924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005925 break;
5926 }
5927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005930 if (value.IsRegisterPair()) {
5931 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5932 value.AsRegisterPairLow<Register>());
5933 codegen_->MaybeRecordImplicitNullCheck(instruction);
5934 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5935 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005937 DCHECK(value.IsConstant());
5938 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5939 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5940 Immediate(Low32Bits(val)));
5941 codegen_->MaybeRecordImplicitNullCheck(instruction);
5942 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5943 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005944 }
5945 break;
5946 }
5947
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005949 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005950 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005951 if (value.IsFpuRegister()) {
5952 __ movss(address, value.AsFpuRegister<XmmRegister>());
5953 } else {
5954 DCHECK(value.IsConstant());
5955 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5956 __ movl(address, Immediate(v));
5957 }
5958 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005959 break;
5960 }
5961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005963 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005964 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005965 if (value.IsFpuRegister()) {
5966 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5967 } else {
5968 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005969 Address address_hi =
5970 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005971 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5972 __ movl(address, Immediate(Low32Bits(v)));
5973 codegen_->MaybeRecordImplicitNullCheck(instruction);
5974 __ movl(address_hi, Immediate(High32Bits(v)));
5975 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005976 break;
5977 }
5978
Aart Bik66c158e2018-01-31 12:55:04 -08005979 case DataType::Type::kUint32:
5980 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005983 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005984 }
5985}
5986
5987void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005988 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005989 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005990 if (!instruction->IsEmittedAtUseSite()) {
5991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5992 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005993}
5994
5995void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005996 if (instruction->IsEmittedAtUseSite()) {
5997 return;
5998 }
5999
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006000 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006001 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006002 Register obj = locations->InAt(0).AsRegister<Register>();
6003 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006005 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006006 // Mask out most significant bit in case the array is String's array of char.
6007 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006008 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006009 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006010}
6011
6012void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006013 RegisterSet caller_saves = RegisterSet::Empty();
6014 InvokeRuntimeCallingConvention calling_convention;
6015 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6016 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6017 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006018 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006019 HInstruction* length = instruction->InputAt(1);
6020 if (!length->IsEmittedAtUseSite()) {
6021 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6022 }
jessicahandojo4877b792016-09-08 19:49:13 -07006023 // Need register to see array's length.
6024 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6025 locations->AddTemp(Location::RequiresRegister());
6026 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006027}
6028
6029void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006030 const bool is_string_compressed_char_at =
6031 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006032 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006033 Location index_loc = locations->InAt(0);
6034 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006035 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006036 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006037
Mark Mendell99dbd682015-04-22 16:18:52 -04006038 if (length_loc.IsConstant()) {
6039 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6040 if (index_loc.IsConstant()) {
6041 // BCE will remove the bounds check if we are guarenteed to pass.
6042 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6043 if (index < 0 || index >= length) {
6044 codegen_->AddSlowPath(slow_path);
6045 __ jmp(slow_path->GetEntryLabel());
6046 } else {
6047 // Some optimization after BCE may have generated this, and we should not
6048 // generate a bounds check if it is a valid range.
6049 }
6050 return;
6051 }
6052
6053 // We have to reverse the jump condition because the length is the constant.
6054 Register index_reg = index_loc.AsRegister<Register>();
6055 __ cmpl(index_reg, Immediate(length));
6056 codegen_->AddSlowPath(slow_path);
6057 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006058 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006059 HInstruction* array_length = instruction->InputAt(1);
6060 if (array_length->IsEmittedAtUseSite()) {
6061 // Address the length field in the array.
6062 DCHECK(array_length->IsArrayLength());
6063 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6064 Location array_loc = array_length->GetLocations()->InAt(0);
6065 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006066 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006067 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6068 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006069 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6070 __ movl(length_reg, array_len);
6071 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006072 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006073 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006074 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006075 // Checking bounds for general case:
6076 // Array of char or string's array with feature compression off.
6077 if (index_loc.IsConstant()) {
6078 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6079 __ cmpl(array_len, Immediate(value));
6080 } else {
6081 __ cmpl(array_len, index_loc.AsRegister<Register>());
6082 }
6083 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006084 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006085 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006086 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006087 }
6088 codegen_->AddSlowPath(slow_path);
6089 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006090 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006091}
6092
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006093void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006094 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006095}
6096
6097void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006098 if (instruction->GetNext()->IsSuspendCheck() &&
6099 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6100 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6101 // The back edge will generate the suspend check.
6102 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6103 }
6104
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006105 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6106}
6107
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006108void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006109 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6110 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006111 // In suspend check slow path, usually there are no caller-save registers at all.
6112 // If SIMD instructions are present, however, we force spilling all live SIMD
6113 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006114 locations->SetCustomSlowPathCallerSaves(
6115 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006116}
6117
6118void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006119 HBasicBlock* block = instruction->GetBlock();
6120 if (block->GetLoopInformation() != nullptr) {
6121 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6122 // The back edge will generate the suspend check.
6123 return;
6124 }
6125 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6126 // The goto will generate the suspend check.
6127 return;
6128 }
6129 GenerateSuspendCheck(instruction, nullptr);
6130}
6131
6132void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6133 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006134 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006135 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6136 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006137 slow_path =
6138 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006139 instruction->SetSlowPath(slow_path);
6140 codegen_->AddSlowPath(slow_path);
6141 if (successor != nullptr) {
6142 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006143 }
6144 } else {
6145 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6146 }
6147
Andreas Gampe542451c2016-07-26 09:02:02 -07006148 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006149 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006150 if (successor == nullptr) {
6151 __ j(kNotEqual, slow_path->GetEntryLabel());
6152 __ Bind(slow_path->GetReturnLabel());
6153 } else {
6154 __ j(kEqual, codegen_->GetLabelOf(successor));
6155 __ jmp(slow_path->GetEntryLabel());
6156 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006157}
6158
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006159X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6160 return codegen_->GetAssembler();
6161}
6162
Aart Bikcfe50bb2017-12-12 14:54:12 -08006163void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006164 ScratchRegisterScope ensure_scratch(
6165 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6166 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6167 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006168
Aart Bikcfe50bb2017-12-12 14:54:12 -08006169 // Now that temp register is available (possibly spilled), move blocks of memory.
6170 for (int i = 0; i < number_of_words; i++) {
6171 __ movl(temp_reg, Address(ESP, src + stack_offset));
6172 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6173 stack_offset += kX86WordSize;
6174 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006175}
6176
6177void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006178 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006179 Location source = move->GetSource();
6180 Location destination = move->GetDestination();
6181
6182 if (source.IsRegister()) {
6183 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006184 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006185 } else if (destination.IsFpuRegister()) {
6186 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006187 } else {
6188 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006189 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006190 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006191 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006193 // Create stack space for 2 elements.
6194 __ subl(ESP, Immediate(2 * elem_size));
6195 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6196 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6197 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6198 // And remove the temporary stack space we allocated.
6199 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006200 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006201 if (destination.IsRegister()) {
6202 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6203 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006204 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006205 } else if (destination.IsRegisterPair()) {
6206 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6207 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6208 __ psrlq(src_reg, Immediate(32));
6209 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006210 } else if (destination.IsStackSlot()) {
6211 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006212 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006213 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006214 } else {
6215 DCHECK(destination.IsSIMDStackSlot());
6216 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006217 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006218 } else if (source.IsStackSlot()) {
6219 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006220 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006221 } else if (destination.IsFpuRegister()) {
6222 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006223 } else {
6224 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006225 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006226 }
6227 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006228 if (destination.IsRegisterPair()) {
6229 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6230 __ movl(destination.AsRegisterPairHigh<Register>(),
6231 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6232 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006233 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6234 } else {
6235 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006236 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006237 }
Aart Bik5576f372017-03-23 16:17:37 -07006238 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006239 if (destination.IsFpuRegister()) {
6240 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6241 } else {
6242 DCHECK(destination.IsSIMDStackSlot());
6243 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6244 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006245 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006246 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006247 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006248 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006249 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006250 if (value == 0) {
6251 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6252 } else {
6253 __ movl(destination.AsRegister<Register>(), Immediate(value));
6254 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006255 } else {
6256 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006257 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006258 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006259 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006260 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006261 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006262 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006263 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006264 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6265 if (value == 0) {
6266 // Easy handling of 0.0.
6267 __ xorps(dest, dest);
6268 } else {
6269 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006270 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6271 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6272 __ movl(temp, Immediate(value));
6273 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006274 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006275 } else {
6276 DCHECK(destination.IsStackSlot()) << destination;
6277 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6278 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006279 } else if (constant->IsLongConstant()) {
6280 int64_t value = constant->AsLongConstant()->GetValue();
6281 int32_t low_value = Low32Bits(value);
6282 int32_t high_value = High32Bits(value);
6283 Immediate low(low_value);
6284 Immediate high(high_value);
6285 if (destination.IsDoubleStackSlot()) {
6286 __ movl(Address(ESP, destination.GetStackIndex()), low);
6287 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6288 } else {
6289 __ movl(destination.AsRegisterPairLow<Register>(), low);
6290 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6291 }
6292 } else {
6293 DCHECK(constant->IsDoubleConstant());
6294 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006295 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006296 int32_t low_value = Low32Bits(value);
6297 int32_t high_value = High32Bits(value);
6298 Immediate low(low_value);
6299 Immediate high(high_value);
6300 if (destination.IsFpuRegister()) {
6301 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6302 if (value == 0) {
6303 // Easy handling of 0.0.
6304 __ xorpd(dest, dest);
6305 } else {
6306 __ pushl(high);
6307 __ pushl(low);
6308 __ movsd(dest, Address(ESP, 0));
6309 __ addl(ESP, Immediate(8));
6310 }
6311 } else {
6312 DCHECK(destination.IsDoubleStackSlot()) << destination;
6313 __ movl(Address(ESP, destination.GetStackIndex()), low);
6314 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6315 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006316 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006317 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006318 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006319 }
6320}
6321
Mark Mendella5c19ce2015-04-01 12:51:05 -04006322void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006323 Register suggested_scratch = reg == EAX ? EBX : EAX;
6324 ScratchRegisterScope ensure_scratch(
6325 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6326
6327 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6328 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6329 __ movl(Address(ESP, mem + stack_offset), reg);
6330 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006331}
6332
Mark Mendell7c8d0092015-01-26 11:21:33 -05006333void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006334 ScratchRegisterScope ensure_scratch(
6335 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6336
6337 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6338 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6339 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6340 __ movss(Address(ESP, mem + stack_offset), reg);
6341 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006342}
6343
Aart Bikcfe50bb2017-12-12 14:54:12 -08006344void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6345 size_t extra_slot = 4 * kX86WordSize;
6346 __ subl(ESP, Immediate(extra_slot));
6347 __ movups(Address(ESP, 0), XmmRegister(reg));
6348 ExchangeMemory(0, mem + extra_slot, 4);
6349 __ movups(XmmRegister(reg), Address(ESP, 0));
6350 __ addl(ESP, Immediate(extra_slot));
6351}
6352
6353void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006354 ScratchRegisterScope ensure_scratch1(
6355 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006356
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006357 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6358 ScratchRegisterScope ensure_scratch2(
6359 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006360
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006361 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6362 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006363
6364 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6365 for (int i = 0; i < number_of_words; i++) {
6366 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6367 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6368 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6369 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6370 stack_offset += kX86WordSize;
6371 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006372}
6373
6374void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006375 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006376 Location source = move->GetSource();
6377 Location destination = move->GetDestination();
6378
6379 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006380 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6381 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6382 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6383 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6384 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006385 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006386 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006387 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006388 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006389 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006390 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006391 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6392 // Use XOR Swap algorithm to avoid a temporary.
6393 DCHECK_NE(source.reg(), destination.reg());
6394 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6395 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6396 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6397 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6398 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6399 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6400 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006401 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6402 // Take advantage of the 16 bytes in the XMM register.
6403 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6404 Address stack(ESP, destination.GetStackIndex());
6405 // Load the double into the high doubleword.
6406 __ movhpd(reg, stack);
6407
6408 // Store the low double into the destination.
6409 __ movsd(stack, reg);
6410
6411 // Move the high double to the low double.
6412 __ psrldq(reg, Immediate(8));
6413 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6414 // Take advantage of the 16 bytes in the XMM register.
6415 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6416 Address stack(ESP, source.GetStackIndex());
6417 // Load the double into the high doubleword.
6418 __ movhpd(reg, stack);
6419
6420 // Store the low double into the destination.
6421 __ movsd(stack, reg);
6422
6423 // Move the high double to the low double.
6424 __ psrldq(reg, Immediate(8));
6425 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006426 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6427 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6428 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6429 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6430 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6431 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6432 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006433 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006434 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006435 }
6436}
6437
6438void ParallelMoveResolverX86::SpillScratch(int reg) {
6439 __ pushl(static_cast<Register>(reg));
6440}
6441
6442void ParallelMoveResolverX86::RestoreScratch(int reg) {
6443 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006444}
6445
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006446HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6447 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006448 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006449 case HLoadClass::LoadKind::kInvalid:
6450 LOG(FATAL) << "UNREACHABLE";
6451 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 case HLoadClass::LoadKind::kReferrersClass:
6453 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006454 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006455 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006456 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006457 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006458 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006459 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006460 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006461 DCHECK(Runtime::Current()->UseJitCompilation());
6462 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006463 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 break;
6465 }
6466 return desired_class_load_kind;
6467}
6468
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006469void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006470 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006471 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006472 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006473 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006474 cls,
6475 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006476 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006477 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006478 return;
6479 }
Vladimir Marko41559982017-01-06 14:04:23 +00006480 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006481
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006482 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6483 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006484 ? LocationSummary::kCallOnSlowPath
6485 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006486 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006487 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006488 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006489 }
6490
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006491 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006492 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006493 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006494 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006495 locations->SetInAt(0, Location::RequiresRegister());
6496 }
6497 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006498 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6499 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6500 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006501 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006502 } else {
6503 // For non-Baker read barrier we have a temp-clobbering call.
6504 }
6505 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006506}
6507
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006508Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006509 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006510 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006511 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006512 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006513 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006514 PatchInfo<Label>* info = &jit_class_patches_.back();
6515 return &info->label;
6516}
6517
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006518// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6519// move.
6520void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006521 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006522 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006523 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006524 return;
6525 }
Vladimir Marko41559982017-01-06 14:04:23 +00006526 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006527
Vladimir Marko41559982017-01-06 14:04:23 +00006528 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529 Location out_loc = locations->Out();
6530 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006532 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006533 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6534 ? kWithoutReadBarrier
6535 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006536 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006537 case HLoadClass::LoadKind::kReferrersClass: {
6538 DCHECK(!cls->CanCallRuntime());
6539 DCHECK(!cls->MustGenerateClinitCheck());
6540 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6541 Register current_method = locations->InAt(0).AsRegister<Register>();
6542 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006543 cls,
6544 out_loc,
6545 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006546 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006547 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006548 break;
6549 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006550 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006551 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006552 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006553 Register method_address = locations->InAt(0).AsRegister<Register>();
6554 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006555 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006556 break;
6557 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006558 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006559 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6560 Register method_address = locations->InAt(0).AsRegister<Register>();
6561 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006562 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6563 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006564 break;
6565 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006566 case HLoadClass::LoadKind::kBssEntry: {
6567 Register method_address = locations->InAt(0).AsRegister<Register>();
6568 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6569 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6570 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6571 generate_null_check = true;
6572 break;
6573 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006574 case HLoadClass::LoadKind::kJitBootImageAddress: {
6575 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6576 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6577 DCHECK_NE(address, 0u);
6578 __ movl(out, Immediate(address));
6579 break;
6580 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006581 case HLoadClass::LoadKind::kJitTableAddress: {
6582 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6583 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006584 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006585 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006586 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006587 break;
6588 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006589 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006590 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006591 LOG(FATAL) << "UNREACHABLE";
6592 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006593 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006594
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006595 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6596 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006597 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006598 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006599
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006600 if (generate_null_check) {
6601 __ testl(out, out);
6602 __ j(kEqual, slow_path->GetEntryLabel());
6603 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006604
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006605 if (cls->MustGenerateClinitCheck()) {
6606 GenerateClassInitializationCheck(slow_path, out);
6607 } else {
6608 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006609 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006610 }
6611}
6612
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006613void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6614 InvokeRuntimeCallingConvention calling_convention;
6615 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6616 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6617}
6618
6619void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6620 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6621}
6622
Orion Hodson18259d72018-04-12 11:18:23 +01006623void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6624 InvokeRuntimeCallingConvention calling_convention;
6625 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6626 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6627}
6628
6629void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6630 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6631}
6632
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006633void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6634 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006635 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006636 locations->SetInAt(0, Location::RequiresRegister());
6637 if (check->HasUses()) {
6638 locations->SetOut(Location::SameAsFirstInput());
6639 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006640 // Rely on the type initialization to save everything we need.
6641 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006642}
6643
6644void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006645 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006646 SlowPathCode* slow_path =
6647 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006648 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006649 GenerateClassInitializationCheck(slow_path,
6650 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006651}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006652
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006653void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006654 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006655 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6656 const size_t status_byte_offset =
6657 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6658 constexpr uint32_t shifted_initialized_value =
6659 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6660
6661 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006662 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006663 __ Bind(slow_path->GetExitLabel());
6664 // No need for memory fence, thanks to the X86 memory model.
6665}
6666
Vladimir Marko175e7862018-03-27 09:03:13 +00006667void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6668 Register temp) {
6669 uint32_t path_to_root = check->GetBitstringPathToRoot();
6670 uint32_t mask = check->GetBitstringMask();
6671 DCHECK(IsPowerOfTwo(mask + 1));
6672 size_t mask_bits = WhichPowerOf2(mask + 1);
6673
6674 if (mask_bits == 16u) {
6675 // Compare the bitstring in memory.
6676 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6677 } else {
6678 // /* uint32_t */ temp = temp->status_
6679 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6680 // Compare the bitstring bits using SUB.
6681 __ subl(temp, Immediate(path_to_root));
6682 // Shift out bits that do not contribute to the comparison.
6683 __ shll(temp, Immediate(32u - mask_bits));
6684 }
6685}
6686
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006687HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6688 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006689 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006690 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006691 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006692 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006693 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006694 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006695 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006696 case HLoadString::LoadKind::kJitTableAddress:
6697 DCHECK(Runtime::Current()->UseJitCompilation());
6698 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006699 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006700 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006701 }
6702 return desired_string_load_kind;
6703}
6704
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006705void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006706 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006708 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006709 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006710 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006711 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006712 locations->SetInAt(0, Location::RequiresRegister());
6713 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006714 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006715 locations->SetOut(Location::RegisterLocation(EAX));
6716 } else {
6717 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006718 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6719 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006720 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006721 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006722 } else {
6723 // For non-Baker read barrier we have a temp-clobbering call.
6724 }
6725 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006726 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006727}
6728
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006729Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006730 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006731 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006732 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006733 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006734 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006735 PatchInfo<Label>* info = &jit_string_patches_.back();
6736 return &info->label;
6737}
6738
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006739// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6740// move.
6741void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006742 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743 Location out_loc = locations->Out();
6744 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006745
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006746 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006747 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006748 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006749 Register method_address = locations->InAt(0).AsRegister<Register>();
6750 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006751 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006752 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006753 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006754 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006755 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6756 Register method_address = locations->InAt(0).AsRegister<Register>();
6757 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006758 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6759 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006760 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006761 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006762 case HLoadString::LoadKind::kBssEntry: {
6763 Register method_address = locations->InAt(0).AsRegister<Register>();
6764 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6765 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006766 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006767 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006768 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006769 codegen_->AddSlowPath(slow_path);
6770 __ testl(out, out);
6771 __ j(kEqual, slow_path->GetEntryLabel());
6772 __ Bind(slow_path->GetExitLabel());
6773 return;
6774 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006775 case HLoadString::LoadKind::kJitBootImageAddress: {
6776 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6777 DCHECK_NE(address, 0u);
6778 __ movl(out, Immediate(address));
6779 return;
6780 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006781 case HLoadString::LoadKind::kJitTableAddress: {
6782 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6783 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006784 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006785 // /* GcRoot<mirror::String> */ out = *address
6786 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6787 return;
6788 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006789 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006790 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006791 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006792
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006793 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006794 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006795 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006796 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006797 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6798 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006799}
6800
David Brazdilcb1c0552015-08-04 16:22:25 +01006801static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006802 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006803}
6804
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006805void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6806 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006807 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006808 locations->SetOut(Location::RequiresRegister());
6809}
6810
6811void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006812 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6813}
6814
6815void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006816 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006817}
6818
6819void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6820 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006821}
6822
6823void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006824 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6825 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006826 InvokeRuntimeCallingConvention calling_convention;
6827 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6828}
6829
6830void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006831 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006832 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006833}
6834
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006835// Temp is used for read barrier.
6836static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6837 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006838 !kUseBakerReadBarrier &&
6839 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006840 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006841 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6842 return 1;
6843 }
6844 return 0;
6845}
6846
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006847// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6848// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006849// The other checks have one temp for loading the object's class.
6850static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006851 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006852 return 2;
6853 }
6854 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006855}
6856
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006857void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006858 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006859 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006860 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006861 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006862 case TypeCheckKind::kExactCheck:
6863 case TypeCheckKind::kAbstractClassCheck:
6864 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006865 case TypeCheckKind::kArrayObjectCheck: {
6866 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6867 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6868 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006869 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006870 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006871 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006872 case TypeCheckKind::kUnresolvedCheck:
6873 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006874 call_kind = LocationSummary::kCallOnSlowPath;
6875 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006876 case TypeCheckKind::kBitstringCheck:
6877 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006878 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006879
Vladimir Markoca6fff82017-10-03 14:49:14 +01006880 LocationSummary* locations =
6881 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006882 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006883 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006884 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006885 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006886 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6887 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6888 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6889 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6890 } else {
6891 locations->SetInAt(1, Location::Any());
6892 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006893 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6894 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006895 // When read barriers are enabled, we need a temporary register for some cases.
6896 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006897}
6898
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006899void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006900 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006901 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006902 Location obj_loc = locations->InAt(0);
6903 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006904 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006905 Location out_loc = locations->Out();
6906 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006907 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6908 DCHECK_LE(num_temps, 1u);
6909 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006910 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006911 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6912 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6913 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006914 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006915 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006916
6917 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006918 // Avoid null check if we know obj is not null.
6919 if (instruction->MustDoNullCheck()) {
6920 __ testl(obj, obj);
6921 __ j(kEqual, &zero);
6922 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006923
Roland Levillain7c1559a2015-12-15 10:55:36 +00006924 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006925 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006926 ReadBarrierOption read_barrier_option =
6927 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006928 // /* HeapReference<Class> */ out = obj->klass_
6929 GenerateReferenceLoadTwoRegisters(instruction,
6930 out_loc,
6931 obj_loc,
6932 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006933 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006934 if (cls.IsRegister()) {
6935 __ cmpl(out, cls.AsRegister<Register>());
6936 } else {
6937 DCHECK(cls.IsStackSlot()) << cls;
6938 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6939 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006940
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006941 // Classes must be equal for the instanceof to succeed.
6942 __ j(kNotEqual, &zero);
6943 __ movl(out, Immediate(1));
6944 __ jmp(&done);
6945 break;
6946 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006948 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006949 ReadBarrierOption read_barrier_option =
6950 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006951 // /* HeapReference<Class> */ out = obj->klass_
6952 GenerateReferenceLoadTwoRegisters(instruction,
6953 out_loc,
6954 obj_loc,
6955 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006956 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006957 // If the class is abstract, we eagerly fetch the super class of the
6958 // object to avoid doing a comparison we know will fail.
6959 NearLabel loop;
6960 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006961 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006962 GenerateReferenceLoadOneRegister(instruction,
6963 out_loc,
6964 super_offset,
6965 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006966 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006967 __ testl(out, out);
6968 // If `out` is null, we use it for the result, and jump to `done`.
6969 __ j(kEqual, &done);
6970 if (cls.IsRegister()) {
6971 __ cmpl(out, cls.AsRegister<Register>());
6972 } else {
6973 DCHECK(cls.IsStackSlot()) << cls;
6974 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6975 }
6976 __ j(kNotEqual, &loop);
6977 __ movl(out, Immediate(1));
6978 if (zero.IsLinked()) {
6979 __ jmp(&done);
6980 }
6981 break;
6982 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006983
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006984 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006985 ReadBarrierOption read_barrier_option =
6986 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006987 // /* HeapReference<Class> */ out = obj->klass_
6988 GenerateReferenceLoadTwoRegisters(instruction,
6989 out_loc,
6990 obj_loc,
6991 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006992 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006993 // Walk over the class hierarchy to find a match.
6994 NearLabel loop, success;
6995 __ Bind(&loop);
6996 if (cls.IsRegister()) {
6997 __ cmpl(out, cls.AsRegister<Register>());
6998 } else {
6999 DCHECK(cls.IsStackSlot()) << cls;
7000 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7001 }
7002 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007003 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007004 GenerateReferenceLoadOneRegister(instruction,
7005 out_loc,
7006 super_offset,
7007 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007008 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007009 __ testl(out, out);
7010 __ j(kNotEqual, &loop);
7011 // If `out` is null, we use it for the result, and jump to `done`.
7012 __ jmp(&done);
7013 __ Bind(&success);
7014 __ movl(out, Immediate(1));
7015 if (zero.IsLinked()) {
7016 __ jmp(&done);
7017 }
7018 break;
7019 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007020
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007021 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007022 ReadBarrierOption read_barrier_option =
7023 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007024 // /* HeapReference<Class> */ out = obj->klass_
7025 GenerateReferenceLoadTwoRegisters(instruction,
7026 out_loc,
7027 obj_loc,
7028 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007029 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007030 // Do an exact check.
7031 NearLabel exact_check;
7032 if (cls.IsRegister()) {
7033 __ cmpl(out, cls.AsRegister<Register>());
7034 } else {
7035 DCHECK(cls.IsStackSlot()) << cls;
7036 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7037 }
7038 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007039 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007040 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007041 GenerateReferenceLoadOneRegister(instruction,
7042 out_loc,
7043 component_offset,
7044 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007045 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007046 __ testl(out, out);
7047 // If `out` is null, we use it for the result, and jump to `done`.
7048 __ j(kEqual, &done);
7049 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7050 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007051 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007052 __ movl(out, Immediate(1));
7053 __ jmp(&done);
7054 break;
7055 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007056
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007057 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007058 // No read barrier since the slow path will retry upon failure.
7059 // /* HeapReference<Class> */ out = obj->klass_
7060 GenerateReferenceLoadTwoRegisters(instruction,
7061 out_loc,
7062 obj_loc,
7063 class_offset,
7064 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007065 if (cls.IsRegister()) {
7066 __ cmpl(out, cls.AsRegister<Register>());
7067 } else {
7068 DCHECK(cls.IsStackSlot()) << cls;
7069 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7070 }
7071 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007072 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7073 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007074 codegen_->AddSlowPath(slow_path);
7075 __ j(kNotEqual, slow_path->GetEntryLabel());
7076 __ movl(out, Immediate(1));
7077 if (zero.IsLinked()) {
7078 __ jmp(&done);
7079 }
7080 break;
7081 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007082
Calin Juravle98893e12015-10-02 21:05:03 +01007083 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007084 case TypeCheckKind::kInterfaceCheck: {
7085 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007086 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007087 // cases.
7088 //
7089 // We cannot directly call the InstanceofNonTrivial runtime
7090 // entry point without resorting to a type checking slow path
7091 // here (i.e. by calling InvokeRuntime directly), as it would
7092 // require to assign fixed registers for the inputs of this
7093 // HInstanceOf instruction (following the runtime calling
7094 // convention), which might be cluttered by the potential first
7095 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007096 //
7097 // TODO: Introduce a new runtime entry point taking the object
7098 // to test (instead of its class) as argument, and let it deal
7099 // with the read barrier issues. This will let us refactor this
7100 // case of the `switch` code as it was previously (with a direct
7101 // call to the runtime not using a type checking slow path).
7102 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007103 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007104 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7105 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007106 codegen_->AddSlowPath(slow_path);
7107 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007108 if (zero.IsLinked()) {
7109 __ jmp(&done);
7110 }
7111 break;
7112 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007113
7114 case TypeCheckKind::kBitstringCheck: {
7115 // /* HeapReference<Class> */ temp = obj->klass_
7116 GenerateReferenceLoadTwoRegisters(instruction,
7117 out_loc,
7118 obj_loc,
7119 class_offset,
7120 kWithoutReadBarrier);
7121
7122 GenerateBitstringTypeCheckCompare(instruction, out);
7123 __ j(kNotEqual, &zero);
7124 __ movl(out, Immediate(1));
7125 __ jmp(&done);
7126 break;
7127 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007128 }
7129
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007131 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007132 __ xorl(out, out);
7133 }
7134
7135 if (done.IsLinked()) {
7136 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007137 }
7138
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007139 if (slow_path != nullptr) {
7140 __ Bind(slow_path->GetExitLabel());
7141 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007142}
7143
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007144void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007145 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007146 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007147 LocationSummary* locations =
7148 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007149 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007150 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7151 // Require a register for the interface check since there is a loop that compares the class to
7152 // a memory address.
7153 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007154 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7155 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7156 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7157 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007158 } else {
7159 locations->SetInAt(1, Location::Any());
7160 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007161 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007162 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7163}
7164
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007165void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007166 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007167 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007168 Location obj_loc = locations->InAt(0);
7169 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007170 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007171 Location temp_loc = locations->GetTemp(0);
7172 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007173 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7174 DCHECK_GE(num_temps, 1u);
7175 DCHECK_LE(num_temps, 2u);
7176 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7177 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7178 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7179 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7180 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7181 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7182 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7183 const uint32_t object_array_data_offset =
7184 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007185
Vladimir Marko87584542017-12-12 17:47:52 +00007186 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007187 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007188 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7189 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007190 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007191
Roland Levillain0d5a2812015-11-13 10:07:31 +00007192 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007193 // Avoid null check if we know obj is not null.
7194 if (instruction->MustDoNullCheck()) {
7195 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007196 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007197 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007198
Roland Levillain0d5a2812015-11-13 10:07:31 +00007199 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007200 case TypeCheckKind::kExactCheck:
7201 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007202 // /* HeapReference<Class> */ temp = obj->klass_
7203 GenerateReferenceLoadTwoRegisters(instruction,
7204 temp_loc,
7205 obj_loc,
7206 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007207 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007209 if (cls.IsRegister()) {
7210 __ cmpl(temp, cls.AsRegister<Register>());
7211 } else {
7212 DCHECK(cls.IsStackSlot()) << cls;
7213 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7214 }
7215 // Jump to slow path for throwing the exception or doing a
7216 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007217 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007218 break;
7219 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007220
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007221 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007222 // /* HeapReference<Class> */ temp = obj->klass_
7223 GenerateReferenceLoadTwoRegisters(instruction,
7224 temp_loc,
7225 obj_loc,
7226 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007227 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 // If the class is abstract, we eagerly fetch the super class of the
7230 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007231 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007233 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007234 GenerateReferenceLoadOneRegister(instruction,
7235 temp_loc,
7236 super_offset,
7237 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007238 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007239
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007240 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7241 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007242 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007243 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007244
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007245 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007246 if (cls.IsRegister()) {
7247 __ cmpl(temp, cls.AsRegister<Register>());
7248 } else {
7249 DCHECK(cls.IsStackSlot()) << cls;
7250 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7251 }
7252 __ j(kNotEqual, &loop);
7253 break;
7254 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007256 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007257 // /* HeapReference<Class> */ temp = obj->klass_
7258 GenerateReferenceLoadTwoRegisters(instruction,
7259 temp_loc,
7260 obj_loc,
7261 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007262 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007263
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007264 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007265 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007266 __ Bind(&loop);
7267 if (cls.IsRegister()) {
7268 __ cmpl(temp, cls.AsRegister<Register>());
7269 } else {
7270 DCHECK(cls.IsStackSlot()) << cls;
7271 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7272 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007273 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007274
Roland Levillain0d5a2812015-11-13 10:07:31 +00007275 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007276 GenerateReferenceLoadOneRegister(instruction,
7277 temp_loc,
7278 super_offset,
7279 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007280 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007281
7282 // If the class reference currently in `temp` is not null, jump
7283 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007284 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007285 __ j(kNotZero, &loop);
7286 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007287 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007288 break;
7289 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007290
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007291 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007292 // /* HeapReference<Class> */ temp = obj->klass_
7293 GenerateReferenceLoadTwoRegisters(instruction,
7294 temp_loc,
7295 obj_loc,
7296 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007297 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007298
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007299 // Do an exact check.
7300 if (cls.IsRegister()) {
7301 __ cmpl(temp, cls.AsRegister<Register>());
7302 } else {
7303 DCHECK(cls.IsStackSlot()) << cls;
7304 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7305 }
7306 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007307
7308 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007310 GenerateReferenceLoadOneRegister(instruction,
7311 temp_loc,
7312 component_offset,
7313 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007314 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007315
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007316 // If the component type is null (i.e. the object not an array), jump to the slow path to
7317 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007318 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007319 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007321 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007322 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007323 break;
7324 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007325
Calin Juravle98893e12015-10-02 21:05:03 +01007326 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007327 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 // We cannot directly call the CheckCast runtime entry point
7329 // without resorting to a type checking slow path here (i.e. by
7330 // calling InvokeRuntime directly), as it would require to
7331 // assign fixed registers for the inputs of this HInstanceOf
7332 // instruction (following the runtime calling convention), which
7333 // might be cluttered by the potential first read barrier
7334 // emission at the beginning of this method.
7335 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007336 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007337
7338 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007339 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7340 // We can not get false positives by doing this.
7341 // /* HeapReference<Class> */ temp = obj->klass_
7342 GenerateReferenceLoadTwoRegisters(instruction,
7343 temp_loc,
7344 obj_loc,
7345 class_offset,
7346 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007347
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007348 // /* HeapReference<Class> */ temp = temp->iftable_
7349 GenerateReferenceLoadTwoRegisters(instruction,
7350 temp_loc,
7351 temp_loc,
7352 iftable_offset,
7353 kWithoutReadBarrier);
7354 // Iftable is never null.
7355 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7356 // Maybe poison the `cls` for direct comparison with memory.
7357 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7358 // Loop through the iftable and check if any class matches.
7359 NearLabel start_loop;
7360 __ Bind(&start_loop);
7361 // Need to subtract first to handle the empty array case.
7362 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7363 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7364 // Go to next interface if the classes do not match.
7365 __ cmpl(cls.AsRegister<Register>(),
7366 CodeGeneratorX86::ArrayAddress(temp,
7367 maybe_temp2_loc,
7368 TIMES_4,
7369 object_array_data_offset));
7370 __ j(kNotEqual, &start_loop);
7371 // If `cls` was poisoned above, unpoison it.
7372 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007373 break;
7374 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007375
7376 case TypeCheckKind::kBitstringCheck: {
7377 // /* HeapReference<Class> */ temp = obj->klass_
7378 GenerateReferenceLoadTwoRegisters(instruction,
7379 temp_loc,
7380 obj_loc,
7381 class_offset,
7382 kWithoutReadBarrier);
7383
7384 GenerateBitstringTypeCheckCompare(instruction, temp);
7385 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7386 break;
7387 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007388 }
7389 __ Bind(&done);
7390
Roland Levillain0d5a2812015-11-13 10:07:31 +00007391 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007392}
7393
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007394void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007395 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7396 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007397 InvokeRuntimeCallingConvention calling_convention;
7398 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7399}
7400
7401void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007402 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7403 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007404 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007405 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007406 if (instruction->IsEnter()) {
7407 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7408 } else {
7409 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7410 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007411}
7412
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007413void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7414void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7415void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7416
7417void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7418 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007419 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007420 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7421 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007422 locations->SetInAt(0, Location::RequiresRegister());
7423 locations->SetInAt(1, Location::Any());
7424 locations->SetOut(Location::SameAsFirstInput());
7425}
7426
7427void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7428 HandleBitwiseOperation(instruction);
7429}
7430
7431void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7432 HandleBitwiseOperation(instruction);
7433}
7434
7435void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7436 HandleBitwiseOperation(instruction);
7437}
7438
7439void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7440 LocationSummary* locations = instruction->GetLocations();
7441 Location first = locations->InAt(0);
7442 Location second = locations->InAt(1);
7443 DCHECK(first.Equals(locations->Out()));
7444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007445 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007446 if (second.IsRegister()) {
7447 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007448 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007449 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007450 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007451 } else {
7452 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007453 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007454 }
7455 } else if (second.IsConstant()) {
7456 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007457 __ andl(first.AsRegister<Register>(),
7458 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007459 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007460 __ orl(first.AsRegister<Register>(),
7461 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007462 } else {
7463 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007464 __ xorl(first.AsRegister<Register>(),
7465 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007466 }
7467 } else {
7468 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007469 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007470 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007471 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007472 } else {
7473 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007474 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007475 }
7476 }
7477 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007478 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007479 if (second.IsRegisterPair()) {
7480 if (instruction->IsAnd()) {
7481 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7482 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7483 } else if (instruction->IsOr()) {
7484 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7485 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7486 } else {
7487 DCHECK(instruction->IsXor());
7488 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7489 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7490 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007491 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007492 if (instruction->IsAnd()) {
7493 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7494 __ andl(first.AsRegisterPairHigh<Register>(),
7495 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7496 } else if (instruction->IsOr()) {
7497 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7498 __ orl(first.AsRegisterPairHigh<Register>(),
7499 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7500 } else {
7501 DCHECK(instruction->IsXor());
7502 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7503 __ xorl(first.AsRegisterPairHigh<Register>(),
7504 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7505 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007506 } else {
7507 DCHECK(second.IsConstant()) << second;
7508 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007509 int32_t low_value = Low32Bits(value);
7510 int32_t high_value = High32Bits(value);
7511 Immediate low(low_value);
7512 Immediate high(high_value);
7513 Register first_low = first.AsRegisterPairLow<Register>();
7514 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007515 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007516 if (low_value == 0) {
7517 __ xorl(first_low, first_low);
7518 } else if (low_value != -1) {
7519 __ andl(first_low, low);
7520 }
7521 if (high_value == 0) {
7522 __ xorl(first_high, first_high);
7523 } else if (high_value != -1) {
7524 __ andl(first_high, high);
7525 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007526 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007527 if (low_value != 0) {
7528 __ orl(first_low, low);
7529 }
7530 if (high_value != 0) {
7531 __ orl(first_high, high);
7532 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007533 } else {
7534 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007535 if (low_value != 0) {
7536 __ xorl(first_low, low);
7537 }
7538 if (high_value != 0) {
7539 __ xorl(first_high, high);
7540 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007541 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007542 }
7543 }
7544}
7545
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007546void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7547 HInstruction* instruction,
7548 Location out,
7549 uint32_t offset,
7550 Location maybe_temp,
7551 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007552 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007553 if (read_barrier_option == kWithReadBarrier) {
7554 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007555 if (kUseBakerReadBarrier) {
7556 // Load with fast path based Baker's read barrier.
7557 // /* HeapReference<Object> */ out = *(out + offset)
7558 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007559 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007560 } else {
7561 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007562 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007563 // in the following move operation, as we will need it for the
7564 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007565 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007566 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007567 // /* HeapReference<Object> */ out = *(out + offset)
7568 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007569 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007570 }
7571 } else {
7572 // Plain load with no read barrier.
7573 // /* HeapReference<Object> */ out = *(out + offset)
7574 __ movl(out_reg, Address(out_reg, offset));
7575 __ MaybeUnpoisonHeapReference(out_reg);
7576 }
7577}
7578
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007579void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7580 HInstruction* instruction,
7581 Location out,
7582 Location obj,
7583 uint32_t offset,
7584 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007585 Register out_reg = out.AsRegister<Register>();
7586 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007587 if (read_barrier_option == kWithReadBarrier) {
7588 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007589 if (kUseBakerReadBarrier) {
7590 // Load with fast path based Baker's read barrier.
7591 // /* HeapReference<Object> */ out = *(obj + offset)
7592 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007593 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007594 } else {
7595 // Load with slow path based read barrier.
7596 // /* HeapReference<Object> */ out = *(obj + offset)
7597 __ movl(out_reg, Address(obj_reg, offset));
7598 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7599 }
7600 } else {
7601 // Plain load with no read barrier.
7602 // /* HeapReference<Object> */ out = *(obj + offset)
7603 __ movl(out_reg, Address(obj_reg, offset));
7604 __ MaybeUnpoisonHeapReference(out_reg);
7605 }
7606}
7607
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007608void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7609 HInstruction* instruction,
7610 Location root,
7611 const Address& address,
7612 Label* fixup_label,
7613 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007614 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007615 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007616 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007617 if (kUseBakerReadBarrier) {
7618 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7619 // Baker's read barrier are used:
7620 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007621 // root = obj.field;
7622 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7623 // if (temp != null) {
7624 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007625 // }
7626
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007627 // /* GcRoot<mirror::Object> */ root = *address
7628 __ movl(root_reg, address);
7629 if (fixup_label != nullptr) {
7630 __ Bind(fixup_label);
7631 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007632 static_assert(
7633 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7634 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7635 "have different sizes.");
7636 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7637 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7638 "have different sizes.");
7639
Vladimir Marko953437b2016-08-24 08:30:46 +00007640 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007641 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007642 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007643 codegen_->AddSlowPath(slow_path);
7644
Roland Levillaind966ce72017-02-09 16:20:14 +00007645 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7646 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007647 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007648 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7649 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007650 __ j(kNotEqual, slow_path->GetEntryLabel());
7651 __ Bind(slow_path->GetExitLabel());
7652 } else {
7653 // GC root loaded through a slow path for read barriers other
7654 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007655 // /* GcRoot<mirror::Object>* */ root = address
7656 __ leal(root_reg, address);
7657 if (fixup_label != nullptr) {
7658 __ Bind(fixup_label);
7659 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007660 // /* mirror::Object* */ root = root->Read()
7661 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7662 }
7663 } else {
7664 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007665 // /* GcRoot<mirror::Object> */ root = *address
7666 __ movl(root_reg, address);
7667 if (fixup_label != nullptr) {
7668 __ Bind(fixup_label);
7669 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007670 // Note that GC roots are not affected by heap poisoning, thus we
7671 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007672 }
7673}
7674
7675void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7676 Location ref,
7677 Register obj,
7678 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007679 bool needs_null_check) {
7680 DCHECK(kEmitCompilerReadBarrier);
7681 DCHECK(kUseBakerReadBarrier);
7682
7683 // /* HeapReference<Object> */ ref = *(obj + offset)
7684 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007685 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007686}
7687
7688void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7689 Location ref,
7690 Register obj,
7691 uint32_t data_offset,
7692 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007693 bool needs_null_check) {
7694 DCHECK(kEmitCompilerReadBarrier);
7695 DCHECK(kUseBakerReadBarrier);
7696
Roland Levillain3d312422016-06-23 13:53:42 +01007697 static_assert(
7698 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7699 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007700 // /* HeapReference<Object> */ ref =
7701 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007702 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007703 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007704}
7705
7706void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7707 Location ref,
7708 Register obj,
7709 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007710 bool needs_null_check,
7711 bool always_update_field,
7712 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007713 DCHECK(kEmitCompilerReadBarrier);
7714 DCHECK(kUseBakerReadBarrier);
7715
7716 // In slow path based read barriers, the read barrier call is
7717 // inserted after the original load. However, in fast path based
7718 // Baker's read barriers, we need to perform the load of
7719 // mirror::Object::monitor_ *before* the original reference load.
7720 // This load-load ordering is required by the read barrier.
7721 // The fast path/slow path (for Baker's algorithm) should look like:
7722 //
7723 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7724 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7725 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007726 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007727 // if (is_gray) {
7728 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7729 // }
7730 //
7731 // Note: the original implementation in ReadBarrier::Barrier is
7732 // slightly more complex as:
7733 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007734 // the high-bits of rb_state, which are expected to be all zeroes
7735 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7736 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007737 // - it performs additional checks that we do not do here for
7738 // performance reasons.
7739
7740 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007741 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7742
Vladimir Marko953437b2016-08-24 08:30:46 +00007743 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007744 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7745 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007746 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7747 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7748 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7749
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007750 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007751 // ref = ReadBarrier::Mark(ref);
7752 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7753 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007754 if (needs_null_check) {
7755 MaybeRecordImplicitNullCheck(instruction);
7756 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007757
7758 // Load fence to prevent load-load reordering.
7759 // Note that this is a no-op, thanks to the x86 memory model.
7760 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7761
7762 // The actual reference load.
7763 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007764 __ movl(ref_reg, src); // Flags are unaffected.
7765
7766 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7767 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007768 SlowPathCode* slow_path;
7769 if (always_update_field) {
7770 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007771 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007772 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7773 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007774 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007775 instruction, ref, /* unpoison_ref_before_marking */ true);
7776 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007777 AddSlowPath(slow_path);
7778
7779 // We have done the "if" of the gray bit check above, now branch based on the flags.
7780 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007781
7782 // Object* ref = ref_addr->AsMirrorPtr()
7783 __ MaybeUnpoisonHeapReference(ref_reg);
7784
Roland Levillain7c1559a2015-12-15 10:55:36 +00007785 __ Bind(slow_path->GetExitLabel());
7786}
7787
7788void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7789 Location out,
7790 Location ref,
7791 Location obj,
7792 uint32_t offset,
7793 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007794 DCHECK(kEmitCompilerReadBarrier);
7795
Roland Levillain7c1559a2015-12-15 10:55:36 +00007796 // Insert a slow path based read barrier *after* the reference load.
7797 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007798 // If heap poisoning is enabled, the unpoisoning of the loaded
7799 // reference will be carried out by the runtime within the slow
7800 // path.
7801 //
7802 // Note that `ref` currently does not get unpoisoned (when heap
7803 // poisoning is enabled), which is alright as the `ref` argument is
7804 // not used by the artReadBarrierSlow entry point.
7805 //
7806 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007807 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007808 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7809 AddSlowPath(slow_path);
7810
Roland Levillain0d5a2812015-11-13 10:07:31 +00007811 __ jmp(slow_path->GetEntryLabel());
7812 __ Bind(slow_path->GetExitLabel());
7813}
7814
Roland Levillain7c1559a2015-12-15 10:55:36 +00007815void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7816 Location out,
7817 Location ref,
7818 Location obj,
7819 uint32_t offset,
7820 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007821 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007822 // Baker's read barriers shall be handled by the fast path
7823 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7824 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007825 // If heap poisoning is enabled, unpoisoning will be taken care of
7826 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007827 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007828 } else if (kPoisonHeapReferences) {
7829 __ UnpoisonHeapReference(out.AsRegister<Register>());
7830 }
7831}
7832
Roland Levillain7c1559a2015-12-15 10:55:36 +00007833void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7834 Location out,
7835 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007836 DCHECK(kEmitCompilerReadBarrier);
7837
Roland Levillain7c1559a2015-12-15 10:55:36 +00007838 // Insert a slow path based read barrier *after* the GC root load.
7839 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007840 // Note that GC roots are not affected by heap poisoning, so we do
7841 // not need to do anything special for this here.
7842 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007843 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007844 AddSlowPath(slow_path);
7845
Roland Levillain0d5a2812015-11-13 10:07:31 +00007846 __ jmp(slow_path->GetEntryLabel());
7847 __ Bind(slow_path->GetExitLabel());
7848}
7849
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007850void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007851 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007852 LOG(FATAL) << "Unreachable";
7853}
7854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007855void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007856 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007857 LOG(FATAL) << "Unreachable";
7858}
7859
Mark Mendellfe57faa2015-09-18 09:26:15 -04007860// Simple implementation of packed switch - generate cascaded compare/jumps.
7861void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7862 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007863 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007864 locations->SetInAt(0, Location::RequiresRegister());
7865}
7866
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007867void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7868 int32_t lower_bound,
7869 uint32_t num_entries,
7870 HBasicBlock* switch_block,
7871 HBasicBlock* default_block) {
7872 // Figure out the correct compare values and jump conditions.
7873 // Handle the first compare/branch as a special case because it might
7874 // jump to the default case.
7875 DCHECK_GT(num_entries, 2u);
7876 Condition first_condition;
7877 uint32_t index;
7878 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7879 if (lower_bound != 0) {
7880 first_condition = kLess;
7881 __ cmpl(value_reg, Immediate(lower_bound));
7882 __ j(first_condition, codegen_->GetLabelOf(default_block));
7883 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007884
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007885 index = 1;
7886 } else {
7887 // Handle all the compare/jumps below.
7888 first_condition = kBelow;
7889 index = 0;
7890 }
7891
7892 // Handle the rest of the compare/jumps.
7893 for (; index + 1 < num_entries; index += 2) {
7894 int32_t compare_to_value = lower_bound + index + 1;
7895 __ cmpl(value_reg, Immediate(compare_to_value));
7896 // Jump to successors[index] if value < case_value[index].
7897 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7898 // Jump to successors[index + 1] if value == case_value[index + 1].
7899 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7900 }
7901
7902 if (index != num_entries) {
7903 // There are an odd number of entries. Handle the last one.
7904 DCHECK_EQ(index + 1, num_entries);
7905 __ cmpl(value_reg, Immediate(lower_bound + index));
7906 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007907 }
7908
7909 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007910 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7911 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007912 }
7913}
7914
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007915void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7916 int32_t lower_bound = switch_instr->GetStartValue();
7917 uint32_t num_entries = switch_instr->GetNumEntries();
7918 LocationSummary* locations = switch_instr->GetLocations();
7919 Register value_reg = locations->InAt(0).AsRegister<Register>();
7920
7921 GenPackedSwitchWithCompares(value_reg,
7922 lower_bound,
7923 num_entries,
7924 switch_instr->GetBlock(),
7925 switch_instr->GetDefaultBlock());
7926}
7927
Mark Mendell805b3b52015-09-18 14:10:29 -04007928void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007930 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007931 locations->SetInAt(0, Location::RequiresRegister());
7932
7933 // Constant area pointer.
7934 locations->SetInAt(1, Location::RequiresRegister());
7935
7936 // And the temporary we need.
7937 locations->AddTemp(Location::RequiresRegister());
7938}
7939
7940void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7941 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007942 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007943 LocationSummary* locations = switch_instr->GetLocations();
7944 Register value_reg = locations->InAt(0).AsRegister<Register>();
7945 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7946
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007947 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7948 GenPackedSwitchWithCompares(value_reg,
7949 lower_bound,
7950 num_entries,
7951 switch_instr->GetBlock(),
7952 default_block);
7953 return;
7954 }
7955
Mark Mendell805b3b52015-09-18 14:10:29 -04007956 // Optimizing has a jump area.
7957 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7958 Register constant_area = locations->InAt(1).AsRegister<Register>();
7959
7960 // Remove the bias, if needed.
7961 if (lower_bound != 0) {
7962 __ leal(temp_reg, Address(value_reg, -lower_bound));
7963 value_reg = temp_reg;
7964 }
7965
7966 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007967 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007968 __ cmpl(value_reg, Immediate(num_entries - 1));
7969 __ j(kAbove, codegen_->GetLabelOf(default_block));
7970
7971 // We are in the range of the table.
7972 // Load (target-constant_area) from the jump table, indexing by the value.
7973 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7974
7975 // Compute the actual target address by adding in constant_area.
7976 __ addl(temp_reg, constant_area);
7977
7978 // And jump.
7979 __ jmp(temp_reg);
7980}
7981
Mark Mendell0616ae02015-04-17 12:49:27 -04007982void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7983 HX86ComputeBaseMethodAddress* insn) {
7984 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007985 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007986 locations->SetOut(Location::RequiresRegister());
7987}
7988
7989void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7990 HX86ComputeBaseMethodAddress* insn) {
7991 LocationSummary* locations = insn->GetLocations();
7992 Register reg = locations->Out().AsRegister<Register>();
7993
7994 // Generate call to next instruction.
7995 Label next_instruction;
7996 __ call(&next_instruction);
7997 __ Bind(&next_instruction);
7998
7999 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008000 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008001
8002 // Grab the return address off the stack.
8003 __ popl(reg);
8004}
8005
8006void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8007 HX86LoadFromConstantTable* insn) {
8008 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008009 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008010
8011 locations->SetInAt(0, Location::RequiresRegister());
8012 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8013
8014 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008015 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008016 return;
8017 }
8018
8019 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008020 case DataType::Type::kFloat32:
8021 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008022 locations->SetOut(Location::RequiresFpuRegister());
8023 break;
8024
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008025 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008026 locations->SetOut(Location::RequiresRegister());
8027 break;
8028
8029 default:
8030 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8031 }
8032}
8033
8034void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008035 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008036 return;
8037 }
8038
8039 LocationSummary* locations = insn->GetLocations();
8040 Location out = locations->Out();
8041 Register const_area = locations->InAt(0).AsRegister<Register>();
8042 HConstant *value = insn->GetConstant();
8043
8044 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008045 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008046 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008047 codegen_->LiteralFloatAddress(
8048 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008049 break;
8050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008051 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008052 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008053 codegen_->LiteralDoubleAddress(
8054 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008055 break;
8056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008057 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008058 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008059 codegen_->LiteralInt32Address(
8060 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008061 break;
8062
8063 default:
8064 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8065 }
8066}
8067
Mark Mendell0616ae02015-04-17 12:49:27 -04008068/**
8069 * Class to handle late fixup of offsets into constant area.
8070 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008071class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008072 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008073 RIPFixup(CodeGeneratorX86& codegen,
8074 HX86ComputeBaseMethodAddress* base_method_address,
8075 size_t offset)
8076 : codegen_(&codegen),
8077 base_method_address_(base_method_address),
8078 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008079
8080 protected:
8081 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8082
8083 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008084 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008085
8086 private:
8087 void Process(const MemoryRegion& region, int pos) OVERRIDE {
8088 // Patch the correct offset for the instruction. The place to patch is the
8089 // last 4 bytes of the instruction.
8090 // The value to patch is the distance from the offset in the constant area
8091 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008092 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008093 int32_t relative_position =
8094 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008095
8096 // Patch in the right value.
8097 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8098 }
8099
Mark Mendell0616ae02015-04-17 12:49:27 -04008100 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008101 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008102};
8103
Mark Mendell805b3b52015-09-18 14:10:29 -04008104/**
8105 * Class to handle late fixup of offsets to a jump table that will be created in the
8106 * constant area.
8107 */
8108class JumpTableRIPFixup : public RIPFixup {
8109 public:
8110 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008111 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8112 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008113
8114 void CreateJumpTable() {
8115 X86Assembler* assembler = codegen_->GetAssembler();
8116
8117 // Ensure that the reference to the jump table has the correct offset.
8118 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8119 SetOffset(offset_in_constant_table);
8120
8121 // The label values in the jump table are computed relative to the
8122 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008123 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008124
8125 // Populate the jump table with the correct values for the jump table.
8126 int32_t num_entries = switch_instr_->GetNumEntries();
8127 HBasicBlock* block = switch_instr_->GetBlock();
8128 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8129 // The value that we want is the target offset - the position of the table.
8130 for (int32_t i = 0; i < num_entries; i++) {
8131 HBasicBlock* b = successors[i];
8132 Label* l = codegen_->GetLabelOf(b);
8133 DCHECK(l->IsBound());
8134 int32_t offset_to_block = l->Position() - relative_offset;
8135 assembler->AppendInt32(offset_to_block);
8136 }
8137 }
8138
8139 private:
8140 const HX86PackedSwitch* switch_instr_;
8141};
8142
8143void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8144 // Generate the constant area if needed.
8145 X86Assembler* assembler = GetAssembler();
8146 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8147 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8148 // byte values.
8149 assembler->Align(4, 0);
8150 constant_area_start_ = assembler->CodeSize();
8151
8152 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008153 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008154 jump_table->CreateJumpTable();
8155 }
8156
8157 // And now add the constant area to the generated code.
8158 assembler->AddConstantArea();
8159 }
8160
8161 // And finish up.
8162 CodeGenerator::Finalize(allocator);
8163}
8164
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008165Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8166 HX86ComputeBaseMethodAddress* method_base,
8167 Register reg) {
8168 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008169 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008170 return Address(reg, kDummy32BitOffset, fixup);
8171}
8172
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008173Address CodeGeneratorX86::LiteralFloatAddress(float v,
8174 HX86ComputeBaseMethodAddress* method_base,
8175 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008176 AssemblerFixup* fixup =
8177 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008178 return Address(reg, kDummy32BitOffset, fixup);
8179}
8180
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008181Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8182 HX86ComputeBaseMethodAddress* method_base,
8183 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008184 AssemblerFixup* fixup =
8185 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008186 return Address(reg, kDummy32BitOffset, fixup);
8187}
8188
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008189Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8190 HX86ComputeBaseMethodAddress* method_base,
8191 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008192 AssemblerFixup* fixup =
8193 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008194 return Address(reg, kDummy32BitOffset, fixup);
8195}
8196
Aart Bika19616e2016-02-01 18:57:58 -08008197void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8198 if (value == 0) {
8199 __ xorl(dest, dest);
8200 } else {
8201 __ movl(dest, Immediate(value));
8202 }
8203}
8204
8205void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8206 if (value == 0) {
8207 __ testl(dest, dest);
8208 } else {
8209 __ cmpl(dest, Immediate(value));
8210 }
8211}
8212
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008213void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8214 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008215 GenerateIntCompare(lhs_reg, rhs);
8216}
8217
8218void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008219 if (rhs.IsConstant()) {
8220 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008221 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008222 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008223 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008224 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008225 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008226 }
8227}
8228
8229Address CodeGeneratorX86::ArrayAddress(Register obj,
8230 Location index,
8231 ScaleFactor scale,
8232 uint32_t data_offset) {
8233 return index.IsConstant() ?
8234 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8235 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8236}
8237
Mark Mendell805b3b52015-09-18 14:10:29 -04008238Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8239 Register reg,
8240 Register value) {
8241 // Create a fixup to be used to create and address the jump table.
8242 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008243 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008244
8245 // We have to populate the jump tables.
8246 fixups_to_jump_tables_.push_back(table_fixup);
8247
8248 // We want a scaled address, as we are extracting the correct offset from the table.
8249 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8250}
8251
Andreas Gampe85b62f22015-09-09 13:15:38 -07008252// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008253void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008254 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008255 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008256 return;
8257 }
8258
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008259 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008260
8261 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8262 if (target.Equals(return_loc)) {
8263 return;
8264 }
8265
8266 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8267 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008268 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008269 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008270 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8271 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008272 GetMoveResolver()->EmitNativeCode(&parallel_move);
8273 } else {
8274 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008275 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008276 parallel_move.AddMove(return_loc, target, type, nullptr);
8277 GetMoveResolver()->EmitNativeCode(&parallel_move);
8278 }
8279}
8280
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008281void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8282 const uint8_t* roots_data,
8283 const PatchInfo<Label>& info,
8284 uint64_t index_in_table) const {
8285 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8286 uintptr_t address =
8287 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8288 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8289 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8290 dchecked_integral_cast<uint32_t>(address);
8291}
8292
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008293void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8294 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008295 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008296 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008297 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008298 }
8299
8300 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008301 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008302 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008303 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008304 }
8305}
8306
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008307void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8308 ATTRIBUTE_UNUSED) {
8309 LOG(FATAL) << "Unreachable";
8310}
8311
8312void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8313 ATTRIBUTE_UNUSED) {
8314 LOG(FATAL) << "Unreachable";
8315}
8316
Roland Levillain4d027112015-07-01 15:41:14 +01008317#undef __
8318
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008319} // namespace x86
8320} // namespace art