blob: 9f42ac76f5c499b96423ed453ee7a0305d02a76c [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
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002314void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2315 HandleInvoke(invoke);
2316}
2317
2318void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2319 codegen_->GenerateInvokeCustomCall(invoke);
2320}
2321
Roland Levillain88cb1752014-10-20 16:36:47 +01002322void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002324 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002325 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 case DataType::Type::kInt32:
2327 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002328 locations->SetInAt(0, Location::RequiresRegister());
2329 locations->SetOut(Location::SameAsFirstInput());
2330 break;
2331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002333 locations->SetInAt(0, Location::RequiresFpuRegister());
2334 locations->SetOut(Location::SameAsFirstInput());
2335 locations->AddTemp(Location::RequiresRegister());
2336 locations->AddTemp(Location::RequiresFpuRegister());
2337 break;
2338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002339 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002340 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002341 locations->SetOut(Location::SameAsFirstInput());
2342 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002343 break;
2344
2345 default:
2346 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2347 }
2348}
2349
2350void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2351 LocationSummary* locations = neg->GetLocations();
2352 Location out = locations->Out();
2353 Location in = locations->InAt(0);
2354 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002356 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002357 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002359 break;
2360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002362 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002363 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002364 __ negl(out.AsRegisterPairLow<Register>());
2365 // Negation is similar to subtraction from zero. The least
2366 // significant byte triggers a borrow when it is different from
2367 // zero; to take it into account, add 1 to the most significant
2368 // byte if the carry flag (CF) is set to 1 after the first NEGL
2369 // operation.
2370 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2371 __ negl(out.AsRegisterPairHigh<Register>());
2372 break;
2373
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002374 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002375 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002376 Register constant = locations->GetTemp(0).AsRegister<Register>();
2377 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002378 // Implement float negation with an exclusive or with value
2379 // 0x80000000 (mask for bit 31, representing the sign of a
2380 // single-precision floating-point number).
2381 __ movl(constant, Immediate(INT32_C(0x80000000)));
2382 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002384 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002385 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002386
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002388 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002389 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002390 // Implement double negation with an exclusive or with value
2391 // 0x8000000000000000 (mask for bit 63, representing the sign of
2392 // a double-precision floating-point number).
2393 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002394 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002395 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002396 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002397
2398 default:
2399 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2400 }
2401}
2402
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002403void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2404 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002405 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002407 locations->SetInAt(0, Location::RequiresFpuRegister());
2408 locations->SetInAt(1, Location::RequiresRegister());
2409 locations->SetOut(Location::SameAsFirstInput());
2410 locations->AddTemp(Location::RequiresFpuRegister());
2411}
2412
2413void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2414 LocationSummary* locations = neg->GetLocations();
2415 Location out = locations->Out();
2416 DCHECK(locations->InAt(0).Equals(out));
2417
2418 Register constant_area = locations->InAt(1).AsRegister<Register>();
2419 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002421 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2422 neg->GetBaseMethodAddress(),
2423 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002424 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2425 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002426 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2427 neg->GetBaseMethodAddress(),
2428 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002429 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2430 }
2431}
2432
Roland Levillaindff1f282014-11-05 14:15:05 +00002433void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002434 DataType::Type result_type = conversion->GetResultType();
2435 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002436 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2437 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002438
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002439 // The float-to-long and double-to-long type conversions rely on a
2440 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002441 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2443 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002444 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002445 : LocationSummary::kNoCall;
2446 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002447 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002448
Roland Levillaindff1f282014-11-05 14:15:05 +00002449 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002450 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002452 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002453 case DataType::Type::kUint8:
2454 case DataType::Type::kInt8:
2455 case DataType::Type::kUint16:
2456 case DataType::Type::kInt16:
2457 case DataType::Type::kInt32:
2458 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2459 // Make the output overlap to please the register allocator. This greatly simplifies
2460 // the validation of the linear scan implementation
2461 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2462 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002463 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002464 HInstruction* input = conversion->InputAt(0);
2465 Location input_location = input->IsConstant()
2466 ? Location::ConstantLocation(input->AsConstant())
2467 : Location::RegisterPairLocation(EAX, EDX);
2468 locations->SetInAt(0, input_location);
2469 // Make the output overlap to please the register allocator. This greatly simplifies
2470 // the validation of the linear scan implementation
2471 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2472 break;
2473 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002474
2475 default:
2476 LOG(FATAL) << "Unexpected type conversion from " << input_type
2477 << " to " << result_type;
2478 }
2479 break;
2480
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002481 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002483 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2484 locations->SetInAt(0, Location::Any());
2485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002486 break;
2487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002489 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002491 locations->SetInAt(0, Location::Any());
2492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2493 break;
2494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetOut(Location::RequiresRegister());
2498 locations->AddTemp(Location::RequiresFpuRegister());
2499 break;
2500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002502 locations->SetInAt(0, Location::RequiresFpuRegister());
2503 locations->SetOut(Location::RequiresRegister());
2504 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 }
2511 break;
2512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002516 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002517 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002518 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 case DataType::Type::kInt16:
2520 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002521 locations->SetInAt(0, Location::RegisterLocation(EAX));
2522 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2523 break;
2524
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kFloat32:
2526 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002527 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002528 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2529 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2530
Vladimir Marko949c91f2015-01-27 10:48:44 +00002531 // The runtime helper puts the result in EAX, EDX.
2532 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002533 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002534 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002535
2536 default:
2537 LOG(FATAL) << "Unexpected type conversion from " << input_type
2538 << " to " << result_type;
2539 }
2540 break;
2541
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002543 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002545 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002547 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 case DataType::Type::kInt16:
2549 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002550 locations->SetInAt(0, Location::RequiresRegister());
2551 locations->SetOut(Location::RequiresFpuRegister());
2552 break;
2553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002555 locations->SetInAt(0, Location::Any());
2556 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002557 break;
2558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002560 locations->SetInAt(0, Location::RequiresFpuRegister());
2561 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002562 break;
2563
2564 default:
2565 LOG(FATAL) << "Unexpected type conversion from " << input_type
2566 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002567 }
Roland Levillaincff13742014-11-17 14:32:17 +00002568 break;
2569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002571 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002573 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002574 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002575 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kInt16:
2577 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002578 locations->SetInAt(0, Location::RequiresRegister());
2579 locations->SetOut(Location::RequiresFpuRegister());
2580 break;
2581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002583 locations->SetInAt(0, Location::Any());
2584 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002585 break;
2586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002588 locations->SetInAt(0, Location::RequiresFpuRegister());
2589 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002590 break;
2591
2592 default:
2593 LOG(FATAL) << "Unexpected type conversion from " << input_type
2594 << " to " << result_type;
2595 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002596 break;
2597
2598 default:
2599 LOG(FATAL) << "Unexpected type conversion from " << input_type
2600 << " to " << result_type;
2601 }
2602}
2603
2604void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2605 LocationSummary* locations = conversion->GetLocations();
2606 Location out = locations->Out();
2607 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002608 DataType::Type result_type = conversion->GetResultType();
2609 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002610 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2611 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002612 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002613 case DataType::Type::kUint8:
2614 switch (input_type) {
2615 case DataType::Type::kInt8:
2616 case DataType::Type::kUint16:
2617 case DataType::Type::kInt16:
2618 case DataType::Type::kInt32:
2619 if (in.IsRegister()) {
2620 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2621 } else {
2622 DCHECK(in.GetConstant()->IsIntConstant());
2623 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2624 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2625 }
2626 break;
2627 case DataType::Type::kInt64:
2628 if (in.IsRegisterPair()) {
2629 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2630 } else {
2631 DCHECK(in.GetConstant()->IsLongConstant());
2632 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2633 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2634 }
2635 break;
2636
2637 default:
2638 LOG(FATAL) << "Unexpected type conversion from " << input_type
2639 << " to " << result_type;
2640 }
2641 break;
2642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002643 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002644 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002645 case DataType::Type::kUint8:
2646 case DataType::Type::kUint16:
2647 case DataType::Type::kInt16:
2648 case DataType::Type::kInt32:
2649 if (in.IsRegister()) {
2650 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2651 } else {
2652 DCHECK(in.GetConstant()->IsIntConstant());
2653 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2654 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2655 }
2656 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002658 if (in.IsRegisterPair()) {
2659 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2660 } else {
2661 DCHECK(in.GetConstant()->IsLongConstant());
2662 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2663 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2664 }
2665 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002666
2667 default:
2668 LOG(FATAL) << "Unexpected type conversion from " << input_type
2669 << " to " << result_type;
2670 }
2671 break;
2672
2673 case DataType::Type::kUint16:
2674 switch (input_type) {
2675 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kInt16:
2677 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002678 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002679 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2680 } else if (in.IsStackSlot()) {
2681 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002682 } else {
2683 DCHECK(in.GetConstant()->IsIntConstant());
2684 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002685 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2686 }
2687 break;
2688 case DataType::Type::kInt64:
2689 if (in.IsRegisterPair()) {
2690 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2691 } else if (in.IsDoubleStackSlot()) {
2692 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2693 } else {
2694 DCHECK(in.GetConstant()->IsLongConstant());
2695 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2696 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002697 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002698 break;
2699
2700 default:
2701 LOG(FATAL) << "Unexpected type conversion from " << input_type
2702 << " to " << result_type;
2703 }
2704 break;
2705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002707 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002709 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002710 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002712 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002714 } else {
2715 DCHECK(in.GetConstant()->IsIntConstant());
2716 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002718 }
2719 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002720 case DataType::Type::kInt64:
2721 if (in.IsRegisterPair()) {
2722 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2723 } else if (in.IsDoubleStackSlot()) {
2724 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2725 } else {
2726 DCHECK(in.GetConstant()->IsLongConstant());
2727 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2728 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2729 }
2730 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002731
2732 default:
2733 LOG(FATAL) << "Unexpected type conversion from " << input_type
2734 << " to " << result_type;
2735 }
2736 break;
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002739 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002741 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002743 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002744 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002745 } else {
2746 DCHECK(in.IsConstant());
2747 DCHECK(in.GetConstant()->IsLongConstant());
2748 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002750 }
2751 break;
2752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002754 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2755 Register output = out.AsRegister<Register>();
2756 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002757 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002758
2759 __ movl(output, Immediate(kPrimIntMax));
2760 // temp = int-to-float(output)
2761 __ cvtsi2ss(temp, output);
2762 // if input >= temp goto done
2763 __ comiss(input, temp);
2764 __ j(kAboveEqual, &done);
2765 // if input == NaN goto nan
2766 __ j(kUnordered, &nan);
2767 // output = float-to-int-truncate(input)
2768 __ cvttss2si(output, input);
2769 __ jmp(&done);
2770 __ Bind(&nan);
2771 // output = 0
2772 __ xorl(output, output);
2773 __ Bind(&done);
2774 break;
2775 }
2776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002778 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2779 Register output = out.AsRegister<Register>();
2780 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002781 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002782
2783 __ movl(output, Immediate(kPrimIntMax));
2784 // temp = int-to-double(output)
2785 __ cvtsi2sd(temp, output);
2786 // if input >= temp goto done
2787 __ comisd(input, temp);
2788 __ j(kAboveEqual, &done);
2789 // if input == NaN goto nan
2790 __ j(kUnordered, &nan);
2791 // output = double-to-int-truncate(input)
2792 __ cvttsd2si(output, input);
2793 __ jmp(&done);
2794 __ Bind(&nan);
2795 // output = 0
2796 __ xorl(output, output);
2797 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002798 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002799 }
Roland Levillain946e1432014-11-11 17:35:19 +00002800
2801 default:
2802 LOG(FATAL) << "Unexpected type conversion from " << input_type
2803 << " to " << result_type;
2804 }
2805 break;
2806
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002808 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002810 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002812 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kInt16:
2814 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002815 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2816 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002817 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002818 __ cdq();
2819 break;
2820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002822 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002823 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002824 break;
2825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002827 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002828 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002829 break;
2830
2831 default:
2832 LOG(FATAL) << "Unexpected type conversion from " << input_type
2833 << " to " << result_type;
2834 }
2835 break;
2836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002838 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002840 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002842 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kInt16:
2844 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002845 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002846 break;
2847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002849 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002850
Roland Levillain232ade02015-04-20 15:14:36 +01002851 // Create stack space for the call to
2852 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2853 // TODO: enhance register allocator to ask for stack temporaries.
2854 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002856 __ subl(ESP, Immediate(adjustment));
2857 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002858
Roland Levillain232ade02015-04-20 15:14:36 +01002859 // Load the value to the FP stack, using temporaries if needed.
2860 PushOntoFPStack(in, 0, adjustment, false, true);
2861
2862 if (out.IsStackSlot()) {
2863 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2864 } else {
2865 __ fstps(Address(ESP, 0));
2866 Location stack_temp = Location::StackSlot(0);
2867 codegen_->Move32(out, stack_temp);
2868 }
2869
2870 // Remove the temporary stack space we allocated.
2871 if (adjustment != 0) {
2872 __ addl(ESP, Immediate(adjustment));
2873 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002874 break;
2875 }
2876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002878 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002879 break;
2880
2881 default:
2882 LOG(FATAL) << "Unexpected type conversion from " << input_type
2883 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002884 }
Roland Levillaincff13742014-11-17 14:32:17 +00002885 break;
2886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002888 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002889 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002890 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002892 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 case DataType::Type::kInt16:
2894 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002895 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002896 break;
2897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002899 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002900
Roland Levillain232ade02015-04-20 15:14:36 +01002901 // Create stack space for the call to
2902 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2903 // TODO: enhance register allocator to ask for stack temporaries.
2904 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002906 __ subl(ESP, Immediate(adjustment));
2907 }
2908
2909 // Load the value to the FP stack, using temporaries if needed.
2910 PushOntoFPStack(in, 0, adjustment, false, true);
2911
2912 if (out.IsDoubleStackSlot()) {
2913 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2914 } else {
2915 __ fstpl(Address(ESP, 0));
2916 Location stack_temp = Location::DoubleStackSlot(0);
2917 codegen_->Move64(out, stack_temp);
2918 }
2919
2920 // Remove the temporary stack space we allocated.
2921 if (adjustment != 0) {
2922 __ addl(ESP, Immediate(adjustment));
2923 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002924 break;
2925 }
2926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002928 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002929 break;
2930
2931 default:
2932 LOG(FATAL) << "Unexpected type conversion from " << input_type
2933 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002934 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002935 break;
2936
2937 default:
2938 LOG(FATAL) << "Unexpected type conversion from " << input_type
2939 << " to " << result_type;
2940 }
2941}
2942
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002943void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002944 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002945 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002946 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002948 locations->SetInAt(0, Location::RequiresRegister());
2949 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2951 break;
2952 }
2953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002955 locations->SetInAt(0, Location::RequiresRegister());
2956 locations->SetInAt(1, Location::Any());
2957 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002958 break;
2959 }
2960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kFloat32:
2962 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002964 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2965 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002966 } else if (add->InputAt(1)->IsConstant()) {
2967 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002968 } else {
2969 locations->SetInAt(1, Location::Any());
2970 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002971 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002973 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002974
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002976 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2977 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002979}
2980
2981void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2982 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 Location first = locations->InAt(0);
2984 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002985 Location out = locations->Out();
2986
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002987 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002990 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2991 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002992 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2993 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002994 } else {
2995 __ leal(out.AsRegister<Register>(), Address(
2996 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2997 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002999 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3000 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3001 __ addl(out.AsRegister<Register>(), Immediate(value));
3002 } else {
3003 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3004 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003005 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003006 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003007 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003008 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003009 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003010 }
3011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003013 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003014 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3015 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003016 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003017 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3018 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003019 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003020 } else {
3021 DCHECK(second.IsConstant()) << second;
3022 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3023 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3024 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003025 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003026 break;
3027 }
3028
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003029 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003030 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003032 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3033 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003034 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003035 __ addss(first.AsFpuRegister<XmmRegister>(),
3036 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003037 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3038 const_area->GetBaseMethodAddress(),
3039 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003040 } else {
3041 DCHECK(second.IsStackSlot());
3042 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003043 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003044 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 }
3046
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003047 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003048 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003049 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003050 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3051 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003052 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 __ addsd(first.AsFpuRegister<XmmRegister>(),
3054 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003055 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3056 const_area->GetBaseMethodAddress(),
3057 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003058 } else {
3059 DCHECK(second.IsDoubleStackSlot());
3060 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003061 }
3062 break;
3063 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003064
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003065 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003066 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003067 }
3068}
3069
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003070void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003071 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003072 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003073 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kInt32:
3075 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003076 locations->SetInAt(0, Location::RequiresRegister());
3077 locations->SetInAt(1, Location::Any());
3078 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079 break;
3080 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003081 case DataType::Type::kFloat32:
3082 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003083 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003084 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3085 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003086 } else if (sub->InputAt(1)->IsConstant()) {
3087 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003088 } else {
3089 locations->SetInAt(1, Location::Any());
3090 }
Calin Juravle11351682014-10-23 15:38:15 +01003091 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003092 break;
Calin Juravle11351682014-10-23 15:38:15 +01003093 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003094
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003095 default:
Calin Juravle11351682014-10-23 15:38:15 +01003096 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003097 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003098}
3099
3100void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3101 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003102 Location first = locations->InAt(0);
3103 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003104 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003105 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003107 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003109 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003110 __ subl(first.AsRegister<Register>(),
3111 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003112 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003114 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003115 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003116 }
3117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003118 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003119 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003120 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3121 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003122 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003123 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003124 __ sbbl(first.AsRegisterPairHigh<Register>(),
3125 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003126 } else {
3127 DCHECK(second.IsConstant()) << second;
3128 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3129 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3130 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003131 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003132 break;
3133 }
3134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003136 if (second.IsFpuRegister()) {
3137 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3138 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3139 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003140 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003141 __ subss(first.AsFpuRegister<XmmRegister>(),
3142 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003143 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3144 const_area->GetBaseMethodAddress(),
3145 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 } else {
3147 DCHECK(second.IsStackSlot());
3148 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3149 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003150 break;
Calin Juravle11351682014-10-23 15:38:15 +01003151 }
3152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003154 if (second.IsFpuRegister()) {
3155 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3156 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3157 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003158 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003159 __ subsd(first.AsFpuRegister<XmmRegister>(),
3160 codegen_->LiteralDoubleAddress(
3161 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003162 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003163 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3164 } else {
3165 DCHECK(second.IsDoubleStackSlot());
3166 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3167 }
Calin Juravle11351682014-10-23 15:38:15 +01003168 break;
3169 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003170
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003171 default:
Calin Juravle11351682014-10-23 15:38:15 +01003172 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003173 }
3174}
3175
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176void LocationsBuilderX86::VisitMul(HMul* mul) {
3177 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003178 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 locations->SetInAt(0, Location::RequiresRegister());
3182 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003183 if (mul->InputAt(1)->IsIntConstant()) {
3184 // Can use 3 operand multiply.
3185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3186 } else {
3187 locations->SetOut(Location::SameAsFirstInput());
3188 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192 locations->SetInAt(1, Location::Any());
3193 locations->SetOut(Location::SameAsFirstInput());
3194 // Needed for imul on 32bits with 64bits output.
3195 locations->AddTemp(Location::RegisterLocation(EAX));
3196 locations->AddTemp(Location::RegisterLocation(EDX));
3197 break;
3198 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kFloat32:
3200 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003202 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3203 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003204 } else if (mul->InputAt(1)->IsConstant()) {
3205 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003206 } else {
3207 locations->SetInAt(1, Location::Any());
3208 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003209 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003210 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003211 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212
3213 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003214 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 }
3216}
3217
3218void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3219 LocationSummary* locations = mul->GetLocations();
3220 Location first = locations->InAt(0);
3221 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003222 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223
3224 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003226 // The constant may have ended up in a register, so test explicitly to avoid
3227 // problems where the output may not be the same as the first operand.
3228 if (mul->InputAt(1)->IsIntConstant()) {
3229 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3230 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3231 } else if (second.IsRegister()) {
3232 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003233 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003234 } else {
3235 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003236 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238 }
3239 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003242 Register in1_hi = first.AsRegisterPairHigh<Register>();
3243 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 Register eax = locations->GetTemp(0).AsRegister<Register>();
3245 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246
3247 DCHECK_EQ(EAX, eax);
3248 DCHECK_EQ(EDX, edx);
3249
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003250 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251 // output: in1
3252 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3253 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3254 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003255 if (second.IsConstant()) {
3256 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003258 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3259 int32_t low_value = Low32Bits(value);
3260 int32_t high_value = High32Bits(value);
3261 Immediate low(low_value);
3262 Immediate high(high_value);
3263
3264 __ movl(eax, high);
3265 // eax <- in1.lo * in2.hi
3266 __ imull(eax, in1_lo);
3267 // in1.hi <- in1.hi * in2.lo
3268 __ imull(in1_hi, low);
3269 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3270 __ addl(in1_hi, eax);
3271 // move in2_lo to eax to prepare for double precision
3272 __ movl(eax, low);
3273 // edx:eax <- in1.lo * in2.lo
3274 __ mull(in1_lo);
3275 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3276 __ addl(in1_hi, edx);
3277 // in1.lo <- (in1.lo * in2.lo)[31:0];
3278 __ movl(in1_lo, eax);
3279 } else if (second.IsRegisterPair()) {
3280 Register in2_hi = second.AsRegisterPairHigh<Register>();
3281 Register in2_lo = second.AsRegisterPairLow<Register>();
3282
3283 __ movl(eax, in2_hi);
3284 // eax <- in1.lo * in2.hi
3285 __ imull(eax, in1_lo);
3286 // in1.hi <- in1.hi * in2.lo
3287 __ imull(in1_hi, in2_lo);
3288 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3289 __ addl(in1_hi, eax);
3290 // move in1_lo to eax to prepare for double precision
3291 __ movl(eax, in1_lo);
3292 // edx:eax <- in1.lo * in2.lo
3293 __ mull(in2_lo);
3294 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3295 __ addl(in1_hi, edx);
3296 // in1.lo <- (in1.lo * in2.lo)[31:0];
3297 __ movl(in1_lo, eax);
3298 } else {
3299 DCHECK(second.IsDoubleStackSlot()) << second;
3300 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3301 Address in2_lo(ESP, second.GetStackIndex());
3302
3303 __ movl(eax, in2_hi);
3304 // eax <- in1.lo * in2.hi
3305 __ imull(eax, in1_lo);
3306 // in1.hi <- in1.hi * in2.lo
3307 __ imull(in1_hi, in2_lo);
3308 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3309 __ addl(in1_hi, eax);
3310 // move in1_lo to eax to prepare for double precision
3311 __ movl(eax, in1_lo);
3312 // edx:eax <- in1.lo * in2.lo
3313 __ mull(in2_lo);
3314 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3315 __ addl(in1_hi, edx);
3316 // in1.lo <- (in1.lo * in2.lo)[31:0];
3317 __ movl(in1_lo, eax);
3318 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319
3320 break;
3321 }
3322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003323 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003324 DCHECK(first.Equals(locations->Out()));
3325 if (second.IsFpuRegister()) {
3326 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3327 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3328 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003329 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003330 __ mulss(first.AsFpuRegister<XmmRegister>(),
3331 codegen_->LiteralFloatAddress(
3332 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003333 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003334 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3335 } else {
3336 DCHECK(second.IsStackSlot());
3337 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3338 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003340 }
3341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003343 DCHECK(first.Equals(locations->Out()));
3344 if (second.IsFpuRegister()) {
3345 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3346 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3347 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003348 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3350 codegen_->LiteralDoubleAddress(
3351 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003352 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003353 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3354 } else {
3355 DCHECK(second.IsDoubleStackSlot());
3356 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3357 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003358 break;
3359 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003360
3361 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003362 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003363 }
3364}
3365
Roland Levillain232ade02015-04-20 15:14:36 +01003366void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3367 uint32_t temp_offset,
3368 uint32_t stack_adjustment,
3369 bool is_fp,
3370 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003371 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003372 DCHECK(!is_wide);
3373 if (is_fp) {
3374 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3375 } else {
3376 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3377 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003378 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003379 DCHECK(is_wide);
3380 if (is_fp) {
3381 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3382 } else {
3383 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3384 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003385 } else {
3386 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003387 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003388 Location stack_temp = Location::StackSlot(temp_offset);
3389 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003390 if (is_fp) {
3391 __ flds(Address(ESP, temp_offset));
3392 } else {
3393 __ filds(Address(ESP, temp_offset));
3394 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003395 } else {
3396 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3397 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003398 if (is_fp) {
3399 __ fldl(Address(ESP, temp_offset));
3400 } else {
3401 __ fildl(Address(ESP, temp_offset));
3402 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403 }
3404 }
3405}
3406
3407void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003408 DataType::Type type = rem->GetResultType();
3409 bool is_float = type == DataType::Type::kFloat32;
3410 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003411 LocationSummary* locations = rem->GetLocations();
3412 Location first = locations->InAt(0);
3413 Location second = locations->InAt(1);
3414 Location out = locations->Out();
3415
3416 // Create stack space for 2 elements.
3417 // TODO: enhance register allocator to ask for stack temporaries.
3418 __ subl(ESP, Immediate(2 * elem_size));
3419
3420 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003421 const bool is_wide = !is_float;
3422 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3423 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003424
3425 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003426 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003427 __ Bind(&retry);
3428 __ fprem();
3429
3430 // Move FP status to AX.
3431 __ fstsw();
3432
3433 // And see if the argument reduction is complete. This is signaled by the
3434 // C2 FPU flag bit set to 0.
3435 __ andl(EAX, Immediate(kC2ConditionMask));
3436 __ j(kNotEqual, &retry);
3437
3438 // We have settled on the final value. Retrieve it into an XMM register.
3439 // Store FP top of stack to real stack.
3440 if (is_float) {
3441 __ fsts(Address(ESP, 0));
3442 } else {
3443 __ fstl(Address(ESP, 0));
3444 }
3445
3446 // Pop the 2 items from the FP stack.
3447 __ fucompp();
3448
3449 // Load the value from the stack into an XMM register.
3450 DCHECK(out.IsFpuRegister()) << out;
3451 if (is_float) {
3452 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3453 } else {
3454 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3455 }
3456
3457 // And remove the temporary stack space we allocated.
3458 __ addl(ESP, Immediate(2 * elem_size));
3459}
3460
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003461
3462void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3463 DCHECK(instruction->IsDiv() || instruction->IsRem());
3464
3465 LocationSummary* locations = instruction->GetLocations();
3466 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003467 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003468
3469 Register out_register = locations->Out().AsRegister<Register>();
3470 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003471 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472
3473 DCHECK(imm == 1 || imm == -1);
3474
3475 if (instruction->IsRem()) {
3476 __ xorl(out_register, out_register);
3477 } else {
3478 __ movl(out_register, input_register);
3479 if (imm == -1) {
3480 __ negl(out_register);
3481 }
3482 }
3483}
3484
3485
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 LocationSummary* locations = instruction->GetLocations();
3488
3489 Register out_register = locations->Out().AsRegister<Register>();
3490 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003491 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003492 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3493 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003494
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495 Register num = locations->GetTemp(0).AsRegister<Register>();
3496
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003497 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 __ testl(input_register, input_register);
3499 __ cmovl(kGreaterEqual, num, input_register);
3500 int shift = CTZ(imm);
3501 __ sarl(num, Immediate(shift));
3502
3503 if (imm < 0) {
3504 __ negl(num);
3505 }
3506
3507 __ movl(out_register, num);
3508}
3509
3510void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3511 DCHECK(instruction->IsDiv() || instruction->IsRem());
3512
3513 LocationSummary* locations = instruction->GetLocations();
3514 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3515
3516 Register eax = locations->InAt(0).AsRegister<Register>();
3517 Register out = locations->Out().AsRegister<Register>();
3518 Register num;
3519 Register edx;
3520
3521 if (instruction->IsDiv()) {
3522 edx = locations->GetTemp(0).AsRegister<Register>();
3523 num = locations->GetTemp(1).AsRegister<Register>();
3524 } else {
3525 edx = locations->Out().AsRegister<Register>();
3526 num = locations->GetTemp(0).AsRegister<Register>();
3527 }
3528
3529 DCHECK_EQ(EAX, eax);
3530 DCHECK_EQ(EDX, edx);
3531 if (instruction->IsDiv()) {
3532 DCHECK_EQ(EAX, out);
3533 } else {
3534 DCHECK_EQ(EDX, out);
3535 }
3536
3537 int64_t magic;
3538 int shift;
3539 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3540
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 // Save the numerator.
3542 __ movl(num, eax);
3543
3544 // EAX = magic
3545 __ movl(eax, Immediate(magic));
3546
3547 // EDX:EAX = magic * numerator
3548 __ imull(num);
3549
3550 if (imm > 0 && magic < 0) {
3551 // EDX += num
3552 __ addl(edx, num);
3553 } else if (imm < 0 && magic > 0) {
3554 __ subl(edx, num);
3555 }
3556
3557 // Shift if needed.
3558 if (shift != 0) {
3559 __ sarl(edx, Immediate(shift));
3560 }
3561
3562 // EDX += 1 if EDX < 0
3563 __ movl(eax, edx);
3564 __ shrl(edx, Immediate(31));
3565 __ addl(edx, eax);
3566
3567 if (instruction->IsRem()) {
3568 __ movl(eax, num);
3569 __ imull(edx, Immediate(imm));
3570 __ subl(eax, edx);
3571 __ movl(edx, eax);
3572 } else {
3573 __ movl(eax, edx);
3574 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003575}
3576
Calin Juravlebacfec32014-11-14 15:54:36 +00003577void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3578 DCHECK(instruction->IsDiv() || instruction->IsRem());
3579
3580 LocationSummary* locations = instruction->GetLocations();
3581 Location out = locations->Out();
3582 Location first = locations->InAt(0);
3583 Location second = locations->InAt(1);
3584 bool is_div = instruction->IsDiv();
3585
3586 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003588 DCHECK_EQ(EAX, first.AsRegister<Register>());
3589 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003590
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003591 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003592 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003593
3594 if (imm == 0) {
3595 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3596 } else if (imm == 1 || imm == -1) {
3597 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003598 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003599 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003600 } else {
3601 DCHECK(imm <= -2 || imm >= 2);
3602 GenerateDivRemWithAnyConstant(instruction);
3603 }
3604 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003605 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003606 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003608
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 Register second_reg = second.AsRegister<Register>();
3610 // 0x80000000/-1 triggers an arithmetic exception!
3611 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3612 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003613
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 __ cmpl(second_reg, Immediate(-1));
3615 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003616
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003617 // edx:eax <- sign-extended of eax
3618 __ cdq();
3619 // eax = quotient, edx = remainder
3620 __ idivl(second_reg);
3621 __ Bind(slow_path->GetExitLabel());
3622 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003623 break;
3624 }
3625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003626 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003627 InvokeRuntimeCallingConvention calling_convention;
3628 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3629 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3630 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3631 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3632 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3633 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3634
3635 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003636 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003637 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003639 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003640 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003641 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003642 break;
3643 }
3644
3645 default:
3646 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3647 }
3648}
3649
Calin Juravle7c4954d2014-10-28 16:57:40 +00003650void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003652 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003653 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003654 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003655
Calin Juravle7c4954d2014-10-28 16:57:40 +00003656 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003657 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003658 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003659 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003660 locations->SetOut(Location::SameAsFirstInput());
3661 // Intel uses edx:eax as the dividend.
3662 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003663 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3664 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3665 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003666 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003667 locations->AddTemp(Location::RequiresRegister());
3668 }
Calin Juravled0d48522014-11-04 16:40:20 +00003669 break;
3670 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003672 InvokeRuntimeCallingConvention calling_convention;
3673 locations->SetInAt(0, Location::RegisterPairLocation(
3674 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3675 locations->SetInAt(1, Location::RegisterPairLocation(
3676 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3677 // Runtime helper puts the result in EAX, EDX.
3678 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003679 break;
3680 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003681 case DataType::Type::kFloat32:
3682 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003684 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3685 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003686 } else if (div->InputAt(1)->IsConstant()) {
3687 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003688 } else {
3689 locations->SetInAt(1, Location::Any());
3690 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003691 locations->SetOut(Location::SameAsFirstInput());
3692 break;
3693 }
3694
3695 default:
3696 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3697 }
3698}
3699
3700void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3701 LocationSummary* locations = div->GetLocations();
3702 Location first = locations->InAt(0);
3703 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003704
3705 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003706 case DataType::Type::kInt32:
3707 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003708 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003709 break;
3710 }
3711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003712 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003713 if (second.IsFpuRegister()) {
3714 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3715 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3716 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003717 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003718 __ divss(first.AsFpuRegister<XmmRegister>(),
3719 codegen_->LiteralFloatAddress(
3720 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003721 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003722 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3723 } else {
3724 DCHECK(second.IsStackSlot());
3725 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3726 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003727 break;
3728 }
3729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003731 if (second.IsFpuRegister()) {
3732 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3733 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3734 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003735 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003736 __ divsd(first.AsFpuRegister<XmmRegister>(),
3737 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003738 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3739 const_area->GetBaseMethodAddress(),
3740 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003741 } else {
3742 DCHECK(second.IsDoubleStackSlot());
3743 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3744 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003745 break;
3746 }
3747
3748 default:
3749 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3750 }
3751}
3752
Calin Juravlebacfec32014-11-14 15:54:36 +00003753void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003757 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003758 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003759 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003760
Calin Juravled2ec87d2014-12-08 14:24:46 +00003761 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003762 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003763 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003764 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003765 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003766 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3767 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3768 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003769 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770 locations->AddTemp(Location::RequiresRegister());
3771 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003772 break;
3773 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003774 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 InvokeRuntimeCallingConvention calling_convention;
3776 locations->SetInAt(0, Location::RegisterPairLocation(
3777 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3778 locations->SetInAt(1, Location::RegisterPairLocation(
3779 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3780 // Runtime helper puts the result in EAX, EDX.
3781 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3782 break;
3783 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003784 case DataType::Type::kFloat64:
3785 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003786 locations->SetInAt(0, Location::Any());
3787 locations->SetInAt(1, Location::Any());
3788 locations->SetOut(Location::RequiresFpuRegister());
3789 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003790 break;
3791 }
3792
3793 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003794 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003795 }
3796}
3797
3798void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003799 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003800 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 case DataType::Type::kInt32:
3802 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003803 GenerateDivRemIntegral(rem);
3804 break;
3805 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 case DataType::Type::kFloat32:
3807 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003808 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 break;
3810 }
3811 default:
3812 LOG(FATAL) << "Unexpected rem type " << type;
3813 }
3814}
3815
Aart Bik1f8d51b2018-02-15 10:42:37 -08003816static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3817 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3818 switch (minmax->GetResultType()) {
3819 case DataType::Type::kInt32:
3820 locations->SetInAt(0, Location::RequiresRegister());
3821 locations->SetInAt(1, Location::RequiresRegister());
3822 locations->SetOut(Location::SameAsFirstInput());
3823 break;
3824 case DataType::Type::kInt64:
3825 locations->SetInAt(0, Location::RequiresRegister());
3826 locations->SetInAt(1, Location::RequiresRegister());
3827 locations->SetOut(Location::SameAsFirstInput());
3828 // Register to use to perform a long subtract to set cc.
3829 locations->AddTemp(Location::RequiresRegister());
3830 break;
3831 case DataType::Type::kFloat32:
3832 locations->SetInAt(0, Location::RequiresFpuRegister());
3833 locations->SetInAt(1, Location::RequiresFpuRegister());
3834 locations->SetOut(Location::SameAsFirstInput());
3835 locations->AddTemp(Location::RequiresRegister());
3836 break;
3837 case DataType::Type::kFloat64:
3838 locations->SetInAt(0, Location::RequiresFpuRegister());
3839 locations->SetInAt(1, Location::RequiresFpuRegister());
3840 locations->SetOut(Location::SameAsFirstInput());
3841 break;
3842 default:
3843 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3844 }
3845}
3846
Aart Bik351df3e2018-03-07 11:54:57 -08003847void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3848 bool is_min,
3849 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003850 Location op1_loc = locations->InAt(0);
3851 Location op2_loc = locations->InAt(1);
3852
3853 // Shortcut for same input locations.
3854 if (op1_loc.Equals(op2_loc)) {
3855 // Can return immediately, as op1_loc == out_loc.
3856 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3857 // a copy here.
3858 DCHECK(locations->Out().Equals(op1_loc));
3859 return;
3860 }
3861
3862 if (type == DataType::Type::kInt64) {
3863 // Need to perform a subtract to get the sign right.
3864 // op1 is already in the same location as the output.
3865 Location output = locations->Out();
3866 Register output_lo = output.AsRegisterPairLow<Register>();
3867 Register output_hi = output.AsRegisterPairHigh<Register>();
3868
3869 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3870 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3871
3872 // The comparison is performed by subtracting the second operand from
3873 // the first operand and then setting the status flags in the same
3874 // manner as the SUB instruction."
3875 __ cmpl(output_lo, op2_lo);
3876
3877 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3878 Register temp = locations->GetTemp(0).AsRegister<Register>();
3879 __ movl(temp, output_hi);
3880 __ sbbl(temp, op2_hi);
3881
3882 // Now the condition code is correct.
3883 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3884 __ cmovl(cond, output_lo, op2_lo);
3885 __ cmovl(cond, output_hi, op2_hi);
3886 } else {
3887 DCHECK_EQ(type, DataType::Type::kInt32);
3888 Register out = locations->Out().AsRegister<Register>();
3889 Register op2 = op2_loc.AsRegister<Register>();
3890
3891 // (out := op1)
3892 // out <=? op2
3893 // if out is min jmp done
3894 // out := op2
3895 // done:
3896
3897 __ cmpl(out, op2);
3898 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3899 __ cmovl(cond, out, op2);
3900 }
3901}
3902
3903void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3904 bool is_min,
3905 DataType::Type type) {
3906 Location op1_loc = locations->InAt(0);
3907 Location op2_loc = locations->InAt(1);
3908 Location out_loc = locations->Out();
3909 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3910
3911 // Shortcut for same input locations.
3912 if (op1_loc.Equals(op2_loc)) {
3913 DCHECK(out_loc.Equals(op1_loc));
3914 return;
3915 }
3916
3917 // (out := op1)
3918 // out <=? op2
3919 // if Nan jmp Nan_label
3920 // if out is min jmp done
3921 // if op2 is min jmp op2_label
3922 // handle -0/+0
3923 // jmp done
3924 // Nan_label:
3925 // out := NaN
3926 // op2_label:
3927 // out := op2
3928 // done:
3929 //
3930 // This removes one jmp, but needs to copy one input (op1) to out.
3931 //
3932 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3933
3934 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3935
3936 NearLabel nan, done, op2_label;
3937 if (type == DataType::Type::kFloat64) {
3938 __ ucomisd(out, op2);
3939 } else {
3940 DCHECK_EQ(type, DataType::Type::kFloat32);
3941 __ ucomiss(out, op2);
3942 }
3943
3944 __ j(Condition::kParityEven, &nan);
3945
3946 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3947 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3948
3949 // Handle 0.0/-0.0.
3950 if (is_min) {
3951 if (type == DataType::Type::kFloat64) {
3952 __ orpd(out, op2);
3953 } else {
3954 __ orps(out, op2);
3955 }
3956 } else {
3957 if (type == DataType::Type::kFloat64) {
3958 __ andpd(out, op2);
3959 } else {
3960 __ andps(out, op2);
3961 }
3962 }
3963 __ jmp(&done);
3964
3965 // NaN handling.
3966 __ Bind(&nan);
3967 if (type == DataType::Type::kFloat64) {
3968 // TODO: Use a constant from the constant table (requires extra input).
3969 __ LoadLongConstant(out, kDoubleNaN);
3970 } else {
3971 Register constant = locations->GetTemp(0).AsRegister<Register>();
3972 __ movl(constant, Immediate(kFloatNaN));
3973 __ movd(out, constant);
3974 }
3975 __ jmp(&done);
3976
3977 // out := op2;
3978 __ Bind(&op2_label);
3979 if (type == DataType::Type::kFloat64) {
3980 __ movsd(out, op2);
3981 } else {
3982 __ movss(out, op2);
3983 }
3984
3985 // Done.
3986 __ Bind(&done);
3987}
3988
Aart Bik351df3e2018-03-07 11:54:57 -08003989void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3990 DataType::Type type = minmax->GetResultType();
3991 switch (type) {
3992 case DataType::Type::kInt32:
3993 case DataType::Type::kInt64:
3994 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
3995 break;
3996 case DataType::Type::kFloat32:
3997 case DataType::Type::kFloat64:
3998 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
3999 break;
4000 default:
4001 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4002 }
4003}
4004
Aart Bik1f8d51b2018-02-15 10:42:37 -08004005void LocationsBuilderX86::VisitMin(HMin* min) {
4006 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4007}
4008
4009void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004010 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004011}
4012
4013void LocationsBuilderX86::VisitMax(HMax* max) {
4014 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4015}
4016
4017void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004018 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004019}
4020
Aart Bik3dad3412018-02-28 12:01:46 -08004021void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4022 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4023 switch (abs->GetResultType()) {
4024 case DataType::Type::kInt32:
4025 locations->SetInAt(0, Location::RegisterLocation(EAX));
4026 locations->SetOut(Location::SameAsFirstInput());
4027 locations->AddTemp(Location::RegisterLocation(EDX));
4028 break;
4029 case DataType::Type::kInt64:
4030 locations->SetInAt(0, Location::RequiresRegister());
4031 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4032 locations->AddTemp(Location::RequiresRegister());
4033 break;
4034 case DataType::Type::kFloat32:
4035 locations->SetInAt(0, Location::RequiresFpuRegister());
4036 locations->SetOut(Location::SameAsFirstInput());
4037 locations->AddTemp(Location::RequiresFpuRegister());
4038 locations->AddTemp(Location::RequiresRegister());
4039 break;
4040 case DataType::Type::kFloat64:
4041 locations->SetInAt(0, Location::RequiresFpuRegister());
4042 locations->SetOut(Location::SameAsFirstInput());
4043 locations->AddTemp(Location::RequiresFpuRegister());
4044 break;
4045 default:
4046 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4047 }
4048}
4049
4050void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4051 LocationSummary* locations = abs->GetLocations();
4052 switch (abs->GetResultType()) {
4053 case DataType::Type::kInt32: {
4054 Register out = locations->Out().AsRegister<Register>();
4055 DCHECK_EQ(out, EAX);
4056 Register temp = locations->GetTemp(0).AsRegister<Register>();
4057 DCHECK_EQ(temp, EDX);
4058 // Sign extend EAX into EDX.
4059 __ cdq();
4060 // XOR EAX with sign.
4061 __ xorl(EAX, EDX);
4062 // Subtract out sign to correct.
4063 __ subl(EAX, EDX);
4064 // The result is in EAX.
4065 break;
4066 }
4067 case DataType::Type::kInt64: {
4068 Location input = locations->InAt(0);
4069 Register input_lo = input.AsRegisterPairLow<Register>();
4070 Register input_hi = input.AsRegisterPairHigh<Register>();
4071 Location output = locations->Out();
4072 Register output_lo = output.AsRegisterPairLow<Register>();
4073 Register output_hi = output.AsRegisterPairHigh<Register>();
4074 Register temp = locations->GetTemp(0).AsRegister<Register>();
4075 // Compute the sign into the temporary.
4076 __ movl(temp, input_hi);
4077 __ sarl(temp, Immediate(31));
4078 // Store the sign into the output.
4079 __ movl(output_lo, temp);
4080 __ movl(output_hi, temp);
4081 // XOR the input to the output.
4082 __ xorl(output_lo, input_lo);
4083 __ xorl(output_hi, input_hi);
4084 // Subtract the sign.
4085 __ subl(output_lo, temp);
4086 __ sbbl(output_hi, temp);
4087 break;
4088 }
4089 case DataType::Type::kFloat32: {
4090 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4091 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4092 Register constant = locations->GetTemp(1).AsRegister<Register>();
4093 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4094 __ movd(temp, constant);
4095 __ andps(out, temp);
4096 break;
4097 }
4098 case DataType::Type::kFloat64: {
4099 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4100 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4101 // TODO: Use a constant from the constant table (requires extra input).
4102 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4103 __ andpd(out, temp);
4104 break;
4105 }
4106 default:
4107 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4108 }
4109}
4110
Calin Juravled0d48522014-11-04 16:40:20 +00004111void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004112 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004113 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004114 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004115 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 case DataType::Type::kInt8:
4117 case DataType::Type::kUint16:
4118 case DataType::Type::kInt16:
4119 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004120 locations->SetInAt(0, Location::Any());
4121 break;
4122 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004124 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4125 if (!instruction->IsConstant()) {
4126 locations->AddTemp(Location::RequiresRegister());
4127 }
4128 break;
4129 }
4130 default:
4131 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4132 }
Calin Juravled0d48522014-11-04 16:40:20 +00004133}
4134
4135void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004136 SlowPathCode* slow_path =
4137 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004138 codegen_->AddSlowPath(slow_path);
4139
4140 LocationSummary* locations = instruction->GetLocations();
4141 Location value = locations->InAt(0);
4142
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004143 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004145 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004146 case DataType::Type::kInt8:
4147 case DataType::Type::kUint16:
4148 case DataType::Type::kInt16:
4149 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004150 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004151 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004152 __ j(kEqual, slow_path->GetEntryLabel());
4153 } else if (value.IsStackSlot()) {
4154 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4155 __ j(kEqual, slow_path->GetEntryLabel());
4156 } else {
4157 DCHECK(value.IsConstant()) << value;
4158 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004159 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004160 }
4161 }
4162 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004163 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004165 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004166 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004167 __ movl(temp, value.AsRegisterPairLow<Register>());
4168 __ orl(temp, value.AsRegisterPairHigh<Register>());
4169 __ j(kEqual, slow_path->GetEntryLabel());
4170 } else {
4171 DCHECK(value.IsConstant()) << value;
4172 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4173 __ jmp(slow_path->GetEntryLabel());
4174 }
4175 }
4176 break;
4177 }
4178 default:
4179 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004180 }
Calin Juravled0d48522014-11-04 16:40:20 +00004181}
4182
Calin Juravle9aec02f2014-11-18 23:06:35 +00004183void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4184 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4185
4186 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004187 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004188
4189 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004190 case DataType::Type::kInt32:
4191 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004192 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004193 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004194 // The shift count needs to be in CL or a constant.
4195 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004196 locations->SetOut(Location::SameAsFirstInput());
4197 break;
4198 }
4199 default:
4200 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4201 }
4202}
4203
4204void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4205 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4206
4207 LocationSummary* locations = op->GetLocations();
4208 Location first = locations->InAt(0);
4209 Location second = locations->InAt(1);
4210 DCHECK(first.Equals(locations->Out()));
4211
4212 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004214 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004215 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004216 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004217 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004218 DCHECK_EQ(ECX, second_reg);
4219 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004220 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004221 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004222 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004223 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004224 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004225 }
4226 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004227 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004228 if (shift == 0) {
4229 return;
4230 }
4231 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004232 if (op->IsShl()) {
4233 __ shll(first_reg, imm);
4234 } else if (op->IsShr()) {
4235 __ sarl(first_reg, imm);
4236 } else {
4237 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004238 }
4239 }
4240 break;
4241 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004242 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004243 if (second.IsRegister()) {
4244 Register second_reg = second.AsRegister<Register>();
4245 DCHECK_EQ(ECX, second_reg);
4246 if (op->IsShl()) {
4247 GenerateShlLong(first, second_reg);
4248 } else if (op->IsShr()) {
4249 GenerateShrLong(first, second_reg);
4250 } else {
4251 GenerateUShrLong(first, second_reg);
4252 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004253 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004254 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004255 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004256 // Nothing to do if the shift is 0, as the input is already the output.
4257 if (shift != 0) {
4258 if (op->IsShl()) {
4259 GenerateShlLong(first, shift);
4260 } else if (op->IsShr()) {
4261 GenerateShrLong(first, shift);
4262 } else {
4263 GenerateUShrLong(first, shift);
4264 }
4265 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004266 }
4267 break;
4268 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004269 default:
4270 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4271 }
4272}
4273
Mark P Mendell73945692015-04-29 14:56:17 +00004274void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4275 Register low = loc.AsRegisterPairLow<Register>();
4276 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004277 if (shift == 1) {
4278 // This is just an addition.
4279 __ addl(low, low);
4280 __ adcl(high, high);
4281 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004282 // Shift by 32 is easy. High gets low, and low gets 0.
4283 codegen_->EmitParallelMoves(
4284 loc.ToLow(),
4285 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004286 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004287 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4288 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004289 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004290 } else if (shift > 32) {
4291 // Low part becomes 0. High part is low part << (shift-32).
4292 __ movl(high, low);
4293 __ shll(high, Immediate(shift - 32));
4294 __ xorl(low, low);
4295 } else {
4296 // Between 1 and 31.
4297 __ shld(high, low, Immediate(shift));
4298 __ shll(low, Immediate(shift));
4299 }
4300}
4301
Calin Juravle9aec02f2014-11-18 23:06:35 +00004302void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004303 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004304 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4305 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4306 __ testl(shifter, Immediate(32));
4307 __ j(kEqual, &done);
4308 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4309 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4310 __ Bind(&done);
4311}
4312
Mark P Mendell73945692015-04-29 14:56:17 +00004313void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4314 Register low = loc.AsRegisterPairLow<Register>();
4315 Register high = loc.AsRegisterPairHigh<Register>();
4316 if (shift == 32) {
4317 // Need to copy the sign.
4318 DCHECK_NE(low, high);
4319 __ movl(low, high);
4320 __ sarl(high, Immediate(31));
4321 } else if (shift > 32) {
4322 DCHECK_NE(low, high);
4323 // High part becomes sign. Low part is shifted by shift - 32.
4324 __ movl(low, high);
4325 __ sarl(high, Immediate(31));
4326 __ sarl(low, Immediate(shift - 32));
4327 } else {
4328 // Between 1 and 31.
4329 __ shrd(low, high, Immediate(shift));
4330 __ sarl(high, Immediate(shift));
4331 }
4332}
4333
Calin Juravle9aec02f2014-11-18 23:06:35 +00004334void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004335 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004336 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4337 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4338 __ testl(shifter, Immediate(32));
4339 __ j(kEqual, &done);
4340 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4341 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4342 __ Bind(&done);
4343}
4344
Mark P Mendell73945692015-04-29 14:56:17 +00004345void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4346 Register low = loc.AsRegisterPairLow<Register>();
4347 Register high = loc.AsRegisterPairHigh<Register>();
4348 if (shift == 32) {
4349 // Shift by 32 is easy. Low gets high, and high gets 0.
4350 codegen_->EmitParallelMoves(
4351 loc.ToHigh(),
4352 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004354 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4355 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004356 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004357 } else if (shift > 32) {
4358 // Low part is high >> (shift - 32). High part becomes 0.
4359 __ movl(low, high);
4360 __ shrl(low, Immediate(shift - 32));
4361 __ xorl(high, high);
4362 } else {
4363 // Between 1 and 31.
4364 __ shrd(low, high, Immediate(shift));
4365 __ shrl(high, Immediate(shift));
4366 }
4367}
4368
Calin Juravle9aec02f2014-11-18 23:06:35 +00004369void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004370 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004371 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4372 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4373 __ testl(shifter, Immediate(32));
4374 __ j(kEqual, &done);
4375 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4376 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4377 __ Bind(&done);
4378}
4379
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004380void LocationsBuilderX86::VisitRor(HRor* ror) {
4381 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004382 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004383
4384 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004386 // Add the temporary needed.
4387 locations->AddTemp(Location::RequiresRegister());
4388 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004389 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004390 locations->SetInAt(0, Location::RequiresRegister());
4391 // The shift count needs to be in CL (unless it is a constant).
4392 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4393 locations->SetOut(Location::SameAsFirstInput());
4394 break;
4395 default:
4396 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4397 UNREACHABLE();
4398 }
4399}
4400
4401void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4402 LocationSummary* locations = ror->GetLocations();
4403 Location first = locations->InAt(0);
4404 Location second = locations->InAt(1);
4405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004406 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004407 Register first_reg = first.AsRegister<Register>();
4408 if (second.IsRegister()) {
4409 Register second_reg = second.AsRegister<Register>();
4410 __ rorl(first_reg, second_reg);
4411 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004412 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004413 __ rorl(first_reg, imm);
4414 }
4415 return;
4416 }
4417
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004418 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004419 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4420 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4421 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4422 if (second.IsRegister()) {
4423 Register second_reg = second.AsRegister<Register>();
4424 DCHECK_EQ(second_reg, ECX);
4425 __ movl(temp_reg, first_reg_hi);
4426 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4427 __ shrd(first_reg_lo, temp_reg, second_reg);
4428 __ movl(temp_reg, first_reg_hi);
4429 __ testl(second_reg, Immediate(32));
4430 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4431 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4432 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004433 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004434 if (shift_amt == 0) {
4435 // Already fine.
4436 return;
4437 }
4438 if (shift_amt == 32) {
4439 // Just swap.
4440 __ movl(temp_reg, first_reg_lo);
4441 __ movl(first_reg_lo, first_reg_hi);
4442 __ movl(first_reg_hi, temp_reg);
4443 return;
4444 }
4445
4446 Immediate imm(shift_amt);
4447 // Save the constents of the low value.
4448 __ movl(temp_reg, first_reg_lo);
4449
4450 // Shift right into low, feeding bits from high.
4451 __ shrd(first_reg_lo, first_reg_hi, imm);
4452
4453 // Shift right into high, feeding bits from the original low.
4454 __ shrd(first_reg_hi, temp_reg, imm);
4455
4456 // Swap if needed.
4457 if (shift_amt > 32) {
4458 __ movl(temp_reg, first_reg_lo);
4459 __ movl(first_reg_lo, first_reg_hi);
4460 __ movl(first_reg_hi, temp_reg);
4461 }
4462 }
4463}
4464
Calin Juravle9aec02f2014-11-18 23:06:35 +00004465void LocationsBuilderX86::VisitShl(HShl* shl) {
4466 HandleShift(shl);
4467}
4468
4469void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4470 HandleShift(shl);
4471}
4472
4473void LocationsBuilderX86::VisitShr(HShr* shr) {
4474 HandleShift(shr);
4475}
4476
4477void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4478 HandleShift(shr);
4479}
4480
4481void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4482 HandleShift(ushr);
4483}
4484
4485void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4486 HandleShift(ushr);
4487}
4488
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004489void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004490 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4491 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004492 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004493 if (instruction->IsStringAlloc()) {
4494 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4495 } else {
4496 InvokeRuntimeCallingConvention calling_convention;
4497 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004498 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004499}
4500
4501void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004502 // Note: if heap poisoning is enabled, the entry point takes cares
4503 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004504 if (instruction->IsStringAlloc()) {
4505 // String is allocated through StringFactory. Call NewEmptyString entry point.
4506 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004507 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004508 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4509 __ call(Address(temp, code_offset.Int32Value()));
4510 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4511 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004512 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004513 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004514 DCHECK(!codegen_->IsLeafMethod());
4515 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004516}
4517
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004518void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004519 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4520 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004521 locations->SetOut(Location::RegisterLocation(EAX));
4522 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004523 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4524 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525}
4526
4527void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004528 // Note: if heap poisoning is enabled, the entry point takes cares
4529 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004530 QuickEntrypointEnum entrypoint =
4531 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4532 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004533 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004534 DCHECK(!codegen_->IsLeafMethod());
4535}
4536
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004537void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004539 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004540 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4541 if (location.IsStackSlot()) {
4542 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4543 } else if (location.IsDoubleStackSlot()) {
4544 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004545 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004546 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004547}
4548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004549void InstructionCodeGeneratorX86::VisitParameterValue(
4550 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4551}
4552
4553void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4554 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004555 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004556 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4557}
4558
4559void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004560}
4561
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004562void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4563 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004564 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004565 locations->SetInAt(0, Location::RequiresRegister());
4566 locations->SetOut(Location::RequiresRegister());
4567}
4568
4569void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4570 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004571 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004572 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004573 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004574 __ movl(locations->Out().AsRegister<Register>(),
4575 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004576 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004577 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004578 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004579 __ movl(locations->Out().AsRegister<Register>(),
4580 Address(locations->InAt(0).AsRegister<Register>(),
4581 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4582 // temp = temp->GetImtEntryAt(method_offset);
4583 __ movl(locations->Out().AsRegister<Register>(),
4584 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004585 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004586}
4587
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004588void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004589 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004590 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004591 locations->SetInAt(0, Location::RequiresRegister());
4592 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004593}
4594
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004595void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4596 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004597 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004598 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004599 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004600 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004601 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004602 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004603 break;
4604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004605 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004606 __ notl(out.AsRegisterPairLow<Register>());
4607 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004608 break;
4609
4610 default:
4611 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4612 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004613}
4614
David Brazdil66d126e2015-04-03 16:02:44 +01004615void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4616 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004617 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004618 locations->SetInAt(0, Location::RequiresRegister());
4619 locations->SetOut(Location::SameAsFirstInput());
4620}
4621
4622void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004623 LocationSummary* locations = bool_not->GetLocations();
4624 Location in = locations->InAt(0);
4625 Location out = locations->Out();
4626 DCHECK(in.Equals(out));
4627 __ xorl(out.AsRegister<Register>(), Immediate(1));
4628}
4629
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004630void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004631 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004632 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004633 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004634 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004635 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004636 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004638 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 case DataType::Type::kInt32:
4640 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004641 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004642 locations->SetInAt(1, Location::Any());
4643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4644 break;
4645 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004646 case DataType::Type::kFloat32:
4647 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004648 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004649 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4650 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4651 } else if (compare->InputAt(1)->IsConstant()) {
4652 locations->SetInAt(1, Location::RequiresFpuRegister());
4653 } else {
4654 locations->SetInAt(1, Location::Any());
4655 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004656 locations->SetOut(Location::RequiresRegister());
4657 break;
4658 }
4659 default:
4660 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4661 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004662}
4663
4664void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004665 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004667 Location left = locations->InAt(0);
4668 Location right = locations->InAt(1);
4669
Mark Mendell0c9497d2015-08-21 09:30:05 -04004670 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004671 Condition less_cond = kLess;
4672
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004673 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004675 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004676 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004678 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004679 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004680 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004681 break;
4682 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004683 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004684 Register left_low = left.AsRegisterPairLow<Register>();
4685 Register left_high = left.AsRegisterPairHigh<Register>();
4686 int32_t val_low = 0;
4687 int32_t val_high = 0;
4688 bool right_is_const = false;
4689
4690 if (right.IsConstant()) {
4691 DCHECK(right.GetConstant()->IsLongConstant());
4692 right_is_const = true;
4693 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4694 val_low = Low32Bits(val);
4695 val_high = High32Bits(val);
4696 }
4697
Calin Juravleddb7df22014-11-25 20:56:51 +00004698 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004699 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004700 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004701 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004702 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004703 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004704 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004705 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004706 __ j(kLess, &less); // Signed compare.
4707 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004708 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004709 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004710 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004711 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004712 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004713 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004714 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004715 }
Aart Bika19616e2016-02-01 18:57:58 -08004716 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004717 break;
4718 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004720 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004721 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004722 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004723 break;
4724 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004726 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004727 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004728 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004729 break;
4730 }
4731 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004732 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004733 }
Aart Bika19616e2016-02-01 18:57:58 -08004734
Calin Juravleddb7df22014-11-25 20:56:51 +00004735 __ movl(out, Immediate(0));
4736 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004737 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004738
4739 __ Bind(&greater);
4740 __ movl(out, Immediate(1));
4741 __ jmp(&done);
4742
4743 __ Bind(&less);
4744 __ movl(out, Immediate(-1));
4745
4746 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004747}
4748
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004749void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004750 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004751 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004752 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004753 locations->SetInAt(i, Location::Any());
4754 }
4755 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004756}
4757
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004758void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004759 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004760}
4761
Roland Levillain7c1559a2015-12-15 10:55:36 +00004762void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004763 /*
4764 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4765 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4766 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4767 */
4768 switch (kind) {
4769 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004770 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004771 break;
4772 }
4773 case MemBarrierKind::kAnyStore:
4774 case MemBarrierKind::kLoadAny:
4775 case MemBarrierKind::kStoreStore: {
4776 // nop
4777 break;
4778 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004779 case MemBarrierKind::kNTStoreStore:
4780 // Non-Temporal Store/Store needs an explicit fence.
4781 MemoryFence(/* non-temporal */ true);
4782 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004783 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784}
4785
Vladimir Markodc151b22015-10-15 18:02:30 +01004786HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4787 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004788 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004789 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004790}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004791
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004792Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4793 Register temp) {
4794 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004795 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004796 if (!invoke->GetLocations()->Intrinsified()) {
4797 return location.AsRegister<Register>();
4798 }
4799 // For intrinsics we allow any location, so it may be on the stack.
4800 if (!location.IsRegister()) {
4801 __ movl(temp, Address(ESP, location.GetStackIndex()));
4802 return temp;
4803 }
4804 // For register locations, check if the register was saved. If so, get it from the stack.
4805 // Note: There is a chance that the register was saved but not overwritten, so we could
4806 // save one load. However, since this is just an intrinsic slow path we prefer this
4807 // simple and more robust approach rather that trying to determine if that's the case.
4808 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004809 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4810 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4811 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4812 __ movl(temp, Address(ESP, stack_offset));
4813 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004814 }
4815 return location.AsRegister<Register>();
4816}
4817
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004818void CodeGeneratorX86::GenerateStaticOrDirectCall(
4819 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004820 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4821 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004822 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004823 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004824 uint32_t offset =
4825 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4826 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004827 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004828 }
Vladimir Marko58155012015-08-19 12:49:41 +00004829 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004830 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004831 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004832 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4833 DCHECK(GetCompilerOptions().IsBootImage());
4834 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4835 temp.AsRegister<Register>());
4836 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004837 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004838 break;
4839 }
Vladimir Marko58155012015-08-19 12:49:41 +00004840 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4841 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4842 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004843 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4844 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4845 temp.AsRegister<Register>());
4846 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4847 RecordBootImageRelRoPatch(
4848 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004849 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004850 break;
4851 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004852 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004853 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4854 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004855 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004856 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004857 break;
4858 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004859 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4860 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4861 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004862 }
Vladimir Marko58155012015-08-19 12:49:41 +00004863 }
4864
4865 switch (invoke->GetCodePtrLocation()) {
4866 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4867 __ call(GetFrameEntryLabel());
4868 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004869 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4870 // (callee_method + offset_of_quick_compiled_code)()
4871 __ call(Address(callee_method.AsRegister<Register>(),
4872 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004873 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004874 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004875 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004876 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004877
4878 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004879}
4880
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004881void CodeGeneratorX86::GenerateVirtualCall(
4882 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004883 Register temp = temp_in.AsRegister<Register>();
4884 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4885 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004886
4887 // Use the calling convention instead of the location of the receiver, as
4888 // intrinsics may have put the receiver in a different register. In the intrinsics
4889 // slow path, the arguments have been moved to the right place, so here we are
4890 // guaranteed that the receiver is the first register of the calling convention.
4891 InvokeDexCallingConvention calling_convention;
4892 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004893 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004894 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004895 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004896 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004897 // Instead of simply (possibly) unpoisoning `temp` here, we should
4898 // emit a read barrier for the previous class reference load.
4899 // However this is not required in practice, as this is an
4900 // intermediate/temporary reference and because the current
4901 // concurrent copying collector keeps the from-space memory
4902 // intact/accessible until the end of the marking phase (the
4903 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004904 __ MaybeUnpoisonHeapReference(temp);
4905 // temp = temp->GetMethodAt(method_offset);
4906 __ movl(temp, Address(temp, method_offset));
4907 // call temp->GetEntryPoint();
4908 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004909 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004910 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004911}
4912
Vladimir Markob066d432018-01-03 13:14:37 +00004913void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4914 uint32_t boot_image_offset) {
4915 boot_image_method_patches_.emplace_back(
4916 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4917 __ Bind(&boot_image_method_patches_.back().label);
4918}
4919
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004920void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004921 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004922 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004923 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004924 boot_image_method_patches_.emplace_back(
4925 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004926 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004927}
4928
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004929void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4930 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4931 HX86ComputeBaseMethodAddress* method_address =
4932 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004933 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004934 method_bss_entry_patches_.emplace_back(
4935 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4936 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004937}
4938
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004939void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4940 HX86ComputeBaseMethodAddress* method_address =
4941 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4942 boot_image_type_patches_.emplace_back(
4943 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004944 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004945}
4946
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004947Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004948 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004949 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4950 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004951 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004952 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004953}
4954
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004955void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4956 HX86ComputeBaseMethodAddress* method_address =
4957 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4958 boot_image_string_patches_.emplace_back(
4959 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4960 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004961}
4962
Vladimir Markoaad75c62016-10-03 08:46:48 +00004963Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4964 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004965 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004966 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004967 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004968 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004969 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004970}
4971
Vladimir Markoaad75c62016-10-03 08:46:48 +00004972// The label points to the end of the "movl" or another instruction but the literal offset
4973// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4974constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4975
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004976template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004977inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004978 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004979 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004980 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004981 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004982 linker_patches->push_back(Factory(literal_offset,
4983 info.target_dex_file,
4984 GetMethodAddressOffset(info.method_address),
4985 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004986 }
4987}
4988
Vladimir Markob066d432018-01-03 13:14:37 +00004989linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
4990 const DexFile* target_dex_file,
4991 uint32_t pc_insn_offset,
4992 uint32_t boot_image_offset) {
4993 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
4994 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
4995}
4996
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004997void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004998 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004999 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005000 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005001 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005002 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005003 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005004 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005005 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005006 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005007 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005008 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5009 boot_image_method_patches_, linker_patches);
5010 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5011 boot_image_type_patches_, linker_patches);
5012 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005013 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005014 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00005015 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
5016 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005017 DCHECK(boot_image_type_patches_.empty());
5018 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005019 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005020 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5021 method_bss_entry_patches_, linker_patches);
5022 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5023 type_bss_entry_patches_, linker_patches);
5024 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5025 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005026 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005027}
5028
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005029void CodeGeneratorX86::MarkGCCard(Register temp,
5030 Register card,
5031 Register object,
5032 Register value,
5033 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005034 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005035 if (value_can_be_null) {
5036 __ testl(value, value);
5037 __ j(kEqual, &is_null);
5038 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005039 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 __ movl(temp, object);
5041 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005042 __ movb(Address(temp, card, TIMES_1, 0),
5043 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005044 if (value_can_be_null) {
5045 __ Bind(&is_null);
5046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047}
5048
Calin Juravle52c48962014-12-16 17:02:57 +00005049void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5050 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005051
5052 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005054 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005055 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5056 kEmitCompilerReadBarrier
5057 ? LocationSummary::kCallOnSlowPath
5058 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005059 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005060 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005061 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005062 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005063
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005064 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005065 locations->SetOut(Location::RequiresFpuRegister());
5066 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005067 // The output overlaps in case of long: we don't want the low move
5068 // to overwrite the object's location. Likewise, in the case of
5069 // an object field get with read barriers enabled, we do not want
5070 // the move to overwrite the object's location, as we need it to emit
5071 // the read barrier.
5072 locations->SetOut(
5073 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005074 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005075 Location::kOutputOverlap :
5076 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005077 }
Calin Juravle52c48962014-12-16 17:02:57 +00005078
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005080 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005081 // So we use an XMM register as a temp to achieve atomicity (first
5082 // load the temp into the XMM and then copy the XMM into the
5083 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005084 locations->AddTemp(Location::RequiresFpuRegister());
5085 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086}
5087
Calin Juravle52c48962014-12-16 17:02:57 +00005088void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5089 const FieldInfo& field_info) {
5090 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005091
Calin Juravle52c48962014-12-16 17:02:57 +00005092 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005093 Location base_loc = locations->InAt(0);
5094 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005095 Location out = locations->Out();
5096 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005097 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5098 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005099 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5100
Vladimir Marko61b92282017-10-11 13:23:17 +01005101 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005102 case DataType::Type::kBool:
5103 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005104 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005105 break;
5106 }
5107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005108 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005109 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005110 break;
5111 }
5112
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005113 case DataType::Type::kUint16: {
5114 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005115 break;
5116 }
5117
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005118 case DataType::Type::kInt16: {
5119 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005120 break;
5121 }
5122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005123 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005124 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005128 // /* HeapReference<Object> */ out = *(base + offset)
5129 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005130 // Note that a potential implicit null check is handled in this
5131 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5132 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005133 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005134 if (is_volatile) {
5135 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5136 }
5137 } else {
5138 __ movl(out.AsRegister<Register>(), Address(base, offset));
5139 codegen_->MaybeRecordImplicitNullCheck(instruction);
5140 if (is_volatile) {
5141 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5142 }
5143 // If read barriers are enabled, emit read barriers other than
5144 // Baker's using a slow path (and also unpoison the loaded
5145 // reference, if heap poisoning is enabled).
5146 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5147 }
5148 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005149 }
5150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005151 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005152 if (is_volatile) {
5153 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5154 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005156 __ movd(out.AsRegisterPairLow<Register>(), temp);
5157 __ psrlq(temp, Immediate(32));
5158 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5159 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005160 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005161 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005162 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005163 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5164 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165 break;
5166 }
5167
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005168 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005169 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005170 break;
5171 }
5172
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005173 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005174 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005175 break;
5176 }
5177
Aart Bik66c158e2018-01-31 12:55:04 -08005178 case DataType::Type::kUint32:
5179 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005180 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005181 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005182 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183 }
Calin Juravle52c48962014-12-16 17:02:57 +00005184
Vladimir Marko61b92282017-10-11 13:23:17 +01005185 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005186 // Potential implicit null checks, in the case of reference or
5187 // long fields, are handled in the previous switch statement.
5188 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005189 codegen_->MaybeRecordImplicitNullCheck(instruction);
5190 }
5191
Calin Juravle52c48962014-12-16 17:02:57 +00005192 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005193 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005194 // Memory barriers, in the case of references, are also handled
5195 // in the previous switch statement.
5196 } else {
5197 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5198 }
Roland Levillain4d027112015-07-01 15:41:14 +01005199 }
Calin Juravle52c48962014-12-16 17:02:57 +00005200}
5201
5202void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5203 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5204
5205 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005206 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005207 locations->SetInAt(0, Location::RequiresRegister());
5208 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005210 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005211
5212 // The register allocator does not support multiple
5213 // inputs that die at entry with one in a specific register.
5214 if (is_byte_type) {
5215 // Ensure the value is in a byte register.
5216 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 } else if (DataType::IsFloatingPointType(field_type)) {
5218 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005219 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5220 locations->SetInAt(1, Location::RequiresFpuRegister());
5221 } else {
5222 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5223 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005225 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005226 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005227
Calin Juravle52c48962014-12-16 17:02:57 +00005228 // 64bits value can be atomically written to an address with movsd and an XMM register.
5229 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5230 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5231 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5232 // isolated cases when we need this it isn't worth adding the extra complexity.
5233 locations->AddTemp(Location::RequiresFpuRegister());
5234 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005235 } else {
5236 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5237
5238 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5239 // Temporary registers for the write barrier.
5240 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5241 // Ensure the card is in a byte register.
5242 locations->AddTemp(Location::RegisterLocation(ECX));
5243 }
Calin Juravle52c48962014-12-16 17:02:57 +00005244 }
5245}
5246
5247void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005248 const FieldInfo& field_info,
5249 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005250 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5251
5252 LocationSummary* locations = instruction->GetLocations();
5253 Register base = locations->InAt(0).AsRegister<Register>();
5254 Location value = locations->InAt(1);
5255 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005256 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005257 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005258 bool needs_write_barrier =
5259 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005260
5261 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005263 }
5264
Mark Mendell81489372015-11-04 11:30:41 -05005265 bool maybe_record_implicit_null_check_done = false;
5266
Calin Juravle52c48962014-12-16 17:02:57 +00005267 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005268 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005269 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005270 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005271 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5272 break;
5273 }
5274
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005275 case DataType::Type::kUint16:
5276 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005277 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005278 __ movw(Address(base, offset),
5279 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005280 } else {
5281 __ movw(Address(base, offset), value.AsRegister<Register>());
5282 }
Calin Juravle52c48962014-12-16 17:02:57 +00005283 break;
5284 }
5285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 case DataType::Type::kInt32:
5287 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005288 if (kPoisonHeapReferences && needs_write_barrier) {
5289 // Note that in the case where `value` is a null reference,
5290 // we do not enter this block, as the reference does not
5291 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005292 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005293 Register temp = locations->GetTemp(0).AsRegister<Register>();
5294 __ movl(temp, value.AsRegister<Register>());
5295 __ PoisonHeapReference(temp);
5296 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005297 } else if (value.IsConstant()) {
5298 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5299 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005300 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005301 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005302 __ movl(Address(base, offset), value.AsRegister<Register>());
5303 }
Calin Juravle52c48962014-12-16 17:02:57 +00005304 break;
5305 }
5306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005308 if (is_volatile) {
5309 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5310 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5311 __ movd(temp1, value.AsRegisterPairLow<Register>());
5312 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5313 __ punpckldq(temp1, temp2);
5314 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005315 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005316 } else if (value.IsConstant()) {
5317 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5318 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5319 codegen_->MaybeRecordImplicitNullCheck(instruction);
5320 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005321 } else {
5322 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005323 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005324 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5325 }
Mark Mendell81489372015-11-04 11:30:41 -05005326 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005327 break;
5328 }
5329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005331 if (value.IsConstant()) {
5332 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5333 __ movl(Address(base, offset), Immediate(v));
5334 } else {
5335 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5336 }
Calin Juravle52c48962014-12-16 17:02:57 +00005337 break;
5338 }
5339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005341 if (value.IsConstant()) {
5342 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5343 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5344 codegen_->MaybeRecordImplicitNullCheck(instruction);
5345 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5346 maybe_record_implicit_null_check_done = true;
5347 } else {
5348 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5349 }
Calin Juravle52c48962014-12-16 17:02:57 +00005350 break;
5351 }
5352
Aart Bik66c158e2018-01-31 12:55:04 -08005353 case DataType::Type::kUint32:
5354 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005355 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005356 LOG(FATAL) << "Unreachable type " << field_type;
5357 UNREACHABLE();
5358 }
5359
Mark Mendell81489372015-11-04 11:30:41 -05005360 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005361 codegen_->MaybeRecordImplicitNullCheck(instruction);
5362 }
5363
Roland Levillain4d027112015-07-01 15:41:14 +01005364 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005365 Register temp = locations->GetTemp(0).AsRegister<Register>();
5366 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005367 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005368 }
5369
Calin Juravle52c48962014-12-16 17:02:57 +00005370 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005371 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005372 }
5373}
5374
5375void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5376 HandleFieldGet(instruction, instruction->GetFieldInfo());
5377}
5378
5379void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5380 HandleFieldGet(instruction, instruction->GetFieldInfo());
5381}
5382
5383void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5384 HandleFieldSet(instruction, instruction->GetFieldInfo());
5385}
5386
5387void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* 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::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5392 HandleFieldSet(instruction, instruction->GetFieldInfo());
5393}
5394
5395void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005396 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005397}
5398
5399void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5400 HandleFieldGet(instruction, instruction->GetFieldInfo());
5401}
5402
5403void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5404 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005405}
5406
Calin Juravlee460d1d2015-09-29 04:52:17 +01005407void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5408 HUnresolvedInstanceFieldGet* instruction) {
5409 FieldAccessCallingConventionX86 calling_convention;
5410 codegen_->CreateUnresolvedFieldLocationSummary(
5411 instruction, instruction->GetFieldType(), calling_convention);
5412}
5413
5414void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5415 HUnresolvedInstanceFieldGet* instruction) {
5416 FieldAccessCallingConventionX86 calling_convention;
5417 codegen_->GenerateUnresolvedFieldAccess(instruction,
5418 instruction->GetFieldType(),
5419 instruction->GetFieldIndex(),
5420 instruction->GetDexPc(),
5421 calling_convention);
5422}
5423
5424void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5425 HUnresolvedInstanceFieldSet* instruction) {
5426 FieldAccessCallingConventionX86 calling_convention;
5427 codegen_->CreateUnresolvedFieldLocationSummary(
5428 instruction, instruction->GetFieldType(), calling_convention);
5429}
5430
5431void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5432 HUnresolvedInstanceFieldSet* instruction) {
5433 FieldAccessCallingConventionX86 calling_convention;
5434 codegen_->GenerateUnresolvedFieldAccess(instruction,
5435 instruction->GetFieldType(),
5436 instruction->GetFieldIndex(),
5437 instruction->GetDexPc(),
5438 calling_convention);
5439}
5440
5441void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5442 HUnresolvedStaticFieldGet* instruction) {
5443 FieldAccessCallingConventionX86 calling_convention;
5444 codegen_->CreateUnresolvedFieldLocationSummary(
5445 instruction, instruction->GetFieldType(), calling_convention);
5446}
5447
5448void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5449 HUnresolvedStaticFieldGet* instruction) {
5450 FieldAccessCallingConventionX86 calling_convention;
5451 codegen_->GenerateUnresolvedFieldAccess(instruction,
5452 instruction->GetFieldType(),
5453 instruction->GetFieldIndex(),
5454 instruction->GetDexPc(),
5455 calling_convention);
5456}
5457
5458void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5459 HUnresolvedStaticFieldSet* instruction) {
5460 FieldAccessCallingConventionX86 calling_convention;
5461 codegen_->CreateUnresolvedFieldLocationSummary(
5462 instruction, instruction->GetFieldType(), calling_convention);
5463}
5464
5465void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5466 HUnresolvedStaticFieldSet* instruction) {
5467 FieldAccessCallingConventionX86 calling_convention;
5468 codegen_->GenerateUnresolvedFieldAccess(instruction,
5469 instruction->GetFieldType(),
5470 instruction->GetFieldIndex(),
5471 instruction->GetDexPc(),
5472 calling_convention);
5473}
5474
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005475void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005476 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5477 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5478 ? Location::RequiresRegister()
5479 : Location::Any();
5480 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005481}
5482
Calin Juravle2ae48182016-03-16 14:05:09 +00005483void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5484 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005485 return;
5486 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005487 LocationSummary* locations = instruction->GetLocations();
5488 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005489
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005490 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005491 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005492}
5493
Calin Juravle2ae48182016-03-16 14:05:09 +00005494void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005495 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005496 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005497
5498 LocationSummary* locations = instruction->GetLocations();
5499 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005500
5501 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005502 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005503 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005504 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005505 } else {
5506 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005507 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005508 __ jmp(slow_path->GetEntryLabel());
5509 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005510 }
5511 __ j(kEqual, slow_path->GetEntryLabel());
5512}
5513
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005514void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005515 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005516}
5517
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005518void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005519 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005522 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5523 object_array_get_with_read_barrier
5524 ? LocationSummary::kCallOnSlowPath
5525 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005526 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005527 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005528 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005529 locations->SetInAt(0, Location::RequiresRegister());
5530 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005532 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5533 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005534 // The output overlaps in case of long: we don't want the low move
5535 // to overwrite the array's location. Likewise, in the case of an
5536 // object array get with read barriers enabled, we do not want the
5537 // move to overwrite the array's location, as we need it to emit
5538 // the read barrier.
5539 locations->SetOut(
5540 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005541 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5542 ? Location::kOutputOverlap
5543 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005544 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545}
5546
5547void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5548 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005549 Location obj_loc = locations->InAt(0);
5550 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005551 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005552 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005553 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005555 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005556 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005557 case DataType::Type::kBool:
5558 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005559 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005560 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 break;
5562 }
5563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005565 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005566 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567 break;
5568 }
5569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005570 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005571 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005572 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5573 // Branch cases into compressed and uncompressed for each index's type.
5574 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5575 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005576 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005577 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005578 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5579 "Expecting 0=compressed, 1=uncompressed");
5580 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005581 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5582 __ jmp(&done);
5583 __ Bind(&not_compressed);
5584 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5585 __ Bind(&done);
5586 } else {
5587 // Common case for charAt of array of char or when string compression's
5588 // feature is turned off.
5589 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5590 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 break;
5592 }
5593
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005594 case DataType::Type::kInt16: {
5595 Register out = out_loc.AsRegister<Register>();
5596 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5597 break;
5598 }
5599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005601 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005602 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005603 break;
5604 }
5605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005606 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005607 static_assert(
5608 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5609 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005610 // /* HeapReference<Object> */ out =
5611 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5612 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005613 // Note that a potential implicit null check is handled in this
5614 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5615 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005616 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005617 } else {
5618 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005619 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5620 codegen_->MaybeRecordImplicitNullCheck(instruction);
5621 // If read barriers are enabled, emit read barriers other than
5622 // Baker's using a slow path (and also unpoison the loaded
5623 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005624 if (index.IsConstant()) {
5625 uint32_t offset =
5626 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005627 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5628 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005629 codegen_->MaybeGenerateReadBarrierSlow(
5630 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5631 }
5632 }
5633 break;
5634 }
5635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005637 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005638 __ movl(out_loc.AsRegisterPairLow<Register>(),
5639 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5640 codegen_->MaybeRecordImplicitNullCheck(instruction);
5641 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5642 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643 break;
5644 }
5645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005647 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005648 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005649 break;
5650 }
5651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005653 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005654 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005655 break;
5656 }
5657
Aart Bik66c158e2018-01-31 12:55:04 -08005658 case DataType::Type::kUint32:
5659 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005661 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005662 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005665 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005666 // Potential implicit null checks, in the case of reference or
5667 // long arrays, are handled in the previous switch statement.
5668 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005669 codegen_->MaybeRecordImplicitNullCheck(instruction);
5670 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005671}
5672
5673void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005675
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005676 bool needs_write_barrier =
5677 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005678 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005679
Vladimir Markoca6fff82017-10-03 14:49:14 +01005680 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005681 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005682 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005683 LocationSummary::kCallOnSlowPath :
5684 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005685
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005686 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005687 // We need the inputs to be different than the output in case of long operation.
5688 // In case of a byte operation, the register allocator does not support multiple
5689 // inputs that die at entry with one in a specific register.
5690 locations->SetInAt(0, Location::RequiresRegister());
5691 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5692 if (is_byte_type) {
5693 // Ensure the value is in a byte register.
5694 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005696 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005697 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005698 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5699 }
5700 if (needs_write_barrier) {
5701 // Temporary registers for the write barrier.
5702 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5703 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005704 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005705 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005706}
5707
5708void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5709 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710 Location array_loc = locations->InAt(0);
5711 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005712 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005713 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005714 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005715 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5716 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5717 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005718 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005719 bool needs_write_barrier =
5720 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005721
5722 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005723 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005724 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005726 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005727 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005728 if (value.IsRegister()) {
5729 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005730 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005731 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005732 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005733 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005734 break;
5735 }
5736
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005737 case DataType::Type::kUint16:
5738 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005739 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005740 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005741 if (value.IsRegister()) {
5742 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005743 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005744 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005745 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005746 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005747 break;
5748 }
5749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005750 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005751 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005752 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005753
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005754 if (!value.IsRegister()) {
5755 // Just setting null.
5756 DCHECK(instruction->InputAt(2)->IsNullConstant());
5757 DCHECK(value.IsConstant()) << value;
5758 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005759 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005760 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005761 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005762 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005763 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005764
5765 DCHECK(needs_write_barrier);
5766 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005767 // We cannot use a NearLabel for `done`, as its range may be too
5768 // short when Baker read barriers are enabled.
5769 Label done;
5770 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005771 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005772 Location temp_loc = locations->GetTemp(0);
5773 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005774 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005775 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005776 codegen_->AddSlowPath(slow_path);
5777 if (instruction->GetValueCanBeNull()) {
5778 __ testl(register_value, register_value);
5779 __ j(kNotEqual, &not_null);
5780 __ movl(address, Immediate(0));
5781 codegen_->MaybeRecordImplicitNullCheck(instruction);
5782 __ jmp(&done);
5783 __ Bind(&not_null);
5784 }
5785
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005786 // Note that when Baker read barriers are enabled, the type
5787 // checks are performed without read barriers. This is fine,
5788 // even in the case where a class object is in the from-space
5789 // after the flip, as a comparison involving such a type would
5790 // not produce a false positive; it may of course produce a
5791 // false negative, in which case we would take the ArraySet
5792 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005793
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005794 // /* HeapReference<Class> */ temp = array->klass_
5795 __ movl(temp, Address(array, class_offset));
5796 codegen_->MaybeRecordImplicitNullCheck(instruction);
5797 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005798
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005799 // /* HeapReference<Class> */ temp = temp->component_type_
5800 __ movl(temp, Address(temp, component_offset));
5801 // If heap poisoning is enabled, no need to unpoison `temp`
5802 // nor the object reference in `register_value->klass`, as
5803 // we are comparing two poisoned references.
5804 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005805
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005806 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5807 __ j(kEqual, &do_put);
5808 // If heap poisoning is enabled, the `temp` reference has
5809 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005810 __ MaybeUnpoisonHeapReference(temp);
5811
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005812 // If heap poisoning is enabled, no need to unpoison the
5813 // heap reference loaded below, as it is only used for a
5814 // comparison with null.
5815 __ cmpl(Address(temp, super_offset), Immediate(0));
5816 __ j(kNotEqual, slow_path->GetEntryLabel());
5817 __ Bind(&do_put);
5818 } else {
5819 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005820 }
5821 }
5822
5823 if (kPoisonHeapReferences) {
5824 __ movl(temp, register_value);
5825 __ PoisonHeapReference(temp);
5826 __ movl(address, temp);
5827 } else {
5828 __ movl(address, register_value);
5829 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005830 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005831 codegen_->MaybeRecordImplicitNullCheck(instruction);
5832 }
5833
5834 Register card = locations->GetTemp(1).AsRegister<Register>();
5835 codegen_->MarkGCCard(
5836 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5837 __ Bind(&done);
5838
5839 if (slow_path != nullptr) {
5840 __ Bind(slow_path->GetExitLabel());
5841 }
5842
5843 break;
5844 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005846 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005847 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005848 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005849 if (value.IsRegister()) {
5850 __ movl(address, value.AsRegister<Register>());
5851 } else {
5852 DCHECK(value.IsConstant()) << value;
5853 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5854 __ movl(address, Immediate(v));
5855 }
5856 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005857 break;
5858 }
5859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005860 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005862 if (value.IsRegisterPair()) {
5863 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5864 value.AsRegisterPairLow<Register>());
5865 codegen_->MaybeRecordImplicitNullCheck(instruction);
5866 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5867 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005868 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005869 DCHECK(value.IsConstant());
5870 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5871 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5872 Immediate(Low32Bits(val)));
5873 codegen_->MaybeRecordImplicitNullCheck(instruction);
5874 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5875 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005876 }
5877 break;
5878 }
5879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005881 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005882 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005883 if (value.IsFpuRegister()) {
5884 __ movss(address, value.AsFpuRegister<XmmRegister>());
5885 } else {
5886 DCHECK(value.IsConstant());
5887 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5888 __ movl(address, Immediate(v));
5889 }
5890 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005891 break;
5892 }
5893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005895 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005896 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005897 if (value.IsFpuRegister()) {
5898 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5899 } else {
5900 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005901 Address address_hi =
5902 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005903 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5904 __ movl(address, Immediate(Low32Bits(v)));
5905 codegen_->MaybeRecordImplicitNullCheck(instruction);
5906 __ movl(address_hi, Immediate(High32Bits(v)));
5907 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005908 break;
5909 }
5910
Aart Bik66c158e2018-01-31 12:55:04 -08005911 case DataType::Type::kUint32:
5912 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005914 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005915 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005916 }
5917}
5918
5919void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005920 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005921 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005922 if (!instruction->IsEmittedAtUseSite()) {
5923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5924 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005925}
5926
5927void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005928 if (instruction->IsEmittedAtUseSite()) {
5929 return;
5930 }
5931
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005932 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005933 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005934 Register obj = locations->InAt(0).AsRegister<Register>();
5935 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005937 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005938 // Mask out most significant bit in case the array is String's array of char.
5939 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005940 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005941 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005942}
5943
5944void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005945 RegisterSet caller_saves = RegisterSet::Empty();
5946 InvokeRuntimeCallingConvention calling_convention;
5947 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5948 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5949 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005950 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005951 HInstruction* length = instruction->InputAt(1);
5952 if (!length->IsEmittedAtUseSite()) {
5953 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5954 }
jessicahandojo4877b792016-09-08 19:49:13 -07005955 // Need register to see array's length.
5956 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5957 locations->AddTemp(Location::RequiresRegister());
5958 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005959}
5960
5961void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005962 const bool is_string_compressed_char_at =
5963 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005964 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005965 Location index_loc = locations->InAt(0);
5966 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005967 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005968 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005969
Mark Mendell99dbd682015-04-22 16:18:52 -04005970 if (length_loc.IsConstant()) {
5971 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5972 if (index_loc.IsConstant()) {
5973 // BCE will remove the bounds check if we are guarenteed to pass.
5974 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5975 if (index < 0 || index >= length) {
5976 codegen_->AddSlowPath(slow_path);
5977 __ jmp(slow_path->GetEntryLabel());
5978 } else {
5979 // Some optimization after BCE may have generated this, and we should not
5980 // generate a bounds check if it is a valid range.
5981 }
5982 return;
5983 }
5984
5985 // We have to reverse the jump condition because the length is the constant.
5986 Register index_reg = index_loc.AsRegister<Register>();
5987 __ cmpl(index_reg, Immediate(length));
5988 codegen_->AddSlowPath(slow_path);
5989 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005990 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005991 HInstruction* array_length = instruction->InputAt(1);
5992 if (array_length->IsEmittedAtUseSite()) {
5993 // Address the length field in the array.
5994 DCHECK(array_length->IsArrayLength());
5995 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5996 Location array_loc = array_length->GetLocations()->InAt(0);
5997 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005998 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005999 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6000 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006001 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6002 __ movl(length_reg, array_len);
6003 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006004 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006005 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006006 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006007 // Checking bounds for general case:
6008 // Array of char or string's array with feature compression off.
6009 if (index_loc.IsConstant()) {
6010 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6011 __ cmpl(array_len, Immediate(value));
6012 } else {
6013 __ cmpl(array_len, index_loc.AsRegister<Register>());
6014 }
6015 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006016 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006017 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006018 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006019 }
6020 codegen_->AddSlowPath(slow_path);
6021 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006023}
6024
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006025void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006026 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006027}
6028
6029void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006030 if (instruction->GetNext()->IsSuspendCheck() &&
6031 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6032 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6033 // The back edge will generate the suspend check.
6034 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6035 }
6036
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006037 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6038}
6039
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006040void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006041 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6042 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006043 // In suspend check slow path, usually there are no caller-save registers at all.
6044 // If SIMD instructions are present, however, we force spilling all live SIMD
6045 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006046 locations->SetCustomSlowPathCallerSaves(
6047 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006048}
6049
6050void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006051 HBasicBlock* block = instruction->GetBlock();
6052 if (block->GetLoopInformation() != nullptr) {
6053 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6054 // The back edge will generate the suspend check.
6055 return;
6056 }
6057 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6058 // The goto will generate the suspend check.
6059 return;
6060 }
6061 GenerateSuspendCheck(instruction, nullptr);
6062}
6063
6064void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6065 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006066 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006067 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6068 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006069 slow_path =
6070 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006071 instruction->SetSlowPath(slow_path);
6072 codegen_->AddSlowPath(slow_path);
6073 if (successor != nullptr) {
6074 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006075 }
6076 } else {
6077 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6078 }
6079
Andreas Gampe542451c2016-07-26 09:02:02 -07006080 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006081 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006082 if (successor == nullptr) {
6083 __ j(kNotEqual, slow_path->GetEntryLabel());
6084 __ Bind(slow_path->GetReturnLabel());
6085 } else {
6086 __ j(kEqual, codegen_->GetLabelOf(successor));
6087 __ jmp(slow_path->GetEntryLabel());
6088 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006089}
6090
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006091X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6092 return codegen_->GetAssembler();
6093}
6094
Aart Bikcfe50bb2017-12-12 14:54:12 -08006095void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006096 ScratchRegisterScope ensure_scratch(
6097 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6098 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6099 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006100
Aart Bikcfe50bb2017-12-12 14:54:12 -08006101 // Now that temp register is available (possibly spilled), move blocks of memory.
6102 for (int i = 0; i < number_of_words; i++) {
6103 __ movl(temp_reg, Address(ESP, src + stack_offset));
6104 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6105 stack_offset += kX86WordSize;
6106 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006107}
6108
6109void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006110 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006111 Location source = move->GetSource();
6112 Location destination = move->GetDestination();
6113
6114 if (source.IsRegister()) {
6115 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006116 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006117 } else if (destination.IsFpuRegister()) {
6118 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006119 } else {
6120 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006121 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006122 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006123 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006125 // Create stack space for 2 elements.
6126 __ subl(ESP, Immediate(2 * elem_size));
6127 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6128 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6129 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6130 // And remove the temporary stack space we allocated.
6131 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006132 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006133 if (destination.IsRegister()) {
6134 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6135 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006136 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006137 } else if (destination.IsRegisterPair()) {
6138 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6139 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6140 __ psrlq(src_reg, Immediate(32));
6141 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006142 } else if (destination.IsStackSlot()) {
6143 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006144 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006145 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006146 } else {
6147 DCHECK(destination.IsSIMDStackSlot());
6148 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006149 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006150 } else if (source.IsStackSlot()) {
6151 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006152 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006153 } else if (destination.IsFpuRegister()) {
6154 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006155 } else {
6156 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006157 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006158 }
6159 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006160 if (destination.IsRegisterPair()) {
6161 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6162 __ movl(destination.AsRegisterPairHigh<Register>(),
6163 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6164 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006165 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6166 } else {
6167 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006168 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006169 }
Aart Bik5576f372017-03-23 16:17:37 -07006170 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006171 if (destination.IsFpuRegister()) {
6172 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6173 } else {
6174 DCHECK(destination.IsSIMDStackSlot());
6175 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6176 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006177 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006178 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006179 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006180 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006181 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006182 if (value == 0) {
6183 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6184 } else {
6185 __ movl(destination.AsRegister<Register>(), Immediate(value));
6186 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006187 } else {
6188 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006189 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006190 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006191 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006192 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006193 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006194 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006195 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006196 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6197 if (value == 0) {
6198 // Easy handling of 0.0.
6199 __ xorps(dest, dest);
6200 } else {
6201 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006202 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6203 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6204 __ movl(temp, Immediate(value));
6205 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006206 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006207 } else {
6208 DCHECK(destination.IsStackSlot()) << destination;
6209 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6210 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006211 } else if (constant->IsLongConstant()) {
6212 int64_t value = constant->AsLongConstant()->GetValue();
6213 int32_t low_value = Low32Bits(value);
6214 int32_t high_value = High32Bits(value);
6215 Immediate low(low_value);
6216 Immediate high(high_value);
6217 if (destination.IsDoubleStackSlot()) {
6218 __ movl(Address(ESP, destination.GetStackIndex()), low);
6219 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6220 } else {
6221 __ movl(destination.AsRegisterPairLow<Register>(), low);
6222 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6223 }
6224 } else {
6225 DCHECK(constant->IsDoubleConstant());
6226 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006227 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006228 int32_t low_value = Low32Bits(value);
6229 int32_t high_value = High32Bits(value);
6230 Immediate low(low_value);
6231 Immediate high(high_value);
6232 if (destination.IsFpuRegister()) {
6233 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6234 if (value == 0) {
6235 // Easy handling of 0.0.
6236 __ xorpd(dest, dest);
6237 } else {
6238 __ pushl(high);
6239 __ pushl(low);
6240 __ movsd(dest, Address(ESP, 0));
6241 __ addl(ESP, Immediate(8));
6242 }
6243 } else {
6244 DCHECK(destination.IsDoubleStackSlot()) << destination;
6245 __ movl(Address(ESP, destination.GetStackIndex()), low);
6246 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6247 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006248 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006249 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006250 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006251 }
6252}
6253
Mark Mendella5c19ce2015-04-01 12:51:05 -04006254void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006255 Register suggested_scratch = reg == EAX ? EBX : EAX;
6256 ScratchRegisterScope ensure_scratch(
6257 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6258
6259 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6260 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6261 __ movl(Address(ESP, mem + stack_offset), reg);
6262 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006263}
6264
Mark Mendell7c8d0092015-01-26 11:21:33 -05006265void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006266 ScratchRegisterScope ensure_scratch(
6267 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6268
6269 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6270 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6271 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6272 __ movss(Address(ESP, mem + stack_offset), reg);
6273 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006274}
6275
Aart Bikcfe50bb2017-12-12 14:54:12 -08006276void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6277 size_t extra_slot = 4 * kX86WordSize;
6278 __ subl(ESP, Immediate(extra_slot));
6279 __ movups(Address(ESP, 0), XmmRegister(reg));
6280 ExchangeMemory(0, mem + extra_slot, 4);
6281 __ movups(XmmRegister(reg), Address(ESP, 0));
6282 __ addl(ESP, Immediate(extra_slot));
6283}
6284
6285void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006286 ScratchRegisterScope ensure_scratch1(
6287 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006288
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006289 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6290 ScratchRegisterScope ensure_scratch2(
6291 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006292
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006293 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6294 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006295
6296 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6297 for (int i = 0; i < number_of_words; i++) {
6298 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6299 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6300 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6301 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6302 stack_offset += kX86WordSize;
6303 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006304}
6305
6306void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006307 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006308 Location source = move->GetSource();
6309 Location destination = move->GetDestination();
6310
6311 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006312 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6313 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6314 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6315 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6316 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006317 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006318 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006319 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006320 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006321 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006322 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006323 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6324 // Use XOR Swap algorithm to avoid a temporary.
6325 DCHECK_NE(source.reg(), destination.reg());
6326 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6327 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6328 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6329 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6330 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6331 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6332 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006333 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6334 // Take advantage of the 16 bytes in the XMM register.
6335 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6336 Address stack(ESP, destination.GetStackIndex());
6337 // Load the double into the high doubleword.
6338 __ movhpd(reg, stack);
6339
6340 // Store the low double into the destination.
6341 __ movsd(stack, reg);
6342
6343 // Move the high double to the low double.
6344 __ psrldq(reg, Immediate(8));
6345 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6346 // Take advantage of the 16 bytes in the XMM register.
6347 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6348 Address stack(ESP, source.GetStackIndex());
6349 // Load the double into the high doubleword.
6350 __ movhpd(reg, stack);
6351
6352 // Store the low double into the destination.
6353 __ movsd(stack, reg);
6354
6355 // Move the high double to the low double.
6356 __ psrldq(reg, Immediate(8));
6357 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006358 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6359 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6360 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6361 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6362 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6363 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6364 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006365 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006366 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006367 }
6368}
6369
6370void ParallelMoveResolverX86::SpillScratch(int reg) {
6371 __ pushl(static_cast<Register>(reg));
6372}
6373
6374void ParallelMoveResolverX86::RestoreScratch(int reg) {
6375 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006376}
6377
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006378HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6379 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006380 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006381 case HLoadClass::LoadKind::kInvalid:
6382 LOG(FATAL) << "UNREACHABLE";
6383 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006384 case HLoadClass::LoadKind::kReferrersClass:
6385 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006387 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006388 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006389 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006391 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006392 DCHECK(Runtime::Current()->UseJitCompilation());
6393 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006394 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006395 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006396 break;
6397 }
6398 return desired_class_load_kind;
6399}
6400
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006401void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006402 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006403 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006404 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006405 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 cls,
6407 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006408 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006409 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006410 return;
6411 }
Vladimir Marko41559982017-01-06 14:04:23 +00006412 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006413
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006414 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6415 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006416 ? LocationSummary::kCallOnSlowPath
6417 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006418 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006419 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006420 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006421 }
6422
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006424 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006425 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006426 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006427 locations->SetInAt(0, Location::RequiresRegister());
6428 }
6429 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006430 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6431 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6432 // Rely on the type resolution and/or initialization to save everything.
6433 RegisterSet caller_saves = RegisterSet::Empty();
6434 InvokeRuntimeCallingConvention calling_convention;
6435 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6436 locations->SetCustomSlowPathCallerSaves(caller_saves);
6437 } else {
6438 // For non-Baker read barrier we have a temp-clobbering call.
6439 }
6440 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006441}
6442
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006443Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006444 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006445 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006446 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006447 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006448 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006449 PatchInfo<Label>* info = &jit_class_patches_.back();
6450 return &info->label;
6451}
6452
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006453// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6454// move.
6455void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006456 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006457 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006458 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006459 return;
6460 }
Vladimir Marko41559982017-01-06 14:04:23 +00006461 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006462
Vladimir Marko41559982017-01-06 14:04:23 +00006463 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006464 Location out_loc = locations->Out();
6465 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006467 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006468 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6469 ? kWithoutReadBarrier
6470 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006471 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006472 case HLoadClass::LoadKind::kReferrersClass: {
6473 DCHECK(!cls->CanCallRuntime());
6474 DCHECK(!cls->MustGenerateClinitCheck());
6475 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6476 Register current_method = locations->InAt(0).AsRegister<Register>();
6477 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006478 cls,
6479 out_loc,
6480 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006481 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006482 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006483 break;
6484 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006485 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006486 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006487 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006488 Register method_address = locations->InAt(0).AsRegister<Register>();
6489 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006490 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006491 break;
6492 }
6493 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006494 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006495 uint32_t address = dchecked_integral_cast<uint32_t>(
6496 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6497 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006498 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006499 break;
6500 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006501 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006502 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6503 Register method_address = locations->InAt(0).AsRegister<Register>();
6504 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006505 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6506 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006507 break;
6508 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006509 case HLoadClass::LoadKind::kBssEntry: {
6510 Register method_address = locations->InAt(0).AsRegister<Register>();
6511 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6512 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6513 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6514 generate_null_check = true;
6515 break;
6516 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006517 case HLoadClass::LoadKind::kJitTableAddress: {
6518 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6519 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006520 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006521 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006522 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006523 break;
6524 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006525 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006526 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006527 LOG(FATAL) << "UNREACHABLE";
6528 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006529 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006530
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006531 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6532 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006533 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006534 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6535 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006536
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006537 if (generate_null_check) {
6538 __ testl(out, out);
6539 __ j(kEqual, slow_path->GetEntryLabel());
6540 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006541
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006542 if (cls->MustGenerateClinitCheck()) {
6543 GenerateClassInitializationCheck(slow_path, out);
6544 } else {
6545 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006546 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006547 }
6548}
6549
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006550void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6551 InvokeRuntimeCallingConvention calling_convention;
6552 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6553 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6554}
6555
6556void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6557 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6558}
6559
Orion Hodson18259d72018-04-12 11:18:23 +01006560void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6561 InvokeRuntimeCallingConvention calling_convention;
6562 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6563 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6564}
6565
6566void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6567 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6568}
6569
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006570void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6571 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006572 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006573 locations->SetInAt(0, Location::RequiresRegister());
6574 if (check->HasUses()) {
6575 locations->SetOut(Location::SameAsFirstInput());
6576 }
6577}
6578
6579void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006580 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006581 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006582 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006583 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006584 GenerateClassInitializationCheck(slow_path,
6585 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006586}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006587
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006588void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006589 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006590 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6591 const size_t status_byte_offset =
6592 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6593 constexpr uint32_t shifted_initialized_value =
6594 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6595
6596 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006597 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006598 __ Bind(slow_path->GetExitLabel());
6599 // No need for memory fence, thanks to the X86 memory model.
6600}
6601
Vladimir Marko175e7862018-03-27 09:03:13 +00006602void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6603 Register temp) {
6604 uint32_t path_to_root = check->GetBitstringPathToRoot();
6605 uint32_t mask = check->GetBitstringMask();
6606 DCHECK(IsPowerOfTwo(mask + 1));
6607 size_t mask_bits = WhichPowerOf2(mask + 1);
6608
6609 if (mask_bits == 16u) {
6610 // Compare the bitstring in memory.
6611 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6612 } else {
6613 // /* uint32_t */ temp = temp->status_
6614 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6615 // Compare the bitstring bits using SUB.
6616 __ subl(temp, Immediate(path_to_root));
6617 // Shift out bits that do not contribute to the comparison.
6618 __ shll(temp, Immediate(32u - mask_bits));
6619 }
6620}
6621
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006622HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6623 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006625 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006626 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006627 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006628 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006629 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006630 case HLoadString::LoadKind::kJitTableAddress:
6631 DCHECK(Runtime::Current()->UseJitCompilation());
6632 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006633 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006634 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006635 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006636 }
6637 return desired_string_load_kind;
6638}
6639
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006640void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006641 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006642 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006643 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006644 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006645 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006646 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006647 locations->SetInAt(0, Location::RequiresRegister());
6648 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006649 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006650 locations->SetOut(Location::RegisterLocation(EAX));
6651 } else {
6652 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006653 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6654 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006655 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006656 RegisterSet caller_saves = RegisterSet::Empty();
6657 InvokeRuntimeCallingConvention calling_convention;
6658 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6659 locations->SetCustomSlowPathCallerSaves(caller_saves);
6660 } else {
6661 // For non-Baker read barrier we have a temp-clobbering call.
6662 }
6663 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006664 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006665}
6666
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006667Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006668 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006669 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006670 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006671 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006672 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006673 PatchInfo<Label>* info = &jit_string_patches_.back();
6674 return &info->label;
6675}
6676
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006677// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6678// move.
6679void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006680 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 Location out_loc = locations->Out();
6682 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006684 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006685 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006686 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006687 Register method_address = locations->InAt(0).AsRegister<Register>();
6688 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006689 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006690 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006691 }
6692 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006693 uint32_t address = dchecked_integral_cast<uint32_t>(
6694 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6695 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006696 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006697 return;
6698 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006699 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006700 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6701 Register method_address = locations->InAt(0).AsRegister<Register>();
6702 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006703 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6704 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006705 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006706 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006707 case HLoadString::LoadKind::kBssEntry: {
6708 Register method_address = locations->InAt(0).AsRegister<Register>();
6709 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6710 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006711 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006712 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006713 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006714 codegen_->AddSlowPath(slow_path);
6715 __ testl(out, out);
6716 __ j(kEqual, slow_path->GetEntryLabel());
6717 __ Bind(slow_path->GetExitLabel());
6718 return;
6719 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006720 case HLoadString::LoadKind::kJitTableAddress: {
6721 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6722 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006723 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006724 // /* GcRoot<mirror::String> */ out = *address
6725 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6726 return;
6727 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006728 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006729 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006730 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006731
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006732 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006733 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006734 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006735 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006736 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6737 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006738}
6739
David Brazdilcb1c0552015-08-04 16:22:25 +01006740static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006741 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006742}
6743
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006744void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6745 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006746 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006747 locations->SetOut(Location::RequiresRegister());
6748}
6749
6750void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006751 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6752}
6753
6754void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006755 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006756}
6757
6758void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6759 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006760}
6761
6762void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006763 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6764 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006765 InvokeRuntimeCallingConvention calling_convention;
6766 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6767}
6768
6769void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006770 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006771 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006772}
6773
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006774// Temp is used for read barrier.
6775static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6776 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006777 !kUseBakerReadBarrier &&
6778 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006779 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006780 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6781 return 1;
6782 }
6783 return 0;
6784}
6785
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006786// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6787// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006788// The other checks have one temp for loading the object's class.
6789static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006790 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006791 return 2;
6792 }
6793 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006794}
6795
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006796void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006797 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006798 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006799 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006800 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006801 case TypeCheckKind::kExactCheck:
6802 case TypeCheckKind::kAbstractClassCheck:
6803 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006804 case TypeCheckKind::kArrayObjectCheck: {
6805 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6806 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6807 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006808 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006809 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006810 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006811 case TypeCheckKind::kUnresolvedCheck:
6812 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 call_kind = LocationSummary::kCallOnSlowPath;
6814 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006815 case TypeCheckKind::kBitstringCheck:
6816 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006817 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006818
Vladimir Markoca6fff82017-10-03 14:49:14 +01006819 LocationSummary* locations =
6820 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006821 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006822 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006823 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006824 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006825 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6826 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6827 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6828 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6829 } else {
6830 locations->SetInAt(1, Location::Any());
6831 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006832 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6833 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006834 // When read barriers are enabled, we need a temporary register for some cases.
6835 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006836}
6837
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006838void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006839 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006840 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006841 Location obj_loc = locations->InAt(0);
6842 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006843 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844 Location out_loc = locations->Out();
6845 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006846 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6847 DCHECK_LE(num_temps, 1u);
6848 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006849 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006850 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6851 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6852 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006853 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006854 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006855
6856 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006857 // Avoid null check if we know obj is not null.
6858 if (instruction->MustDoNullCheck()) {
6859 __ testl(obj, obj);
6860 __ j(kEqual, &zero);
6861 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006862
Roland Levillain7c1559a2015-12-15 10:55:36 +00006863 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006864 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006865 ReadBarrierOption read_barrier_option =
6866 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006867 // /* HeapReference<Class> */ out = obj->klass_
6868 GenerateReferenceLoadTwoRegisters(instruction,
6869 out_loc,
6870 obj_loc,
6871 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006872 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006873 if (cls.IsRegister()) {
6874 __ cmpl(out, cls.AsRegister<Register>());
6875 } else {
6876 DCHECK(cls.IsStackSlot()) << cls;
6877 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6878 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006879
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006880 // Classes must be equal for the instanceof to succeed.
6881 __ j(kNotEqual, &zero);
6882 __ movl(out, Immediate(1));
6883 __ jmp(&done);
6884 break;
6885 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006886
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006887 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006888 ReadBarrierOption read_barrier_option =
6889 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006890 // /* HeapReference<Class> */ out = obj->klass_
6891 GenerateReferenceLoadTwoRegisters(instruction,
6892 out_loc,
6893 obj_loc,
6894 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006895 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006896 // If the class is abstract, we eagerly fetch the super class of the
6897 // object to avoid doing a comparison we know will fail.
6898 NearLabel loop;
6899 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006900 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006901 GenerateReferenceLoadOneRegister(instruction,
6902 out_loc,
6903 super_offset,
6904 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006905 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006906 __ testl(out, out);
6907 // If `out` is null, we use it for the result, and jump to `done`.
6908 __ j(kEqual, &done);
6909 if (cls.IsRegister()) {
6910 __ cmpl(out, cls.AsRegister<Register>());
6911 } else {
6912 DCHECK(cls.IsStackSlot()) << cls;
6913 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6914 }
6915 __ j(kNotEqual, &loop);
6916 __ movl(out, Immediate(1));
6917 if (zero.IsLinked()) {
6918 __ jmp(&done);
6919 }
6920 break;
6921 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006922
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006923 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006924 ReadBarrierOption read_barrier_option =
6925 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006926 // /* HeapReference<Class> */ out = obj->klass_
6927 GenerateReferenceLoadTwoRegisters(instruction,
6928 out_loc,
6929 obj_loc,
6930 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006931 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006932 // Walk over the class hierarchy to find a match.
6933 NearLabel loop, success;
6934 __ Bind(&loop);
6935 if (cls.IsRegister()) {
6936 __ cmpl(out, cls.AsRegister<Register>());
6937 } else {
6938 DCHECK(cls.IsStackSlot()) << cls;
6939 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6940 }
6941 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006942 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006943 GenerateReferenceLoadOneRegister(instruction,
6944 out_loc,
6945 super_offset,
6946 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006947 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006948 __ testl(out, out);
6949 __ j(kNotEqual, &loop);
6950 // If `out` is null, we use it for the result, and jump to `done`.
6951 __ jmp(&done);
6952 __ Bind(&success);
6953 __ movl(out, Immediate(1));
6954 if (zero.IsLinked()) {
6955 __ jmp(&done);
6956 }
6957 break;
6958 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006959
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006960 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006961 ReadBarrierOption read_barrier_option =
6962 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006963 // /* HeapReference<Class> */ out = obj->klass_
6964 GenerateReferenceLoadTwoRegisters(instruction,
6965 out_loc,
6966 obj_loc,
6967 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006968 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006969 // Do an exact check.
6970 NearLabel exact_check;
6971 if (cls.IsRegister()) {
6972 __ cmpl(out, cls.AsRegister<Register>());
6973 } else {
6974 DCHECK(cls.IsStackSlot()) << cls;
6975 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6976 }
6977 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006978 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006979 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006980 GenerateReferenceLoadOneRegister(instruction,
6981 out_loc,
6982 component_offset,
6983 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006984 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006985 __ testl(out, out);
6986 // If `out` is null, we use it for the result, and jump to `done`.
6987 __ j(kEqual, &done);
6988 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6989 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006990 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006991 __ movl(out, Immediate(1));
6992 __ jmp(&done);
6993 break;
6994 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006995
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006996 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006997 // No read barrier since the slow path will retry upon failure.
6998 // /* HeapReference<Class> */ out = obj->klass_
6999 GenerateReferenceLoadTwoRegisters(instruction,
7000 out_loc,
7001 obj_loc,
7002 class_offset,
7003 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007004 if (cls.IsRegister()) {
7005 __ cmpl(out, cls.AsRegister<Register>());
7006 } else {
7007 DCHECK(cls.IsStackSlot()) << cls;
7008 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7009 }
7010 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007011 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7012 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007013 codegen_->AddSlowPath(slow_path);
7014 __ j(kNotEqual, slow_path->GetEntryLabel());
7015 __ movl(out, Immediate(1));
7016 if (zero.IsLinked()) {
7017 __ jmp(&done);
7018 }
7019 break;
7020 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007021
Calin Juravle98893e12015-10-02 21:05:03 +01007022 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007023 case TypeCheckKind::kInterfaceCheck: {
7024 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007025 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007026 // cases.
7027 //
7028 // We cannot directly call the InstanceofNonTrivial runtime
7029 // entry point without resorting to a type checking slow path
7030 // here (i.e. by calling InvokeRuntime directly), as it would
7031 // require to assign fixed registers for the inputs of this
7032 // HInstanceOf instruction (following the runtime calling
7033 // convention), which might be cluttered by the potential first
7034 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007035 //
7036 // TODO: Introduce a new runtime entry point taking the object
7037 // to test (instead of its class) as argument, and let it deal
7038 // with the read barrier issues. This will let us refactor this
7039 // case of the `switch` code as it was previously (with a direct
7040 // call to the runtime not using a type checking slow path).
7041 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007042 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007043 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7044 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007045 codegen_->AddSlowPath(slow_path);
7046 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007047 if (zero.IsLinked()) {
7048 __ jmp(&done);
7049 }
7050 break;
7051 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007052
7053 case TypeCheckKind::kBitstringCheck: {
7054 // /* HeapReference<Class> */ temp = obj->klass_
7055 GenerateReferenceLoadTwoRegisters(instruction,
7056 out_loc,
7057 obj_loc,
7058 class_offset,
7059 kWithoutReadBarrier);
7060
7061 GenerateBitstringTypeCheckCompare(instruction, out);
7062 __ j(kNotEqual, &zero);
7063 __ movl(out, Immediate(1));
7064 __ jmp(&done);
7065 break;
7066 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007067 }
7068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007069 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007070 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007071 __ xorl(out, out);
7072 }
7073
7074 if (done.IsLinked()) {
7075 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007076 }
7077
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007078 if (slow_path != nullptr) {
7079 __ Bind(slow_path->GetExitLabel());
7080 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007081}
7082
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007083void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007085 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007086 LocationSummary* locations =
7087 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007088 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007089 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7090 // Require a register for the interface check since there is a loop that compares the class to
7091 // a memory address.
7092 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007093 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7094 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7095 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7096 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007097 } else {
7098 locations->SetInAt(1, Location::Any());
7099 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007100 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007101 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7102}
7103
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007104void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007105 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007106 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007107 Location obj_loc = locations->InAt(0);
7108 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007109 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007110 Location temp_loc = locations->GetTemp(0);
7111 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007112 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7113 DCHECK_GE(num_temps, 1u);
7114 DCHECK_LE(num_temps, 2u);
7115 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7116 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7117 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7118 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7119 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7120 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7121 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7122 const uint32_t object_array_data_offset =
7123 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007124
Vladimir Marko87584542017-12-12 17:47:52 +00007125 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007126 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007127 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7128 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007129 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130
Roland Levillain0d5a2812015-11-13 10:07:31 +00007131 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007132 // Avoid null check if we know obj is not null.
7133 if (instruction->MustDoNullCheck()) {
7134 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007135 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007136 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007137
Roland Levillain0d5a2812015-11-13 10:07:31 +00007138 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007139 case TypeCheckKind::kExactCheck:
7140 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007141 // /* HeapReference<Class> */ temp = obj->klass_
7142 GenerateReferenceLoadTwoRegisters(instruction,
7143 temp_loc,
7144 obj_loc,
7145 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007146 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007147
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007148 if (cls.IsRegister()) {
7149 __ cmpl(temp, cls.AsRegister<Register>());
7150 } else {
7151 DCHECK(cls.IsStackSlot()) << cls;
7152 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7153 }
7154 // Jump to slow path for throwing the exception or doing a
7155 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007156 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007157 break;
7158 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007159
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007160 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007161 // /* HeapReference<Class> */ temp = obj->klass_
7162 GenerateReferenceLoadTwoRegisters(instruction,
7163 temp_loc,
7164 obj_loc,
7165 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007166 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007168 // If the class is abstract, we eagerly fetch the super class of the
7169 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007170 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007171 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007172 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007173 GenerateReferenceLoadOneRegister(instruction,
7174 temp_loc,
7175 super_offset,
7176 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007177 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007178
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007179 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7180 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007181 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007182 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007183
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007184 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007185 if (cls.IsRegister()) {
7186 __ cmpl(temp, cls.AsRegister<Register>());
7187 } else {
7188 DCHECK(cls.IsStackSlot()) << cls;
7189 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7190 }
7191 __ j(kNotEqual, &loop);
7192 break;
7193 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007195 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007196 // /* HeapReference<Class> */ temp = obj->klass_
7197 GenerateReferenceLoadTwoRegisters(instruction,
7198 temp_loc,
7199 obj_loc,
7200 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007201 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007202
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007203 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007204 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205 __ Bind(&loop);
7206 if (cls.IsRegister()) {
7207 __ cmpl(temp, cls.AsRegister<Register>());
7208 } else {
7209 DCHECK(cls.IsStackSlot()) << cls;
7210 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7211 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007212 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007213
Roland Levillain0d5a2812015-11-13 10:07:31 +00007214 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007215 GenerateReferenceLoadOneRegister(instruction,
7216 temp_loc,
7217 super_offset,
7218 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007219 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007220
7221 // If the class reference currently in `temp` is not null, jump
7222 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007223 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007224 __ j(kNotZero, &loop);
7225 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007226 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007227 break;
7228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007230 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007231 // /* HeapReference<Class> */ temp = obj->klass_
7232 GenerateReferenceLoadTwoRegisters(instruction,
7233 temp_loc,
7234 obj_loc,
7235 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007236 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007237
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007238 // Do an exact check.
7239 if (cls.IsRegister()) {
7240 __ cmpl(temp, cls.AsRegister<Register>());
7241 } else {
7242 DCHECK(cls.IsStackSlot()) << cls;
7243 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7244 }
7245 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007246
7247 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007248 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007249 GenerateReferenceLoadOneRegister(instruction,
7250 temp_loc,
7251 component_offset,
7252 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007253 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007254
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007255 // If the component type is null (i.e. the object not an array), jump to the slow path to
7256 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007257 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007258 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007259
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007260 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007261 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007262 break;
7263 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007264
Calin Juravle98893e12015-10-02 21:05:03 +01007265 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007266 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007267 // We cannot directly call the CheckCast runtime entry point
7268 // without resorting to a type checking slow path here (i.e. by
7269 // calling InvokeRuntime directly), as it would require to
7270 // assign fixed registers for the inputs of this HInstanceOf
7271 // instruction (following the runtime calling convention), which
7272 // might be cluttered by the potential first read barrier
7273 // emission at the beginning of this method.
7274 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007275 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007276
7277 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007278 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7279 // We can not get false positives by doing this.
7280 // /* HeapReference<Class> */ temp = obj->klass_
7281 GenerateReferenceLoadTwoRegisters(instruction,
7282 temp_loc,
7283 obj_loc,
7284 class_offset,
7285 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007286
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007287 // /* HeapReference<Class> */ temp = temp->iftable_
7288 GenerateReferenceLoadTwoRegisters(instruction,
7289 temp_loc,
7290 temp_loc,
7291 iftable_offset,
7292 kWithoutReadBarrier);
7293 // Iftable is never null.
7294 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7295 // Maybe poison the `cls` for direct comparison with memory.
7296 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7297 // Loop through the iftable and check if any class matches.
7298 NearLabel start_loop;
7299 __ Bind(&start_loop);
7300 // Need to subtract first to handle the empty array case.
7301 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7302 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7303 // Go to next interface if the classes do not match.
7304 __ cmpl(cls.AsRegister<Register>(),
7305 CodeGeneratorX86::ArrayAddress(temp,
7306 maybe_temp2_loc,
7307 TIMES_4,
7308 object_array_data_offset));
7309 __ j(kNotEqual, &start_loop);
7310 // If `cls` was poisoned above, unpoison it.
7311 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007312 break;
7313 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007314
7315 case TypeCheckKind::kBitstringCheck: {
7316 // /* HeapReference<Class> */ temp = obj->klass_
7317 GenerateReferenceLoadTwoRegisters(instruction,
7318 temp_loc,
7319 obj_loc,
7320 class_offset,
7321 kWithoutReadBarrier);
7322
7323 GenerateBitstringTypeCheckCompare(instruction, temp);
7324 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7325 break;
7326 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007327 }
7328 __ Bind(&done);
7329
Roland Levillain0d5a2812015-11-13 10:07:31 +00007330 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007331}
7332
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007333void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007334 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7335 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007336 InvokeRuntimeCallingConvention calling_convention;
7337 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7338}
7339
7340void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007341 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7342 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007343 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007344 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007345 if (instruction->IsEnter()) {
7346 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7347 } else {
7348 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7349 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007350}
7351
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007352void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7353void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7354void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7355
7356void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7357 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007358 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007359 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7360 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007361 locations->SetInAt(0, Location::RequiresRegister());
7362 locations->SetInAt(1, Location::Any());
7363 locations->SetOut(Location::SameAsFirstInput());
7364}
7365
7366void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7367 HandleBitwiseOperation(instruction);
7368}
7369
7370void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7371 HandleBitwiseOperation(instruction);
7372}
7373
7374void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7375 HandleBitwiseOperation(instruction);
7376}
7377
7378void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7379 LocationSummary* locations = instruction->GetLocations();
7380 Location first = locations->InAt(0);
7381 Location second = locations->InAt(1);
7382 DCHECK(first.Equals(locations->Out()));
7383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007384 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007385 if (second.IsRegister()) {
7386 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007387 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007388 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007389 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007390 } else {
7391 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007392 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007393 }
7394 } else if (second.IsConstant()) {
7395 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007396 __ andl(first.AsRegister<Register>(),
7397 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007398 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007399 __ orl(first.AsRegister<Register>(),
7400 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007401 } else {
7402 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007403 __ xorl(first.AsRegister<Register>(),
7404 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007405 }
7406 } else {
7407 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007408 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007409 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007410 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007411 } else {
7412 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007413 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007414 }
7415 }
7416 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007417 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007418 if (second.IsRegisterPair()) {
7419 if (instruction->IsAnd()) {
7420 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7421 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7422 } else if (instruction->IsOr()) {
7423 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7424 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7425 } else {
7426 DCHECK(instruction->IsXor());
7427 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7428 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7429 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007430 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007431 if (instruction->IsAnd()) {
7432 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7433 __ andl(first.AsRegisterPairHigh<Register>(),
7434 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7435 } else if (instruction->IsOr()) {
7436 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7437 __ orl(first.AsRegisterPairHigh<Register>(),
7438 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7439 } else {
7440 DCHECK(instruction->IsXor());
7441 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7442 __ xorl(first.AsRegisterPairHigh<Register>(),
7443 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7444 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007445 } else {
7446 DCHECK(second.IsConstant()) << second;
7447 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007448 int32_t low_value = Low32Bits(value);
7449 int32_t high_value = High32Bits(value);
7450 Immediate low(low_value);
7451 Immediate high(high_value);
7452 Register first_low = first.AsRegisterPairLow<Register>();
7453 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007454 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007455 if (low_value == 0) {
7456 __ xorl(first_low, first_low);
7457 } else if (low_value != -1) {
7458 __ andl(first_low, low);
7459 }
7460 if (high_value == 0) {
7461 __ xorl(first_high, first_high);
7462 } else if (high_value != -1) {
7463 __ andl(first_high, high);
7464 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007465 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007466 if (low_value != 0) {
7467 __ orl(first_low, low);
7468 }
7469 if (high_value != 0) {
7470 __ orl(first_high, high);
7471 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007472 } else {
7473 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007474 if (low_value != 0) {
7475 __ xorl(first_low, low);
7476 }
7477 if (high_value != 0) {
7478 __ xorl(first_high, high);
7479 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007480 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007481 }
7482 }
7483}
7484
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007485void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7486 HInstruction* instruction,
7487 Location out,
7488 uint32_t offset,
7489 Location maybe_temp,
7490 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007491 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007492 if (read_barrier_option == kWithReadBarrier) {
7493 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007494 if (kUseBakerReadBarrier) {
7495 // Load with fast path based Baker's read barrier.
7496 // /* HeapReference<Object> */ out = *(out + offset)
7497 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007498 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007499 } else {
7500 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007501 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007502 // in the following move operation, as we will need it for the
7503 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007504 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007505 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007506 // /* HeapReference<Object> */ out = *(out + offset)
7507 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007508 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007509 }
7510 } else {
7511 // Plain load with no read barrier.
7512 // /* HeapReference<Object> */ out = *(out + offset)
7513 __ movl(out_reg, Address(out_reg, offset));
7514 __ MaybeUnpoisonHeapReference(out_reg);
7515 }
7516}
7517
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007518void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7519 HInstruction* instruction,
7520 Location out,
7521 Location obj,
7522 uint32_t offset,
7523 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007524 Register out_reg = out.AsRegister<Register>();
7525 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007526 if (read_barrier_option == kWithReadBarrier) {
7527 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007528 if (kUseBakerReadBarrier) {
7529 // Load with fast path based Baker's read barrier.
7530 // /* HeapReference<Object> */ out = *(obj + offset)
7531 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007532 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007533 } else {
7534 // Load with slow path based read barrier.
7535 // /* HeapReference<Object> */ out = *(obj + offset)
7536 __ movl(out_reg, Address(obj_reg, offset));
7537 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7538 }
7539 } else {
7540 // Plain load with no read barrier.
7541 // /* HeapReference<Object> */ out = *(obj + offset)
7542 __ movl(out_reg, Address(obj_reg, offset));
7543 __ MaybeUnpoisonHeapReference(out_reg);
7544 }
7545}
7546
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007547void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7548 HInstruction* instruction,
7549 Location root,
7550 const Address& address,
7551 Label* fixup_label,
7552 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007553 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007554 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007555 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007556 if (kUseBakerReadBarrier) {
7557 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7558 // Baker's read barrier are used:
7559 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007560 // root = obj.field;
7561 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7562 // if (temp != null) {
7563 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007564 // }
7565
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007566 // /* GcRoot<mirror::Object> */ root = *address
7567 __ movl(root_reg, address);
7568 if (fixup_label != nullptr) {
7569 __ Bind(fixup_label);
7570 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007571 static_assert(
7572 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7573 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7574 "have different sizes.");
7575 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7576 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7577 "have different sizes.");
7578
Vladimir Marko953437b2016-08-24 08:30:46 +00007579 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007580 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007581 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007582 codegen_->AddSlowPath(slow_path);
7583
Roland Levillaind966ce72017-02-09 16:20:14 +00007584 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7585 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007586 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007587 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7588 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007589 __ j(kNotEqual, slow_path->GetEntryLabel());
7590 __ Bind(slow_path->GetExitLabel());
7591 } else {
7592 // GC root loaded through a slow path for read barriers other
7593 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007594 // /* GcRoot<mirror::Object>* */ root = address
7595 __ leal(root_reg, address);
7596 if (fixup_label != nullptr) {
7597 __ Bind(fixup_label);
7598 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007599 // /* mirror::Object* */ root = root->Read()
7600 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7601 }
7602 } else {
7603 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007604 // /* GcRoot<mirror::Object> */ root = *address
7605 __ movl(root_reg, address);
7606 if (fixup_label != nullptr) {
7607 __ Bind(fixup_label);
7608 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007609 // Note that GC roots are not affected by heap poisoning, thus we
7610 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007611 }
7612}
7613
7614void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7615 Location ref,
7616 Register obj,
7617 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007618 bool needs_null_check) {
7619 DCHECK(kEmitCompilerReadBarrier);
7620 DCHECK(kUseBakerReadBarrier);
7621
7622 // /* HeapReference<Object> */ ref = *(obj + offset)
7623 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007624 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007625}
7626
7627void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7628 Location ref,
7629 Register obj,
7630 uint32_t data_offset,
7631 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007632 bool needs_null_check) {
7633 DCHECK(kEmitCompilerReadBarrier);
7634 DCHECK(kUseBakerReadBarrier);
7635
Roland Levillain3d312422016-06-23 13:53:42 +01007636 static_assert(
7637 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7638 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007639 // /* HeapReference<Object> */ ref =
7640 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007641 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007642 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007643}
7644
7645void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7646 Location ref,
7647 Register obj,
7648 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007649 bool needs_null_check,
7650 bool always_update_field,
7651 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007652 DCHECK(kEmitCompilerReadBarrier);
7653 DCHECK(kUseBakerReadBarrier);
7654
7655 // In slow path based read barriers, the read barrier call is
7656 // inserted after the original load. However, in fast path based
7657 // Baker's read barriers, we need to perform the load of
7658 // mirror::Object::monitor_ *before* the original reference load.
7659 // This load-load ordering is required by the read barrier.
7660 // The fast path/slow path (for Baker's algorithm) should look like:
7661 //
7662 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7663 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7664 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007665 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007666 // if (is_gray) {
7667 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7668 // }
7669 //
7670 // Note: the original implementation in ReadBarrier::Barrier is
7671 // slightly more complex as:
7672 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007673 // the high-bits of rb_state, which are expected to be all zeroes
7674 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7675 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007676 // - it performs additional checks that we do not do here for
7677 // performance reasons.
7678
7679 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007680 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7681
Vladimir Marko953437b2016-08-24 08:30:46 +00007682 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007683 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7684 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007685 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7686 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7687 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7688
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007689 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007690 // ref = ReadBarrier::Mark(ref);
7691 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7692 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007693 if (needs_null_check) {
7694 MaybeRecordImplicitNullCheck(instruction);
7695 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007696
7697 // Load fence to prevent load-load reordering.
7698 // Note that this is a no-op, thanks to the x86 memory model.
7699 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7700
7701 // The actual reference load.
7702 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007703 __ movl(ref_reg, src); // Flags are unaffected.
7704
7705 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7706 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007707 SlowPathCode* slow_path;
7708 if (always_update_field) {
7709 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007710 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007711 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7712 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007713 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007714 instruction, ref, /* unpoison_ref_before_marking */ true);
7715 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007716 AddSlowPath(slow_path);
7717
7718 // We have done the "if" of the gray bit check above, now branch based on the flags.
7719 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007720
7721 // Object* ref = ref_addr->AsMirrorPtr()
7722 __ MaybeUnpoisonHeapReference(ref_reg);
7723
Roland Levillain7c1559a2015-12-15 10:55:36 +00007724 __ Bind(slow_path->GetExitLabel());
7725}
7726
7727void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7728 Location out,
7729 Location ref,
7730 Location obj,
7731 uint32_t offset,
7732 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007733 DCHECK(kEmitCompilerReadBarrier);
7734
Roland Levillain7c1559a2015-12-15 10:55:36 +00007735 // Insert a slow path based read barrier *after* the reference load.
7736 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007737 // If heap poisoning is enabled, the unpoisoning of the loaded
7738 // reference will be carried out by the runtime within the slow
7739 // path.
7740 //
7741 // Note that `ref` currently does not get unpoisoned (when heap
7742 // poisoning is enabled), which is alright as the `ref` argument is
7743 // not used by the artReadBarrierSlow entry point.
7744 //
7745 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007746 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007747 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7748 AddSlowPath(slow_path);
7749
Roland Levillain0d5a2812015-11-13 10:07:31 +00007750 __ jmp(slow_path->GetEntryLabel());
7751 __ Bind(slow_path->GetExitLabel());
7752}
7753
Roland Levillain7c1559a2015-12-15 10:55:36 +00007754void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7755 Location out,
7756 Location ref,
7757 Location obj,
7758 uint32_t offset,
7759 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007760 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007761 // Baker's read barriers shall be handled by the fast path
7762 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7763 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007764 // If heap poisoning is enabled, unpoisoning will be taken care of
7765 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007766 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007767 } else if (kPoisonHeapReferences) {
7768 __ UnpoisonHeapReference(out.AsRegister<Register>());
7769 }
7770}
7771
Roland Levillain7c1559a2015-12-15 10:55:36 +00007772void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7773 Location out,
7774 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007775 DCHECK(kEmitCompilerReadBarrier);
7776
Roland Levillain7c1559a2015-12-15 10:55:36 +00007777 // Insert a slow path based read barrier *after* the GC root load.
7778 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007779 // Note that GC roots are not affected by heap poisoning, so we do
7780 // not need to do anything special for this here.
7781 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007782 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007783 AddSlowPath(slow_path);
7784
Roland Levillain0d5a2812015-11-13 10:07:31 +00007785 __ jmp(slow_path->GetEntryLabel());
7786 __ Bind(slow_path->GetExitLabel());
7787}
7788
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007789void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007790 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007791 LOG(FATAL) << "Unreachable";
7792}
7793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007794void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007795 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007796 LOG(FATAL) << "Unreachable";
7797}
7798
Mark Mendellfe57faa2015-09-18 09:26:15 -04007799// Simple implementation of packed switch - generate cascaded compare/jumps.
7800void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7801 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007802 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007803 locations->SetInAt(0, Location::RequiresRegister());
7804}
7805
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007806void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7807 int32_t lower_bound,
7808 uint32_t num_entries,
7809 HBasicBlock* switch_block,
7810 HBasicBlock* default_block) {
7811 // Figure out the correct compare values and jump conditions.
7812 // Handle the first compare/branch as a special case because it might
7813 // jump to the default case.
7814 DCHECK_GT(num_entries, 2u);
7815 Condition first_condition;
7816 uint32_t index;
7817 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7818 if (lower_bound != 0) {
7819 first_condition = kLess;
7820 __ cmpl(value_reg, Immediate(lower_bound));
7821 __ j(first_condition, codegen_->GetLabelOf(default_block));
7822 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007823
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007824 index = 1;
7825 } else {
7826 // Handle all the compare/jumps below.
7827 first_condition = kBelow;
7828 index = 0;
7829 }
7830
7831 // Handle the rest of the compare/jumps.
7832 for (; index + 1 < num_entries; index += 2) {
7833 int32_t compare_to_value = lower_bound + index + 1;
7834 __ cmpl(value_reg, Immediate(compare_to_value));
7835 // Jump to successors[index] if value < case_value[index].
7836 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7837 // Jump to successors[index + 1] if value == case_value[index + 1].
7838 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7839 }
7840
7841 if (index != num_entries) {
7842 // There are an odd number of entries. Handle the last one.
7843 DCHECK_EQ(index + 1, num_entries);
7844 __ cmpl(value_reg, Immediate(lower_bound + index));
7845 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007846 }
7847
7848 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007849 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7850 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007851 }
7852}
7853
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007854void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7855 int32_t lower_bound = switch_instr->GetStartValue();
7856 uint32_t num_entries = switch_instr->GetNumEntries();
7857 LocationSummary* locations = switch_instr->GetLocations();
7858 Register value_reg = locations->InAt(0).AsRegister<Register>();
7859
7860 GenPackedSwitchWithCompares(value_reg,
7861 lower_bound,
7862 num_entries,
7863 switch_instr->GetBlock(),
7864 switch_instr->GetDefaultBlock());
7865}
7866
Mark Mendell805b3b52015-09-18 14:10:29 -04007867void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7868 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007869 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007870 locations->SetInAt(0, Location::RequiresRegister());
7871
7872 // Constant area pointer.
7873 locations->SetInAt(1, Location::RequiresRegister());
7874
7875 // And the temporary we need.
7876 locations->AddTemp(Location::RequiresRegister());
7877}
7878
7879void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7880 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007881 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007882 LocationSummary* locations = switch_instr->GetLocations();
7883 Register value_reg = locations->InAt(0).AsRegister<Register>();
7884 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7885
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007886 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7887 GenPackedSwitchWithCompares(value_reg,
7888 lower_bound,
7889 num_entries,
7890 switch_instr->GetBlock(),
7891 default_block);
7892 return;
7893 }
7894
Mark Mendell805b3b52015-09-18 14:10:29 -04007895 // Optimizing has a jump area.
7896 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7897 Register constant_area = locations->InAt(1).AsRegister<Register>();
7898
7899 // Remove the bias, if needed.
7900 if (lower_bound != 0) {
7901 __ leal(temp_reg, Address(value_reg, -lower_bound));
7902 value_reg = temp_reg;
7903 }
7904
7905 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007906 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007907 __ cmpl(value_reg, Immediate(num_entries - 1));
7908 __ j(kAbove, codegen_->GetLabelOf(default_block));
7909
7910 // We are in the range of the table.
7911 // Load (target-constant_area) from the jump table, indexing by the value.
7912 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7913
7914 // Compute the actual target address by adding in constant_area.
7915 __ addl(temp_reg, constant_area);
7916
7917 // And jump.
7918 __ jmp(temp_reg);
7919}
7920
Mark Mendell0616ae02015-04-17 12:49:27 -04007921void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7922 HX86ComputeBaseMethodAddress* insn) {
7923 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007924 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007925 locations->SetOut(Location::RequiresRegister());
7926}
7927
7928void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7929 HX86ComputeBaseMethodAddress* insn) {
7930 LocationSummary* locations = insn->GetLocations();
7931 Register reg = locations->Out().AsRegister<Register>();
7932
7933 // Generate call to next instruction.
7934 Label next_instruction;
7935 __ call(&next_instruction);
7936 __ Bind(&next_instruction);
7937
7938 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007939 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007940
7941 // Grab the return address off the stack.
7942 __ popl(reg);
7943}
7944
7945void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7946 HX86LoadFromConstantTable* insn) {
7947 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007948 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007949
7950 locations->SetInAt(0, Location::RequiresRegister());
7951 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7952
7953 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007954 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007955 return;
7956 }
7957
7958 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007959 case DataType::Type::kFloat32:
7960 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007961 locations->SetOut(Location::RequiresFpuRegister());
7962 break;
7963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007964 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007965 locations->SetOut(Location::RequiresRegister());
7966 break;
7967
7968 default:
7969 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7970 }
7971}
7972
7973void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007974 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007975 return;
7976 }
7977
7978 LocationSummary* locations = insn->GetLocations();
7979 Location out = locations->Out();
7980 Register const_area = locations->InAt(0).AsRegister<Register>();
7981 HConstant *value = insn->GetConstant();
7982
7983 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007984 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007985 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007986 codegen_->LiteralFloatAddress(
7987 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007988 break;
7989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007990 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007991 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007992 codegen_->LiteralDoubleAddress(
7993 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007994 break;
7995
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007996 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007997 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007998 codegen_->LiteralInt32Address(
7999 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008000 break;
8001
8002 default:
8003 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8004 }
8005}
8006
Mark Mendell0616ae02015-04-17 12:49:27 -04008007/**
8008 * Class to handle late fixup of offsets into constant area.
8009 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008010class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008011 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008012 RIPFixup(CodeGeneratorX86& codegen,
8013 HX86ComputeBaseMethodAddress* base_method_address,
8014 size_t offset)
8015 : codegen_(&codegen),
8016 base_method_address_(base_method_address),
8017 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008018
8019 protected:
8020 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8021
8022 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008023 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008024
8025 private:
8026 void Process(const MemoryRegion& region, int pos) OVERRIDE {
8027 // Patch the correct offset for the instruction. The place to patch is the
8028 // last 4 bytes of the instruction.
8029 // The value to patch is the distance from the offset in the constant area
8030 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008031 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008032 int32_t relative_position =
8033 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008034
8035 // Patch in the right value.
8036 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8037 }
8038
Mark Mendell0616ae02015-04-17 12:49:27 -04008039 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008040 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008041};
8042
Mark Mendell805b3b52015-09-18 14:10:29 -04008043/**
8044 * Class to handle late fixup of offsets to a jump table that will be created in the
8045 * constant area.
8046 */
8047class JumpTableRIPFixup : public RIPFixup {
8048 public:
8049 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008050 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8051 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008052
8053 void CreateJumpTable() {
8054 X86Assembler* assembler = codegen_->GetAssembler();
8055
8056 // Ensure that the reference to the jump table has the correct offset.
8057 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8058 SetOffset(offset_in_constant_table);
8059
8060 // The label values in the jump table are computed relative to the
8061 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008062 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008063
8064 // Populate the jump table with the correct values for the jump table.
8065 int32_t num_entries = switch_instr_->GetNumEntries();
8066 HBasicBlock* block = switch_instr_->GetBlock();
8067 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8068 // The value that we want is the target offset - the position of the table.
8069 for (int32_t i = 0; i < num_entries; i++) {
8070 HBasicBlock* b = successors[i];
8071 Label* l = codegen_->GetLabelOf(b);
8072 DCHECK(l->IsBound());
8073 int32_t offset_to_block = l->Position() - relative_offset;
8074 assembler->AppendInt32(offset_to_block);
8075 }
8076 }
8077
8078 private:
8079 const HX86PackedSwitch* switch_instr_;
8080};
8081
8082void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8083 // Generate the constant area if needed.
8084 X86Assembler* assembler = GetAssembler();
8085 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8086 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8087 // byte values.
8088 assembler->Align(4, 0);
8089 constant_area_start_ = assembler->CodeSize();
8090
8091 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008092 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008093 jump_table->CreateJumpTable();
8094 }
8095
8096 // And now add the constant area to the generated code.
8097 assembler->AddConstantArea();
8098 }
8099
8100 // And finish up.
8101 CodeGenerator::Finalize(allocator);
8102}
8103
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008104Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8105 HX86ComputeBaseMethodAddress* method_base,
8106 Register reg) {
8107 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008108 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008109 return Address(reg, kDummy32BitOffset, fixup);
8110}
8111
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008112Address CodeGeneratorX86::LiteralFloatAddress(float v,
8113 HX86ComputeBaseMethodAddress* method_base,
8114 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008115 AssemblerFixup* fixup =
8116 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008117 return Address(reg, kDummy32BitOffset, fixup);
8118}
8119
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008120Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8121 HX86ComputeBaseMethodAddress* method_base,
8122 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008123 AssemblerFixup* fixup =
8124 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008125 return Address(reg, kDummy32BitOffset, fixup);
8126}
8127
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008128Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8129 HX86ComputeBaseMethodAddress* method_base,
8130 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008131 AssemblerFixup* fixup =
8132 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008133 return Address(reg, kDummy32BitOffset, fixup);
8134}
8135
Aart Bika19616e2016-02-01 18:57:58 -08008136void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8137 if (value == 0) {
8138 __ xorl(dest, dest);
8139 } else {
8140 __ movl(dest, Immediate(value));
8141 }
8142}
8143
8144void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8145 if (value == 0) {
8146 __ testl(dest, dest);
8147 } else {
8148 __ cmpl(dest, Immediate(value));
8149 }
8150}
8151
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008152void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8153 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008154 GenerateIntCompare(lhs_reg, rhs);
8155}
8156
8157void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008158 if (rhs.IsConstant()) {
8159 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008160 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008161 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008162 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008163 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008164 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008165 }
8166}
8167
8168Address CodeGeneratorX86::ArrayAddress(Register obj,
8169 Location index,
8170 ScaleFactor scale,
8171 uint32_t data_offset) {
8172 return index.IsConstant() ?
8173 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8174 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8175}
8176
Mark Mendell805b3b52015-09-18 14:10:29 -04008177Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8178 Register reg,
8179 Register value) {
8180 // Create a fixup to be used to create and address the jump table.
8181 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008182 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008183
8184 // We have to populate the jump tables.
8185 fixups_to_jump_tables_.push_back(table_fixup);
8186
8187 // We want a scaled address, as we are extracting the correct offset from the table.
8188 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8189}
8190
Andreas Gampe85b62f22015-09-09 13:15:38 -07008191// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008192void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008193 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008194 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008195 return;
8196 }
8197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008198 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008199
8200 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8201 if (target.Equals(return_loc)) {
8202 return;
8203 }
8204
8205 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8206 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008207 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008208 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008209 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8210 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008211 GetMoveResolver()->EmitNativeCode(&parallel_move);
8212 } else {
8213 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008214 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008215 parallel_move.AddMove(return_loc, target, type, nullptr);
8216 GetMoveResolver()->EmitNativeCode(&parallel_move);
8217 }
8218}
8219
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008220void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8221 const uint8_t* roots_data,
8222 const PatchInfo<Label>& info,
8223 uint64_t index_in_table) const {
8224 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8225 uintptr_t address =
8226 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8227 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8228 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8229 dchecked_integral_cast<uint32_t>(address);
8230}
8231
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008232void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8233 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008234 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008235 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008236 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008237 }
8238
8239 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008240 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008241 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008242 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008243 }
8244}
8245
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008246void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8247 ATTRIBUTE_UNUSED) {
8248 LOG(FATAL) << "Unreachable";
8249}
8250
8251void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8252 ATTRIBUTE_UNUSED) {
8253 LOG(FATAL) << "Unreachable";
8254}
8255
Roland Levillain4d027112015-07-01 15:41:14 +01008256#undef __
8257
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008258} // namespace x86
8259} // namespace art