blob: 82d1fda87890ef7dcad818aac8052940ff76e285 [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"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#include "intrinsics.h"
28#include "intrinsics_x86.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010029#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070031#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010033#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010040
Roland Levillain0d5a2812015-11-13 10:07:31 +000041template<class MirrorType>
42class GcRoot;
43
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044namespace x86 {
45
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050048static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Mark Mendell24f2dfa2015-01-14 19:51:45 -050050static constexpr int kC2ConditionMask = 0x400;
51
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000052static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000053
Aart Bik1f8d51b2018-02-15 10:42:37 -080054static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
55static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
56
Roland Levillain7cbd27f2016-08-11 23:53:33 +010057// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
58#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070059#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Andreas Gampe85b62f22015-09-09 13:15:38 -070061class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000063 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Alexandre Rames2ed20af2015-03-06 13:55:35 +000065 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010066 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000068 if (instruction_->CanThrowIntoCatchBlock()) {
69 // Live registers will be restored in the catch block if caught.
70 SaveLiveRegisters(codegen, instruction_->GetLocations());
71 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010072 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010073 instruction_,
74 instruction_->GetDexPc(),
75 this);
Roland Levillain888d0672015-11-23 18:53:50 +000076 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 }
78
Alexandre Rames8158f282015-08-07 10:26:17 +010079 bool IsFatal() const OVERRIDE { return true; }
80
Alexandre Rames9931f312015-06-19 14:47:01 +010081 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
82
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
85};
86
Andreas Gampe85b62f22015-09-09 13:15:38 -070087class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000088 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000089 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000090
Alexandre Rames2ed20af2015-03-06 13:55:35 +000091 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010092 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000093 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010094 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400143
144 // Are we using an array length from memory?
145 HInstruction* array_length = instruction_->InputAt(1);
146 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400148 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
149 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100150 HArrayLength* length = array_length->AsArrayLength();
151 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400152 Location array_loc = array_length->GetLocations()->InAt(0);
153 Address array_len(array_loc.AsRegister<Register>(), len_offset);
154 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
155 // Check for conflicts with index.
156 if (length_loc.Equals(locations->InAt(0))) {
157 // We know we aren't using parameter 2.
158 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
159 }
160 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100161 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100162 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700163 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400164 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100166 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000167 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100168 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400169 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100170 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100171 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100172 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
173 ? kQuickThrowStringBounds
174 : kQuickThrowArrayBounds;
175 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100176 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000177 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100178 }
179
Alexandre Rames8158f282015-08-07 10:26:17 +0100180 bool IsFatal() const OVERRIDE { return true; }
181
Alexandre Rames9931f312015-06-19 14:47:01 +0100182 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
186};
187
Andreas Gampe85b62f22015-09-09 13:15:38 -0700188class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000190 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000191 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000193 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700194 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100195 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700197 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100198 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000199 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700200 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100201 if (successor_ == nullptr) {
202 __ jmp(GetReturnLabel());
203 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100204 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100205 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206 }
207
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 Label* GetReturnLabel() {
209 DCHECK(successor_ == nullptr);
210 return &return_label_;
211 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100213 HBasicBlock* GetSuccessor() const {
214 return successor_;
215 }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
218
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000219 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000221 Label return_label_;
222
223 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
224};
225
Vladimir Markoaad75c62016-10-03 08:46:48 +0000226class LoadStringSlowPathX86 : public SlowPathCode {
227 public:
228 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = instruction_->GetLocations();
232 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
233
234 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
235 __ Bind(GetEntryLabel());
236 SaveLiveRegisters(codegen, locations);
237
238 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000239 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
240 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000241 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
242 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
243 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
244 RestoreLiveRegisters(codegen, locations);
245
Vladimir Markoaad75c62016-10-03 08:46:48 +0000246 __ jmp(GetExitLabel());
247 }
248
249 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
250
251 private:
252 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
253};
254
Andreas Gampe85b62f22015-09-09 13:15:38 -0700255class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 public:
257 LoadClassSlowPathX86(HLoadClass* cls,
258 HInstruction* at,
259 uint32_t dex_pc,
260 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000261 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
263 }
264
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000265 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000266 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000267 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
268 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270
271 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 dex::TypeIndex type_index = cls_->GetTypeIndex();
273 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100274 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
275 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 instruction_,
277 dex_pc_,
278 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 if (do_clinit_) {
280 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
281 } else {
282 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
283 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000284
285 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 Location out = locations->Out();
287 if (out.IsValid()) {
288 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
289 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000292 __ jmp(GetExitLabel());
293 }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
296
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000297 private:
298 // The class this slow path will load.
299 HLoadClass* const cls_;
300
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000301 // The dex PC of `at_`.
302 const uint32_t dex_pc_;
303
304 // Whether to initialize the class.
305 const bool do_clinit_;
306
307 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
308};
309
Andreas Gampe85b62f22015-09-09 13:15:38 -0700310class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000312 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000313 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000315 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 DCHECK(instruction_->IsCheckCast()
318 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
321 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000323 if (kPoisonHeapReferences &&
324 instruction_->IsCheckCast() &&
325 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
326 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
327 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
328 }
329
Vladimir Marko87584542017-12-12 17:47:52 +0000330 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331 SaveLiveRegisters(codegen, locations);
332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
334 // We're moving two locations to locations that could overlap, so we need a parallel
335 // move resolver.
336 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800337 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100339 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800340 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100342 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100344 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100345 instruction_,
346 instruction_->GetDexPc(),
347 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800348 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 } else {
350 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
352 instruction_,
353 instruction_->GetDexPc(),
354 this);
355 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 }
357
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000358 if (!is_fatal_) {
359 if (instruction_->IsInstanceOf()) {
360 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
361 }
362 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 __ jmp(GetExitLabel());
365 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000366 }
367
Alexandre Rames9931f312015-06-19 14:47:01 +0100368 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100370
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000371 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373
374 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
375};
376
Andreas Gampe85b62f22015-09-09 13:15:38 -0700377class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 public:
Aart Bik42249c32016-01-07 15:33:50 -0800379 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000380 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381
382 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100383 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100385 LocationSummary* locations = instruction_->GetLocations();
386 SaveLiveRegisters(codegen, locations);
387 InvokeRuntimeCallingConvention calling_convention;
388 x86_codegen->Load32BitValue(
389 calling_convention.GetRegisterAt(0),
390 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100391 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100392 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 }
394
Alexandre Rames9931f312015-06-19 14:47:01 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
396
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
399};
400
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401class ArraySetSlowPathX86 : public SlowPathCode {
402 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 LocationSummary* locations = instruction_->GetLocations();
407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, locations);
409
410 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100411 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100412 parallel_move.AddMove(
413 locations->InAt(0),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100415 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 nullptr);
417 parallel_move.AddMove(
418 locations->InAt(1),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100420 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100421 nullptr);
422 parallel_move.AddMove(
423 locations->InAt(2),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100425 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100426 nullptr);
427 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
428
429 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100430 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000431 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 RestoreLiveRegisters(codegen, locations);
433 __ jmp(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
437
438 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
440};
441
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100442// Slow path marking an object reference `ref` during a read
443// barrier. The field `obj.field` in the object `obj` holding this
444// reference does not get updated by this slow path after marking (see
445// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
446//
447// This means that after the execution of this slow path, `ref` will
448// always be up-to-date, but `obj.field` may not; i.e., after the
449// flip, `ref` will be a to-space reference, but `obj.field` will
450// probably still be a from-space reference (unless it gets updated by
451// another thread, or if another thread installed another object
452// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000453class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
454 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
456 Location ref,
457 bool unpoison_ref_before_marking)
458 : SlowPathCode(instruction),
459 ref_(ref),
460 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000461 DCHECK(kEmitCompilerReadBarrier);
462 }
463
464 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000469 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 DCHECK(instruction_->IsInstanceFieldGet() ||
472 instruction_->IsStaticFieldGet() ||
473 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100474 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 instruction_->IsLoadClass() ||
476 instruction_->IsLoadString() ||
477 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100478 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100479 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
480 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 << "Unexpected instruction in read barrier marking slow path: "
482 << instruction_->DebugName();
483
484 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100485 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000486 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000488 }
Roland Levillain4359e612016-07-20 11:32:19 +0100489 // No need to save live registers; it's taken care of by the
490 // entrypoint. Also, there is no need to update the stack mask,
491 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000492 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100493 DCHECK_NE(ref_reg, ESP);
494 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100495 // "Compact" slow path, saving two moves.
496 //
497 // Instead of using the standard runtime calling convention (input
498 // and output in EAX):
499 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100501 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100503 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100505 // of a dedicated entrypoint:
506 //
507 // rX <- ReadBarrierMarkRegX(rX)
508 //
Roland Levillain97c46462017-05-11 14:04:03 +0100509 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100510 // This runtime call does not require a stack map.
511 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 __ jmp(GetExitLabel());
513 }
514
515 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // The location (register) of the marked object reference.
517 const Location ref_;
518 // Should the reference in `ref_` be unpoisoned prior to marking it?
519 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000520
521 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
522};
523
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100524// Slow path marking an object reference `ref` during a read barrier,
525// and if needed, atomically updating the field `obj.field` in the
526// object `obj` holding this reference after marking (contrary to
527// ReadBarrierMarkSlowPathX86 above, which never tries to update
528// `obj.field`).
529//
530// This means that after the execution of this slow path, both `ref`
531// and `obj.field` will be up-to-date; i.e., after the flip, both will
532// hold the same to-space reference (unless another thread installed
533// another object reference (different from `ref`) in `obj.field`).
534class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
535 public:
536 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
537 Location ref,
538 Register obj,
539 const Address& field_addr,
540 bool unpoison_ref_before_marking,
541 Register temp)
542 : SlowPathCode(instruction),
543 ref_(ref),
544 obj_(obj),
545 field_addr_(field_addr),
546 unpoison_ref_before_marking_(unpoison_ref_before_marking),
547 temp_(temp) {
548 DCHECK(kEmitCompilerReadBarrier);
549 }
550
551 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
552
553 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
554 LocationSummary* locations = instruction_->GetLocations();
555 Register ref_reg = ref_.AsRegister<Register>();
556 DCHECK(locations->CanCall());
557 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
558 // This slow path is only used by the UnsafeCASObject intrinsic.
559 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
560 << "Unexpected instruction in read barrier marking and field updating slow path: "
561 << instruction_->DebugName();
562 DCHECK(instruction_->GetLocations()->Intrinsified());
563 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
564
565 __ Bind(GetEntryLabel());
566 if (unpoison_ref_before_marking_) {
567 // Object* ref = ref_addr->AsMirrorPtr()
568 __ MaybeUnpoisonHeapReference(ref_reg);
569 }
570
571 // Save the old (unpoisoned) reference.
572 __ movl(temp_, ref_reg);
573
574 // No need to save live registers; it's taken care of by the
575 // entrypoint. Also, there is no need to update the stack mask,
576 // as this runtime call will not trigger a garbage collection.
577 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
578 DCHECK_NE(ref_reg, ESP);
579 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
580 // "Compact" slow path, saving two moves.
581 //
582 // Instead of using the standard runtime calling convention (input
583 // and output in EAX):
584 //
585 // EAX <- ref
586 // EAX <- ReadBarrierMark(EAX)
587 // ref <- EAX
588 //
589 // we just use rX (the register containing `ref`) as input and output
590 // of a dedicated entrypoint:
591 //
592 // rX <- ReadBarrierMarkRegX(rX)
593 //
Roland Levillain97c46462017-05-11 14:04:03 +0100594 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100595 // This runtime call does not require a stack map.
596 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
597
598 // If the new reference is different from the old reference,
599 // update the field in the holder (`*field_addr`).
600 //
601 // Note that this field could also hold a different object, if
602 // another thread had concurrently changed it. In that case, the
603 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
604 // operation below would abort the CAS, leaving the field as-is.
605 NearLabel done;
606 __ cmpl(temp_, ref_reg);
607 __ j(kEqual, &done);
608
609 // Update the the holder's field atomically. This may fail if
610 // mutator updates before us, but it's OK. This is achieved
611 // using a strong compare-and-set (CAS) operation with relaxed
612 // memory synchronization ordering, where the expected value is
613 // the old reference and the desired value is the new reference.
614 // This operation is implemented with a 32-bit LOCK CMPXLCHG
615 // instruction, which requires the expected value (the old
616 // reference) to be in EAX. Save EAX beforehand, and move the
617 // expected value (stored in `temp_`) into EAX.
618 __ pushl(EAX);
619 __ movl(EAX, temp_);
620
621 // Convenience aliases.
622 Register base = obj_;
623 Register expected = EAX;
624 Register value = ref_reg;
625
626 bool base_equals_value = (base == value);
627 if (kPoisonHeapReferences) {
628 if (base_equals_value) {
629 // If `base` and `value` are the same register location, move
630 // `value` to a temporary register. This way, poisoning
631 // `value` won't invalidate `base`.
632 value = temp_;
633 __ movl(value, base);
634 }
635
636 // Check that the register allocator did not assign the location
637 // of `expected` (EAX) to `value` nor to `base`, so that heap
638 // poisoning (when enabled) works as intended below.
639 // - If `value` were equal to `expected`, both references would
640 // be poisoned twice, meaning they would not be poisoned at
641 // all, as heap poisoning uses address negation.
642 // - If `base` were equal to `expected`, poisoning `expected`
643 // would invalidate `base`.
644 DCHECK_NE(value, expected);
645 DCHECK_NE(base, expected);
646
647 __ PoisonHeapReference(expected);
648 __ PoisonHeapReference(value);
649 }
650
651 __ LockCmpxchgl(field_addr_, value);
652
653 // If heap poisoning is enabled, we need to unpoison the values
654 // that were poisoned earlier.
655 if (kPoisonHeapReferences) {
656 if (base_equals_value) {
657 // `value` has been moved to a temporary register, no need
658 // to unpoison it.
659 } else {
660 __ UnpoisonHeapReference(value);
661 }
662 // No need to unpoison `expected` (EAX), as it is be overwritten below.
663 }
664
665 // Restore EAX.
666 __ popl(EAX);
667
668 __ Bind(&done);
669 __ jmp(GetExitLabel());
670 }
671
672 private:
673 // The location (register) of the marked object reference.
674 const Location ref_;
675 // The register containing the object holding the marked object reference field.
676 const Register obj_;
677 // The address of the marked reference field. The base of this address must be `obj_`.
678 const Address field_addr_;
679
680 // Should the reference in `ref_` be unpoisoned prior to marking it?
681 const bool unpoison_ref_before_marking_;
682
683 const Register temp_;
684
685 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
686};
687
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688// Slow path generating a read barrier for a heap reference.
689class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
690 public:
691 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
692 Location out,
693 Location ref,
694 Location obj,
695 uint32_t offset,
696 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000697 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698 out_(out),
699 ref_(ref),
700 obj_(obj),
701 offset_(offset),
702 index_(index) {
703 DCHECK(kEmitCompilerReadBarrier);
704 // If `obj` is equal to `out` or `ref`, it means the initial object
705 // has been overwritten by (or after) the heap object reference load
706 // to be instrumented, e.g.:
707 //
708 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000709 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 //
711 // In that case, we have lost the information about the original
712 // object, and the emitted read barrier cannot work properly.
713 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
714 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
715 }
716
717 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
718 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
719 LocationSummary* locations = instruction_->GetLocations();
720 Register reg_out = out_.AsRegister<Register>();
721 DCHECK(locations->CanCall());
722 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100723 DCHECK(instruction_->IsInstanceFieldGet() ||
724 instruction_->IsStaticFieldGet() ||
725 instruction_->IsArrayGet() ||
726 instruction_->IsInstanceOf() ||
727 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700728 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000729 << "Unexpected instruction in read barrier for heap reference slow path: "
730 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731
732 __ Bind(GetEntryLabel());
733 SaveLiveRegisters(codegen, locations);
734
735 // We may have to change the index's value, but as `index_` is a
736 // constant member (like other "inputs" of this slow path),
737 // introduce a copy of it, `index`.
738 Location index = index_;
739 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100740 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000741 if (instruction_->IsArrayGet()) {
742 // Compute the actual memory offset and store it in `index`.
743 Register index_reg = index_.AsRegister<Register>();
744 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
745 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
746 // We are about to change the value of `index_reg` (see the
747 // calls to art::x86::X86Assembler::shll and
748 // art::x86::X86Assembler::AddImmediate below), but it has
749 // not been saved by the previous call to
750 // art::SlowPathCode::SaveLiveRegisters, as it is a
751 // callee-save register --
752 // art::SlowPathCode::SaveLiveRegisters does not consider
753 // callee-save registers, as it has been designed with the
754 // assumption that callee-save registers are supposed to be
755 // handled by the called function. So, as a callee-save
756 // register, `index_reg` _would_ eventually be saved onto
757 // the stack, but it would be too late: we would have
758 // changed its value earlier. Therefore, we manually save
759 // it here into another freely available register,
760 // `free_reg`, chosen of course among the caller-save
761 // registers (as a callee-save `free_reg` register would
762 // exhibit the same problem).
763 //
764 // Note we could have requested a temporary register from
765 // the register allocator instead; but we prefer not to, as
766 // this is a slow path, and we know we can find a
767 // caller-save register that is available.
768 Register free_reg = FindAvailableCallerSaveRegister(codegen);
769 __ movl(free_reg, index_reg);
770 index_reg = free_reg;
771 index = Location::RegisterLocation(index_reg);
772 } else {
773 // The initial register stored in `index_` has already been
774 // saved in the call to art::SlowPathCode::SaveLiveRegisters
775 // (as it is not a callee-save register), so we can freely
776 // use it.
777 }
778 // Shifting the index value contained in `index_reg` by the scale
779 // factor (2) cannot overflow in practice, as the runtime is
780 // unable to allocate object arrays with a size larger than
781 // 2^26 - 1 (that is, 2^28 - 4 bytes).
782 __ shll(index_reg, Immediate(TIMES_4));
783 static_assert(
784 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
785 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
786 __ AddImmediate(index_reg, Immediate(offset_));
787 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100788 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
789 // intrinsics, `index_` is not shifted by a scale factor of 2
790 // (as in the case of ArrayGet), as it is actually an offset
791 // to an object field within an object.
792 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000793 DCHECK(instruction_->GetLocations()->Intrinsified());
794 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
795 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
796 << instruction_->AsInvoke()->GetIntrinsic();
797 DCHECK_EQ(offset_, 0U);
798 DCHECK(index_.IsRegisterPair());
799 // UnsafeGet's offset location is a register pair, the low
800 // part contains the correct offset.
801 index = index_.ToLow();
802 }
803 }
804
805 // We're moving two or three locations to locations that could
806 // overlap, so we need a parallel move resolver.
807 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100808 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000809 parallel_move.AddMove(ref_,
810 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100811 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000812 nullptr);
813 parallel_move.AddMove(obj_,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100815 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000816 nullptr);
817 if (index.IsValid()) {
818 parallel_move.AddMove(index,
819 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100820 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 nullptr);
822 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
823 } else {
824 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
825 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
826 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100827 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 CheckEntrypointTypes<
829 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
830 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
831
832 RestoreLiveRegisters(codegen, locations);
833 __ jmp(GetExitLabel());
834 }
835
836 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
837
838 private:
839 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
840 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
841 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
842 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
843 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
844 return static_cast<Register>(i);
845 }
846 }
847 // We shall never fail to find a free caller-save register, as
848 // there are more than two core caller-save registers on x86
849 // (meaning it is possible to find one which is different from
850 // `ref` and `obj`).
851 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
852 LOG(FATAL) << "Could not find a free caller-save register";
853 UNREACHABLE();
854 }
855
Roland Levillain0d5a2812015-11-13 10:07:31 +0000856 const Location out_;
857 const Location ref_;
858 const Location obj_;
859 const uint32_t offset_;
860 // An additional location containing an index to an array.
861 // Only used for HArrayGet and the UnsafeGetObject &
862 // UnsafeGetObjectVolatile intrinsics.
863 const Location index_;
864
865 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
866};
867
868// Slow path generating a read barrier for a GC root.
869class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
870 public:
871 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000872 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000873 DCHECK(kEmitCompilerReadBarrier);
874 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000875
876 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
877 LocationSummary* locations = instruction_->GetLocations();
878 Register reg_out = out_.AsRegister<Register>();
879 DCHECK(locations->CanCall());
880 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000881 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
882 << "Unexpected instruction in read barrier for GC root slow path: "
883 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000884
885 __ Bind(GetEntryLabel());
886 SaveLiveRegisters(codegen, locations);
887
888 InvokeRuntimeCallingConvention calling_convention;
889 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
890 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100891 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000892 instruction_,
893 instruction_->GetDexPc(),
894 this);
895 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
896 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
897
898 RestoreLiveRegisters(codegen, locations);
899 __ jmp(GetExitLabel());
900 }
901
902 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
903
904 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 const Location out_;
906 const Location root_;
907
908 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
909};
910
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100911#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100912// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
913#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100914
Aart Bike9f37602015-10-09 11:15:55 -0700915inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700916 switch (cond) {
917 case kCondEQ: return kEqual;
918 case kCondNE: return kNotEqual;
919 case kCondLT: return kLess;
920 case kCondLE: return kLessEqual;
921 case kCondGT: return kGreater;
922 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700923 case kCondB: return kBelow;
924 case kCondBE: return kBelowEqual;
925 case kCondA: return kAbove;
926 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700927 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928 LOG(FATAL) << "Unreachable";
929 UNREACHABLE();
930}
931
Aart Bike9f37602015-10-09 11:15:55 -0700932// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
934 switch (cond) {
935 case kCondEQ: return kEqual;
936 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700937 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938 case kCondLT: return kBelow;
939 case kCondLE: return kBelowEqual;
940 case kCondGT: return kAbove;
941 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700942 // Unsigned remain unchanged.
943 case kCondB: return kBelow;
944 case kCondBE: return kBelowEqual;
945 case kCondA: return kAbove;
946 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100947 }
948 LOG(FATAL) << "Unreachable";
949 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700950}
951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100952void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100953 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100954}
955
956void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
962 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
967 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100968}
969
Mark Mendell7c8d0092015-01-26 11:21:33 -0500970size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700971 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700972 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700973 } else {
974 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
975 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500976 return GetFloatingPointSpillSlotSize();
977}
978
979size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700980 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700981 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700982 } else {
983 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
984 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500985 return GetFloatingPointSpillSlotSize();
986}
987
Calin Juravle175dc732015-08-25 15:42:32 +0100988void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
989 HInstruction* instruction,
990 uint32_t dex_pc,
991 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100992 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100993 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
994 if (EntrypointRequiresStackMap(entrypoint)) {
995 RecordPcInfo(instruction, dex_pc, slow_path);
996 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100997}
998
Roland Levillaindec8f632016-07-22 17:10:06 +0100999void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1000 HInstruction* instruction,
1001 SlowPathCode* slow_path) {
1002 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001003 GenerateInvokeRuntime(entry_point_offset);
1004}
1005
1006void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001007 __ fs()->call(Address::Absolute(entry_point_offset));
1008}
1009
Mark Mendellfb8d2792015-03-31 22:16:59 -04001010CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001011 const X86InstructionSetFeatures& isa_features,
1012 const CompilerOptions& compiler_options,
1013 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001014 : CodeGenerator(graph,
1015 kNumberOfCpuRegisters,
1016 kNumberOfXmmRegisters,
1017 kNumberOfRegisterPairs,
1018 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1019 arraysize(kCoreCalleeSaves))
1020 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001021 0,
1022 compiler_options,
1023 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001026 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001027 move_resolver_(graph->GetAllocator(), this),
1028 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001029 isa_features_(isa_features),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001030 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1031 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1032 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1033 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001034 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001035 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1036 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1037 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001038 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001039 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001040 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001041 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001042 // Use a fake return address register to mimic Quick.
1043 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001044}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001045
David Brazdil58282f42016-01-14 12:45:10 +00001046void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001047 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001048 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049}
1050
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001051InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001052 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001053 assembler_(codegen->GetAssembler()),
1054 codegen_(codegen) {}
1055
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001057 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058}
1059
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001060void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001062 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001063 bool skip_overflow_check =
1064 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001065 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001066
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001067 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1068 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1069 Immediate(1));
1070 }
1071
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001072 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001073 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1074 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001075 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001076 }
1077
Mark Mendell5f874182015-03-04 15:42:45 -05001078 if (HasEmptyFrame()) {
1079 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001080 }
Mark Mendell5f874182015-03-04 15:42:45 -05001081
1082 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1083 Register reg = kCoreCalleeSaves[i];
1084 if (allocated_registers_.ContainsCoreRegister(reg)) {
1085 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 __ cfi().AdjustCFAOffset(kX86WordSize);
1087 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001088 }
1089 }
1090
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001091 int adjust = GetFrameSize() - FrameEntrySpillSize();
1092 __ subl(ESP, Immediate(adjust));
1093 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001094 // Save the current method if we need it. Note that we do not
1095 // do this in HCurrentMethod, as the instruction might have been removed
1096 // in the SSA graph.
1097 if (RequiresCurrentMethod()) {
1098 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1099 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001100
1101 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1102 // Initialize should_deoptimize flag to 0.
1103 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1104 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001105}
1106
1107void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001108 __ cfi().RememberState();
1109 if (!HasEmptyFrame()) {
1110 int adjust = GetFrameSize() - FrameEntrySpillSize();
1111 __ addl(ESP, Immediate(adjust));
1112 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001113
David Srbeckyc34dc932015-04-12 09:27:43 +01001114 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1115 Register reg = kCoreCalleeSaves[i];
1116 if (allocated_registers_.ContainsCoreRegister(reg)) {
1117 __ popl(reg);
1118 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1119 __ cfi().Restore(DWARFReg(reg));
1120 }
Mark Mendell5f874182015-03-04 15:42:45 -05001121 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001122 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001123 __ ret();
1124 __ cfi().RestoreState();
1125 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001126}
1127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001128void CodeGeneratorX86::Bind(HBasicBlock* block) {
1129 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001130}
1131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001133 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001134 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001135 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001136 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137 case DataType::Type::kInt8:
1138 case DataType::Type::kUint16:
1139 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001140 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001142 return Location::RegisterLocation(EAX);
1143
Aart Bik66c158e2018-01-31 12:55:04 -08001144 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::RegisterPairLocation(EAX, EDX);
1147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001149 return Location::NoLocation();
1150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 case DataType::Type::kFloat64:
1152 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001153 return Location::FpuRegisterLocation(XMM0);
1154 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001155
1156 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001157}
1158
1159Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1160 return Location::RegisterLocation(kMethodRegisterArgument);
1161}
1162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001163Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001164 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001165 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001166 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001167 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001168 case DataType::Type::kInt8:
1169 case DataType::Type::kUint16:
1170 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001171 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001175 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001179 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 uint32_t index = gp_index_;
1183 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001184 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001186 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1187 calling_convention.GetRegisterPairAt(index));
1188 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001190 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1191 }
1192 }
1193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001195 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001196 stack_index_++;
1197 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1198 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1199 } else {
1200 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1201 }
1202 }
1203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001205 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001206 stack_index_ += 2;
1207 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1208 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1209 } else {
1210 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211 }
1212 }
1213
Aart Bik66c158e2018-01-31 12:55:04 -08001214 case DataType::Type::kUint32:
1215 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001217 LOG(FATAL) << "Unexpected parameter type " << type;
1218 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001219 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001220 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001221}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001222
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223void CodeGeneratorX86::Move32(Location destination, Location source) {
1224 if (source.Equals(destination)) {
1225 return;
1226 }
1227 if (destination.IsRegister()) {
1228 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 } else {
1233 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else if (destination.IsFpuRegister()) {
1237 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 } else {
1242 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001246 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001248 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001249 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001250 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001251 } else if (source.IsConstant()) {
1252 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001253 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001254 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 } else {
1256 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001257 __ pushl(Address(ESP, source.GetStackIndex()));
1258 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 }
1260 }
1261}
1262
1263void CodeGeneratorX86::Move64(Location destination, Location source) {
1264 if (source.Equals(destination)) {
1265 return;
1266 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 if (destination.IsRegisterPair()) {
1268 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001269 EmitParallelMoves(
1270 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1271 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001274 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001275 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1278 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1279 __ psrlq(src_reg, Immediate(32));
1280 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001282 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001284 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1285 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1287 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001288 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001289 if (source.IsFpuRegister()) {
1290 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1291 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001292 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001294 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001295 // Create stack space for 2 elements.
1296 __ subl(ESP, Immediate(2 * elem_size));
1297 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1298 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1299 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1300 // And remove the temporary stack space we allocated.
1301 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001302 } else {
1303 LOG(FATAL) << "Unimplemented";
1304 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001305 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001306 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001307 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001308 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001309 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001310 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001311 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001312 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001313 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001314 } else if (source.IsConstant()) {
1315 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001316 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1317 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001318 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001319 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1320 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001322 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001323 EmitParallelMoves(
1324 Location::StackSlot(source.GetStackIndex()),
1325 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001326 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001327 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001328 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001330 }
1331 }
1332}
1333
Calin Juravle175dc732015-08-25 15:42:32 +01001334void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1335 DCHECK(location.IsRegister());
1336 __ movl(location.AsRegister<Register>(), Immediate(value));
1337}
1338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001339void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001340 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001341 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1342 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1343 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001345 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001346 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001347 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001348}
1349
1350void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1351 if (location.IsRegister()) {
1352 locations->AddTemp(location);
1353 } else if (location.IsRegisterPair()) {
1354 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1355 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1356 } else {
1357 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1358 }
1359}
1360
David Brazdilfc6a86a2015-06-26 10:33:45 +00001361void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001362 if (successor->IsExitBlock()) {
1363 DCHECK(got->GetPrevious()->AlwaysThrows());
1364 return; // no code needed
1365 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001366
1367 HBasicBlock* block = got->GetBlock();
1368 HInstruction* previous = got->GetPrevious();
1369
1370 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001371 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001372 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1373 __ pushl(EAX);
1374 __ movl(EAX, Address(ESP, kX86WordSize));
1375 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1376 __ popl(EAX);
1377 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001378 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1379 return;
1380 }
1381
1382 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1383 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1384 }
1385 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001386 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001387 }
1388}
1389
David Brazdilfc6a86a2015-06-26 10:33:45 +00001390void LocationsBuilderX86::VisitGoto(HGoto* got) {
1391 got->SetLocations(nullptr);
1392}
1393
1394void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1395 HandleGoto(got, got->GetSuccessor());
1396}
1397
1398void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1399 try_boundary->SetLocations(nullptr);
1400}
1401
1402void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1403 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1404 if (!successor->IsExitBlock()) {
1405 HandleGoto(try_boundary, successor);
1406 }
1407}
1408
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001411}
1412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001413void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001414}
1415
Mark Mendell152408f2015-12-31 12:28:50 -05001416template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001417void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001418 LabelType* true_label,
1419 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 if (cond->IsFPConditionTrueIfNaN()) {
1421 __ j(kUnordered, true_label);
1422 } else if (cond->IsFPConditionFalseIfNaN()) {
1423 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001424 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001426}
1427
Mark Mendell152408f2015-12-31 12:28:50 -05001428template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001429void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001430 LabelType* true_label,
1431 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001432 LocationSummary* locations = cond->GetLocations();
1433 Location left = locations->InAt(0);
1434 Location right = locations->InAt(1);
1435 IfCondition if_cond = cond->GetCondition();
1436
Mark Mendellc4701932015-04-10 13:18:51 -04001437 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001439 IfCondition true_high_cond = if_cond;
1440 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001441 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001442
1443 // Set the conditions for the test, remembering that == needs to be
1444 // decided using the low words.
1445 switch (if_cond) {
1446 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001447 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001449 break;
1450 case kCondLT:
1451 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001452 break;
1453 case kCondLE:
1454 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001455 break;
1456 case kCondGT:
1457 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001458 break;
1459 case kCondGE:
1460 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001461 break;
Aart Bike9f37602015-10-09 11:15:55 -07001462 case kCondB:
1463 false_high_cond = kCondA;
1464 break;
1465 case kCondBE:
1466 true_high_cond = kCondB;
1467 break;
1468 case kCondA:
1469 false_high_cond = kCondB;
1470 break;
1471 case kCondAE:
1472 true_high_cond = kCondA;
1473 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001474 }
1475
1476 if (right.IsConstant()) {
1477 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001478 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001479 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001480
Aart Bika19616e2016-02-01 18:57:58 -08001481 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001484 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001485 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(true_high_cond), true_label);
1488 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001489 }
1490 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001491 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001492 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001493 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001494 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001495
1496 __ cmpl(left_high, right_high);
1497 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001498 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001499 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001500 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001501 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001502 __ j(X86Condition(true_high_cond), true_label);
1503 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001504 }
1505 // Must be equal high, so compare the lows.
1506 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001507 } else {
1508 DCHECK(right.IsDoubleStackSlot());
1509 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1510 if (if_cond == kCondNE) {
1511 __ j(X86Condition(true_high_cond), true_label);
1512 } else if (if_cond == kCondEQ) {
1513 __ j(X86Condition(false_high_cond), false_label);
1514 } else {
1515 __ j(X86Condition(true_high_cond), true_label);
1516 __ j(X86Condition(false_high_cond), false_label);
1517 }
1518 // Must be equal high, so compare the lows.
1519 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001520 }
1521 // The last comparison might be unsigned.
1522 __ j(final_condition, true_label);
1523}
1524
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001525void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1526 Location rhs,
1527 HInstruction* insn,
1528 bool is_double) {
1529 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1530 if (is_double) {
1531 if (rhs.IsFpuRegister()) {
1532 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1533 } else if (const_area != nullptr) {
1534 DCHECK(const_area->IsEmittedAtUseSite());
1535 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1536 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001537 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1538 const_area->GetBaseMethodAddress(),
1539 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001540 } else {
1541 DCHECK(rhs.IsDoubleStackSlot());
1542 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1543 }
1544 } else {
1545 if (rhs.IsFpuRegister()) {
1546 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1547 } else if (const_area != nullptr) {
1548 DCHECK(const_area->IsEmittedAtUseSite());
1549 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1550 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001551 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1552 const_area->GetBaseMethodAddress(),
1553 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001554 } else {
1555 DCHECK(rhs.IsStackSlot());
1556 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1557 }
1558 }
1559}
1560
Mark Mendell152408f2015-12-31 12:28:50 -05001561template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001562void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001563 LabelType* true_target_in,
1564 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001565 // Generated branching requires both targets to be explicit. If either of the
1566 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001567 LabelType fallthrough_target;
1568 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1569 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001570
Mark Mendellc4701932015-04-10 13:18:51 -04001571 LocationSummary* locations = condition->GetLocations();
1572 Location left = locations->InAt(0);
1573 Location right = locations->InAt(1);
1574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001575 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001576 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001578 GenerateLongComparesAndJumps(condition, true_target, false_target);
1579 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001580 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001581 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001582 GenerateFPJumps(condition, true_target, false_target);
1583 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001585 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001586 GenerateFPJumps(condition, true_target, false_target);
1587 break;
1588 default:
1589 LOG(FATAL) << "Unexpected compare type " << type;
1590 }
1591
David Brazdil0debae72015-11-12 18:37:00 +00001592 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001593 __ jmp(false_target);
1594 }
David Brazdil0debae72015-11-12 18:37:00 +00001595
1596 if (fallthrough_target.IsLinked()) {
1597 __ Bind(&fallthrough_target);
1598 }
Mark Mendellc4701932015-04-10 13:18:51 -04001599}
1600
David Brazdil0debae72015-11-12 18:37:00 +00001601static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1602 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1603 // are set only strictly before `branch`. We can't use the eflags on long/FP
1604 // conditions if they are materialized due to the complex branching.
1605 return cond->IsCondition() &&
1606 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1608 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001609}
1610
Mark Mendell152408f2015-12-31 12:28:50 -05001611template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001612void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001613 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001614 LabelType* true_target,
1615 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001616 HInstruction* cond = instruction->InputAt(condition_input_index);
1617
1618 if (true_target == nullptr && false_target == nullptr) {
1619 // Nothing to do. The code always falls through.
1620 return;
1621 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001622 // Constant condition, statically compared against "true" (integer value 1).
1623 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001624 if (true_target != nullptr) {
1625 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001626 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001627 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001628 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001629 if (false_target != nullptr) {
1630 __ jmp(false_target);
1631 }
1632 }
1633 return;
1634 }
1635
1636 // The following code generates these patterns:
1637 // (1) true_target == nullptr && false_target != nullptr
1638 // - opposite condition true => branch to false_target
1639 // (2) true_target != nullptr && false_target == nullptr
1640 // - condition true => branch to true_target
1641 // (3) true_target != nullptr && false_target != nullptr
1642 // - condition true => branch to true_target
1643 // - branch to false_target
1644 if (IsBooleanValueOrMaterializedCondition(cond)) {
1645 if (AreEflagsSetFrom(cond, instruction)) {
1646 if (true_target == nullptr) {
1647 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1648 } else {
1649 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1650 }
1651 } else {
1652 // Materialized condition, compare against 0.
1653 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1654 if (lhs.IsRegister()) {
1655 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1656 } else {
1657 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1658 }
1659 if (true_target == nullptr) {
1660 __ j(kEqual, false_target);
1661 } else {
1662 __ j(kNotEqual, true_target);
1663 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001664 }
1665 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001666 // Condition has not been materialized, use its inputs as the comparison and
1667 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001668 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001669
1670 // If this is a long or FP comparison that has been folded into
1671 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 DataType::Type type = condition->InputAt(0)->GetType();
1673 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001674 GenerateCompareTestAndBranch(condition, true_target, false_target);
1675 return;
1676 }
1677
1678 Location lhs = condition->GetLocations()->InAt(0);
1679 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001680 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001681 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001682 if (true_target == nullptr) {
1683 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1684 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001685 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001686 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001687 }
David Brazdil0debae72015-11-12 18:37:00 +00001688
1689 // If neither branch falls through (case 3), the conditional branch to `true_target`
1690 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1691 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001692 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001693 }
1694}
1695
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001697 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001698 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001699 locations->SetInAt(0, Location::Any());
1700 }
1701}
1702
1703void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001704 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1705 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1706 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1707 nullptr : codegen_->GetLabelOf(true_successor);
1708 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1709 nullptr : codegen_->GetLabelOf(false_successor);
1710 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001711}
1712
1713void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001714 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001716 InvokeRuntimeCallingConvention calling_convention;
1717 RegisterSet caller_saves = RegisterSet::Empty();
1718 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1719 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001720 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001721 locations->SetInAt(0, Location::Any());
1722 }
1723}
1724
1725void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001726 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001727 GenerateTestAndBranch<Label>(deoptimize,
1728 /* condition_input_index */ 0,
1729 slow_path->GetEntryLabel(),
1730 /* false_target */ nullptr);
1731}
1732
Mingyao Yang063fc772016-08-02 11:02:54 -07001733void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001734 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001735 LocationSummary(flag, LocationSummary::kNoCall);
1736 locations->SetOut(Location::RequiresRegister());
1737}
1738
1739void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1740 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1741 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1742}
1743
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744static bool SelectCanUseCMOV(HSelect* select) {
1745 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001747 return false;
1748 }
1749
1750 // A FP condition doesn't generate the single CC that we need.
1751 // In 32 bit mode, a long condition doesn't generate a single CC either.
1752 HInstruction* condition = select->GetCondition();
1753 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 DataType::Type compare_type = condition->InputAt(0)->GetType();
1755 if (compare_type == DataType::Type::kInt64 ||
1756 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001757 return false;
1758 }
1759 }
1760
1761 // We can generate a CMOV for this Select.
1762 return true;
1763}
1764
David Brazdil74eb1b22015-12-14 11:44:01 +00001765void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001766 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001768 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001769 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001770 } else {
1771 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772 if (SelectCanUseCMOV(select)) {
1773 if (select->InputAt(1)->IsConstant()) {
1774 // Cmov can't handle a constant value.
1775 locations->SetInAt(1, Location::RequiresRegister());
1776 } else {
1777 locations->SetInAt(1, Location::Any());
1778 }
1779 } else {
1780 locations->SetInAt(1, Location::Any());
1781 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001782 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001783 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1784 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001785 }
1786 locations->SetOut(Location::SameAsFirstInput());
1787}
1788
1789void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1790 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001791 DCHECK(locations->InAt(0).Equals(locations->Out()));
1792 if (SelectCanUseCMOV(select)) {
1793 // If both the condition and the source types are integer, we can generate
1794 // a CMOV to implement Select.
1795
1796 HInstruction* select_condition = select->GetCondition();
1797 Condition cond = kNotEqual;
1798
1799 // Figure out how to test the 'condition'.
1800 if (select_condition->IsCondition()) {
1801 HCondition* condition = select_condition->AsCondition();
1802 if (!condition->IsEmittedAtUseSite()) {
1803 // This was a previously materialized condition.
1804 // Can we use the existing condition code?
1805 if (AreEflagsSetFrom(condition, select)) {
1806 // Materialization was the previous instruction. Condition codes are right.
1807 cond = X86Condition(condition->GetCondition());
1808 } else {
1809 // No, we have to recreate the condition code.
1810 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1811 __ testl(cond_reg, cond_reg);
1812 }
1813 } else {
1814 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1816 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001817 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001818 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001819 cond = X86Condition(condition->GetCondition());
1820 }
1821 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001822 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001823 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1824 __ testl(cond_reg, cond_reg);
1825 }
1826
1827 // If the condition is true, overwrite the output, which already contains false.
1828 Location false_loc = locations->InAt(0);
1829 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001831 // 64 bit conditional move.
1832 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1833 Register false_low = false_loc.AsRegisterPairLow<Register>();
1834 if (true_loc.IsRegisterPair()) {
1835 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1836 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1837 } else {
1838 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1839 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1840 }
1841 } else {
1842 // 32 bit conditional move.
1843 Register false_reg = false_loc.AsRegister<Register>();
1844 if (true_loc.IsRegister()) {
1845 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1846 } else {
1847 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1848 }
1849 }
1850 } else {
1851 NearLabel false_target;
1852 GenerateTestAndBranch<NearLabel>(
1853 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1854 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1855 __ Bind(&false_target);
1856 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001857}
1858
David Srbecky0cf44932015-12-09 14:09:59 +00001859void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001860 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001861}
1862
David Srbeckyd28f4a02016-03-14 17:14:24 +00001863void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1864 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001865}
1866
1867void CodeGeneratorX86::GenerateNop() {
1868 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001869}
1870
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001871void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001873 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001874 // Handle the long/FP comparisons made in instruction simplification.
1875 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001877 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001878 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001879 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001880 locations->SetOut(Location::RequiresRegister());
1881 }
1882 break;
1883 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001884 case DataType::Type::kFloat32:
1885 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001886 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001887 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1888 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1889 } else if (cond->InputAt(1)->IsConstant()) {
1890 locations->SetInAt(1, Location::RequiresFpuRegister());
1891 } else {
1892 locations->SetInAt(1, Location::Any());
1893 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001894 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001895 locations->SetOut(Location::RequiresRegister());
1896 }
1897 break;
1898 }
1899 default:
1900 locations->SetInAt(0, Location::RequiresRegister());
1901 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001902 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001903 // We need a byte register.
1904 locations->SetOut(Location::RegisterLocation(ECX));
1905 }
1906 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908}
1909
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001910void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001911 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001913 }
Mark Mendellc4701932015-04-10 13:18:51 -04001914
1915 LocationSummary* locations = cond->GetLocations();
1916 Location lhs = locations->InAt(0);
1917 Location rhs = locations->InAt(1);
1918 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001919 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001920
1921 switch (cond->InputAt(0)->GetType()) {
1922 default: {
1923 // Integer case.
1924
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001925 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001926 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001927 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001928 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001929 return;
1930 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001932 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1933 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001935 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001936 GenerateFPJumps(cond, &true_label, &false_label);
1937 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001939 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001940 GenerateFPJumps(cond, &true_label, &false_label);
1941 break;
1942 }
1943
1944 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001945 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001946
Roland Levillain4fa13f62015-07-06 18:11:54 +01001947 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001948 __ Bind(&false_label);
1949 __ xorl(reg, reg);
1950 __ jmp(&done_label);
1951
Roland Levillain4fa13f62015-07-06 18:11:54 +01001952 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001953 __ Bind(&true_label);
1954 __ movl(reg, Immediate(1));
1955 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001956}
1957
1958void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004}
2005
Aart Bike9f37602015-10-09 11:15:55 -07002006void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002008}
2009
2010void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002039 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002040 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002041 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002042}
2043
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002044void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002045 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002046}
2047
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002048void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2049 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002050 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002051 locations->SetOut(Location::ConstantLocation(constant));
2052}
2053
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002054void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002055 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002056}
2057
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002059 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002060 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002061 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062}
2063
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002064void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 // Will be generated at use site.
2066}
2067
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2069 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002070 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071 locations->SetOut(Location::ConstantLocation(constant));
2072}
2073
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002074void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 // Will be generated at use site.
2076}
2077
2078void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002080 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081 locations->SetOut(Location::ConstantLocation(constant));
2082}
2083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002084void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 // Will be generated at use site.
2086}
2087
Igor Murashkind01745e2017-04-05 16:40:31 -07002088void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2089 constructor_fence->SetLocations(nullptr);
2090}
2091
2092void InstructionCodeGeneratorX86::VisitConstructorFence(
2093 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2094 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2095}
2096
Calin Juravle27df7582015-04-17 19:12:31 +01002097void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2098 memory_barrier->SetLocations(nullptr);
2099}
2100
2101void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002102 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002103}
2104
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002105void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002106 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002107}
2108
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002109void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002111}
2112
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002113void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002114 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002115 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002116 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002117 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002119 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kInt8:
2121 case DataType::Type::kUint16:
2122 case DataType::Type::kInt16:
2123 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 break;
2126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002128 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002130 break;
2131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 case DataType::Type::kFloat32:
2133 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002134 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002136 break;
2137
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002139 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141}
2142
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002143void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002144 if (kIsDebugBuild) {
2145 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002146 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002148 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 case DataType::Type::kInt8:
2150 case DataType::Type::kUint16:
2151 case DataType::Type::kInt16:
2152 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002154 break;
2155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002156 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002157 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2158 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002159 break;
2160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 case DataType::Type::kFloat32:
2162 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002164 break;
2165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002166 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002167 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168 }
2169 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002170 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002171}
2172
Calin Juravle175dc732015-08-25 15:42:32 +01002173void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2174 // The trampoline uses the same calling convention as dex calling conventions,
2175 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2176 // the method_idx.
2177 HandleInvoke(invoke);
2178}
2179
2180void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2181 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2182}
2183
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002184void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002185 // Explicit clinit checks triggered by static invokes must have been pruned by
2186 // art::PrepareForRegisterAllocation.
2187 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002188
Mark Mendellfb8d2792015-03-31 22:16:59 -04002189 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002190 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002191 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002192 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002193 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002194 return;
2195 }
2196
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002197 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002198
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002199 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002200 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002201 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002202 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002203}
2204
Mark Mendell09ed1a32015-03-25 08:30:06 -04002205static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2206 if (invoke->GetLocations()->Intrinsified()) {
2207 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2208 intrinsic.Dispatch(invoke);
2209 return true;
2210 }
2211 return false;
2212}
2213
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002214void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002215 // Explicit clinit checks triggered by static invokes must have been pruned by
2216 // art::PrepareForRegisterAllocation.
2217 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002218
Mark Mendell09ed1a32015-03-25 08:30:06 -04002219 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2220 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002221 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002222
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002223 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002224 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002225 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002226}
2227
2228void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002229 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2230 if (intrinsic.TryDispatch(invoke)) {
2231 return;
2232 }
2233
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002234 HandleInvoke(invoke);
2235}
2236
2237void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002238 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002239 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002240}
2241
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002242void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002243 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2244 return;
2245 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002246
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002247 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002248 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249}
2250
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002251void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002252 // This call to HandleInvoke allocates a temporary (core) register
2253 // which is also used to transfer the hidden argument from FP to
2254 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255 HandleInvoke(invoke);
2256 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002257 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258}
2259
2260void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2261 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 LocationSummary* locations = invoke->GetLocations();
2263 Register temp = locations->GetTemp(0).AsRegister<Register>();
2264 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002265 Location receiver = locations->InAt(0);
2266 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2267
Roland Levillain0d5a2812015-11-13 10:07:31 +00002268 // Set the hidden argument. This is safe to do this here, as XMM7
2269 // won't be modified thereafter, before the `call` instruction.
2270 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002272 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 if (receiver.IsStackSlot()) {
2275 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002276 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 __ movl(temp, Address(temp, class_offset));
2278 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002279 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002280 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281 }
Roland Levillain4d027112015-07-01 15:41:14 +01002282 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002283 // Instead of simply (possibly) unpoisoning `temp` here, we should
2284 // emit a read barrier for the previous class reference load.
2285 // However this is not required in practice, as this is an
2286 // intermediate/temporary reference and because the current
2287 // concurrent copying collector keeps the from-space memory
2288 // intact/accessible until the end of the marking phase (the
2289 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002290 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002291 // temp = temp->GetAddressOfIMT()
2292 __ movl(temp,
2293 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002294 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002295 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002296 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297 __ movl(temp, Address(temp, method_offset));
2298 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002299 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002300 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002301
2302 DCHECK(!codegen_->IsLeafMethod());
2303 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2304}
2305
Orion Hodsonac141392017-01-13 11:53:47 +00002306void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2307 HandleInvoke(invoke);
2308}
2309
2310void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2311 codegen_->GenerateInvokePolymorphicCall(invoke);
2312}
2313
Roland Levillain88cb1752014-10-20 16:36:47 +01002314void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2315 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002316 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 case DataType::Type::kInt32:
2319 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002320 locations->SetInAt(0, Location::RequiresRegister());
2321 locations->SetOut(Location::SameAsFirstInput());
2322 break;
2323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002325 locations->SetInAt(0, Location::RequiresFpuRegister());
2326 locations->SetOut(Location::SameAsFirstInput());
2327 locations->AddTemp(Location::RequiresRegister());
2328 locations->AddTemp(Location::RequiresFpuRegister());
2329 break;
2330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002332 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002333 locations->SetOut(Location::SameAsFirstInput());
2334 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002335 break;
2336
2337 default:
2338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2339 }
2340}
2341
2342void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2343 LocationSummary* locations = neg->GetLocations();
2344 Location out = locations->Out();
2345 Location in = locations->InAt(0);
2346 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002351 break;
2352
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002354 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002355 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002356 __ negl(out.AsRegisterPairLow<Register>());
2357 // Negation is similar to subtraction from zero. The least
2358 // significant byte triggers a borrow when it is different from
2359 // zero; to take it into account, add 1 to the most significant
2360 // byte if the carry flag (CF) is set to 1 after the first NEGL
2361 // operation.
2362 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2363 __ negl(out.AsRegisterPairHigh<Register>());
2364 break;
2365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002367 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 Register constant = locations->GetTemp(0).AsRegister<Register>();
2369 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002370 // Implement float negation with an exclusive or with value
2371 // 0x80000000 (mask for bit 31, representing the sign of a
2372 // single-precision floating-point number).
2373 __ movl(constant, Immediate(INT32_C(0x80000000)));
2374 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002376 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002377 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002380 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002382 // Implement double negation with an exclusive or with value
2383 // 0x8000000000000000 (mask for bit 63, representing the sign of
2384 // a double-precision floating-point number).
2385 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002386 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002387 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002388 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002389
2390 default:
2391 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2392 }
2393}
2394
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002395void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2396 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002397 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002399 locations->SetInAt(0, Location::RequiresFpuRegister());
2400 locations->SetInAt(1, Location::RequiresRegister());
2401 locations->SetOut(Location::SameAsFirstInput());
2402 locations->AddTemp(Location::RequiresFpuRegister());
2403}
2404
2405void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2406 LocationSummary* locations = neg->GetLocations();
2407 Location out = locations->Out();
2408 DCHECK(locations->InAt(0).Equals(out));
2409
2410 Register constant_area = locations->InAt(1).AsRegister<Register>();
2411 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002413 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2414 neg->GetBaseMethodAddress(),
2415 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002416 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2417 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002418 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2419 neg->GetBaseMethodAddress(),
2420 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002421 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2422 }
2423}
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 DataType::Type result_type = conversion->GetResultType();
2427 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002428 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2429 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002430
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002431 // The float-to-long and double-to-long type conversions rely on a
2432 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002433 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002434 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2435 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002436 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002437 : LocationSummary::kNoCall;
2438 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002439 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002440
Roland Levillaindff1f282014-11-05 14:15:05 +00002441 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002442 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002444 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002445 case DataType::Type::kUint8:
2446 case DataType::Type::kInt8:
2447 case DataType::Type::kUint16:
2448 case DataType::Type::kInt16:
2449 case DataType::Type::kInt32:
2450 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2451 // Make the output overlap to please the register allocator. This greatly simplifies
2452 // the validation of the linear scan implementation
2453 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2454 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002455 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002456 HInstruction* input = conversion->InputAt(0);
2457 Location input_location = input->IsConstant()
2458 ? Location::ConstantLocation(input->AsConstant())
2459 : Location::RegisterPairLocation(EAX, EDX);
2460 locations->SetInAt(0, input_location);
2461 // Make the output overlap to please the register allocator. This greatly simplifies
2462 // the validation of the linear scan implementation
2463 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2464 break;
2465 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002466
2467 default:
2468 LOG(FATAL) << "Unexpected type conversion from " << input_type
2469 << " to " << result_type;
2470 }
2471 break;
2472
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002473 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002475 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2476 locations->SetInAt(0, Location::Any());
2477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002478 break;
2479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002481 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488 locations->SetInAt(0, Location::RequiresFpuRegister());
2489 locations->SetOut(Location::RequiresRegister());
2490 locations->AddTemp(Location::RequiresFpuRegister());
2491 break;
2492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetOut(Location::RequiresRegister());
2496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002508 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002509 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002510 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 case DataType::Type::kInt16:
2512 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 locations->SetInAt(0, Location::RegisterLocation(EAX));
2514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2515 break;
2516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002517 case DataType::Type::kFloat32:
2518 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002519 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002520 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2521 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2522
Vladimir Marko949c91f2015-01-27 10:48:44 +00002523 // The runtime helper puts the result in EAX, EDX.
2524 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002525 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002526 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002535 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002537 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002539 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kInt16:
2541 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetOut(Location::RequiresFpuRegister());
2544 break;
2545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002547 locations->SetInAt(0, Location::Any());
2548 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002549 break;
2550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002552 locations->SetInAt(0, Location::RequiresFpuRegister());
2553 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002559 }
Roland Levillaincff13742014-11-17 14:32:17 +00002560 break;
2561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002563 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002565 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002567 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002568 case DataType::Type::kInt16:
2569 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002570 locations->SetInAt(0, Location::RequiresRegister());
2571 locations->SetOut(Location::RequiresFpuRegister());
2572 break;
2573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002574 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002575 locations->SetInAt(0, Location::Any());
2576 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002577 break;
2578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002580 locations->SetInAt(0, Location::RequiresFpuRegister());
2581 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002582 break;
2583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 break;
2589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
2594}
2595
2596void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2597 LocationSummary* locations = conversion->GetLocations();
2598 Location out = locations->Out();
2599 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 DataType::Type result_type = conversion->GetResultType();
2601 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002602 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2603 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002604 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002605 case DataType::Type::kUint8:
2606 switch (input_type) {
2607 case DataType::Type::kInt8:
2608 case DataType::Type::kUint16:
2609 case DataType::Type::kInt16:
2610 case DataType::Type::kInt32:
2611 if (in.IsRegister()) {
2612 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2613 } else {
2614 DCHECK(in.GetConstant()->IsIntConstant());
2615 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2616 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2617 }
2618 break;
2619 case DataType::Type::kInt64:
2620 if (in.IsRegisterPair()) {
2621 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2622 } else {
2623 DCHECK(in.GetConstant()->IsLongConstant());
2624 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2625 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2626 }
2627 break;
2628
2629 default:
2630 LOG(FATAL) << "Unexpected type conversion from " << input_type
2631 << " to " << result_type;
2632 }
2633 break;
2634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002636 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002637 case DataType::Type::kUint8:
2638 case DataType::Type::kUint16:
2639 case DataType::Type::kInt16:
2640 case DataType::Type::kInt32:
2641 if (in.IsRegister()) {
2642 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2643 } else {
2644 DCHECK(in.GetConstant()->IsIntConstant());
2645 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2646 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2647 }
2648 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002650 if (in.IsRegisterPair()) {
2651 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2652 } else {
2653 DCHECK(in.GetConstant()->IsLongConstant());
2654 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2655 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2656 }
2657 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663 break;
2664
2665 case DataType::Type::kUint16:
2666 switch (input_type) {
2667 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kInt16:
2669 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002670 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002671 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2672 } else if (in.IsStackSlot()) {
2673 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002674 } else {
2675 DCHECK(in.GetConstant()->IsIntConstant());
2676 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002677 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2678 }
2679 break;
2680 case DataType::Type::kInt64:
2681 if (in.IsRegisterPair()) {
2682 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2683 } else if (in.IsDoubleStackSlot()) {
2684 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2685 } else {
2686 DCHECK(in.GetConstant()->IsLongConstant());
2687 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2688 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002689 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002690 break;
2691
2692 default:
2693 LOG(FATAL) << "Unexpected type conversion from " << input_type
2694 << " to " << result_type;
2695 }
2696 break;
2697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002699 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002701 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002702 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002703 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002704 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002706 } else {
2707 DCHECK(in.GetConstant()->IsIntConstant());
2708 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002710 }
2711 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002712 case DataType::Type::kInt64:
2713 if (in.IsRegisterPair()) {
2714 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2715 } else if (in.IsDoubleStackSlot()) {
2716 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2717 } else {
2718 DCHECK(in.GetConstant()->IsLongConstant());
2719 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2720 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2721 }
2722 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002723
2724 default:
2725 LOG(FATAL) << "Unexpected type conversion from " << input_type
2726 << " to " << result_type;
2727 }
2728 break;
2729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002731 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002733 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002735 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002737 } else {
2738 DCHECK(in.IsConstant());
2739 DCHECK(in.GetConstant()->IsLongConstant());
2740 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002741 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002742 }
2743 break;
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002746 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2747 Register output = out.AsRegister<Register>();
2748 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002749 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002750
2751 __ movl(output, Immediate(kPrimIntMax));
2752 // temp = int-to-float(output)
2753 __ cvtsi2ss(temp, output);
2754 // if input >= temp goto done
2755 __ comiss(input, temp);
2756 __ j(kAboveEqual, &done);
2757 // if input == NaN goto nan
2758 __ j(kUnordered, &nan);
2759 // output = float-to-int-truncate(input)
2760 __ cvttss2si(output, input);
2761 __ jmp(&done);
2762 __ Bind(&nan);
2763 // output = 0
2764 __ xorl(output, output);
2765 __ Bind(&done);
2766 break;
2767 }
2768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002770 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2771 Register output = out.AsRegister<Register>();
2772 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002773 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002774
2775 __ movl(output, Immediate(kPrimIntMax));
2776 // temp = int-to-double(output)
2777 __ cvtsi2sd(temp, output);
2778 // if input >= temp goto done
2779 __ comisd(input, temp);
2780 __ j(kAboveEqual, &done);
2781 // if input == NaN goto nan
2782 __ j(kUnordered, &nan);
2783 // output = double-to-int-truncate(input)
2784 __ cvttsd2si(output, input);
2785 __ jmp(&done);
2786 __ Bind(&nan);
2787 // output = 0
2788 __ xorl(output, output);
2789 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002790 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002791 }
Roland Levillain946e1432014-11-11 17:35:19 +00002792
2793 default:
2794 LOG(FATAL) << "Unexpected type conversion from " << input_type
2795 << " to " << result_type;
2796 }
2797 break;
2798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002802 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002804 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kInt16:
2806 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002807 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2808 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002810 __ cdq();
2811 break;
2812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002814 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002815 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002816 break;
2817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002819 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002820 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 break;
2822
2823 default:
2824 LOG(FATAL) << "Unexpected type conversion from " << input_type
2825 << " to " << result_type;
2826 }
2827 break;
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002830 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002831 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002832 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002834 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kInt16:
2836 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002838 break;
2839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002841 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002842
Roland Levillain232ade02015-04-20 15:14:36 +01002843 // Create stack space for the call to
2844 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2845 // TODO: enhance register allocator to ask for stack temporaries.
2846 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002848 __ subl(ESP, Immediate(adjustment));
2849 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002850
Roland Levillain232ade02015-04-20 15:14:36 +01002851 // Load the value to the FP stack, using temporaries if needed.
2852 PushOntoFPStack(in, 0, adjustment, false, true);
2853
2854 if (out.IsStackSlot()) {
2855 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2856 } else {
2857 __ fstps(Address(ESP, 0));
2858 Location stack_temp = Location::StackSlot(0);
2859 codegen_->Move32(out, stack_temp);
2860 }
2861
2862 // Remove the temporary stack space we allocated.
2863 if (adjustment != 0) {
2864 __ addl(ESP, Immediate(adjustment));
2865 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002866 break;
2867 }
2868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002870 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002876 }
Roland Levillaincff13742014-11-17 14:32:17 +00002877 break;
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002880 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002881 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002882 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002884 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kInt16:
2886 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002888 break;
2889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002891 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002892
Roland Levillain232ade02015-04-20 15:14:36 +01002893 // Create stack space for the call to
2894 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2895 // TODO: enhance register allocator to ask for stack temporaries.
2896 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002898 __ subl(ESP, Immediate(adjustment));
2899 }
2900
2901 // Load the value to the FP stack, using temporaries if needed.
2902 PushOntoFPStack(in, 0, adjustment, false, true);
2903
2904 if (out.IsDoubleStackSlot()) {
2905 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2906 } else {
2907 __ fstpl(Address(ESP, 0));
2908 Location stack_temp = Location::DoubleStackSlot(0);
2909 codegen_->Move64(out, stack_temp);
2910 }
2911
2912 // Remove the temporary stack space we allocated.
2913 if (adjustment != 0) {
2914 __ addl(ESP, Immediate(adjustment));
2915 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002916 break;
2917 }
2918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002920 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002921 break;
2922
2923 default:
2924 LOG(FATAL) << "Unexpected type conversion from " << input_type
2925 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002926 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002927 break;
2928
2929 default:
2930 LOG(FATAL) << "Unexpected type conversion from " << input_type
2931 << " to " << result_type;
2932 }
2933}
2934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002936 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002937 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002940 locations->SetInAt(0, Location::RequiresRegister());
2941 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2943 break;
2944 }
2945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::Any());
2949 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002950 break;
2951 }
2952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kFloat32:
2954 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002956 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2957 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002958 } else if (add->InputAt(1)->IsConstant()) {
2959 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002960 } else {
2961 locations->SetInAt(1, Location::Any());
2962 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002964 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002966
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2969 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002970 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971}
2972
2973void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2974 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002975 Location first = locations->InAt(0);
2976 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002977 Location out = locations->Out();
2978
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002979 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002980 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002982 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2983 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002984 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2985 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002986 } else {
2987 __ leal(out.AsRegister<Register>(), Address(
2988 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2989 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002991 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2992 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2993 __ addl(out.AsRegister<Register>(), Immediate(value));
2994 } else {
2995 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2996 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002997 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002998 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002999 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003000 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003002 }
3003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003005 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003006 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3007 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003008 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003009 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3010 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003011 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003012 } else {
3013 DCHECK(second.IsConstant()) << second;
3014 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3015 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3016 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003017 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003018 break;
3019 }
3020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003021 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003022 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003024 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3025 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003026 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003027 __ addss(first.AsFpuRegister<XmmRegister>(),
3028 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003029 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3030 const_area->GetBaseMethodAddress(),
3031 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003032 } else {
3033 DCHECK(second.IsStackSlot());
3034 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003036 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003037 }
3038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003039 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003042 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3043 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003044 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 __ addsd(first.AsFpuRegister<XmmRegister>(),
3046 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003047 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3048 const_area->GetBaseMethodAddress(),
3049 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003050 } else {
3051 DCHECK(second.IsDoubleStackSlot());
3052 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003053 }
3054 break;
3055 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003056
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003057 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003059 }
3060}
3061
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003062void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003064 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003065 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kInt32:
3067 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003068 locations->SetInAt(0, Location::RequiresRegister());
3069 locations->SetInAt(1, Location::Any());
3070 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003071 break;
3072 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003073 case DataType::Type::kFloat32:
3074 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003075 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003076 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3077 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003078 } else if (sub->InputAt(1)->IsConstant()) {
3079 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003080 } else {
3081 locations->SetInAt(1, Location::Any());
3082 }
Calin Juravle11351682014-10-23 15:38:15 +01003083 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003084 break;
Calin Juravle11351682014-10-23 15:38:15 +01003085 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003086
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087 default:
Calin Juravle11351682014-10-23 15:38:15 +01003088 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003089 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003090}
3091
3092void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3093 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 Location first = locations->InAt(0);
3095 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003096 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003097 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003098 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003100 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003101 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003102 __ subl(first.AsRegister<Register>(),
3103 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003104 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003106 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003107 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003108 }
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003111 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003112 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3113 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003114 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003115 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003116 __ sbbl(first.AsRegisterPairHigh<Register>(),
3117 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003118 } else {
3119 DCHECK(second.IsConstant()) << second;
3120 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3121 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3122 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003123 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003124 break;
3125 }
3126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003128 if (second.IsFpuRegister()) {
3129 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3130 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3131 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003132 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003133 __ subss(first.AsFpuRegister<XmmRegister>(),
3134 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003135 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3136 const_area->GetBaseMethodAddress(),
3137 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003138 } else {
3139 DCHECK(second.IsStackSlot());
3140 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3141 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003142 break;
Calin Juravle11351682014-10-23 15:38:15 +01003143 }
3144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 if (second.IsFpuRegister()) {
3147 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3148 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3149 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003150 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003151 __ subsd(first.AsFpuRegister<XmmRegister>(),
3152 codegen_->LiteralDoubleAddress(
3153 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003154 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003155 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3156 } else {
3157 DCHECK(second.IsDoubleStackSlot());
3158 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3159 }
Calin Juravle11351682014-10-23 15:38:15 +01003160 break;
3161 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003162
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003163 default:
Calin Juravle11351682014-10-23 15:38:15 +01003164 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003165 }
3166}
3167
Calin Juravle34bacdf2014-10-07 20:23:36 +01003168void LocationsBuilderX86::VisitMul(HMul* mul) {
3169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003170 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003172 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 locations->SetInAt(0, Location::RequiresRegister());
3174 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003175 if (mul->InputAt(1)->IsIntConstant()) {
3176 // Can use 3 operand multiply.
3177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3178 } else {
3179 locations->SetOut(Location::SameAsFirstInput());
3180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003182 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 locations->SetInAt(1, Location::Any());
3185 locations->SetOut(Location::SameAsFirstInput());
3186 // Needed for imul on 32bits with 64bits output.
3187 locations->AddTemp(Location::RegisterLocation(EAX));
3188 locations->AddTemp(Location::RegisterLocation(EDX));
3189 break;
3190 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kFloat32:
3192 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003193 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003194 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3195 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003196 } else if (mul->InputAt(1)->IsConstant()) {
3197 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003198 } else {
3199 locations->SetInAt(1, Location::Any());
3200 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204
3205 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003206 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 }
3208}
3209
3210void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3211 LocationSummary* locations = mul->GetLocations();
3212 Location first = locations->InAt(0);
3213 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003214 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215
3216 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003218 // The constant may have ended up in a register, so test explicitly to avoid
3219 // problems where the output may not be the same as the first operand.
3220 if (mul->InputAt(1)->IsIntConstant()) {
3221 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3222 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3223 } else if (second.IsRegister()) {
3224 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226 } else {
3227 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003228 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 }
3231 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003234 Register in1_hi = first.AsRegisterPairHigh<Register>();
3235 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 Register eax = locations->GetTemp(0).AsRegister<Register>();
3237 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238
3239 DCHECK_EQ(EAX, eax);
3240 DCHECK_EQ(EDX, edx);
3241
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003242 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003243 // output: in1
3244 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3245 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3246 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003247 if (second.IsConstant()) {
3248 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003250 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3251 int32_t low_value = Low32Bits(value);
3252 int32_t high_value = High32Bits(value);
3253 Immediate low(low_value);
3254 Immediate high(high_value);
3255
3256 __ movl(eax, high);
3257 // eax <- in1.lo * in2.hi
3258 __ imull(eax, in1_lo);
3259 // in1.hi <- in1.hi * in2.lo
3260 __ imull(in1_hi, low);
3261 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3262 __ addl(in1_hi, eax);
3263 // move in2_lo to eax to prepare for double precision
3264 __ movl(eax, low);
3265 // edx:eax <- in1.lo * in2.lo
3266 __ mull(in1_lo);
3267 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3268 __ addl(in1_hi, edx);
3269 // in1.lo <- (in1.lo * in2.lo)[31:0];
3270 __ movl(in1_lo, eax);
3271 } else if (second.IsRegisterPair()) {
3272 Register in2_hi = second.AsRegisterPairHigh<Register>();
3273 Register in2_lo = second.AsRegisterPairLow<Register>();
3274
3275 __ movl(eax, in2_hi);
3276 // eax <- in1.lo * in2.hi
3277 __ imull(eax, in1_lo);
3278 // in1.hi <- in1.hi * in2.lo
3279 __ imull(in1_hi, in2_lo);
3280 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3281 __ addl(in1_hi, eax);
3282 // move in1_lo to eax to prepare for double precision
3283 __ movl(eax, in1_lo);
3284 // edx:eax <- in1.lo * in2.lo
3285 __ mull(in2_lo);
3286 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3287 __ addl(in1_hi, edx);
3288 // in1.lo <- (in1.lo * in2.lo)[31:0];
3289 __ movl(in1_lo, eax);
3290 } else {
3291 DCHECK(second.IsDoubleStackSlot()) << second;
3292 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3293 Address in2_lo(ESP, second.GetStackIndex());
3294
3295 __ movl(eax, in2_hi);
3296 // eax <- in1.lo * in2.hi
3297 __ imull(eax, in1_lo);
3298 // in1.hi <- in1.hi * in2.lo
3299 __ imull(in1_hi, in2_lo);
3300 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3301 __ addl(in1_hi, eax);
3302 // move in1_lo to eax to prepare for double precision
3303 __ movl(eax, in1_lo);
3304 // edx:eax <- in1.lo * in2.lo
3305 __ mull(in2_lo);
3306 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3307 __ addl(in1_hi, edx);
3308 // in1.lo <- (in1.lo * in2.lo)[31:0];
3309 __ movl(in1_lo, eax);
3310 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311
3312 break;
3313 }
3314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003316 DCHECK(first.Equals(locations->Out()));
3317 if (second.IsFpuRegister()) {
3318 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3319 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3320 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003321 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003322 __ mulss(first.AsFpuRegister<XmmRegister>(),
3323 codegen_->LiteralFloatAddress(
3324 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003325 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003326 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3327 } else {
3328 DCHECK(second.IsStackSlot());
3329 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3330 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003331 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003332 }
3333
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003334 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003335 DCHECK(first.Equals(locations->Out()));
3336 if (second.IsFpuRegister()) {
3337 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3338 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3339 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003340 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003341 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3342 codegen_->LiteralDoubleAddress(
3343 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003344 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3346 } else {
3347 DCHECK(second.IsDoubleStackSlot());
3348 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3349 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003350 break;
3351 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352
3353 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003354 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355 }
3356}
3357
Roland Levillain232ade02015-04-20 15:14:36 +01003358void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3359 uint32_t temp_offset,
3360 uint32_t stack_adjustment,
3361 bool is_fp,
3362 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003363 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003364 DCHECK(!is_wide);
3365 if (is_fp) {
3366 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3367 } else {
3368 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3369 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003370 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003371 DCHECK(is_wide);
3372 if (is_fp) {
3373 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3374 } else {
3375 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3376 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003377 } else {
3378 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003379 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003380 Location stack_temp = Location::StackSlot(temp_offset);
3381 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003382 if (is_fp) {
3383 __ flds(Address(ESP, temp_offset));
3384 } else {
3385 __ filds(Address(ESP, temp_offset));
3386 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003387 } else {
3388 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3389 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003390 if (is_fp) {
3391 __ fldl(Address(ESP, temp_offset));
3392 } else {
3393 __ fildl(Address(ESP, temp_offset));
3394 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003395 }
3396 }
3397}
3398
3399void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 DataType::Type type = rem->GetResultType();
3401 bool is_float = type == DataType::Type::kFloat32;
3402 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403 LocationSummary* locations = rem->GetLocations();
3404 Location first = locations->InAt(0);
3405 Location second = locations->InAt(1);
3406 Location out = locations->Out();
3407
3408 // Create stack space for 2 elements.
3409 // TODO: enhance register allocator to ask for stack temporaries.
3410 __ subl(ESP, Immediate(2 * elem_size));
3411
3412 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003413 const bool is_wide = !is_float;
3414 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3415 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003416
3417 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003418 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003419 __ Bind(&retry);
3420 __ fprem();
3421
3422 // Move FP status to AX.
3423 __ fstsw();
3424
3425 // And see if the argument reduction is complete. This is signaled by the
3426 // C2 FPU flag bit set to 0.
3427 __ andl(EAX, Immediate(kC2ConditionMask));
3428 __ j(kNotEqual, &retry);
3429
3430 // We have settled on the final value. Retrieve it into an XMM register.
3431 // Store FP top of stack to real stack.
3432 if (is_float) {
3433 __ fsts(Address(ESP, 0));
3434 } else {
3435 __ fstl(Address(ESP, 0));
3436 }
3437
3438 // Pop the 2 items from the FP stack.
3439 __ fucompp();
3440
3441 // Load the value from the stack into an XMM register.
3442 DCHECK(out.IsFpuRegister()) << out;
3443 if (is_float) {
3444 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3445 } else {
3446 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3447 }
3448
3449 // And remove the temporary stack space we allocated.
3450 __ addl(ESP, Immediate(2 * elem_size));
3451}
3452
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453
3454void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3455 DCHECK(instruction->IsDiv() || instruction->IsRem());
3456
3457 LocationSummary* locations = instruction->GetLocations();
3458 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003459 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003460
3461 Register out_register = locations->Out().AsRegister<Register>();
3462 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003463 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464
3465 DCHECK(imm == 1 || imm == -1);
3466
3467 if (instruction->IsRem()) {
3468 __ xorl(out_register, out_register);
3469 } else {
3470 __ movl(out_register, input_register);
3471 if (imm == -1) {
3472 __ negl(out_register);
3473 }
3474 }
3475}
3476
3477
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003478void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479 LocationSummary* locations = instruction->GetLocations();
3480
3481 Register out_register = locations->Out().AsRegister<Register>();
3482 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003483 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003484 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3485 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 Register num = locations->GetTemp(0).AsRegister<Register>();
3488
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003489 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 __ testl(input_register, input_register);
3491 __ cmovl(kGreaterEqual, num, input_register);
3492 int shift = CTZ(imm);
3493 __ sarl(num, Immediate(shift));
3494
3495 if (imm < 0) {
3496 __ negl(num);
3497 }
3498
3499 __ movl(out_register, num);
3500}
3501
3502void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3503 DCHECK(instruction->IsDiv() || instruction->IsRem());
3504
3505 LocationSummary* locations = instruction->GetLocations();
3506 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3507
3508 Register eax = locations->InAt(0).AsRegister<Register>();
3509 Register out = locations->Out().AsRegister<Register>();
3510 Register num;
3511 Register edx;
3512
3513 if (instruction->IsDiv()) {
3514 edx = locations->GetTemp(0).AsRegister<Register>();
3515 num = locations->GetTemp(1).AsRegister<Register>();
3516 } else {
3517 edx = locations->Out().AsRegister<Register>();
3518 num = locations->GetTemp(0).AsRegister<Register>();
3519 }
3520
3521 DCHECK_EQ(EAX, eax);
3522 DCHECK_EQ(EDX, edx);
3523 if (instruction->IsDiv()) {
3524 DCHECK_EQ(EAX, out);
3525 } else {
3526 DCHECK_EQ(EDX, out);
3527 }
3528
3529 int64_t magic;
3530 int shift;
3531 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3532
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533 // Save the numerator.
3534 __ movl(num, eax);
3535
3536 // EAX = magic
3537 __ movl(eax, Immediate(magic));
3538
3539 // EDX:EAX = magic * numerator
3540 __ imull(num);
3541
3542 if (imm > 0 && magic < 0) {
3543 // EDX += num
3544 __ addl(edx, num);
3545 } else if (imm < 0 && magic > 0) {
3546 __ subl(edx, num);
3547 }
3548
3549 // Shift if needed.
3550 if (shift != 0) {
3551 __ sarl(edx, Immediate(shift));
3552 }
3553
3554 // EDX += 1 if EDX < 0
3555 __ movl(eax, edx);
3556 __ shrl(edx, Immediate(31));
3557 __ addl(edx, eax);
3558
3559 if (instruction->IsRem()) {
3560 __ movl(eax, num);
3561 __ imull(edx, Immediate(imm));
3562 __ subl(eax, edx);
3563 __ movl(edx, eax);
3564 } else {
3565 __ movl(eax, edx);
3566 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003567}
3568
Calin Juravlebacfec32014-11-14 15:54:36 +00003569void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3570 DCHECK(instruction->IsDiv() || instruction->IsRem());
3571
3572 LocationSummary* locations = instruction->GetLocations();
3573 Location out = locations->Out();
3574 Location first = locations->InAt(0);
3575 Location second = locations->InAt(1);
3576 bool is_div = instruction->IsDiv();
3577
3578 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003579 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 DCHECK_EQ(EAX, first.AsRegister<Register>());
3581 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003582
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003583 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003584 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585
3586 if (imm == 0) {
3587 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3588 } else if (imm == 1 || imm == -1) {
3589 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003590 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003591 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 } else {
3593 DCHECK(imm <= -2 || imm >= 2);
3594 GenerateDivRemWithAnyConstant(instruction);
3595 }
3596 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003597 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003598 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003600
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601 Register second_reg = second.AsRegister<Register>();
3602 // 0x80000000/-1 triggers an arithmetic exception!
3603 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3604 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 __ cmpl(second_reg, Immediate(-1));
3607 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003608
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 // edx:eax <- sign-extended of eax
3610 __ cdq();
3611 // eax = quotient, edx = remainder
3612 __ idivl(second_reg);
3613 __ Bind(slow_path->GetExitLabel());
3614 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 break;
3616 }
3617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003619 InvokeRuntimeCallingConvention calling_convention;
3620 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3621 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3622 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3623 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3624 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3625 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3626
3627 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003628 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003629 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003631 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003632 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636
3637 default:
3638 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3639 }
3640}
3641
Calin Juravle7c4954d2014-10-28 16:57:40 +00003642void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003643 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003644 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003645 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003646 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003647
Calin Juravle7c4954d2014-10-28 16:57:40 +00003648 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003650 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003652 locations->SetOut(Location::SameAsFirstInput());
3653 // Intel uses edx:eax as the dividend.
3654 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003655 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3656 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3657 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003658 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003659 locations->AddTemp(Location::RequiresRegister());
3660 }
Calin Juravled0d48522014-11-04 16:40:20 +00003661 break;
3662 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 InvokeRuntimeCallingConvention calling_convention;
3665 locations->SetInAt(0, Location::RegisterPairLocation(
3666 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3667 locations->SetInAt(1, Location::RegisterPairLocation(
3668 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3669 // Runtime helper puts the result in EAX, EDX.
3670 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 break;
3672 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 case DataType::Type::kFloat32:
3674 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003675 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003676 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3677 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003678 } else if (div->InputAt(1)->IsConstant()) {
3679 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003680 } else {
3681 locations->SetInAt(1, Location::Any());
3682 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 locations->SetOut(Location::SameAsFirstInput());
3684 break;
3685 }
3686
3687 default:
3688 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3689 }
3690}
3691
3692void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3693 LocationSummary* locations = div->GetLocations();
3694 Location first = locations->InAt(0);
3695 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003696
3697 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003698 case DataType::Type::kInt32:
3699 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003700 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003701 break;
3702 }
3703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003705 if (second.IsFpuRegister()) {
3706 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3707 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3708 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003709 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003710 __ divss(first.AsFpuRegister<XmmRegister>(),
3711 codegen_->LiteralFloatAddress(
3712 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003713 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003714 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3715 } else {
3716 DCHECK(second.IsStackSlot());
3717 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3718 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719 break;
3720 }
3721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003723 if (second.IsFpuRegister()) {
3724 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3725 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3726 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003727 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 __ divsd(first.AsFpuRegister<XmmRegister>(),
3729 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003730 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3731 const_area->GetBaseMethodAddress(),
3732 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 } else {
3734 DCHECK(second.IsDoubleStackSlot());
3735 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3736 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003737 break;
3738 }
3739
3740 default:
3741 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3742 }
3743}
3744
Calin Juravlebacfec32014-11-14 15:54:36 +00003745void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003746 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003749 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003750 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003751 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003752
Calin Juravled2ec87d2014-12-08 14:24:46 +00003753 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003755 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003756 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003757 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003758 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3759 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3760 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003761 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003762 locations->AddTemp(Location::RequiresRegister());
3763 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 break;
3765 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003766 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003767 InvokeRuntimeCallingConvention calling_convention;
3768 locations->SetInAt(0, Location::RegisterPairLocation(
3769 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3770 locations->SetInAt(1, Location::RegisterPairLocation(
3771 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3772 // Runtime helper puts the result in EAX, EDX.
3773 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3774 break;
3775 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003776 case DataType::Type::kFloat64:
3777 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003778 locations->SetInAt(0, Location::Any());
3779 locations->SetInAt(1, Location::Any());
3780 locations->SetOut(Location::RequiresFpuRegister());
3781 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003782 break;
3783 }
3784
3785 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003786 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003787 }
3788}
3789
3790void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003792 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 case DataType::Type::kInt32:
3794 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003795 GenerateDivRemIntegral(rem);
3796 break;
3797 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kFloat32:
3799 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003800 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 break;
3802 }
3803 default:
3804 LOG(FATAL) << "Unexpected rem type " << type;
3805 }
3806}
3807
Aart Bik1f8d51b2018-02-15 10:42:37 -08003808static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3809 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3810 switch (minmax->GetResultType()) {
3811 case DataType::Type::kInt32:
3812 locations->SetInAt(0, Location::RequiresRegister());
3813 locations->SetInAt(1, Location::RequiresRegister());
3814 locations->SetOut(Location::SameAsFirstInput());
3815 break;
3816 case DataType::Type::kInt64:
3817 locations->SetInAt(0, Location::RequiresRegister());
3818 locations->SetInAt(1, Location::RequiresRegister());
3819 locations->SetOut(Location::SameAsFirstInput());
3820 // Register to use to perform a long subtract to set cc.
3821 locations->AddTemp(Location::RequiresRegister());
3822 break;
3823 case DataType::Type::kFloat32:
3824 locations->SetInAt(0, Location::RequiresFpuRegister());
3825 locations->SetInAt(1, Location::RequiresFpuRegister());
3826 locations->SetOut(Location::SameAsFirstInput());
3827 locations->AddTemp(Location::RequiresRegister());
3828 break;
3829 case DataType::Type::kFloat64:
3830 locations->SetInAt(0, Location::RequiresFpuRegister());
3831 locations->SetInAt(1, Location::RequiresFpuRegister());
3832 locations->SetOut(Location::SameAsFirstInput());
3833 break;
3834 default:
3835 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3836 }
3837}
3838
Aart Bik351df3e2018-03-07 11:54:57 -08003839void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3840 bool is_min,
3841 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003842 Location op1_loc = locations->InAt(0);
3843 Location op2_loc = locations->InAt(1);
3844
3845 // Shortcut for same input locations.
3846 if (op1_loc.Equals(op2_loc)) {
3847 // Can return immediately, as op1_loc == out_loc.
3848 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3849 // a copy here.
3850 DCHECK(locations->Out().Equals(op1_loc));
3851 return;
3852 }
3853
3854 if (type == DataType::Type::kInt64) {
3855 // Need to perform a subtract to get the sign right.
3856 // op1 is already in the same location as the output.
3857 Location output = locations->Out();
3858 Register output_lo = output.AsRegisterPairLow<Register>();
3859 Register output_hi = output.AsRegisterPairHigh<Register>();
3860
3861 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3862 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3863
3864 // The comparison is performed by subtracting the second operand from
3865 // the first operand and then setting the status flags in the same
3866 // manner as the SUB instruction."
3867 __ cmpl(output_lo, op2_lo);
3868
3869 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3870 Register temp = locations->GetTemp(0).AsRegister<Register>();
3871 __ movl(temp, output_hi);
3872 __ sbbl(temp, op2_hi);
3873
3874 // Now the condition code is correct.
3875 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3876 __ cmovl(cond, output_lo, op2_lo);
3877 __ cmovl(cond, output_hi, op2_hi);
3878 } else {
3879 DCHECK_EQ(type, DataType::Type::kInt32);
3880 Register out = locations->Out().AsRegister<Register>();
3881 Register op2 = op2_loc.AsRegister<Register>();
3882
3883 // (out := op1)
3884 // out <=? op2
3885 // if out is min jmp done
3886 // out := op2
3887 // done:
3888
3889 __ cmpl(out, op2);
3890 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3891 __ cmovl(cond, out, op2);
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3896 bool is_min,
3897 DataType::Type type) {
3898 Location op1_loc = locations->InAt(0);
3899 Location op2_loc = locations->InAt(1);
3900 Location out_loc = locations->Out();
3901 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3902
3903 // Shortcut for same input locations.
3904 if (op1_loc.Equals(op2_loc)) {
3905 DCHECK(out_loc.Equals(op1_loc));
3906 return;
3907 }
3908
3909 // (out := op1)
3910 // out <=? op2
3911 // if Nan jmp Nan_label
3912 // if out is min jmp done
3913 // if op2 is min jmp op2_label
3914 // handle -0/+0
3915 // jmp done
3916 // Nan_label:
3917 // out := NaN
3918 // op2_label:
3919 // out := op2
3920 // done:
3921 //
3922 // This removes one jmp, but needs to copy one input (op1) to out.
3923 //
3924 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3925
3926 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3927
3928 NearLabel nan, done, op2_label;
3929 if (type == DataType::Type::kFloat64) {
3930 __ ucomisd(out, op2);
3931 } else {
3932 DCHECK_EQ(type, DataType::Type::kFloat32);
3933 __ ucomiss(out, op2);
3934 }
3935
3936 __ j(Condition::kParityEven, &nan);
3937
3938 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3939 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3940
3941 // Handle 0.0/-0.0.
3942 if (is_min) {
3943 if (type == DataType::Type::kFloat64) {
3944 __ orpd(out, op2);
3945 } else {
3946 __ orps(out, op2);
3947 }
3948 } else {
3949 if (type == DataType::Type::kFloat64) {
3950 __ andpd(out, op2);
3951 } else {
3952 __ andps(out, op2);
3953 }
3954 }
3955 __ jmp(&done);
3956
3957 // NaN handling.
3958 __ Bind(&nan);
3959 if (type == DataType::Type::kFloat64) {
3960 // TODO: Use a constant from the constant table (requires extra input).
3961 __ LoadLongConstant(out, kDoubleNaN);
3962 } else {
3963 Register constant = locations->GetTemp(0).AsRegister<Register>();
3964 __ movl(constant, Immediate(kFloatNaN));
3965 __ movd(out, constant);
3966 }
3967 __ jmp(&done);
3968
3969 // out := op2;
3970 __ Bind(&op2_label);
3971 if (type == DataType::Type::kFloat64) {
3972 __ movsd(out, op2);
3973 } else {
3974 __ movss(out, op2);
3975 }
3976
3977 // Done.
3978 __ Bind(&done);
3979}
3980
Aart Bik351df3e2018-03-07 11:54:57 -08003981void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3982 DataType::Type type = minmax->GetResultType();
3983 switch (type) {
3984 case DataType::Type::kInt32:
3985 case DataType::Type::kInt64:
3986 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
3987 break;
3988 case DataType::Type::kFloat32:
3989 case DataType::Type::kFloat64:
3990 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
3991 break;
3992 default:
3993 LOG(FATAL) << "Unexpected type for HMinMax " << type;
3994 }
3995}
3996
Aart Bik1f8d51b2018-02-15 10:42:37 -08003997void LocationsBuilderX86::VisitMin(HMin* min) {
3998 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
3999}
4000
4001void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004002 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004003}
4004
4005void LocationsBuilderX86::VisitMax(HMax* max) {
4006 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4007}
4008
4009void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004010 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004011}
4012
Aart Bik3dad3412018-02-28 12:01:46 -08004013void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4014 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4015 switch (abs->GetResultType()) {
4016 case DataType::Type::kInt32:
4017 locations->SetInAt(0, Location::RegisterLocation(EAX));
4018 locations->SetOut(Location::SameAsFirstInput());
4019 locations->AddTemp(Location::RegisterLocation(EDX));
4020 break;
4021 case DataType::Type::kInt64:
4022 locations->SetInAt(0, Location::RequiresRegister());
4023 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4024 locations->AddTemp(Location::RequiresRegister());
4025 break;
4026 case DataType::Type::kFloat32:
4027 locations->SetInAt(0, Location::RequiresFpuRegister());
4028 locations->SetOut(Location::SameAsFirstInput());
4029 locations->AddTemp(Location::RequiresFpuRegister());
4030 locations->AddTemp(Location::RequiresRegister());
4031 break;
4032 case DataType::Type::kFloat64:
4033 locations->SetInAt(0, Location::RequiresFpuRegister());
4034 locations->SetOut(Location::SameAsFirstInput());
4035 locations->AddTemp(Location::RequiresFpuRegister());
4036 break;
4037 default:
4038 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4039 }
4040}
4041
4042void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4043 LocationSummary* locations = abs->GetLocations();
4044 switch (abs->GetResultType()) {
4045 case DataType::Type::kInt32: {
4046 Register out = locations->Out().AsRegister<Register>();
4047 DCHECK_EQ(out, EAX);
4048 Register temp = locations->GetTemp(0).AsRegister<Register>();
4049 DCHECK_EQ(temp, EDX);
4050 // Sign extend EAX into EDX.
4051 __ cdq();
4052 // XOR EAX with sign.
4053 __ xorl(EAX, EDX);
4054 // Subtract out sign to correct.
4055 __ subl(EAX, EDX);
4056 // The result is in EAX.
4057 break;
4058 }
4059 case DataType::Type::kInt64: {
4060 Location input = locations->InAt(0);
4061 Register input_lo = input.AsRegisterPairLow<Register>();
4062 Register input_hi = input.AsRegisterPairHigh<Register>();
4063 Location output = locations->Out();
4064 Register output_lo = output.AsRegisterPairLow<Register>();
4065 Register output_hi = output.AsRegisterPairHigh<Register>();
4066 Register temp = locations->GetTemp(0).AsRegister<Register>();
4067 // Compute the sign into the temporary.
4068 __ movl(temp, input_hi);
4069 __ sarl(temp, Immediate(31));
4070 // Store the sign into the output.
4071 __ movl(output_lo, temp);
4072 __ movl(output_hi, temp);
4073 // XOR the input to the output.
4074 __ xorl(output_lo, input_lo);
4075 __ xorl(output_hi, input_hi);
4076 // Subtract the sign.
4077 __ subl(output_lo, temp);
4078 __ sbbl(output_hi, temp);
4079 break;
4080 }
4081 case DataType::Type::kFloat32: {
4082 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4083 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4084 Register constant = locations->GetTemp(1).AsRegister<Register>();
4085 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4086 __ movd(temp, constant);
4087 __ andps(out, temp);
4088 break;
4089 }
4090 case DataType::Type::kFloat64: {
4091 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4092 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4093 // TODO: Use a constant from the constant table (requires extra input).
4094 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4095 __ andpd(out, temp);
4096 break;
4097 }
4098 default:
4099 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4100 }
4101}
4102
Calin Juravled0d48522014-11-04 16:40:20 +00004103void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004104 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004105 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004106 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004107 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004108 case DataType::Type::kInt8:
4109 case DataType::Type::kUint16:
4110 case DataType::Type::kInt16:
4111 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004112 locations->SetInAt(0, Location::Any());
4113 break;
4114 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004115 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004116 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4117 if (!instruction->IsConstant()) {
4118 locations->AddTemp(Location::RequiresRegister());
4119 }
4120 break;
4121 }
4122 default:
4123 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4124 }
Calin Juravled0d48522014-11-04 16:40:20 +00004125}
4126
4127void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004128 SlowPathCode* slow_path =
4129 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004130 codegen_->AddSlowPath(slow_path);
4131
4132 LocationSummary* locations = instruction->GetLocations();
4133 Location value = locations->InAt(0);
4134
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004135 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004136 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004137 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004138 case DataType::Type::kInt8:
4139 case DataType::Type::kUint16:
4140 case DataType::Type::kInt16:
4141 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004142 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004143 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004144 __ j(kEqual, slow_path->GetEntryLabel());
4145 } else if (value.IsStackSlot()) {
4146 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4147 __ j(kEqual, slow_path->GetEntryLabel());
4148 } else {
4149 DCHECK(value.IsConstant()) << value;
4150 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004151 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004152 }
4153 }
4154 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004155 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004156 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004157 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004159 __ movl(temp, value.AsRegisterPairLow<Register>());
4160 __ orl(temp, value.AsRegisterPairHigh<Register>());
4161 __ j(kEqual, slow_path->GetEntryLabel());
4162 } else {
4163 DCHECK(value.IsConstant()) << value;
4164 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4165 __ jmp(slow_path->GetEntryLabel());
4166 }
4167 }
4168 break;
4169 }
4170 default:
4171 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004172 }
Calin Juravled0d48522014-11-04 16:40:20 +00004173}
4174
Calin Juravle9aec02f2014-11-18 23:06:35 +00004175void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4176 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4177
4178 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004179 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004180
4181 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 case DataType::Type::kInt32:
4183 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004184 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004185 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004186 // The shift count needs to be in CL or a constant.
4187 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004188 locations->SetOut(Location::SameAsFirstInput());
4189 break;
4190 }
4191 default:
4192 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4193 }
4194}
4195
4196void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4197 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4198
4199 LocationSummary* locations = op->GetLocations();
4200 Location first = locations->InAt(0);
4201 Location second = locations->InAt(1);
4202 DCHECK(first.Equals(locations->Out()));
4203
4204 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004206 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004207 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004208 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004209 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004210 DCHECK_EQ(ECX, second_reg);
4211 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004212 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004213 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004214 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004215 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004216 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004217 }
4218 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004219 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004220 if (shift == 0) {
4221 return;
4222 }
4223 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004224 if (op->IsShl()) {
4225 __ shll(first_reg, imm);
4226 } else if (op->IsShr()) {
4227 __ sarl(first_reg, imm);
4228 } else {
4229 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004230 }
4231 }
4232 break;
4233 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004234 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004235 if (second.IsRegister()) {
4236 Register second_reg = second.AsRegister<Register>();
4237 DCHECK_EQ(ECX, second_reg);
4238 if (op->IsShl()) {
4239 GenerateShlLong(first, second_reg);
4240 } else if (op->IsShr()) {
4241 GenerateShrLong(first, second_reg);
4242 } else {
4243 GenerateUShrLong(first, second_reg);
4244 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004245 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004246 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004247 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004248 // Nothing to do if the shift is 0, as the input is already the output.
4249 if (shift != 0) {
4250 if (op->IsShl()) {
4251 GenerateShlLong(first, shift);
4252 } else if (op->IsShr()) {
4253 GenerateShrLong(first, shift);
4254 } else {
4255 GenerateUShrLong(first, shift);
4256 }
4257 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004258 }
4259 break;
4260 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004261 default:
4262 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4263 }
4264}
4265
Mark P Mendell73945692015-04-29 14:56:17 +00004266void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4267 Register low = loc.AsRegisterPairLow<Register>();
4268 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004269 if (shift == 1) {
4270 // This is just an addition.
4271 __ addl(low, low);
4272 __ adcl(high, high);
4273 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004274 // Shift by 32 is easy. High gets low, and low gets 0.
4275 codegen_->EmitParallelMoves(
4276 loc.ToLow(),
4277 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004278 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004279 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4280 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004281 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004282 } else if (shift > 32) {
4283 // Low part becomes 0. High part is low part << (shift-32).
4284 __ movl(high, low);
4285 __ shll(high, Immediate(shift - 32));
4286 __ xorl(low, low);
4287 } else {
4288 // Between 1 and 31.
4289 __ shld(high, low, Immediate(shift));
4290 __ shll(low, Immediate(shift));
4291 }
4292}
4293
Calin Juravle9aec02f2014-11-18 23:06:35 +00004294void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004295 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004296 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4297 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4298 __ testl(shifter, Immediate(32));
4299 __ j(kEqual, &done);
4300 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4301 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4302 __ Bind(&done);
4303}
4304
Mark P Mendell73945692015-04-29 14:56:17 +00004305void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4306 Register low = loc.AsRegisterPairLow<Register>();
4307 Register high = loc.AsRegisterPairHigh<Register>();
4308 if (shift == 32) {
4309 // Need to copy the sign.
4310 DCHECK_NE(low, high);
4311 __ movl(low, high);
4312 __ sarl(high, Immediate(31));
4313 } else if (shift > 32) {
4314 DCHECK_NE(low, high);
4315 // High part becomes sign. Low part is shifted by shift - 32.
4316 __ movl(low, high);
4317 __ sarl(high, Immediate(31));
4318 __ sarl(low, Immediate(shift - 32));
4319 } else {
4320 // Between 1 and 31.
4321 __ shrd(low, high, Immediate(shift));
4322 __ sarl(high, Immediate(shift));
4323 }
4324}
4325
Calin Juravle9aec02f2014-11-18 23:06:35 +00004326void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004327 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004328 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4329 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4330 __ testl(shifter, Immediate(32));
4331 __ j(kEqual, &done);
4332 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4333 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4334 __ Bind(&done);
4335}
4336
Mark P Mendell73945692015-04-29 14:56:17 +00004337void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4338 Register low = loc.AsRegisterPairLow<Register>();
4339 Register high = loc.AsRegisterPairHigh<Register>();
4340 if (shift == 32) {
4341 // Shift by 32 is easy. Low gets high, and high gets 0.
4342 codegen_->EmitParallelMoves(
4343 loc.ToHigh(),
4344 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004345 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004346 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4347 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004348 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004349 } else if (shift > 32) {
4350 // Low part is high >> (shift - 32). High part becomes 0.
4351 __ movl(low, high);
4352 __ shrl(low, Immediate(shift - 32));
4353 __ xorl(high, high);
4354 } else {
4355 // Between 1 and 31.
4356 __ shrd(low, high, Immediate(shift));
4357 __ shrl(high, Immediate(shift));
4358 }
4359}
4360
Calin Juravle9aec02f2014-11-18 23:06:35 +00004361void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004362 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004363 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4364 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4365 __ testl(shifter, Immediate(32));
4366 __ j(kEqual, &done);
4367 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4368 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4369 __ Bind(&done);
4370}
4371
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004372void LocationsBuilderX86::VisitRor(HRor* ror) {
4373 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004374 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004375
4376 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004377 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004378 // Add the temporary needed.
4379 locations->AddTemp(Location::RequiresRegister());
4380 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004381 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004382 locations->SetInAt(0, Location::RequiresRegister());
4383 // The shift count needs to be in CL (unless it is a constant).
4384 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4385 locations->SetOut(Location::SameAsFirstInput());
4386 break;
4387 default:
4388 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4389 UNREACHABLE();
4390 }
4391}
4392
4393void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4394 LocationSummary* locations = ror->GetLocations();
4395 Location first = locations->InAt(0);
4396 Location second = locations->InAt(1);
4397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004398 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004399 Register first_reg = first.AsRegister<Register>();
4400 if (second.IsRegister()) {
4401 Register second_reg = second.AsRegister<Register>();
4402 __ rorl(first_reg, second_reg);
4403 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004404 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004405 __ rorl(first_reg, imm);
4406 }
4407 return;
4408 }
4409
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004410 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004411 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4412 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4413 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4414 if (second.IsRegister()) {
4415 Register second_reg = second.AsRegister<Register>();
4416 DCHECK_EQ(second_reg, ECX);
4417 __ movl(temp_reg, first_reg_hi);
4418 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4419 __ shrd(first_reg_lo, temp_reg, second_reg);
4420 __ movl(temp_reg, first_reg_hi);
4421 __ testl(second_reg, Immediate(32));
4422 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4423 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4424 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004425 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004426 if (shift_amt == 0) {
4427 // Already fine.
4428 return;
4429 }
4430 if (shift_amt == 32) {
4431 // Just swap.
4432 __ movl(temp_reg, first_reg_lo);
4433 __ movl(first_reg_lo, first_reg_hi);
4434 __ movl(first_reg_hi, temp_reg);
4435 return;
4436 }
4437
4438 Immediate imm(shift_amt);
4439 // Save the constents of the low value.
4440 __ movl(temp_reg, first_reg_lo);
4441
4442 // Shift right into low, feeding bits from high.
4443 __ shrd(first_reg_lo, first_reg_hi, imm);
4444
4445 // Shift right into high, feeding bits from the original low.
4446 __ shrd(first_reg_hi, temp_reg, imm);
4447
4448 // Swap if needed.
4449 if (shift_amt > 32) {
4450 __ movl(temp_reg, first_reg_lo);
4451 __ movl(first_reg_lo, first_reg_hi);
4452 __ movl(first_reg_hi, temp_reg);
4453 }
4454 }
4455}
4456
Calin Juravle9aec02f2014-11-18 23:06:35 +00004457void LocationsBuilderX86::VisitShl(HShl* shl) {
4458 HandleShift(shl);
4459}
4460
4461void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4462 HandleShift(shl);
4463}
4464
4465void LocationsBuilderX86::VisitShr(HShr* shr) {
4466 HandleShift(shr);
4467}
4468
4469void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4470 HandleShift(shr);
4471}
4472
4473void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4474 HandleShift(ushr);
4475}
4476
4477void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4478 HandleShift(ushr);
4479}
4480
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004481void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004482 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4483 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004484 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004485 if (instruction->IsStringAlloc()) {
4486 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4487 } else {
4488 InvokeRuntimeCallingConvention calling_convention;
4489 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004490 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004491}
4492
4493void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004494 // Note: if heap poisoning is enabled, the entry point takes cares
4495 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004496 if (instruction->IsStringAlloc()) {
4497 // String is allocated through StringFactory. Call NewEmptyString entry point.
4498 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004499 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004500 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4501 __ call(Address(temp, code_offset.Int32Value()));
4502 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4503 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004504 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004505 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004506 DCHECK(!codegen_->IsLeafMethod());
4507 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004508}
4509
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004510void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004511 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4512 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004513 locations->SetOut(Location::RegisterLocation(EAX));
4514 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004515 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4516 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004517}
4518
4519void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004520 // Note: if heap poisoning is enabled, the entry point takes cares
4521 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004522 QuickEntrypointEnum entrypoint =
4523 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4524 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004525 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004526 DCHECK(!codegen_->IsLeafMethod());
4527}
4528
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004529void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004530 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004531 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004532 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4533 if (location.IsStackSlot()) {
4534 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4535 } else if (location.IsDoubleStackSlot()) {
4536 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004537 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004538 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004539}
4540
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004541void InstructionCodeGeneratorX86::VisitParameterValue(
4542 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4543}
4544
4545void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4546 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004547 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004548 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4549}
4550
4551void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004552}
4553
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004554void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4555 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004556 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004557 locations->SetInAt(0, Location::RequiresRegister());
4558 locations->SetOut(Location::RequiresRegister());
4559}
4560
4561void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4562 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004563 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004564 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004565 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004566 __ movl(locations->Out().AsRegister<Register>(),
4567 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004568 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004569 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004570 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004571 __ movl(locations->Out().AsRegister<Register>(),
4572 Address(locations->InAt(0).AsRegister<Register>(),
4573 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4574 // temp = temp->GetImtEntryAt(method_offset);
4575 __ movl(locations->Out().AsRegister<Register>(),
4576 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004577 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004578}
4579
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004580void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004581 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004582 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004583 locations->SetInAt(0, Location::RequiresRegister());
4584 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004585}
4586
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004587void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4588 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004589 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004590 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004591 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004592 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004593 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004594 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004595 break;
4596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004597 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004598 __ notl(out.AsRegisterPairLow<Register>());
4599 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004600 break;
4601
4602 default:
4603 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4604 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004605}
4606
David Brazdil66d126e2015-04-03 16:02:44 +01004607void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4608 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004609 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004610 locations->SetInAt(0, Location::RequiresRegister());
4611 locations->SetOut(Location::SameAsFirstInput());
4612}
4613
4614void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004615 LocationSummary* locations = bool_not->GetLocations();
4616 Location in = locations->InAt(0);
4617 Location out = locations->Out();
4618 DCHECK(in.Equals(out));
4619 __ xorl(out.AsRegister<Register>(), Immediate(1));
4620}
4621
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004622void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004623 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004624 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004625 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004626 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004627 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004628 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004630 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 case DataType::Type::kInt32:
4632 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004633 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004634 locations->SetInAt(1, Location::Any());
4635 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4636 break;
4637 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004638 case DataType::Type::kFloat32:
4639 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004640 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004641 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4642 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4643 } else if (compare->InputAt(1)->IsConstant()) {
4644 locations->SetInAt(1, Location::RequiresFpuRegister());
4645 } else {
4646 locations->SetInAt(1, Location::Any());
4647 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004648 locations->SetOut(Location::RequiresRegister());
4649 break;
4650 }
4651 default:
4652 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4653 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004654}
4655
4656void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004657 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004658 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004659 Location left = locations->InAt(0);
4660 Location right = locations->InAt(1);
4661
Mark Mendell0c9497d2015-08-21 09:30:05 -04004662 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004663 Condition less_cond = kLess;
4664
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004665 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004666 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004667 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004668 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004669 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004670 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004671 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004672 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004673 break;
4674 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004675 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004676 Register left_low = left.AsRegisterPairLow<Register>();
4677 Register left_high = left.AsRegisterPairHigh<Register>();
4678 int32_t val_low = 0;
4679 int32_t val_high = 0;
4680 bool right_is_const = false;
4681
4682 if (right.IsConstant()) {
4683 DCHECK(right.GetConstant()->IsLongConstant());
4684 right_is_const = true;
4685 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4686 val_low = Low32Bits(val);
4687 val_high = High32Bits(val);
4688 }
4689
Calin Juravleddb7df22014-11-25 20:56:51 +00004690 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004691 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004692 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004693 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004694 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004695 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004696 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004697 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004698 __ j(kLess, &less); // Signed compare.
4699 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004700 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004701 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004702 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004703 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004704 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004705 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004706 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004707 }
Aart Bika19616e2016-02-01 18:57:58 -08004708 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004709 break;
4710 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004712 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004713 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004714 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004715 break;
4716 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004718 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004719 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004720 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004721 break;
4722 }
4723 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004724 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004725 }
Aart Bika19616e2016-02-01 18:57:58 -08004726
Calin Juravleddb7df22014-11-25 20:56:51 +00004727 __ movl(out, Immediate(0));
4728 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004729 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004730
4731 __ Bind(&greater);
4732 __ movl(out, Immediate(1));
4733 __ jmp(&done);
4734
4735 __ Bind(&less);
4736 __ movl(out, Immediate(-1));
4737
4738 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004739}
4740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004741void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004742 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004743 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004744 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004745 locations->SetInAt(i, Location::Any());
4746 }
4747 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004748}
4749
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004750void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004751 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004752}
4753
Roland Levillain7c1559a2015-12-15 10:55:36 +00004754void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004755 /*
4756 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4757 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4758 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4759 */
4760 switch (kind) {
4761 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004762 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004763 break;
4764 }
4765 case MemBarrierKind::kAnyStore:
4766 case MemBarrierKind::kLoadAny:
4767 case MemBarrierKind::kStoreStore: {
4768 // nop
4769 break;
4770 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004771 case MemBarrierKind::kNTStoreStore:
4772 // Non-Temporal Store/Store needs an explicit fence.
4773 MemoryFence(/* non-temporal */ true);
4774 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004775 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004776}
4777
Vladimir Markodc151b22015-10-15 18:02:30 +01004778HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4779 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004780 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004781 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004782}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004783
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004784Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4785 Register temp) {
4786 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004787 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004788 if (!invoke->GetLocations()->Intrinsified()) {
4789 return location.AsRegister<Register>();
4790 }
4791 // For intrinsics we allow any location, so it may be on the stack.
4792 if (!location.IsRegister()) {
4793 __ movl(temp, Address(ESP, location.GetStackIndex()));
4794 return temp;
4795 }
4796 // For register locations, check if the register was saved. If so, get it from the stack.
4797 // Note: There is a chance that the register was saved but not overwritten, so we could
4798 // save one load. However, since this is just an intrinsic slow path we prefer this
4799 // simple and more robust approach rather that trying to determine if that's the case.
4800 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004801 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4802 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4803 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4804 __ movl(temp, Address(ESP, stack_offset));
4805 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004806 }
4807 return location.AsRegister<Register>();
4808}
4809
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004810void CodeGeneratorX86::GenerateStaticOrDirectCall(
4811 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004812 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4813 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004814 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004815 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004816 uint32_t offset =
4817 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4818 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004819 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004820 }
Vladimir Marko58155012015-08-19 12:49:41 +00004821 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004822 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004823 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004824 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4825 DCHECK(GetCompilerOptions().IsBootImage());
4826 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4827 temp.AsRegister<Register>());
4828 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004829 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004830 break;
4831 }
Vladimir Marko58155012015-08-19 12:49:41 +00004832 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4833 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4834 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004835 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4836 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4837 temp.AsRegister<Register>());
4838 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4839 RecordBootImageRelRoPatch(
4840 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004841 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004842 break;
4843 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004844 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004845 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4846 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004847 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004848 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004849 break;
4850 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004851 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4852 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4853 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004854 }
Vladimir Marko58155012015-08-19 12:49:41 +00004855 }
4856
4857 switch (invoke->GetCodePtrLocation()) {
4858 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4859 __ call(GetFrameEntryLabel());
4860 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004861 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4862 // (callee_method + offset_of_quick_compiled_code)()
4863 __ call(Address(callee_method.AsRegister<Register>(),
4864 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004865 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004866 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004867 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004868 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004869
4870 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004871}
4872
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004873void CodeGeneratorX86::GenerateVirtualCall(
4874 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004875 Register temp = temp_in.AsRegister<Register>();
4876 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4877 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004878
4879 // Use the calling convention instead of the location of the receiver, as
4880 // intrinsics may have put the receiver in a different register. In the intrinsics
4881 // slow path, the arguments have been moved to the right place, so here we are
4882 // guaranteed that the receiver is the first register of the calling convention.
4883 InvokeDexCallingConvention calling_convention;
4884 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004885 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004886 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004887 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004888 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004889 // Instead of simply (possibly) unpoisoning `temp` here, we should
4890 // emit a read barrier for the previous class reference load.
4891 // However this is not required in practice, as this is an
4892 // intermediate/temporary reference and because the current
4893 // concurrent copying collector keeps the from-space memory
4894 // intact/accessible until the end of the marking phase (the
4895 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004896 __ MaybeUnpoisonHeapReference(temp);
4897 // temp = temp->GetMethodAt(method_offset);
4898 __ movl(temp, Address(temp, method_offset));
4899 // call temp->GetEntryPoint();
4900 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004901 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004902 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004903}
4904
Vladimir Markob066d432018-01-03 13:14:37 +00004905void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4906 uint32_t boot_image_offset) {
4907 boot_image_method_patches_.emplace_back(
4908 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4909 __ Bind(&boot_image_method_patches_.back().label);
4910}
4911
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004912void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004913 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004914 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004915 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004916 boot_image_method_patches_.emplace_back(
4917 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004918 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004919}
4920
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004921void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4922 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4923 HX86ComputeBaseMethodAddress* method_address =
4924 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004925 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004926 method_bss_entry_patches_.emplace_back(
4927 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4928 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004929}
4930
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004931void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4932 HX86ComputeBaseMethodAddress* method_address =
4933 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4934 boot_image_type_patches_.emplace_back(
4935 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004936 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937}
4938
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004939Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004940 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004941 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4942 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004943 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004944 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004945}
4946
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004947void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4948 HX86ComputeBaseMethodAddress* method_address =
4949 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4950 boot_image_string_patches_.emplace_back(
4951 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4952 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004953}
4954
Vladimir Markoaad75c62016-10-03 08:46:48 +00004955Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4956 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004957 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004958 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004959 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004960 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004961 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004962}
4963
Vladimir Markoaad75c62016-10-03 08:46:48 +00004964// The label points to the end of the "movl" or another instruction but the literal offset
4965// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4966constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4967
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004968template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004969inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004970 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004971 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004972 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004973 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004974 linker_patches->push_back(Factory(literal_offset,
4975 info.target_dex_file,
4976 GetMethodAddressOffset(info.method_address),
4977 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004978 }
4979}
4980
Vladimir Markob066d432018-01-03 13:14:37 +00004981linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
4982 const DexFile* target_dex_file,
4983 uint32_t pc_insn_offset,
4984 uint32_t boot_image_offset) {
4985 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
4986 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
4987}
4988
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004989void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004990 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004991 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004992 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004993 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004994 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004995 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004996 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004997 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004998 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004999 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005000 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5001 boot_image_method_patches_, linker_patches);
5002 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5003 boot_image_type_patches_, linker_patches);
5004 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005005 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005006 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00005007 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
5008 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005009 DCHECK(boot_image_type_patches_.empty());
5010 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005011 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005012 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5013 method_bss_entry_patches_, linker_patches);
5014 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5015 type_bss_entry_patches_, linker_patches);
5016 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5017 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005018 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005019}
5020
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005021void CodeGeneratorX86::MarkGCCard(Register temp,
5022 Register card,
5023 Register object,
5024 Register value,
5025 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005026 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005027 if (value_can_be_null) {
5028 __ testl(value, value);
5029 __ j(kEqual, &is_null);
5030 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005031 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 __ movl(temp, object);
5033 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005034 __ movb(Address(temp, card, TIMES_1, 0),
5035 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005036 if (value_can_be_null) {
5037 __ Bind(&is_null);
5038 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039}
5040
Calin Juravle52c48962014-12-16 17:02:57 +00005041void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5042 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005043
5044 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005045 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005046 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005047 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5048 kEmitCompilerReadBarrier
5049 ? LocationSummary::kCallOnSlowPath
5050 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005051 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005052 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005053 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005054 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005056 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005057 locations->SetOut(Location::RequiresFpuRegister());
5058 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005059 // The output overlaps in case of long: we don't want the low move
5060 // to overwrite the object's location. Likewise, in the case of
5061 // an object field get with read barriers enabled, we do not want
5062 // the move to overwrite the object's location, as we need it to emit
5063 // the read barrier.
5064 locations->SetOut(
5065 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005066 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005067 Location::kOutputOverlap :
5068 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005069 }
Calin Juravle52c48962014-12-16 17:02:57 +00005070
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005071 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005072 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005073 // So we use an XMM register as a temp to achieve atomicity (first
5074 // load the temp into the XMM and then copy the XMM into the
5075 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005076 locations->AddTemp(Location::RequiresFpuRegister());
5077 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005078}
5079
Calin Juravle52c48962014-12-16 17:02:57 +00005080void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5081 const FieldInfo& field_info) {
5082 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083
Calin Juravle52c48962014-12-16 17:02:57 +00005084 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005085 Location base_loc = locations->InAt(0);
5086 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005087 Location out = locations->Out();
5088 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005089 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5090 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005091 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5092
Vladimir Marko61b92282017-10-11 13:23:17 +01005093 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005094 case DataType::Type::kBool:
5095 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005096 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097 break;
5098 }
5099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005100 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005101 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102 break;
5103 }
5104
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005105 case DataType::Type::kUint16: {
5106 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005107 break;
5108 }
5109
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005110 case DataType::Type::kInt16: {
5111 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112 break;
5113 }
5114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005115 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005116 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005117 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005119 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005120 // /* HeapReference<Object> */ out = *(base + offset)
5121 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005122 // Note that a potential implicit null check is handled in this
5123 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5124 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005125 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 if (is_volatile) {
5127 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5128 }
5129 } else {
5130 __ movl(out.AsRegister<Register>(), Address(base, offset));
5131 codegen_->MaybeRecordImplicitNullCheck(instruction);
5132 if (is_volatile) {
5133 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5134 }
5135 // If read barriers are enabled, emit read barriers other than
5136 // Baker's using a slow path (and also unpoison the loaded
5137 // reference, if heap poisoning is enabled).
5138 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5139 }
5140 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141 }
5142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005143 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005144 if (is_volatile) {
5145 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5146 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005147 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005148 __ movd(out.AsRegisterPairLow<Register>(), temp);
5149 __ psrlq(temp, Immediate(32));
5150 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5151 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005152 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005153 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005155 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5156 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157 break;
5158 }
5159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005160 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005161 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005162 break;
5163 }
5164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005165 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005166 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005167 break;
5168 }
5169
Aart Bik66c158e2018-01-31 12:55:04 -08005170 case DataType::Type::kUint32:
5171 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005172 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005173 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005174 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175 }
Calin Juravle52c48962014-12-16 17:02:57 +00005176
Vladimir Marko61b92282017-10-11 13:23:17 +01005177 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005178 // Potential implicit null checks, in the case of reference or
5179 // long fields, are handled in the previous switch statement.
5180 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005181 codegen_->MaybeRecordImplicitNullCheck(instruction);
5182 }
5183
Calin Juravle52c48962014-12-16 17:02:57 +00005184 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005185 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005186 // Memory barriers, in the case of references, are also handled
5187 // in the previous switch statement.
5188 } else {
5189 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5190 }
Roland Levillain4d027112015-07-01 15:41:14 +01005191 }
Calin Juravle52c48962014-12-16 17:02:57 +00005192}
5193
5194void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5195 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5196
5197 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005198 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005199 locations->SetInAt(0, Location::RequiresRegister());
5200 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005201 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005202 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005203
5204 // The register allocator does not support multiple
5205 // inputs that die at entry with one in a specific register.
5206 if (is_byte_type) {
5207 // Ensure the value is in a byte register.
5208 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 } else if (DataType::IsFloatingPointType(field_type)) {
5210 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005211 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5212 locations->SetInAt(1, Location::RequiresFpuRegister());
5213 } else {
5214 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5215 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005216 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005217 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005218 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005219
Calin Juravle52c48962014-12-16 17:02:57 +00005220 // 64bits value can be atomically written to an address with movsd and an XMM register.
5221 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5222 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5223 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5224 // isolated cases when we need this it isn't worth adding the extra complexity.
5225 locations->AddTemp(Location::RequiresFpuRegister());
5226 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005227 } else {
5228 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5229
5230 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5231 // Temporary registers for the write barrier.
5232 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5233 // Ensure the card is in a byte register.
5234 locations->AddTemp(Location::RegisterLocation(ECX));
5235 }
Calin Juravle52c48962014-12-16 17:02:57 +00005236 }
5237}
5238
5239void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005240 const FieldInfo& field_info,
5241 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005242 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5243
5244 LocationSummary* locations = instruction->GetLocations();
5245 Register base = locations->InAt(0).AsRegister<Register>();
5246 Location value = locations->InAt(1);
5247 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005249 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005250 bool needs_write_barrier =
5251 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005252
5253 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005254 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005255 }
5256
Mark Mendell81489372015-11-04 11:30:41 -05005257 bool maybe_record_implicit_null_check_done = false;
5258
Calin Juravle52c48962014-12-16 17:02:57 +00005259 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005260 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005261 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005263 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5264 break;
5265 }
5266
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005267 case DataType::Type::kUint16:
5268 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005269 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005270 __ movw(Address(base, offset),
5271 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005272 } else {
5273 __ movw(Address(base, offset), value.AsRegister<Register>());
5274 }
Calin Juravle52c48962014-12-16 17:02:57 +00005275 break;
5276 }
5277
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005278 case DataType::Type::kInt32:
5279 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005280 if (kPoisonHeapReferences && needs_write_barrier) {
5281 // Note that in the case where `value` is a null reference,
5282 // we do not enter this block, as the reference does not
5283 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005284 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005285 Register temp = locations->GetTemp(0).AsRegister<Register>();
5286 __ movl(temp, value.AsRegister<Register>());
5287 __ PoisonHeapReference(temp);
5288 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005289 } else if (value.IsConstant()) {
5290 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5291 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005292 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005293 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005294 __ movl(Address(base, offset), value.AsRegister<Register>());
5295 }
Calin Juravle52c48962014-12-16 17:02:57 +00005296 break;
5297 }
5298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005299 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005300 if (is_volatile) {
5301 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5302 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5303 __ movd(temp1, value.AsRegisterPairLow<Register>());
5304 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5305 __ punpckldq(temp1, temp2);
5306 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005307 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005308 } else if (value.IsConstant()) {
5309 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5310 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5311 codegen_->MaybeRecordImplicitNullCheck(instruction);
5312 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005313 } else {
5314 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005315 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005316 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5317 }
Mark Mendell81489372015-11-04 11:30:41 -05005318 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005319 break;
5320 }
5321
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005322 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005323 if (value.IsConstant()) {
5324 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5325 __ movl(Address(base, offset), Immediate(v));
5326 } else {
5327 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5328 }
Calin Juravle52c48962014-12-16 17:02:57 +00005329 break;
5330 }
5331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005332 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005333 if (value.IsConstant()) {
5334 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5335 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5336 codegen_->MaybeRecordImplicitNullCheck(instruction);
5337 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5338 maybe_record_implicit_null_check_done = true;
5339 } else {
5340 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5341 }
Calin Juravle52c48962014-12-16 17:02:57 +00005342 break;
5343 }
5344
Aart Bik66c158e2018-01-31 12:55:04 -08005345 case DataType::Type::kUint32:
5346 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005348 LOG(FATAL) << "Unreachable type " << field_type;
5349 UNREACHABLE();
5350 }
5351
Mark Mendell81489372015-11-04 11:30:41 -05005352 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005353 codegen_->MaybeRecordImplicitNullCheck(instruction);
5354 }
5355
Roland Levillain4d027112015-07-01 15:41:14 +01005356 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005357 Register temp = locations->GetTemp(0).AsRegister<Register>();
5358 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005359 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005360 }
5361
Calin Juravle52c48962014-12-16 17:02:57 +00005362 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005363 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005364 }
5365}
5366
5367void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5368 HandleFieldGet(instruction, instruction->GetFieldInfo());
5369}
5370
5371void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5372 HandleFieldGet(instruction, instruction->GetFieldInfo());
5373}
5374
5375void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5376 HandleFieldSet(instruction, instruction->GetFieldInfo());
5377}
5378
5379void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005380 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005381}
5382
5383void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5384 HandleFieldSet(instruction, instruction->GetFieldInfo());
5385}
5386
5387void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005388 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005389}
5390
5391void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5392 HandleFieldGet(instruction, instruction->GetFieldInfo());
5393}
5394
5395void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5396 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005397}
5398
Calin Juravlee460d1d2015-09-29 04:52:17 +01005399void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5400 HUnresolvedInstanceFieldGet* instruction) {
5401 FieldAccessCallingConventionX86 calling_convention;
5402 codegen_->CreateUnresolvedFieldLocationSummary(
5403 instruction, instruction->GetFieldType(), calling_convention);
5404}
5405
5406void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5407 HUnresolvedInstanceFieldGet* instruction) {
5408 FieldAccessCallingConventionX86 calling_convention;
5409 codegen_->GenerateUnresolvedFieldAccess(instruction,
5410 instruction->GetFieldType(),
5411 instruction->GetFieldIndex(),
5412 instruction->GetDexPc(),
5413 calling_convention);
5414}
5415
5416void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5417 HUnresolvedInstanceFieldSet* instruction) {
5418 FieldAccessCallingConventionX86 calling_convention;
5419 codegen_->CreateUnresolvedFieldLocationSummary(
5420 instruction, instruction->GetFieldType(), calling_convention);
5421}
5422
5423void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5424 HUnresolvedInstanceFieldSet* instruction) {
5425 FieldAccessCallingConventionX86 calling_convention;
5426 codegen_->GenerateUnresolvedFieldAccess(instruction,
5427 instruction->GetFieldType(),
5428 instruction->GetFieldIndex(),
5429 instruction->GetDexPc(),
5430 calling_convention);
5431}
5432
5433void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5434 HUnresolvedStaticFieldGet* instruction) {
5435 FieldAccessCallingConventionX86 calling_convention;
5436 codegen_->CreateUnresolvedFieldLocationSummary(
5437 instruction, instruction->GetFieldType(), calling_convention);
5438}
5439
5440void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5441 HUnresolvedStaticFieldGet* instruction) {
5442 FieldAccessCallingConventionX86 calling_convention;
5443 codegen_->GenerateUnresolvedFieldAccess(instruction,
5444 instruction->GetFieldType(),
5445 instruction->GetFieldIndex(),
5446 instruction->GetDexPc(),
5447 calling_convention);
5448}
5449
5450void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5451 HUnresolvedStaticFieldSet* instruction) {
5452 FieldAccessCallingConventionX86 calling_convention;
5453 codegen_->CreateUnresolvedFieldLocationSummary(
5454 instruction, instruction->GetFieldType(), calling_convention);
5455}
5456
5457void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5458 HUnresolvedStaticFieldSet* instruction) {
5459 FieldAccessCallingConventionX86 calling_convention;
5460 codegen_->GenerateUnresolvedFieldAccess(instruction,
5461 instruction->GetFieldType(),
5462 instruction->GetFieldIndex(),
5463 instruction->GetDexPc(),
5464 calling_convention);
5465}
5466
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005467void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005468 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5469 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5470 ? Location::RequiresRegister()
5471 : Location::Any();
5472 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005473}
5474
Calin Juravle2ae48182016-03-16 14:05:09 +00005475void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5476 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005477 return;
5478 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005479 LocationSummary* locations = instruction->GetLocations();
5480 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005481
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005482 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005483 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005484}
5485
Calin Juravle2ae48182016-03-16 14:05:09 +00005486void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005487 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005488 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005489
5490 LocationSummary* locations = instruction->GetLocations();
5491 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005492
5493 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005494 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005495 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005496 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005497 } else {
5498 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005499 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005500 __ jmp(slow_path->GetEntryLabel());
5501 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005502 }
5503 __ j(kEqual, slow_path->GetEntryLabel());
5504}
5505
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005506void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005507 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005508}
5509
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005510void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005511 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005513 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005514 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5515 object_array_get_with_read_barrier
5516 ? LocationSummary::kCallOnSlowPath
5517 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005518 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005519 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005520 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005521 locations->SetInAt(0, Location::RequiresRegister());
5522 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005524 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5525 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005526 // The output overlaps in case of long: we don't want the low move
5527 // to overwrite the array's location. Likewise, in the case of an
5528 // object array get with read barriers enabled, we do not want the
5529 // move to overwrite the array's location, as we need it to emit
5530 // the read barrier.
5531 locations->SetOut(
5532 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005533 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5534 ? Location::kOutputOverlap
5535 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005536 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005537}
5538
5539void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5540 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005541 Location obj_loc = locations->InAt(0);
5542 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005544 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005545 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005548 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005549 case DataType::Type::kBool:
5550 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005551 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005552 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553 break;
5554 }
5555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005556 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005557 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005558 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005559 break;
5560 }
5561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005563 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005564 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5565 // Branch cases into compressed and uncompressed for each index's type.
5566 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5567 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005568 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005569 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005570 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5571 "Expecting 0=compressed, 1=uncompressed");
5572 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005573 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5574 __ jmp(&done);
5575 __ Bind(&not_compressed);
5576 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5577 __ Bind(&done);
5578 } else {
5579 // Common case for charAt of array of char or when string compression's
5580 // feature is turned off.
5581 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5582 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583 break;
5584 }
5585
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005586 case DataType::Type::kInt16: {
5587 Register out = out_loc.AsRegister<Register>();
5588 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5589 break;
5590 }
5591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005593 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005594 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595 break;
5596 }
5597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005599 static_assert(
5600 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5601 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005602 // /* HeapReference<Object> */ out =
5603 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5604 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005605 // Note that a potential implicit null check is handled in this
5606 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5607 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005608 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005609 } else {
5610 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005611 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5612 codegen_->MaybeRecordImplicitNullCheck(instruction);
5613 // If read barriers are enabled, emit read barriers other than
5614 // Baker's using a slow path (and also unpoison the loaded
5615 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005616 if (index.IsConstant()) {
5617 uint32_t offset =
5618 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005619 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5620 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005621 codegen_->MaybeGenerateReadBarrierSlow(
5622 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5623 }
5624 }
5625 break;
5626 }
5627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005628 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005629 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005630 __ movl(out_loc.AsRegisterPairLow<Register>(),
5631 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5632 codegen_->MaybeRecordImplicitNullCheck(instruction);
5633 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5634 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635 break;
5636 }
5637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005639 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005640 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005641 break;
5642 }
5643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005645 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005646 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005647 break;
5648 }
5649
Aart Bik66c158e2018-01-31 12:55:04 -08005650 case DataType::Type::kUint32:
5651 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005653 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005654 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005655 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005657 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005658 // Potential implicit null checks, in the case of reference or
5659 // long arrays, are handled in the previous switch statement.
5660 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005661 codegen_->MaybeRecordImplicitNullCheck(instruction);
5662 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663}
5664
5665void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005666 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005667
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005668 bool needs_write_barrier =
5669 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005670 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005671
Vladimir Markoca6fff82017-10-03 14:49:14 +01005672 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005673 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005674 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005675 LocationSummary::kCallOnSlowPath :
5676 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005677
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005678 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005679 // We need the inputs to be different than the output in case of long operation.
5680 // In case of a byte operation, the register allocator does not support multiple
5681 // inputs that die at entry with one in a specific register.
5682 locations->SetInAt(0, Location::RequiresRegister());
5683 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5684 if (is_byte_type) {
5685 // Ensure the value is in a byte register.
5686 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005687 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005688 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005689 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005690 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5691 }
5692 if (needs_write_barrier) {
5693 // Temporary registers for the write barrier.
5694 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5695 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005696 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005697 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005698}
5699
5700void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5701 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005702 Location array_loc = locations->InAt(0);
5703 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005704 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005705 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005707 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5708 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5709 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005711 bool needs_write_barrier =
5712 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005713
5714 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005716 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005718 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005719 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005720 if (value.IsRegister()) {
5721 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005723 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005724 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005725 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005726 break;
5727 }
5728
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005729 case DataType::Type::kUint16:
5730 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005731 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005732 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005733 if (value.IsRegister()) {
5734 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005735 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005736 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005737 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005738 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005739 break;
5740 }
5741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005743 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005744 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005746 if (!value.IsRegister()) {
5747 // Just setting null.
5748 DCHECK(instruction->InputAt(2)->IsNullConstant());
5749 DCHECK(value.IsConstant()) << value;
5750 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005751 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005752 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005753 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005754 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005755 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005756
5757 DCHECK(needs_write_barrier);
5758 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005759 // We cannot use a NearLabel for `done`, as its range may be too
5760 // short when Baker read barriers are enabled.
5761 Label done;
5762 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005763 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005764 Location temp_loc = locations->GetTemp(0);
5765 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005767 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005768 codegen_->AddSlowPath(slow_path);
5769 if (instruction->GetValueCanBeNull()) {
5770 __ testl(register_value, register_value);
5771 __ j(kNotEqual, &not_null);
5772 __ movl(address, Immediate(0));
5773 codegen_->MaybeRecordImplicitNullCheck(instruction);
5774 __ jmp(&done);
5775 __ Bind(&not_null);
5776 }
5777
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005778 // Note that when Baker read barriers are enabled, the type
5779 // checks are performed without read barriers. This is fine,
5780 // even in the case where a class object is in the from-space
5781 // after the flip, as a comparison involving such a type would
5782 // not produce a false positive; it may of course produce a
5783 // false negative, in which case we would take the ArraySet
5784 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005785
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005786 // /* HeapReference<Class> */ temp = array->klass_
5787 __ movl(temp, Address(array, class_offset));
5788 codegen_->MaybeRecordImplicitNullCheck(instruction);
5789 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005790
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005791 // /* HeapReference<Class> */ temp = temp->component_type_
5792 __ movl(temp, Address(temp, component_offset));
5793 // If heap poisoning is enabled, no need to unpoison `temp`
5794 // nor the object reference in `register_value->klass`, as
5795 // we are comparing two poisoned references.
5796 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005797
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005798 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5799 __ j(kEqual, &do_put);
5800 // If heap poisoning is enabled, the `temp` reference has
5801 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005802 __ MaybeUnpoisonHeapReference(temp);
5803
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005804 // If heap poisoning is enabled, no need to unpoison the
5805 // heap reference loaded below, as it is only used for a
5806 // comparison with null.
5807 __ cmpl(Address(temp, super_offset), Immediate(0));
5808 __ j(kNotEqual, slow_path->GetEntryLabel());
5809 __ Bind(&do_put);
5810 } else {
5811 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005812 }
5813 }
5814
5815 if (kPoisonHeapReferences) {
5816 __ movl(temp, register_value);
5817 __ PoisonHeapReference(temp);
5818 __ movl(address, temp);
5819 } else {
5820 __ movl(address, register_value);
5821 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005823 codegen_->MaybeRecordImplicitNullCheck(instruction);
5824 }
5825
5826 Register card = locations->GetTemp(1).AsRegister<Register>();
5827 codegen_->MarkGCCard(
5828 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5829 __ Bind(&done);
5830
5831 if (slow_path != nullptr) {
5832 __ Bind(slow_path->GetExitLabel());
5833 }
5834
5835 break;
5836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005839 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005840 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005841 if (value.IsRegister()) {
5842 __ movl(address, value.AsRegister<Register>());
5843 } else {
5844 DCHECK(value.IsConstant()) << value;
5845 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5846 __ movl(address, Immediate(v));
5847 }
5848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005849 break;
5850 }
5851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005852 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005853 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005854 if (value.IsRegisterPair()) {
5855 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5856 value.AsRegisterPairLow<Register>());
5857 codegen_->MaybeRecordImplicitNullCheck(instruction);
5858 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5859 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005860 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005861 DCHECK(value.IsConstant());
5862 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5863 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5864 Immediate(Low32Bits(val)));
5865 codegen_->MaybeRecordImplicitNullCheck(instruction);
5866 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5867 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005868 }
5869 break;
5870 }
5871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005873 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005874 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005875 if (value.IsFpuRegister()) {
5876 __ movss(address, value.AsFpuRegister<XmmRegister>());
5877 } else {
5878 DCHECK(value.IsConstant());
5879 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5880 __ movl(address, Immediate(v));
5881 }
5882 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005883 break;
5884 }
5885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005887 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005888 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005889 if (value.IsFpuRegister()) {
5890 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5891 } else {
5892 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005893 Address address_hi =
5894 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005895 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5896 __ movl(address, Immediate(Low32Bits(v)));
5897 codegen_->MaybeRecordImplicitNullCheck(instruction);
5898 __ movl(address_hi, Immediate(High32Bits(v)));
5899 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005900 break;
5901 }
5902
Aart Bik66c158e2018-01-31 12:55:04 -08005903 case DataType::Type::kUint32:
5904 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005905 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005906 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005907 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005908 }
5909}
5910
5911void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005912 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005913 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005914 if (!instruction->IsEmittedAtUseSite()) {
5915 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5916 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005917}
5918
5919void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005920 if (instruction->IsEmittedAtUseSite()) {
5921 return;
5922 }
5923
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005924 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005925 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005926 Register obj = locations->InAt(0).AsRegister<Register>();
5927 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005928 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005929 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005930 // Mask out most significant bit in case the array is String's array of char.
5931 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005932 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005934}
5935
5936void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005937 RegisterSet caller_saves = RegisterSet::Empty();
5938 InvokeRuntimeCallingConvention calling_convention;
5939 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5940 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5941 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005942 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005943 HInstruction* length = instruction->InputAt(1);
5944 if (!length->IsEmittedAtUseSite()) {
5945 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5946 }
jessicahandojo4877b792016-09-08 19:49:13 -07005947 // Need register to see array's length.
5948 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5949 locations->AddTemp(Location::RequiresRegister());
5950 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005951}
5952
5953void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005954 const bool is_string_compressed_char_at =
5955 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005956 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005957 Location index_loc = locations->InAt(0);
5958 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005959 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005960 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005961
Mark Mendell99dbd682015-04-22 16:18:52 -04005962 if (length_loc.IsConstant()) {
5963 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5964 if (index_loc.IsConstant()) {
5965 // BCE will remove the bounds check if we are guarenteed to pass.
5966 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5967 if (index < 0 || index >= length) {
5968 codegen_->AddSlowPath(slow_path);
5969 __ jmp(slow_path->GetEntryLabel());
5970 } else {
5971 // Some optimization after BCE may have generated this, and we should not
5972 // generate a bounds check if it is a valid range.
5973 }
5974 return;
5975 }
5976
5977 // We have to reverse the jump condition because the length is the constant.
5978 Register index_reg = index_loc.AsRegister<Register>();
5979 __ cmpl(index_reg, Immediate(length));
5980 codegen_->AddSlowPath(slow_path);
5981 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005982 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005983 HInstruction* array_length = instruction->InputAt(1);
5984 if (array_length->IsEmittedAtUseSite()) {
5985 // Address the length field in the array.
5986 DCHECK(array_length->IsArrayLength());
5987 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5988 Location array_loc = array_length->GetLocations()->InAt(0);
5989 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005990 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005991 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5992 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005993 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5994 __ movl(length_reg, array_len);
5995 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005996 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005997 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005998 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005999 // Checking bounds for general case:
6000 // Array of char or string's array with feature compression off.
6001 if (index_loc.IsConstant()) {
6002 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6003 __ cmpl(array_len, Immediate(value));
6004 } else {
6005 __ cmpl(array_len, index_loc.AsRegister<Register>());
6006 }
6007 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006008 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006009 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006010 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006011 }
6012 codegen_->AddSlowPath(slow_path);
6013 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006014 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006015}
6016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006017void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006018 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006019}
6020
6021void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006022 if (instruction->GetNext()->IsSuspendCheck() &&
6023 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6024 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6025 // The back edge will generate the suspend check.
6026 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6027 }
6028
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006029 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6030}
6031
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006032void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006033 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6034 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006035 // In suspend check slow path, usually there are no caller-save registers at all.
6036 // If SIMD instructions are present, however, we force spilling all live SIMD
6037 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006038 locations->SetCustomSlowPathCallerSaves(
6039 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006040}
6041
6042void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006043 HBasicBlock* block = instruction->GetBlock();
6044 if (block->GetLoopInformation() != nullptr) {
6045 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6046 // The back edge will generate the suspend check.
6047 return;
6048 }
6049 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6050 // The goto will generate the suspend check.
6051 return;
6052 }
6053 GenerateSuspendCheck(instruction, nullptr);
6054}
6055
6056void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6057 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006058 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006059 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6060 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006061 slow_path =
6062 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006063 instruction->SetSlowPath(slow_path);
6064 codegen_->AddSlowPath(slow_path);
6065 if (successor != nullptr) {
6066 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006067 }
6068 } else {
6069 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6070 }
6071
Andreas Gampe542451c2016-07-26 09:02:02 -07006072 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006073 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006074 if (successor == nullptr) {
6075 __ j(kNotEqual, slow_path->GetEntryLabel());
6076 __ Bind(slow_path->GetReturnLabel());
6077 } else {
6078 __ j(kEqual, codegen_->GetLabelOf(successor));
6079 __ jmp(slow_path->GetEntryLabel());
6080 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006081}
6082
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006083X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6084 return codegen_->GetAssembler();
6085}
6086
Aart Bikcfe50bb2017-12-12 14:54:12 -08006087void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006088 ScratchRegisterScope ensure_scratch(
6089 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6090 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6091 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006092
Aart Bikcfe50bb2017-12-12 14:54:12 -08006093 // Now that temp register is available (possibly spilled), move blocks of memory.
6094 for (int i = 0; i < number_of_words; i++) {
6095 __ movl(temp_reg, Address(ESP, src + stack_offset));
6096 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6097 stack_offset += kX86WordSize;
6098 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006099}
6100
6101void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006102 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006103 Location source = move->GetSource();
6104 Location destination = move->GetDestination();
6105
6106 if (source.IsRegister()) {
6107 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006108 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006109 } else if (destination.IsFpuRegister()) {
6110 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006111 } else {
6112 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006113 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006114 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006115 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006116 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006117 // Create stack space for 2 elements.
6118 __ subl(ESP, Immediate(2 * elem_size));
6119 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6120 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6121 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6122 // And remove the temporary stack space we allocated.
6123 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006124 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006125 if (destination.IsRegister()) {
6126 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6127 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006128 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006129 } else if (destination.IsRegisterPair()) {
6130 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6131 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6132 __ psrlq(src_reg, Immediate(32));
6133 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006134 } else if (destination.IsStackSlot()) {
6135 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006136 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006137 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006138 } else {
6139 DCHECK(destination.IsSIMDStackSlot());
6140 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006141 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006142 } else if (source.IsStackSlot()) {
6143 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006144 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006145 } else if (destination.IsFpuRegister()) {
6146 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006147 } else {
6148 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006149 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006150 }
6151 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006152 if (destination.IsRegisterPair()) {
6153 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6154 __ movl(destination.AsRegisterPairHigh<Register>(),
6155 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6156 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006157 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6158 } else {
6159 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006160 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006161 }
Aart Bik5576f372017-03-23 16:17:37 -07006162 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006163 if (destination.IsFpuRegister()) {
6164 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6165 } else {
6166 DCHECK(destination.IsSIMDStackSlot());
6167 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6168 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006169 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006170 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006171 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006172 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006173 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006174 if (value == 0) {
6175 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6176 } else {
6177 __ movl(destination.AsRegister<Register>(), Immediate(value));
6178 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006179 } else {
6180 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006181 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006182 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006183 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006184 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006185 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006186 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006187 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006188 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6189 if (value == 0) {
6190 // Easy handling of 0.0.
6191 __ xorps(dest, dest);
6192 } else {
6193 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006194 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6195 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6196 __ movl(temp, Immediate(value));
6197 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006198 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006199 } else {
6200 DCHECK(destination.IsStackSlot()) << destination;
6201 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6202 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006203 } else if (constant->IsLongConstant()) {
6204 int64_t value = constant->AsLongConstant()->GetValue();
6205 int32_t low_value = Low32Bits(value);
6206 int32_t high_value = High32Bits(value);
6207 Immediate low(low_value);
6208 Immediate high(high_value);
6209 if (destination.IsDoubleStackSlot()) {
6210 __ movl(Address(ESP, destination.GetStackIndex()), low);
6211 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6212 } else {
6213 __ movl(destination.AsRegisterPairLow<Register>(), low);
6214 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6215 }
6216 } else {
6217 DCHECK(constant->IsDoubleConstant());
6218 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006219 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006220 int32_t low_value = Low32Bits(value);
6221 int32_t high_value = High32Bits(value);
6222 Immediate low(low_value);
6223 Immediate high(high_value);
6224 if (destination.IsFpuRegister()) {
6225 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6226 if (value == 0) {
6227 // Easy handling of 0.0.
6228 __ xorpd(dest, dest);
6229 } else {
6230 __ pushl(high);
6231 __ pushl(low);
6232 __ movsd(dest, Address(ESP, 0));
6233 __ addl(ESP, Immediate(8));
6234 }
6235 } else {
6236 DCHECK(destination.IsDoubleStackSlot()) << destination;
6237 __ movl(Address(ESP, destination.GetStackIndex()), low);
6238 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6239 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006240 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006241 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006242 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006243 }
6244}
6245
Mark Mendella5c19ce2015-04-01 12:51:05 -04006246void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006247 Register suggested_scratch = reg == EAX ? EBX : EAX;
6248 ScratchRegisterScope ensure_scratch(
6249 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6250
6251 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6252 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6253 __ movl(Address(ESP, mem + stack_offset), reg);
6254 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006255}
6256
Mark Mendell7c8d0092015-01-26 11:21:33 -05006257void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006258 ScratchRegisterScope ensure_scratch(
6259 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6260
6261 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6262 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6263 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6264 __ movss(Address(ESP, mem + stack_offset), reg);
6265 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006266}
6267
Aart Bikcfe50bb2017-12-12 14:54:12 -08006268void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6269 size_t extra_slot = 4 * kX86WordSize;
6270 __ subl(ESP, Immediate(extra_slot));
6271 __ movups(Address(ESP, 0), XmmRegister(reg));
6272 ExchangeMemory(0, mem + extra_slot, 4);
6273 __ movups(XmmRegister(reg), Address(ESP, 0));
6274 __ addl(ESP, Immediate(extra_slot));
6275}
6276
6277void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006278 ScratchRegisterScope ensure_scratch1(
6279 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006280
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006281 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6282 ScratchRegisterScope ensure_scratch2(
6283 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006284
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006285 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6286 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006287
6288 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6289 for (int i = 0; i < number_of_words; i++) {
6290 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6291 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6292 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6293 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6294 stack_offset += kX86WordSize;
6295 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006296}
6297
6298void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006299 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006300 Location source = move->GetSource();
6301 Location destination = move->GetDestination();
6302
6303 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006304 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6305 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6306 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6307 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6308 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006309 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006310 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006311 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006312 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006313 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006314 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006315 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6316 // Use XOR Swap algorithm to avoid a temporary.
6317 DCHECK_NE(source.reg(), destination.reg());
6318 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6319 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6320 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6321 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6322 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6323 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6324 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006325 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6326 // Take advantage of the 16 bytes in the XMM register.
6327 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6328 Address stack(ESP, destination.GetStackIndex());
6329 // Load the double into the high doubleword.
6330 __ movhpd(reg, stack);
6331
6332 // Store the low double into the destination.
6333 __ movsd(stack, reg);
6334
6335 // Move the high double to the low double.
6336 __ psrldq(reg, Immediate(8));
6337 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6338 // Take advantage of the 16 bytes in the XMM register.
6339 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6340 Address stack(ESP, source.GetStackIndex());
6341 // Load the double into the high doubleword.
6342 __ movhpd(reg, stack);
6343
6344 // Store the low double into the destination.
6345 __ movsd(stack, reg);
6346
6347 // Move the high double to the low double.
6348 __ psrldq(reg, Immediate(8));
6349 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006350 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6351 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6352 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6353 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6354 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6355 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6356 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006357 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006358 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006359 }
6360}
6361
6362void ParallelMoveResolverX86::SpillScratch(int reg) {
6363 __ pushl(static_cast<Register>(reg));
6364}
6365
6366void ParallelMoveResolverX86::RestoreScratch(int reg) {
6367 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006368}
6369
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006370HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6371 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006373 case HLoadClass::LoadKind::kInvalid:
6374 LOG(FATAL) << "UNREACHABLE";
6375 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006376 case HLoadClass::LoadKind::kReferrersClass:
6377 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006378 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006379 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006380 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006381 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006383 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006384 DCHECK(Runtime::Current()->UseJitCompilation());
6385 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006386 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006387 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006388 break;
6389 }
6390 return desired_class_load_kind;
6391}
6392
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006393void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006394 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006395 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006396 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006397 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006398 cls,
6399 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006400 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006401 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006402 return;
6403 }
Vladimir Marko41559982017-01-06 14:04:23 +00006404 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006405
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006406 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6407 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006408 ? LocationSummary::kCallOnSlowPath
6409 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006410 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006411 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006412 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006413 }
6414
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006415 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006416 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006417 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006418 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006419 locations->SetInAt(0, Location::RequiresRegister());
6420 }
6421 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006422 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6423 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6424 // Rely on the type resolution and/or initialization to save everything.
6425 RegisterSet caller_saves = RegisterSet::Empty();
6426 InvokeRuntimeCallingConvention calling_convention;
6427 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6428 locations->SetCustomSlowPathCallerSaves(caller_saves);
6429 } else {
6430 // For non-Baker read barrier we have a temp-clobbering call.
6431 }
6432 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006433}
6434
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006435Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006436 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006437 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006438 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006439 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006440 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006441 PatchInfo<Label>* info = &jit_class_patches_.back();
6442 return &info->label;
6443}
6444
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006445// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6446// move.
6447void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006448 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006449 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006450 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006451 return;
6452 }
Vladimir Marko41559982017-01-06 14:04:23 +00006453 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006454
Vladimir Marko41559982017-01-06 14:04:23 +00006455 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006456 Location out_loc = locations->Out();
6457 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006459 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006460 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6461 ? kWithoutReadBarrier
6462 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006463 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 case HLoadClass::LoadKind::kReferrersClass: {
6465 DCHECK(!cls->CanCallRuntime());
6466 DCHECK(!cls->MustGenerateClinitCheck());
6467 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6468 Register current_method = locations->InAt(0).AsRegister<Register>();
6469 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006470 cls,
6471 out_loc,
6472 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006473 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006474 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006475 break;
6476 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006477 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006478 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006479 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006480 Register method_address = locations->InAt(0).AsRegister<Register>();
6481 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006482 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006483 break;
6484 }
6485 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006486 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006487 uint32_t address = dchecked_integral_cast<uint32_t>(
6488 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6489 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006490 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006491 break;
6492 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006493 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006494 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6495 Register method_address = locations->InAt(0).AsRegister<Register>();
6496 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006497 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6498 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006499 break;
6500 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006501 case HLoadClass::LoadKind::kBssEntry: {
6502 Register method_address = locations->InAt(0).AsRegister<Register>();
6503 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6504 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6505 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6506 generate_null_check = true;
6507 break;
6508 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006509 case HLoadClass::LoadKind::kJitTableAddress: {
6510 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6511 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006512 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006513 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006514 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006515 break;
6516 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006517 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006518 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006519 LOG(FATAL) << "UNREACHABLE";
6520 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006521 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006522
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006523 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6524 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006525 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006526 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6527 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006528
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006529 if (generate_null_check) {
6530 __ testl(out, out);
6531 __ j(kEqual, slow_path->GetEntryLabel());
6532 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006533
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006534 if (cls->MustGenerateClinitCheck()) {
6535 GenerateClassInitializationCheck(slow_path, out);
6536 } else {
6537 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006538 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006539 }
6540}
6541
6542void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6543 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006544 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006545 locations->SetInAt(0, Location::RequiresRegister());
6546 if (check->HasUses()) {
6547 locations->SetOut(Location::SameAsFirstInput());
6548 }
6549}
6550
6551void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006552 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006553 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006554 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006555 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006556 GenerateClassInitializationCheck(slow_path,
6557 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006558}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006559
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006560void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006561 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006562 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6563 const size_t status_byte_offset =
6564 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6565 constexpr uint32_t shifted_initialized_value =
6566 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6567
6568 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006569 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006570 __ Bind(slow_path->GetExitLabel());
6571 // No need for memory fence, thanks to the X86 memory model.
6572}
6573
Vladimir Marko175e7862018-03-27 09:03:13 +00006574void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6575 Register temp) {
6576 uint32_t path_to_root = check->GetBitstringPathToRoot();
6577 uint32_t mask = check->GetBitstringMask();
6578 DCHECK(IsPowerOfTwo(mask + 1));
6579 size_t mask_bits = WhichPowerOf2(mask + 1);
6580
6581 if (mask_bits == 16u) {
6582 // Compare the bitstring in memory.
6583 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6584 } else {
6585 // /* uint32_t */ temp = temp->status_
6586 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6587 // Compare the bitstring bits using SUB.
6588 __ subl(temp, Immediate(path_to_root));
6589 // Shift out bits that do not contribute to the comparison.
6590 __ shll(temp, Immediate(32u - mask_bits));
6591 }
6592}
6593
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006594HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6595 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006596 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006597 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006598 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006599 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006600 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006601 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006602 case HLoadString::LoadKind::kJitTableAddress:
6603 DCHECK(Runtime::Current()->UseJitCompilation());
6604 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006605 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006606 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006607 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006608 }
6609 return desired_string_load_kind;
6610}
6611
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006612void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006613 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006614 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006615 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006616 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006617 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006618 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006619 locations->SetInAt(0, Location::RequiresRegister());
6620 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006621 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006622 locations->SetOut(Location::RegisterLocation(EAX));
6623 } else {
6624 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006625 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6626 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006627 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006628 RegisterSet caller_saves = RegisterSet::Empty();
6629 InvokeRuntimeCallingConvention calling_convention;
6630 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6631 locations->SetCustomSlowPathCallerSaves(caller_saves);
6632 } else {
6633 // For non-Baker read barrier we have a temp-clobbering call.
6634 }
6635 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006636 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006637}
6638
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006639Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006640 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006641 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006642 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006643 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006644 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006645 PatchInfo<Label>* info = &jit_string_patches_.back();
6646 return &info->label;
6647}
6648
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006649// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6650// move.
6651void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006652 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006653 Location out_loc = locations->Out();
6654 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006655
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006656 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006657 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006658 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006659 Register method_address = locations->InAt(0).AsRegister<Register>();
6660 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006661 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006662 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006663 }
6664 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006665 uint32_t address = dchecked_integral_cast<uint32_t>(
6666 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6667 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006668 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006669 return;
6670 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006671 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006672 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6673 Register method_address = locations->InAt(0).AsRegister<Register>();
6674 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006675 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6676 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006677 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006678 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006679 case HLoadString::LoadKind::kBssEntry: {
6680 Register method_address = locations->InAt(0).AsRegister<Register>();
6681 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6682 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006683 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006684 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006685 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006686 codegen_->AddSlowPath(slow_path);
6687 __ testl(out, out);
6688 __ j(kEqual, slow_path->GetEntryLabel());
6689 __ Bind(slow_path->GetExitLabel());
6690 return;
6691 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006692 case HLoadString::LoadKind::kJitTableAddress: {
6693 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6694 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006695 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006696 // /* GcRoot<mirror::String> */ out = *address
6697 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6698 return;
6699 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006700 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006701 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006702 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006704 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006705 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006706 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006707 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006708 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6709 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006710}
6711
David Brazdilcb1c0552015-08-04 16:22:25 +01006712static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006713 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006714}
6715
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006716void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6717 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006718 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006719 locations->SetOut(Location::RequiresRegister());
6720}
6721
6722void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006723 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6724}
6725
6726void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006727 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006728}
6729
6730void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6731 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006732}
6733
6734void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006735 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6736 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006737 InvokeRuntimeCallingConvention calling_convention;
6738 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6739}
6740
6741void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006742 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006743 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006744}
6745
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006746// Temp is used for read barrier.
6747static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6748 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006749 !kUseBakerReadBarrier &&
6750 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006751 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006752 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6753 return 1;
6754 }
6755 return 0;
6756}
6757
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006758// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6759// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006760// The other checks have one temp for loading the object's class.
6761static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006762 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006763 return 2;
6764 }
6765 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006766}
6767
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006768void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006769 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006770 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006771 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006772 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006773 case TypeCheckKind::kExactCheck:
6774 case TypeCheckKind::kAbstractClassCheck:
6775 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006776 case TypeCheckKind::kArrayObjectCheck: {
6777 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6778 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6779 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006780 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006781 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006782 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006783 case TypeCheckKind::kUnresolvedCheck:
6784 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 call_kind = LocationSummary::kCallOnSlowPath;
6786 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006787 case TypeCheckKind::kBitstringCheck:
6788 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006789 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006790
Vladimir Markoca6fff82017-10-03 14:49:14 +01006791 LocationSummary* locations =
6792 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006793 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006794 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006795 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006796 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006797 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6798 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6799 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6800 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6801 } else {
6802 locations->SetInAt(1, Location::Any());
6803 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6805 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006806 // When read barriers are enabled, we need a temporary register for some cases.
6807 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006808}
6809
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006810void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006811 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006812 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006813 Location obj_loc = locations->InAt(0);
6814 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006815 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006816 Location out_loc = locations->Out();
6817 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006818 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6819 DCHECK_LE(num_temps, 1u);
6820 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006821 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006822 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6823 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6824 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006825 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006826 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006827
6828 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006829 // Avoid null check if we know obj is not null.
6830 if (instruction->MustDoNullCheck()) {
6831 __ testl(obj, obj);
6832 __ j(kEqual, &zero);
6833 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006834
Roland Levillain7c1559a2015-12-15 10:55:36 +00006835 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006836 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006837 ReadBarrierOption read_barrier_option =
6838 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006839 // /* HeapReference<Class> */ out = obj->klass_
6840 GenerateReferenceLoadTwoRegisters(instruction,
6841 out_loc,
6842 obj_loc,
6843 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006844 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006845 if (cls.IsRegister()) {
6846 __ cmpl(out, cls.AsRegister<Register>());
6847 } else {
6848 DCHECK(cls.IsStackSlot()) << cls;
6849 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6850 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006851
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006852 // Classes must be equal for the instanceof to succeed.
6853 __ j(kNotEqual, &zero);
6854 __ movl(out, Immediate(1));
6855 __ jmp(&done);
6856 break;
6857 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006858
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006859 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006860 ReadBarrierOption read_barrier_option =
6861 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006862 // /* HeapReference<Class> */ out = obj->klass_
6863 GenerateReferenceLoadTwoRegisters(instruction,
6864 out_loc,
6865 obj_loc,
6866 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006867 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006868 // If the class is abstract, we eagerly fetch the super class of the
6869 // object to avoid doing a comparison we know will fail.
6870 NearLabel loop;
6871 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006872 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006873 GenerateReferenceLoadOneRegister(instruction,
6874 out_loc,
6875 super_offset,
6876 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006877 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006878 __ testl(out, out);
6879 // If `out` is null, we use it for the result, and jump to `done`.
6880 __ j(kEqual, &done);
6881 if (cls.IsRegister()) {
6882 __ cmpl(out, cls.AsRegister<Register>());
6883 } else {
6884 DCHECK(cls.IsStackSlot()) << cls;
6885 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6886 }
6887 __ j(kNotEqual, &loop);
6888 __ movl(out, Immediate(1));
6889 if (zero.IsLinked()) {
6890 __ jmp(&done);
6891 }
6892 break;
6893 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006894
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006895 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006896 ReadBarrierOption read_barrier_option =
6897 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006898 // /* HeapReference<Class> */ out = obj->klass_
6899 GenerateReferenceLoadTwoRegisters(instruction,
6900 out_loc,
6901 obj_loc,
6902 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006903 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006904 // Walk over the class hierarchy to find a match.
6905 NearLabel loop, success;
6906 __ Bind(&loop);
6907 if (cls.IsRegister()) {
6908 __ cmpl(out, cls.AsRegister<Register>());
6909 } else {
6910 DCHECK(cls.IsStackSlot()) << cls;
6911 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6912 }
6913 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006914 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006915 GenerateReferenceLoadOneRegister(instruction,
6916 out_loc,
6917 super_offset,
6918 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006919 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006920 __ testl(out, out);
6921 __ j(kNotEqual, &loop);
6922 // If `out` is null, we use it for the result, and jump to `done`.
6923 __ jmp(&done);
6924 __ Bind(&success);
6925 __ movl(out, Immediate(1));
6926 if (zero.IsLinked()) {
6927 __ jmp(&done);
6928 }
6929 break;
6930 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006931
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006932 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006933 ReadBarrierOption read_barrier_option =
6934 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006935 // /* HeapReference<Class> */ out = obj->klass_
6936 GenerateReferenceLoadTwoRegisters(instruction,
6937 out_loc,
6938 obj_loc,
6939 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006940 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006941 // Do an exact check.
6942 NearLabel exact_check;
6943 if (cls.IsRegister()) {
6944 __ cmpl(out, cls.AsRegister<Register>());
6945 } else {
6946 DCHECK(cls.IsStackSlot()) << cls;
6947 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6948 }
6949 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006950 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006951 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006952 GenerateReferenceLoadOneRegister(instruction,
6953 out_loc,
6954 component_offset,
6955 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006956 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006957 __ testl(out, out);
6958 // If `out` is null, we use it for the result, and jump to `done`.
6959 __ j(kEqual, &done);
6960 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6961 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006962 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006963 __ movl(out, Immediate(1));
6964 __ jmp(&done);
6965 break;
6966 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006967
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006968 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006969 // No read barrier since the slow path will retry upon failure.
6970 // /* HeapReference<Class> */ out = obj->klass_
6971 GenerateReferenceLoadTwoRegisters(instruction,
6972 out_loc,
6973 obj_loc,
6974 class_offset,
6975 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006976 if (cls.IsRegister()) {
6977 __ cmpl(out, cls.AsRegister<Register>());
6978 } else {
6979 DCHECK(cls.IsStackSlot()) << cls;
6980 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6981 }
6982 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006983 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6984 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006985 codegen_->AddSlowPath(slow_path);
6986 __ j(kNotEqual, slow_path->GetEntryLabel());
6987 __ movl(out, Immediate(1));
6988 if (zero.IsLinked()) {
6989 __ jmp(&done);
6990 }
6991 break;
6992 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006993
Calin Juravle98893e12015-10-02 21:05:03 +01006994 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006995 case TypeCheckKind::kInterfaceCheck: {
6996 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006997 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006998 // cases.
6999 //
7000 // We cannot directly call the InstanceofNonTrivial runtime
7001 // entry point without resorting to a type checking slow path
7002 // here (i.e. by calling InvokeRuntime directly), as it would
7003 // require to assign fixed registers for the inputs of this
7004 // HInstanceOf instruction (following the runtime calling
7005 // convention), which might be cluttered by the potential first
7006 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007007 //
7008 // TODO: Introduce a new runtime entry point taking the object
7009 // to test (instead of its class) as argument, and let it deal
7010 // with the read barrier issues. This will let us refactor this
7011 // case of the `switch` code as it was previously (with a direct
7012 // call to the runtime not using a type checking slow path).
7013 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007014 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007015 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7016 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007017 codegen_->AddSlowPath(slow_path);
7018 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007019 if (zero.IsLinked()) {
7020 __ jmp(&done);
7021 }
7022 break;
7023 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007024
7025 case TypeCheckKind::kBitstringCheck: {
7026 // /* HeapReference<Class> */ temp = obj->klass_
7027 GenerateReferenceLoadTwoRegisters(instruction,
7028 out_loc,
7029 obj_loc,
7030 class_offset,
7031 kWithoutReadBarrier);
7032
7033 GenerateBitstringTypeCheckCompare(instruction, out);
7034 __ j(kNotEqual, &zero);
7035 __ movl(out, Immediate(1));
7036 __ jmp(&done);
7037 break;
7038 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007039 }
7040
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007041 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007042 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007043 __ xorl(out, out);
7044 }
7045
7046 if (done.IsLinked()) {
7047 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007048 }
7049
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007050 if (slow_path != nullptr) {
7051 __ Bind(slow_path->GetExitLabel());
7052 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007053}
7054
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007055void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007056 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007057 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007058 LocationSummary* locations =
7059 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007060 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007061 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7062 // Require a register for the interface check since there is a loop that compares the class to
7063 // a memory address.
7064 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007065 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7066 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7067 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7068 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007069 } else {
7070 locations->SetInAt(1, Location::Any());
7071 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007072 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007073 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7074}
7075
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007076void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007077 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007078 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007079 Location obj_loc = locations->InAt(0);
7080 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007081 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007082 Location temp_loc = locations->GetTemp(0);
7083 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007084 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7085 DCHECK_GE(num_temps, 1u);
7086 DCHECK_LE(num_temps, 2u);
7087 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7088 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7089 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7090 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7091 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7092 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7093 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7094 const uint32_t object_array_data_offset =
7095 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007096
Vladimir Marko87584542017-12-12 17:47:52 +00007097 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007098 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007099 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7100 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007101 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007102
Roland Levillain0d5a2812015-11-13 10:07:31 +00007103 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007104 // Avoid null check if we know obj is not null.
7105 if (instruction->MustDoNullCheck()) {
7106 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007107 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007108 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007109
Roland Levillain0d5a2812015-11-13 10:07:31 +00007110 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007111 case TypeCheckKind::kExactCheck:
7112 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007113 // /* HeapReference<Class> */ temp = obj->klass_
7114 GenerateReferenceLoadTwoRegisters(instruction,
7115 temp_loc,
7116 obj_loc,
7117 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007118 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007119
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007120 if (cls.IsRegister()) {
7121 __ cmpl(temp, cls.AsRegister<Register>());
7122 } else {
7123 DCHECK(cls.IsStackSlot()) << cls;
7124 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7125 }
7126 // Jump to slow path for throwing the exception or doing a
7127 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007128 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007129 break;
7130 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007131
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007132 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007133 // /* HeapReference<Class> */ temp = obj->klass_
7134 GenerateReferenceLoadTwoRegisters(instruction,
7135 temp_loc,
7136 obj_loc,
7137 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007138 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007139
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007140 // If the class is abstract, we eagerly fetch the super class of the
7141 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007142 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007143 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007144 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007145 GenerateReferenceLoadOneRegister(instruction,
7146 temp_loc,
7147 super_offset,
7148 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007149 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007150
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007151 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7152 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007153 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007154 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007155
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007156 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007157 if (cls.IsRegister()) {
7158 __ cmpl(temp, cls.AsRegister<Register>());
7159 } else {
7160 DCHECK(cls.IsStackSlot()) << cls;
7161 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7162 }
7163 __ j(kNotEqual, &loop);
7164 break;
7165 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007166
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007167 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007168 // /* HeapReference<Class> */ temp = obj->klass_
7169 GenerateReferenceLoadTwoRegisters(instruction,
7170 temp_loc,
7171 obj_loc,
7172 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007173 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007174
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007175 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007176 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007177 __ Bind(&loop);
7178 if (cls.IsRegister()) {
7179 __ cmpl(temp, cls.AsRegister<Register>());
7180 } else {
7181 DCHECK(cls.IsStackSlot()) << cls;
7182 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7183 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007184 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007185
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007187 GenerateReferenceLoadOneRegister(instruction,
7188 temp_loc,
7189 super_offset,
7190 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007191 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007192
7193 // If the class reference currently in `temp` is not null, jump
7194 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007195 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007196 __ j(kNotZero, &loop);
7197 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007198 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007199 break;
7200 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007201
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007202 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007203 // /* HeapReference<Class> */ temp = obj->klass_
7204 GenerateReferenceLoadTwoRegisters(instruction,
7205 temp_loc,
7206 obj_loc,
7207 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007208 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007209
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007210 // Do an exact check.
7211 if (cls.IsRegister()) {
7212 __ cmpl(temp, cls.AsRegister<Register>());
7213 } else {
7214 DCHECK(cls.IsStackSlot()) << cls;
7215 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7216 }
7217 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007218
7219 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007220 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007221 GenerateReferenceLoadOneRegister(instruction,
7222 temp_loc,
7223 component_offset,
7224 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007225 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007226
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007227 // If the component type is null (i.e. the object not an array), jump to the slow path to
7228 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007230 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007233 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007234 break;
7235 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007236
Calin Juravle98893e12015-10-02 21:05:03 +01007237 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007238 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007239 // We cannot directly call the CheckCast runtime entry point
7240 // without resorting to a type checking slow path here (i.e. by
7241 // calling InvokeRuntime directly), as it would require to
7242 // assign fixed registers for the inputs of this HInstanceOf
7243 // instruction (following the runtime calling convention), which
7244 // might be cluttered by the potential first read barrier
7245 // emission at the beginning of this method.
7246 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007247 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007248
7249 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007250 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7251 // We can not get false positives by doing this.
7252 // /* HeapReference<Class> */ temp = obj->klass_
7253 GenerateReferenceLoadTwoRegisters(instruction,
7254 temp_loc,
7255 obj_loc,
7256 class_offset,
7257 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007258
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007259 // /* HeapReference<Class> */ temp = temp->iftable_
7260 GenerateReferenceLoadTwoRegisters(instruction,
7261 temp_loc,
7262 temp_loc,
7263 iftable_offset,
7264 kWithoutReadBarrier);
7265 // Iftable is never null.
7266 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7267 // Maybe poison the `cls` for direct comparison with memory.
7268 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7269 // Loop through the iftable and check if any class matches.
7270 NearLabel start_loop;
7271 __ Bind(&start_loop);
7272 // Need to subtract first to handle the empty array case.
7273 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7274 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7275 // Go to next interface if the classes do not match.
7276 __ cmpl(cls.AsRegister<Register>(),
7277 CodeGeneratorX86::ArrayAddress(temp,
7278 maybe_temp2_loc,
7279 TIMES_4,
7280 object_array_data_offset));
7281 __ j(kNotEqual, &start_loop);
7282 // If `cls` was poisoned above, unpoison it.
7283 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007284 break;
7285 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007286
7287 case TypeCheckKind::kBitstringCheck: {
7288 // /* HeapReference<Class> */ temp = obj->klass_
7289 GenerateReferenceLoadTwoRegisters(instruction,
7290 temp_loc,
7291 obj_loc,
7292 class_offset,
7293 kWithoutReadBarrier);
7294
7295 GenerateBitstringTypeCheckCompare(instruction, temp);
7296 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7297 break;
7298 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007299 }
7300 __ Bind(&done);
7301
Roland Levillain0d5a2812015-11-13 10:07:31 +00007302 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007303}
7304
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007305void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007306 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7307 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007308 InvokeRuntimeCallingConvention calling_convention;
7309 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7310}
7311
7312void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007313 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7314 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007315 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007316 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007317 if (instruction->IsEnter()) {
7318 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7319 } else {
7320 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7321 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007322}
7323
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007324void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7325void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7326void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7327
7328void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7329 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007330 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007331 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7332 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007333 locations->SetInAt(0, Location::RequiresRegister());
7334 locations->SetInAt(1, Location::Any());
7335 locations->SetOut(Location::SameAsFirstInput());
7336}
7337
7338void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7339 HandleBitwiseOperation(instruction);
7340}
7341
7342void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7343 HandleBitwiseOperation(instruction);
7344}
7345
7346void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7347 HandleBitwiseOperation(instruction);
7348}
7349
7350void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7351 LocationSummary* locations = instruction->GetLocations();
7352 Location first = locations->InAt(0);
7353 Location second = locations->InAt(1);
7354 DCHECK(first.Equals(locations->Out()));
7355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007356 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007357 if (second.IsRegister()) {
7358 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007359 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007360 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007361 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007362 } else {
7363 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007364 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007365 }
7366 } else if (second.IsConstant()) {
7367 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007368 __ andl(first.AsRegister<Register>(),
7369 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007370 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007371 __ orl(first.AsRegister<Register>(),
7372 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007373 } else {
7374 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007375 __ xorl(first.AsRegister<Register>(),
7376 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007377 }
7378 } else {
7379 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007380 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007381 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007382 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007383 } else {
7384 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007385 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007386 }
7387 }
7388 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007389 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007390 if (second.IsRegisterPair()) {
7391 if (instruction->IsAnd()) {
7392 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7393 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7394 } else if (instruction->IsOr()) {
7395 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7396 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7397 } else {
7398 DCHECK(instruction->IsXor());
7399 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7400 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7401 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007402 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007403 if (instruction->IsAnd()) {
7404 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7405 __ andl(first.AsRegisterPairHigh<Register>(),
7406 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7407 } else if (instruction->IsOr()) {
7408 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7409 __ orl(first.AsRegisterPairHigh<Register>(),
7410 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7411 } else {
7412 DCHECK(instruction->IsXor());
7413 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7414 __ xorl(first.AsRegisterPairHigh<Register>(),
7415 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7416 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007417 } else {
7418 DCHECK(second.IsConstant()) << second;
7419 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007420 int32_t low_value = Low32Bits(value);
7421 int32_t high_value = High32Bits(value);
7422 Immediate low(low_value);
7423 Immediate high(high_value);
7424 Register first_low = first.AsRegisterPairLow<Register>();
7425 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007426 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007427 if (low_value == 0) {
7428 __ xorl(first_low, first_low);
7429 } else if (low_value != -1) {
7430 __ andl(first_low, low);
7431 }
7432 if (high_value == 0) {
7433 __ xorl(first_high, first_high);
7434 } else if (high_value != -1) {
7435 __ andl(first_high, high);
7436 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007437 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007438 if (low_value != 0) {
7439 __ orl(first_low, low);
7440 }
7441 if (high_value != 0) {
7442 __ orl(first_high, high);
7443 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007444 } else {
7445 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007446 if (low_value != 0) {
7447 __ xorl(first_low, low);
7448 }
7449 if (high_value != 0) {
7450 __ xorl(first_high, high);
7451 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007452 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007453 }
7454 }
7455}
7456
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007457void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7458 HInstruction* instruction,
7459 Location out,
7460 uint32_t offset,
7461 Location maybe_temp,
7462 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007463 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007464 if (read_barrier_option == kWithReadBarrier) {
7465 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007466 if (kUseBakerReadBarrier) {
7467 // Load with fast path based Baker's read barrier.
7468 // /* HeapReference<Object> */ out = *(out + offset)
7469 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007470 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007471 } else {
7472 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007473 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007474 // in the following move operation, as we will need it for the
7475 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007476 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007477 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007478 // /* HeapReference<Object> */ out = *(out + offset)
7479 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007480 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007481 }
7482 } else {
7483 // Plain load with no read barrier.
7484 // /* HeapReference<Object> */ out = *(out + offset)
7485 __ movl(out_reg, Address(out_reg, offset));
7486 __ MaybeUnpoisonHeapReference(out_reg);
7487 }
7488}
7489
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007490void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7491 HInstruction* instruction,
7492 Location out,
7493 Location obj,
7494 uint32_t offset,
7495 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007496 Register out_reg = out.AsRegister<Register>();
7497 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007498 if (read_barrier_option == kWithReadBarrier) {
7499 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007500 if (kUseBakerReadBarrier) {
7501 // Load with fast path based Baker's read barrier.
7502 // /* HeapReference<Object> */ out = *(obj + offset)
7503 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007504 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007505 } else {
7506 // Load with slow path based read barrier.
7507 // /* HeapReference<Object> */ out = *(obj + offset)
7508 __ movl(out_reg, Address(obj_reg, offset));
7509 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7510 }
7511 } else {
7512 // Plain load with no read barrier.
7513 // /* HeapReference<Object> */ out = *(obj + offset)
7514 __ movl(out_reg, Address(obj_reg, offset));
7515 __ MaybeUnpoisonHeapReference(out_reg);
7516 }
7517}
7518
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007519void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7520 HInstruction* instruction,
7521 Location root,
7522 const Address& address,
7523 Label* fixup_label,
7524 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007525 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007526 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007527 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007528 if (kUseBakerReadBarrier) {
7529 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7530 // Baker's read barrier are used:
7531 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007532 // root = obj.field;
7533 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7534 // if (temp != null) {
7535 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007536 // }
7537
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007538 // /* GcRoot<mirror::Object> */ root = *address
7539 __ movl(root_reg, address);
7540 if (fixup_label != nullptr) {
7541 __ Bind(fixup_label);
7542 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007543 static_assert(
7544 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7545 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7546 "have different sizes.");
7547 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7548 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7549 "have different sizes.");
7550
Vladimir Marko953437b2016-08-24 08:30:46 +00007551 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007552 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007553 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007554 codegen_->AddSlowPath(slow_path);
7555
Roland Levillaind966ce72017-02-09 16:20:14 +00007556 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7557 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007558 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007559 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7560 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007561 __ j(kNotEqual, slow_path->GetEntryLabel());
7562 __ Bind(slow_path->GetExitLabel());
7563 } else {
7564 // GC root loaded through a slow path for read barriers other
7565 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007566 // /* GcRoot<mirror::Object>* */ root = address
7567 __ leal(root_reg, address);
7568 if (fixup_label != nullptr) {
7569 __ Bind(fixup_label);
7570 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007571 // /* mirror::Object* */ root = root->Read()
7572 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7573 }
7574 } else {
7575 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007576 // /* GcRoot<mirror::Object> */ root = *address
7577 __ movl(root_reg, address);
7578 if (fixup_label != nullptr) {
7579 __ Bind(fixup_label);
7580 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007581 // Note that GC roots are not affected by heap poisoning, thus we
7582 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007583 }
7584}
7585
7586void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7587 Location ref,
7588 Register obj,
7589 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007590 bool needs_null_check) {
7591 DCHECK(kEmitCompilerReadBarrier);
7592 DCHECK(kUseBakerReadBarrier);
7593
7594 // /* HeapReference<Object> */ ref = *(obj + offset)
7595 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007596 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007597}
7598
7599void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7600 Location ref,
7601 Register obj,
7602 uint32_t data_offset,
7603 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007604 bool needs_null_check) {
7605 DCHECK(kEmitCompilerReadBarrier);
7606 DCHECK(kUseBakerReadBarrier);
7607
Roland Levillain3d312422016-06-23 13:53:42 +01007608 static_assert(
7609 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7610 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007611 // /* HeapReference<Object> */ ref =
7612 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007613 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007614 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007615}
7616
7617void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7618 Location ref,
7619 Register obj,
7620 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007621 bool needs_null_check,
7622 bool always_update_field,
7623 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007624 DCHECK(kEmitCompilerReadBarrier);
7625 DCHECK(kUseBakerReadBarrier);
7626
7627 // In slow path based read barriers, the read barrier call is
7628 // inserted after the original load. However, in fast path based
7629 // Baker's read barriers, we need to perform the load of
7630 // mirror::Object::monitor_ *before* the original reference load.
7631 // This load-load ordering is required by the read barrier.
7632 // The fast path/slow path (for Baker's algorithm) should look like:
7633 //
7634 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7635 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7636 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007637 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007638 // if (is_gray) {
7639 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7640 // }
7641 //
7642 // Note: the original implementation in ReadBarrier::Barrier is
7643 // slightly more complex as:
7644 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007645 // the high-bits of rb_state, which are expected to be all zeroes
7646 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7647 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007648 // - it performs additional checks that we do not do here for
7649 // performance reasons.
7650
7651 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007652 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7653
Vladimir Marko953437b2016-08-24 08:30:46 +00007654 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007655 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7656 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007657 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7658 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7659 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7660
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007661 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007662 // ref = ReadBarrier::Mark(ref);
7663 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7664 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007665 if (needs_null_check) {
7666 MaybeRecordImplicitNullCheck(instruction);
7667 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007668
7669 // Load fence to prevent load-load reordering.
7670 // Note that this is a no-op, thanks to the x86 memory model.
7671 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7672
7673 // The actual reference load.
7674 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007675 __ movl(ref_reg, src); // Flags are unaffected.
7676
7677 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7678 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007679 SlowPathCode* slow_path;
7680 if (always_update_field) {
7681 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007682 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007683 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7684 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007685 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007686 instruction, ref, /* unpoison_ref_before_marking */ true);
7687 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007688 AddSlowPath(slow_path);
7689
7690 // We have done the "if" of the gray bit check above, now branch based on the flags.
7691 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007692
7693 // Object* ref = ref_addr->AsMirrorPtr()
7694 __ MaybeUnpoisonHeapReference(ref_reg);
7695
Roland Levillain7c1559a2015-12-15 10:55:36 +00007696 __ Bind(slow_path->GetExitLabel());
7697}
7698
7699void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7700 Location out,
7701 Location ref,
7702 Location obj,
7703 uint32_t offset,
7704 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007705 DCHECK(kEmitCompilerReadBarrier);
7706
Roland Levillain7c1559a2015-12-15 10:55:36 +00007707 // Insert a slow path based read barrier *after* the reference load.
7708 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007709 // If heap poisoning is enabled, the unpoisoning of the loaded
7710 // reference will be carried out by the runtime within the slow
7711 // path.
7712 //
7713 // Note that `ref` currently does not get unpoisoned (when heap
7714 // poisoning is enabled), which is alright as the `ref` argument is
7715 // not used by the artReadBarrierSlow entry point.
7716 //
7717 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007718 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007719 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7720 AddSlowPath(slow_path);
7721
Roland Levillain0d5a2812015-11-13 10:07:31 +00007722 __ jmp(slow_path->GetEntryLabel());
7723 __ Bind(slow_path->GetExitLabel());
7724}
7725
Roland Levillain7c1559a2015-12-15 10:55:36 +00007726void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7727 Location out,
7728 Location ref,
7729 Location obj,
7730 uint32_t offset,
7731 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007732 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007733 // Baker's read barriers shall be handled by the fast path
7734 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7735 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007736 // If heap poisoning is enabled, unpoisoning will be taken care of
7737 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007738 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007739 } else if (kPoisonHeapReferences) {
7740 __ UnpoisonHeapReference(out.AsRegister<Register>());
7741 }
7742}
7743
Roland Levillain7c1559a2015-12-15 10:55:36 +00007744void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7745 Location out,
7746 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007747 DCHECK(kEmitCompilerReadBarrier);
7748
Roland Levillain7c1559a2015-12-15 10:55:36 +00007749 // Insert a slow path based read barrier *after* the GC root load.
7750 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007751 // Note that GC roots are not affected by heap poisoning, so we do
7752 // not need to do anything special for this here.
7753 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007754 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007755 AddSlowPath(slow_path);
7756
Roland Levillain0d5a2812015-11-13 10:07:31 +00007757 __ jmp(slow_path->GetEntryLabel());
7758 __ Bind(slow_path->GetExitLabel());
7759}
7760
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007761void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007762 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007763 LOG(FATAL) << "Unreachable";
7764}
7765
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007766void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007767 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007768 LOG(FATAL) << "Unreachable";
7769}
7770
Mark Mendellfe57faa2015-09-18 09:26:15 -04007771// Simple implementation of packed switch - generate cascaded compare/jumps.
7772void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7773 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007774 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007775 locations->SetInAt(0, Location::RequiresRegister());
7776}
7777
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007778void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7779 int32_t lower_bound,
7780 uint32_t num_entries,
7781 HBasicBlock* switch_block,
7782 HBasicBlock* default_block) {
7783 // Figure out the correct compare values and jump conditions.
7784 // Handle the first compare/branch as a special case because it might
7785 // jump to the default case.
7786 DCHECK_GT(num_entries, 2u);
7787 Condition first_condition;
7788 uint32_t index;
7789 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7790 if (lower_bound != 0) {
7791 first_condition = kLess;
7792 __ cmpl(value_reg, Immediate(lower_bound));
7793 __ j(first_condition, codegen_->GetLabelOf(default_block));
7794 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007795
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007796 index = 1;
7797 } else {
7798 // Handle all the compare/jumps below.
7799 first_condition = kBelow;
7800 index = 0;
7801 }
7802
7803 // Handle the rest of the compare/jumps.
7804 for (; index + 1 < num_entries; index += 2) {
7805 int32_t compare_to_value = lower_bound + index + 1;
7806 __ cmpl(value_reg, Immediate(compare_to_value));
7807 // Jump to successors[index] if value < case_value[index].
7808 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7809 // Jump to successors[index + 1] if value == case_value[index + 1].
7810 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7811 }
7812
7813 if (index != num_entries) {
7814 // There are an odd number of entries. Handle the last one.
7815 DCHECK_EQ(index + 1, num_entries);
7816 __ cmpl(value_reg, Immediate(lower_bound + index));
7817 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007818 }
7819
7820 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007821 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7822 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007823 }
7824}
7825
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007826void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7827 int32_t lower_bound = switch_instr->GetStartValue();
7828 uint32_t num_entries = switch_instr->GetNumEntries();
7829 LocationSummary* locations = switch_instr->GetLocations();
7830 Register value_reg = locations->InAt(0).AsRegister<Register>();
7831
7832 GenPackedSwitchWithCompares(value_reg,
7833 lower_bound,
7834 num_entries,
7835 switch_instr->GetBlock(),
7836 switch_instr->GetDefaultBlock());
7837}
7838
Mark Mendell805b3b52015-09-18 14:10:29 -04007839void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7840 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007841 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007842 locations->SetInAt(0, Location::RequiresRegister());
7843
7844 // Constant area pointer.
7845 locations->SetInAt(1, Location::RequiresRegister());
7846
7847 // And the temporary we need.
7848 locations->AddTemp(Location::RequiresRegister());
7849}
7850
7851void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7852 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007853 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007854 LocationSummary* locations = switch_instr->GetLocations();
7855 Register value_reg = locations->InAt(0).AsRegister<Register>();
7856 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7857
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007858 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7859 GenPackedSwitchWithCompares(value_reg,
7860 lower_bound,
7861 num_entries,
7862 switch_instr->GetBlock(),
7863 default_block);
7864 return;
7865 }
7866
Mark Mendell805b3b52015-09-18 14:10:29 -04007867 // Optimizing has a jump area.
7868 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7869 Register constant_area = locations->InAt(1).AsRegister<Register>();
7870
7871 // Remove the bias, if needed.
7872 if (lower_bound != 0) {
7873 __ leal(temp_reg, Address(value_reg, -lower_bound));
7874 value_reg = temp_reg;
7875 }
7876
7877 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007878 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007879 __ cmpl(value_reg, Immediate(num_entries - 1));
7880 __ j(kAbove, codegen_->GetLabelOf(default_block));
7881
7882 // We are in the range of the table.
7883 // Load (target-constant_area) from the jump table, indexing by the value.
7884 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7885
7886 // Compute the actual target address by adding in constant_area.
7887 __ addl(temp_reg, constant_area);
7888
7889 // And jump.
7890 __ jmp(temp_reg);
7891}
7892
Mark Mendell0616ae02015-04-17 12:49:27 -04007893void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7894 HX86ComputeBaseMethodAddress* insn) {
7895 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007896 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007897 locations->SetOut(Location::RequiresRegister());
7898}
7899
7900void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7901 HX86ComputeBaseMethodAddress* insn) {
7902 LocationSummary* locations = insn->GetLocations();
7903 Register reg = locations->Out().AsRegister<Register>();
7904
7905 // Generate call to next instruction.
7906 Label next_instruction;
7907 __ call(&next_instruction);
7908 __ Bind(&next_instruction);
7909
7910 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007911 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007912
7913 // Grab the return address off the stack.
7914 __ popl(reg);
7915}
7916
7917void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7918 HX86LoadFromConstantTable* insn) {
7919 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007920 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007921
7922 locations->SetInAt(0, Location::RequiresRegister());
7923 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7924
7925 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007926 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007927 return;
7928 }
7929
7930 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007931 case DataType::Type::kFloat32:
7932 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007933 locations->SetOut(Location::RequiresFpuRegister());
7934 break;
7935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007936 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007937 locations->SetOut(Location::RequiresRegister());
7938 break;
7939
7940 default:
7941 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7942 }
7943}
7944
7945void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007946 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007947 return;
7948 }
7949
7950 LocationSummary* locations = insn->GetLocations();
7951 Location out = locations->Out();
7952 Register const_area = locations->InAt(0).AsRegister<Register>();
7953 HConstant *value = insn->GetConstant();
7954
7955 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007956 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007957 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007958 codegen_->LiteralFloatAddress(
7959 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007960 break;
7961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007962 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007963 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007964 codegen_->LiteralDoubleAddress(
7965 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007966 break;
7967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007968 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007969 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007970 codegen_->LiteralInt32Address(
7971 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007972 break;
7973
7974 default:
7975 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7976 }
7977}
7978
Mark Mendell0616ae02015-04-17 12:49:27 -04007979/**
7980 * Class to handle late fixup of offsets into constant area.
7981 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007982class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007983 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007984 RIPFixup(CodeGeneratorX86& codegen,
7985 HX86ComputeBaseMethodAddress* base_method_address,
7986 size_t offset)
7987 : codegen_(&codegen),
7988 base_method_address_(base_method_address),
7989 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007990
7991 protected:
7992 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7993
7994 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007995 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007996
7997 private:
7998 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7999 // Patch the correct offset for the instruction. The place to patch is the
8000 // last 4 bytes of the instruction.
8001 // The value to patch is the distance from the offset in the constant area
8002 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008003 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008004 int32_t relative_position =
8005 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008006
8007 // Patch in the right value.
8008 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8009 }
8010
Mark Mendell0616ae02015-04-17 12:49:27 -04008011 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008012 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008013};
8014
Mark Mendell805b3b52015-09-18 14:10:29 -04008015/**
8016 * Class to handle late fixup of offsets to a jump table that will be created in the
8017 * constant area.
8018 */
8019class JumpTableRIPFixup : public RIPFixup {
8020 public:
8021 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008022 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8023 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008024
8025 void CreateJumpTable() {
8026 X86Assembler* assembler = codegen_->GetAssembler();
8027
8028 // Ensure that the reference to the jump table has the correct offset.
8029 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8030 SetOffset(offset_in_constant_table);
8031
8032 // The label values in the jump table are computed relative to the
8033 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008034 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008035
8036 // Populate the jump table with the correct values for the jump table.
8037 int32_t num_entries = switch_instr_->GetNumEntries();
8038 HBasicBlock* block = switch_instr_->GetBlock();
8039 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8040 // The value that we want is the target offset - the position of the table.
8041 for (int32_t i = 0; i < num_entries; i++) {
8042 HBasicBlock* b = successors[i];
8043 Label* l = codegen_->GetLabelOf(b);
8044 DCHECK(l->IsBound());
8045 int32_t offset_to_block = l->Position() - relative_offset;
8046 assembler->AppendInt32(offset_to_block);
8047 }
8048 }
8049
8050 private:
8051 const HX86PackedSwitch* switch_instr_;
8052};
8053
8054void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8055 // Generate the constant area if needed.
8056 X86Assembler* assembler = GetAssembler();
8057 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8058 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8059 // byte values.
8060 assembler->Align(4, 0);
8061 constant_area_start_ = assembler->CodeSize();
8062
8063 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008064 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008065 jump_table->CreateJumpTable();
8066 }
8067
8068 // And now add the constant area to the generated code.
8069 assembler->AddConstantArea();
8070 }
8071
8072 // And finish up.
8073 CodeGenerator::Finalize(allocator);
8074}
8075
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008076Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8077 HX86ComputeBaseMethodAddress* method_base,
8078 Register reg) {
8079 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008080 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008081 return Address(reg, kDummy32BitOffset, fixup);
8082}
8083
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008084Address CodeGeneratorX86::LiteralFloatAddress(float v,
8085 HX86ComputeBaseMethodAddress* method_base,
8086 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008087 AssemblerFixup* fixup =
8088 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008089 return Address(reg, kDummy32BitOffset, fixup);
8090}
8091
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008092Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8093 HX86ComputeBaseMethodAddress* method_base,
8094 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008095 AssemblerFixup* fixup =
8096 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008097 return Address(reg, kDummy32BitOffset, fixup);
8098}
8099
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008100Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8101 HX86ComputeBaseMethodAddress* method_base,
8102 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008103 AssemblerFixup* fixup =
8104 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008105 return Address(reg, kDummy32BitOffset, fixup);
8106}
8107
Aart Bika19616e2016-02-01 18:57:58 -08008108void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8109 if (value == 0) {
8110 __ xorl(dest, dest);
8111 } else {
8112 __ movl(dest, Immediate(value));
8113 }
8114}
8115
8116void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8117 if (value == 0) {
8118 __ testl(dest, dest);
8119 } else {
8120 __ cmpl(dest, Immediate(value));
8121 }
8122}
8123
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008124void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8125 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008126 GenerateIntCompare(lhs_reg, rhs);
8127}
8128
8129void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008130 if (rhs.IsConstant()) {
8131 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008132 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008133 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008134 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008135 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008136 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008137 }
8138}
8139
8140Address CodeGeneratorX86::ArrayAddress(Register obj,
8141 Location index,
8142 ScaleFactor scale,
8143 uint32_t data_offset) {
8144 return index.IsConstant() ?
8145 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8146 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8147}
8148
Mark Mendell805b3b52015-09-18 14:10:29 -04008149Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8150 Register reg,
8151 Register value) {
8152 // Create a fixup to be used to create and address the jump table.
8153 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008154 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008155
8156 // We have to populate the jump tables.
8157 fixups_to_jump_tables_.push_back(table_fixup);
8158
8159 // We want a scaled address, as we are extracting the correct offset from the table.
8160 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8161}
8162
Andreas Gampe85b62f22015-09-09 13:15:38 -07008163// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008164void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008165 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008166 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008167 return;
8168 }
8169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008170 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008171
8172 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8173 if (target.Equals(return_loc)) {
8174 return;
8175 }
8176
8177 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8178 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008179 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008180 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008181 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8182 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008183 GetMoveResolver()->EmitNativeCode(&parallel_move);
8184 } else {
8185 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008186 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008187 parallel_move.AddMove(return_loc, target, type, nullptr);
8188 GetMoveResolver()->EmitNativeCode(&parallel_move);
8189 }
8190}
8191
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008192void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8193 const uint8_t* roots_data,
8194 const PatchInfo<Label>& info,
8195 uint64_t index_in_table) const {
8196 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8197 uintptr_t address =
8198 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8199 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8200 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8201 dchecked_integral_cast<uint32_t>(address);
8202}
8203
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008204void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8205 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008206 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008207 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008208 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008209 }
8210
8211 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008212 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008213 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008214 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008215 }
8216}
8217
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008218void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8219 ATTRIBUTE_UNUSED) {
8220 LOG(FATAL) << "Unreachable";
8221}
8222
8223void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8224 ATTRIBUTE_UNUSED) {
8225 LOG(FATAL) << "Unreachable";
8226}
8227
Roland Levillain4d027112015-07-01 15:41:14 +01008228#undef __
8229
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008230} // namespace x86
8231} // namespace art