blob: 536909aa1f7fd98fd366965415daa0e39df30c51 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#include "intrinsics.h"
28#include "intrinsics_x86.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010029#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070031#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010033#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010040
Roland Levillain0d5a2812015-11-13 10:07:31 +000041template<class MirrorType>
42class GcRoot;
43
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044namespace x86 {
45
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050048static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Mark Mendell24f2dfa2015-01-14 19:51:45 -050050static constexpr int kC2ConditionMask = 0x400;
51
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000052static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000053
Aart Bik1f8d51b2018-02-15 10:42:37 -080054static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
55static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
56
Roland Levillain7cbd27f2016-08-11 23:53:33 +010057// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
58#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070059#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Andreas Gampe85b62f22015-09-09 13:15:38 -070061class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000063 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Alexandre Rames2ed20af2015-03-06 13:55:35 +000065 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010066 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000068 if (instruction_->CanThrowIntoCatchBlock()) {
69 // Live registers will be restored in the catch block if caught.
70 SaveLiveRegisters(codegen, instruction_->GetLocations());
71 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010072 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010073 instruction_,
74 instruction_->GetDexPc(),
75 this);
Roland Levillain888d0672015-11-23 18:53:50 +000076 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 }
78
Alexandre Rames8158f282015-08-07 10:26:17 +010079 bool IsFatal() const OVERRIDE { return true; }
80
Alexandre Rames9931f312015-06-19 14:47:01 +010081 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
82
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
85};
86
Andreas Gampe85b62f22015-09-09 13:15:38 -070087class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000088 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000089 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000090
Alexandre Rames2ed20af2015-03-06 13:55:35 +000091 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010092 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000093 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010094 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400143
144 // Are we using an array length from memory?
145 HInstruction* array_length = instruction_->InputAt(1);
146 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400148 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
149 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100150 HArrayLength* length = array_length->AsArrayLength();
151 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400152 Location array_loc = array_length->GetLocations()->InAt(0);
153 Address array_len(array_loc.AsRegister<Register>(), len_offset);
154 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
155 // Check for conflicts with index.
156 if (length_loc.Equals(locations->InAt(0))) {
157 // We know we aren't using parameter 2.
158 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
159 }
160 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100161 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100162 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700163 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400164 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100166 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000167 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100168 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400169 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100170 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100171 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100172 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
173 ? kQuickThrowStringBounds
174 : kQuickThrowArrayBounds;
175 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100176 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000177 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100178 }
179
Alexandre Rames8158f282015-08-07 10:26:17 +0100180 bool IsFatal() const OVERRIDE { return true; }
181
Alexandre Rames9931f312015-06-19 14:47:01 +0100182 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
186};
187
Andreas Gampe85b62f22015-09-09 13:15:38 -0700188class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000190 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000191 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000193 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700194 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100195 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700197 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100198 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000199 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700200 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100201 if (successor_ == nullptr) {
202 __ jmp(GetReturnLabel());
203 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100204 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100205 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206 }
207
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 Label* GetReturnLabel() {
209 DCHECK(successor_ == nullptr);
210 return &return_label_;
211 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100213 HBasicBlock* GetSuccessor() const {
214 return successor_;
215 }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
218
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000219 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000221 Label return_label_;
222
223 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
224};
225
Vladimir Markoaad75c62016-10-03 08:46:48 +0000226class LoadStringSlowPathX86 : public SlowPathCode {
227 public:
228 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = instruction_->GetLocations();
232 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
233
234 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
235 __ Bind(GetEntryLabel());
236 SaveLiveRegisters(codegen, locations);
237
238 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000239 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
240 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000241 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
242 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
243 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
244 RestoreLiveRegisters(codegen, locations);
245
Vladimir Markoaad75c62016-10-03 08:46:48 +0000246 __ jmp(GetExitLabel());
247 }
248
249 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
250
251 private:
252 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
253};
254
Andreas Gampe85b62f22015-09-09 13:15:38 -0700255class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 public:
257 LoadClassSlowPathX86(HLoadClass* cls,
258 HInstruction* at,
259 uint32_t dex_pc,
260 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000261 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
263 }
264
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000265 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000266 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000267 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
268 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270
271 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 dex::TypeIndex type_index = cls_->GetTypeIndex();
273 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100274 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
275 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 instruction_,
277 dex_pc_,
278 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 if (do_clinit_) {
280 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
281 } else {
282 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
283 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000284
285 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 Location out = locations->Out();
287 if (out.IsValid()) {
288 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
289 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000292 __ jmp(GetExitLabel());
293 }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
296
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000297 private:
298 // The class this slow path will load.
299 HLoadClass* const cls_;
300
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000301 // The dex PC of `at_`.
302 const uint32_t dex_pc_;
303
304 // Whether to initialize the class.
305 const bool do_clinit_;
306
307 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
308};
309
Andreas Gampe85b62f22015-09-09 13:15:38 -0700310class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000312 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000313 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000315 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 DCHECK(instruction_->IsCheckCast()
318 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
321 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000323 if (kPoisonHeapReferences &&
324 instruction_->IsCheckCast() &&
325 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
326 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
327 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
328 }
329
Vladimir Marko87584542017-12-12 17:47:52 +0000330 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331 SaveLiveRegisters(codegen, locations);
332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
334 // We're moving two locations to locations that could overlap, so we need a parallel
335 // move resolver.
336 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800337 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100339 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800340 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100342 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100344 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100345 instruction_,
346 instruction_->GetDexPc(),
347 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800348 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 } else {
350 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
352 instruction_,
353 instruction_->GetDexPc(),
354 this);
355 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 }
357
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000358 if (!is_fatal_) {
359 if (instruction_->IsInstanceOf()) {
360 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
361 }
362 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 __ jmp(GetExitLabel());
365 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000366 }
367
Alexandre Rames9931f312015-06-19 14:47:01 +0100368 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100370
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000371 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373
374 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
375};
376
Andreas Gampe85b62f22015-09-09 13:15:38 -0700377class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 public:
Aart Bik42249c32016-01-07 15:33:50 -0800379 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000380 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381
382 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100383 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100385 LocationSummary* locations = instruction_->GetLocations();
386 SaveLiveRegisters(codegen, locations);
387 InvokeRuntimeCallingConvention calling_convention;
388 x86_codegen->Load32BitValue(
389 calling_convention.GetRegisterAt(0),
390 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100391 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100392 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 }
394
Alexandre Rames9931f312015-06-19 14:47:01 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
396
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
399};
400
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401class ArraySetSlowPathX86 : public SlowPathCode {
402 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 LocationSummary* locations = instruction_->GetLocations();
407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, locations);
409
410 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100411 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100412 parallel_move.AddMove(
413 locations->InAt(0),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100415 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 nullptr);
417 parallel_move.AddMove(
418 locations->InAt(1),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100420 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100421 nullptr);
422 parallel_move.AddMove(
423 locations->InAt(2),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100425 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100426 nullptr);
427 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
428
429 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100430 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000431 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 RestoreLiveRegisters(codegen, locations);
433 __ jmp(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
437
438 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
440};
441
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100442// Slow path marking an object reference `ref` during a read
443// barrier. The field `obj.field` in the object `obj` holding this
444// reference does not get updated by this slow path after marking (see
445// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
446//
447// This means that after the execution of this slow path, `ref` will
448// always be up-to-date, but `obj.field` may not; i.e., after the
449// flip, `ref` will be a to-space reference, but `obj.field` will
450// probably still be a from-space reference (unless it gets updated by
451// another thread, or if another thread installed another object
452// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000453class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
454 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
456 Location ref,
457 bool unpoison_ref_before_marking)
458 : SlowPathCode(instruction),
459 ref_(ref),
460 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000461 DCHECK(kEmitCompilerReadBarrier);
462 }
463
464 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000469 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 DCHECK(instruction_->IsInstanceFieldGet() ||
472 instruction_->IsStaticFieldGet() ||
473 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100474 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 instruction_->IsLoadClass() ||
476 instruction_->IsLoadString() ||
477 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100478 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100479 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
480 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 << "Unexpected instruction in read barrier marking slow path: "
482 << instruction_->DebugName();
483
484 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100485 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000486 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000488 }
Roland Levillain4359e612016-07-20 11:32:19 +0100489 // No need to save live registers; it's taken care of by the
490 // entrypoint. Also, there is no need to update the stack mask,
491 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000492 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100493 DCHECK_NE(ref_reg, ESP);
494 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100495 // "Compact" slow path, saving two moves.
496 //
497 // Instead of using the standard runtime calling convention (input
498 // and output in EAX):
499 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100501 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100503 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100505 // of a dedicated entrypoint:
506 //
507 // rX <- ReadBarrierMarkRegX(rX)
508 //
Roland Levillain97c46462017-05-11 14:04:03 +0100509 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100510 // This runtime call does not require a stack map.
511 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 __ jmp(GetExitLabel());
513 }
514
515 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // The location (register) of the marked object reference.
517 const Location ref_;
518 // Should the reference in `ref_` be unpoisoned prior to marking it?
519 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000520
521 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
522};
523
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100524// Slow path marking an object reference `ref` during a read barrier,
525// and if needed, atomically updating the field `obj.field` in the
526// object `obj` holding this reference after marking (contrary to
527// ReadBarrierMarkSlowPathX86 above, which never tries to update
528// `obj.field`).
529//
530// This means that after the execution of this slow path, both `ref`
531// and `obj.field` will be up-to-date; i.e., after the flip, both will
532// hold the same to-space reference (unless another thread installed
533// another object reference (different from `ref`) in `obj.field`).
534class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
535 public:
536 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
537 Location ref,
538 Register obj,
539 const Address& field_addr,
540 bool unpoison_ref_before_marking,
541 Register temp)
542 : SlowPathCode(instruction),
543 ref_(ref),
544 obj_(obj),
545 field_addr_(field_addr),
546 unpoison_ref_before_marking_(unpoison_ref_before_marking),
547 temp_(temp) {
548 DCHECK(kEmitCompilerReadBarrier);
549 }
550
551 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
552
553 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
554 LocationSummary* locations = instruction_->GetLocations();
555 Register ref_reg = ref_.AsRegister<Register>();
556 DCHECK(locations->CanCall());
557 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
558 // This slow path is only used by the UnsafeCASObject intrinsic.
559 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
560 << "Unexpected instruction in read barrier marking and field updating slow path: "
561 << instruction_->DebugName();
562 DCHECK(instruction_->GetLocations()->Intrinsified());
563 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
564
565 __ Bind(GetEntryLabel());
566 if (unpoison_ref_before_marking_) {
567 // Object* ref = ref_addr->AsMirrorPtr()
568 __ MaybeUnpoisonHeapReference(ref_reg);
569 }
570
571 // Save the old (unpoisoned) reference.
572 __ movl(temp_, ref_reg);
573
574 // No need to save live registers; it's taken care of by the
575 // entrypoint. Also, there is no need to update the stack mask,
576 // as this runtime call will not trigger a garbage collection.
577 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
578 DCHECK_NE(ref_reg, ESP);
579 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
580 // "Compact" slow path, saving two moves.
581 //
582 // Instead of using the standard runtime calling convention (input
583 // and output in EAX):
584 //
585 // EAX <- ref
586 // EAX <- ReadBarrierMark(EAX)
587 // ref <- EAX
588 //
589 // we just use rX (the register containing `ref`) as input and output
590 // of a dedicated entrypoint:
591 //
592 // rX <- ReadBarrierMarkRegX(rX)
593 //
Roland Levillain97c46462017-05-11 14:04:03 +0100594 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100595 // This runtime call does not require a stack map.
596 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
597
598 // If the new reference is different from the old reference,
599 // update the field in the holder (`*field_addr`).
600 //
601 // Note that this field could also hold a different object, if
602 // another thread had concurrently changed it. In that case, the
603 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
604 // operation below would abort the CAS, leaving the field as-is.
605 NearLabel done;
606 __ cmpl(temp_, ref_reg);
607 __ j(kEqual, &done);
608
609 // Update the the holder's field atomically. This may fail if
610 // mutator updates before us, but it's OK. This is achieved
611 // using a strong compare-and-set (CAS) operation with relaxed
612 // memory synchronization ordering, where the expected value is
613 // the old reference and the desired value is the new reference.
614 // This operation is implemented with a 32-bit LOCK CMPXLCHG
615 // instruction, which requires the expected value (the old
616 // reference) to be in EAX. Save EAX beforehand, and move the
617 // expected value (stored in `temp_`) into EAX.
618 __ pushl(EAX);
619 __ movl(EAX, temp_);
620
621 // Convenience aliases.
622 Register base = obj_;
623 Register expected = EAX;
624 Register value = ref_reg;
625
626 bool base_equals_value = (base == value);
627 if (kPoisonHeapReferences) {
628 if (base_equals_value) {
629 // If `base` and `value` are the same register location, move
630 // `value` to a temporary register. This way, poisoning
631 // `value` won't invalidate `base`.
632 value = temp_;
633 __ movl(value, base);
634 }
635
636 // Check that the register allocator did not assign the location
637 // of `expected` (EAX) to `value` nor to `base`, so that heap
638 // poisoning (when enabled) works as intended below.
639 // - If `value` were equal to `expected`, both references would
640 // be poisoned twice, meaning they would not be poisoned at
641 // all, as heap poisoning uses address negation.
642 // - If `base` were equal to `expected`, poisoning `expected`
643 // would invalidate `base`.
644 DCHECK_NE(value, expected);
645 DCHECK_NE(base, expected);
646
647 __ PoisonHeapReference(expected);
648 __ PoisonHeapReference(value);
649 }
650
651 __ LockCmpxchgl(field_addr_, value);
652
653 // If heap poisoning is enabled, we need to unpoison the values
654 // that were poisoned earlier.
655 if (kPoisonHeapReferences) {
656 if (base_equals_value) {
657 // `value` has been moved to a temporary register, no need
658 // to unpoison it.
659 } else {
660 __ UnpoisonHeapReference(value);
661 }
662 // No need to unpoison `expected` (EAX), as it is be overwritten below.
663 }
664
665 // Restore EAX.
666 __ popl(EAX);
667
668 __ Bind(&done);
669 __ jmp(GetExitLabel());
670 }
671
672 private:
673 // The location (register) of the marked object reference.
674 const Location ref_;
675 // The register containing the object holding the marked object reference field.
676 const Register obj_;
677 // The address of the marked reference field. The base of this address must be `obj_`.
678 const Address field_addr_;
679
680 // Should the reference in `ref_` be unpoisoned prior to marking it?
681 const bool unpoison_ref_before_marking_;
682
683 const Register temp_;
684
685 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
686};
687
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688// Slow path generating a read barrier for a heap reference.
689class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
690 public:
691 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
692 Location out,
693 Location ref,
694 Location obj,
695 uint32_t offset,
696 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000697 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698 out_(out),
699 ref_(ref),
700 obj_(obj),
701 offset_(offset),
702 index_(index) {
703 DCHECK(kEmitCompilerReadBarrier);
704 // If `obj` is equal to `out` or `ref`, it means the initial object
705 // has been overwritten by (or after) the heap object reference load
706 // to be instrumented, e.g.:
707 //
708 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000709 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 //
711 // In that case, we have lost the information about the original
712 // object, and the emitted read barrier cannot work properly.
713 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
714 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
715 }
716
717 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
718 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
719 LocationSummary* locations = instruction_->GetLocations();
720 Register reg_out = out_.AsRegister<Register>();
721 DCHECK(locations->CanCall());
722 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100723 DCHECK(instruction_->IsInstanceFieldGet() ||
724 instruction_->IsStaticFieldGet() ||
725 instruction_->IsArrayGet() ||
726 instruction_->IsInstanceOf() ||
727 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700728 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000729 << "Unexpected instruction in read barrier for heap reference slow path: "
730 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731
732 __ Bind(GetEntryLabel());
733 SaveLiveRegisters(codegen, locations);
734
735 // We may have to change the index's value, but as `index_` is a
736 // constant member (like other "inputs" of this slow path),
737 // introduce a copy of it, `index`.
738 Location index = index_;
739 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100740 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000741 if (instruction_->IsArrayGet()) {
742 // Compute the actual memory offset and store it in `index`.
743 Register index_reg = index_.AsRegister<Register>();
744 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
745 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
746 // We are about to change the value of `index_reg` (see the
747 // calls to art::x86::X86Assembler::shll and
748 // art::x86::X86Assembler::AddImmediate below), but it has
749 // not been saved by the previous call to
750 // art::SlowPathCode::SaveLiveRegisters, as it is a
751 // callee-save register --
752 // art::SlowPathCode::SaveLiveRegisters does not consider
753 // callee-save registers, as it has been designed with the
754 // assumption that callee-save registers are supposed to be
755 // handled by the called function. So, as a callee-save
756 // register, `index_reg` _would_ eventually be saved onto
757 // the stack, but it would be too late: we would have
758 // changed its value earlier. Therefore, we manually save
759 // it here into another freely available register,
760 // `free_reg`, chosen of course among the caller-save
761 // registers (as a callee-save `free_reg` register would
762 // exhibit the same problem).
763 //
764 // Note we could have requested a temporary register from
765 // the register allocator instead; but we prefer not to, as
766 // this is a slow path, and we know we can find a
767 // caller-save register that is available.
768 Register free_reg = FindAvailableCallerSaveRegister(codegen);
769 __ movl(free_reg, index_reg);
770 index_reg = free_reg;
771 index = Location::RegisterLocation(index_reg);
772 } else {
773 // The initial register stored in `index_` has already been
774 // saved in the call to art::SlowPathCode::SaveLiveRegisters
775 // (as it is not a callee-save register), so we can freely
776 // use it.
777 }
778 // Shifting the index value contained in `index_reg` by the scale
779 // factor (2) cannot overflow in practice, as the runtime is
780 // unable to allocate object arrays with a size larger than
781 // 2^26 - 1 (that is, 2^28 - 4 bytes).
782 __ shll(index_reg, Immediate(TIMES_4));
783 static_assert(
784 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
785 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
786 __ AddImmediate(index_reg, Immediate(offset_));
787 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100788 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
789 // intrinsics, `index_` is not shifted by a scale factor of 2
790 // (as in the case of ArrayGet), as it is actually an offset
791 // to an object field within an object.
792 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000793 DCHECK(instruction_->GetLocations()->Intrinsified());
794 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
795 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
796 << instruction_->AsInvoke()->GetIntrinsic();
797 DCHECK_EQ(offset_, 0U);
798 DCHECK(index_.IsRegisterPair());
799 // UnsafeGet's offset location is a register pair, the low
800 // part contains the correct offset.
801 index = index_.ToLow();
802 }
803 }
804
805 // We're moving two or three locations to locations that could
806 // overlap, so we need a parallel move resolver.
807 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100808 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000809 parallel_move.AddMove(ref_,
810 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100811 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000812 nullptr);
813 parallel_move.AddMove(obj_,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100815 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000816 nullptr);
817 if (index.IsValid()) {
818 parallel_move.AddMove(index,
819 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100820 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 nullptr);
822 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
823 } else {
824 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
825 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
826 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100827 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 CheckEntrypointTypes<
829 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
830 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
831
832 RestoreLiveRegisters(codegen, locations);
833 __ jmp(GetExitLabel());
834 }
835
836 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
837
838 private:
839 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
840 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
841 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
842 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
843 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
844 return static_cast<Register>(i);
845 }
846 }
847 // We shall never fail to find a free caller-save register, as
848 // there are more than two core caller-save registers on x86
849 // (meaning it is possible to find one which is different from
850 // `ref` and `obj`).
851 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
852 LOG(FATAL) << "Could not find a free caller-save register";
853 UNREACHABLE();
854 }
855
Roland Levillain0d5a2812015-11-13 10:07:31 +0000856 const Location out_;
857 const Location ref_;
858 const Location obj_;
859 const uint32_t offset_;
860 // An additional location containing an index to an array.
861 // Only used for HArrayGet and the UnsafeGetObject &
862 // UnsafeGetObjectVolatile intrinsics.
863 const Location index_;
864
865 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
866};
867
868// Slow path generating a read barrier for a GC root.
869class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
870 public:
871 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000872 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000873 DCHECK(kEmitCompilerReadBarrier);
874 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000875
876 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
877 LocationSummary* locations = instruction_->GetLocations();
878 Register reg_out = out_.AsRegister<Register>();
879 DCHECK(locations->CanCall());
880 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000881 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
882 << "Unexpected instruction in read barrier for GC root slow path: "
883 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000884
885 __ Bind(GetEntryLabel());
886 SaveLiveRegisters(codegen, locations);
887
888 InvokeRuntimeCallingConvention calling_convention;
889 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
890 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100891 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000892 instruction_,
893 instruction_->GetDexPc(),
894 this);
895 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
896 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
897
898 RestoreLiveRegisters(codegen, locations);
899 __ jmp(GetExitLabel());
900 }
901
902 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
903
904 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 const Location out_;
906 const Location root_;
907
908 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
909};
910
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100911#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100912// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
913#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100914
Aart Bike9f37602015-10-09 11:15:55 -0700915inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700916 switch (cond) {
917 case kCondEQ: return kEqual;
918 case kCondNE: return kNotEqual;
919 case kCondLT: return kLess;
920 case kCondLE: return kLessEqual;
921 case kCondGT: return kGreater;
922 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700923 case kCondB: return kBelow;
924 case kCondBE: return kBelowEqual;
925 case kCondA: return kAbove;
926 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700927 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928 LOG(FATAL) << "Unreachable";
929 UNREACHABLE();
930}
931
Aart Bike9f37602015-10-09 11:15:55 -0700932// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
934 switch (cond) {
935 case kCondEQ: return kEqual;
936 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700937 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938 case kCondLT: return kBelow;
939 case kCondLE: return kBelowEqual;
940 case kCondGT: return kAbove;
941 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700942 // Unsigned remain unchanged.
943 case kCondB: return kBelow;
944 case kCondBE: return kBelowEqual;
945 case kCondA: return kAbove;
946 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100947 }
948 LOG(FATAL) << "Unreachable";
949 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700950}
951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100952void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100953 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100954}
955
956void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
962 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
967 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100968}
969
Mark Mendell7c8d0092015-01-26 11:21:33 -0500970size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700971 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700972 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700973 } else {
974 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
975 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500976 return GetFloatingPointSpillSlotSize();
977}
978
979size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700980 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700981 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700982 } else {
983 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
984 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500985 return GetFloatingPointSpillSlotSize();
986}
987
Calin Juravle175dc732015-08-25 15:42:32 +0100988void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
989 HInstruction* instruction,
990 uint32_t dex_pc,
991 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100992 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100993 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
994 if (EntrypointRequiresStackMap(entrypoint)) {
995 RecordPcInfo(instruction, dex_pc, slow_path);
996 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100997}
998
Roland Levillaindec8f632016-07-22 17:10:06 +0100999void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1000 HInstruction* instruction,
1001 SlowPathCode* slow_path) {
1002 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001003 GenerateInvokeRuntime(entry_point_offset);
1004}
1005
1006void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001007 __ fs()->call(Address::Absolute(entry_point_offset));
1008}
1009
Mark Mendellfb8d2792015-03-31 22:16:59 -04001010CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001011 const X86InstructionSetFeatures& isa_features,
1012 const CompilerOptions& compiler_options,
1013 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001014 : CodeGenerator(graph,
1015 kNumberOfCpuRegisters,
1016 kNumberOfXmmRegisters,
1017 kNumberOfRegisterPairs,
1018 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1019 arraysize(kCoreCalleeSaves))
1020 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001021 0,
1022 compiler_options,
1023 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001026 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001027 move_resolver_(graph->GetAllocator(), this),
1028 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001029 isa_features_(isa_features),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001030 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1031 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1032 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1033 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001034 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001035 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1036 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1037 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001038 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001039 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001040 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001041 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001042 // Use a fake return address register to mimic Quick.
1043 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001044}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001045
David Brazdil58282f42016-01-14 12:45:10 +00001046void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001047 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001048 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049}
1050
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001051InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001052 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001053 assembler_(codegen->GetAssembler()),
1054 codegen_(codegen) {}
1055
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001057 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058}
1059
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001060void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001062 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001063 bool skip_overflow_check =
1064 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001065 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001066
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001067 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1068 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1069 Immediate(1));
1070 }
1071
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001072 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001073 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1074 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001075 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001076 }
1077
Mark Mendell5f874182015-03-04 15:42:45 -05001078 if (HasEmptyFrame()) {
1079 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001080 }
Mark Mendell5f874182015-03-04 15:42:45 -05001081
1082 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1083 Register reg = kCoreCalleeSaves[i];
1084 if (allocated_registers_.ContainsCoreRegister(reg)) {
1085 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 __ cfi().AdjustCFAOffset(kX86WordSize);
1087 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001088 }
1089 }
1090
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001091 int adjust = GetFrameSize() - FrameEntrySpillSize();
1092 __ subl(ESP, Immediate(adjust));
1093 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001094 // Save the current method if we need it. Note that we do not
1095 // do this in HCurrentMethod, as the instruction might have been removed
1096 // in the SSA graph.
1097 if (RequiresCurrentMethod()) {
1098 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1099 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001100
1101 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1102 // Initialize should_deoptimize flag to 0.
1103 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1104 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001105}
1106
1107void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001108 __ cfi().RememberState();
1109 if (!HasEmptyFrame()) {
1110 int adjust = GetFrameSize() - FrameEntrySpillSize();
1111 __ addl(ESP, Immediate(adjust));
1112 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001113
David Srbeckyc34dc932015-04-12 09:27:43 +01001114 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1115 Register reg = kCoreCalleeSaves[i];
1116 if (allocated_registers_.ContainsCoreRegister(reg)) {
1117 __ popl(reg);
1118 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1119 __ cfi().Restore(DWARFReg(reg));
1120 }
Mark Mendell5f874182015-03-04 15:42:45 -05001121 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001122 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001123 __ ret();
1124 __ cfi().RestoreState();
1125 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001126}
1127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001128void CodeGeneratorX86::Bind(HBasicBlock* block) {
1129 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001130}
1131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001133 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001134 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001135 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001136 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137 case DataType::Type::kInt8:
1138 case DataType::Type::kUint16:
1139 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001140 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001142 return Location::RegisterLocation(EAX);
1143
Aart Bik66c158e2018-01-31 12:55:04 -08001144 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::RegisterPairLocation(EAX, EDX);
1147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001149 return Location::NoLocation();
1150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 case DataType::Type::kFloat64:
1152 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001153 return Location::FpuRegisterLocation(XMM0);
1154 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001155
1156 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001157}
1158
1159Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1160 return Location::RegisterLocation(kMethodRegisterArgument);
1161}
1162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001163Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001164 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001165 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001166 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001167 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001168 case DataType::Type::kInt8:
1169 case DataType::Type::kUint16:
1170 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001171 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001175 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001179 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 uint32_t index = gp_index_;
1183 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001184 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001186 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1187 calling_convention.GetRegisterPairAt(index));
1188 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001190 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1191 }
1192 }
1193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001195 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001196 stack_index_++;
1197 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1198 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1199 } else {
1200 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1201 }
1202 }
1203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001205 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001206 stack_index_ += 2;
1207 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1208 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1209 } else {
1210 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211 }
1212 }
1213
Aart Bik66c158e2018-01-31 12:55:04 -08001214 case DataType::Type::kUint32:
1215 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001217 LOG(FATAL) << "Unexpected parameter type " << type;
1218 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001219 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001220 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001221}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001222
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223void CodeGeneratorX86::Move32(Location destination, Location source) {
1224 if (source.Equals(destination)) {
1225 return;
1226 }
1227 if (destination.IsRegister()) {
1228 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 } else {
1233 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else if (destination.IsFpuRegister()) {
1237 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 } else {
1242 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001246 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001248 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001249 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001250 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001251 } else if (source.IsConstant()) {
1252 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001253 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001254 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 } else {
1256 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001257 __ pushl(Address(ESP, source.GetStackIndex()));
1258 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 }
1260 }
1261}
1262
1263void CodeGeneratorX86::Move64(Location destination, Location source) {
1264 if (source.Equals(destination)) {
1265 return;
1266 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 if (destination.IsRegisterPair()) {
1268 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001269 EmitParallelMoves(
1270 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1271 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001274 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001275 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1278 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1279 __ psrlq(src_reg, Immediate(32));
1280 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001282 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001284 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1285 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1287 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001288 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001289 if (source.IsFpuRegister()) {
1290 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1291 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001292 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001294 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001295 // Create stack space for 2 elements.
1296 __ subl(ESP, Immediate(2 * elem_size));
1297 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1298 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1299 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1300 // And remove the temporary stack space we allocated.
1301 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001302 } else {
1303 LOG(FATAL) << "Unimplemented";
1304 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001305 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001306 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001307 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001308 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001309 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001310 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001311 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001312 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001313 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001314 } else if (source.IsConstant()) {
1315 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001316 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1317 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001318 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001319 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1320 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001322 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001323 EmitParallelMoves(
1324 Location::StackSlot(source.GetStackIndex()),
1325 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001326 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001327 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001328 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001330 }
1331 }
1332}
1333
Calin Juravle175dc732015-08-25 15:42:32 +01001334void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1335 DCHECK(location.IsRegister());
1336 __ movl(location.AsRegister<Register>(), Immediate(value));
1337}
1338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001339void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001340 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001341 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1342 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1343 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001345 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001346 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001347 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001348}
1349
1350void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1351 if (location.IsRegister()) {
1352 locations->AddTemp(location);
1353 } else if (location.IsRegisterPair()) {
1354 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1355 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1356 } else {
1357 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1358 }
1359}
1360
David Brazdilfc6a86a2015-06-26 10:33:45 +00001361void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001362 if (successor->IsExitBlock()) {
1363 DCHECK(got->GetPrevious()->AlwaysThrows());
1364 return; // no code needed
1365 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001366
1367 HBasicBlock* block = got->GetBlock();
1368 HInstruction* previous = got->GetPrevious();
1369
1370 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001371 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001372 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1373 __ pushl(EAX);
1374 __ movl(EAX, Address(ESP, kX86WordSize));
1375 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1376 __ popl(EAX);
1377 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001378 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1379 return;
1380 }
1381
1382 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1383 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1384 }
1385 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001386 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001387 }
1388}
1389
David Brazdilfc6a86a2015-06-26 10:33:45 +00001390void LocationsBuilderX86::VisitGoto(HGoto* got) {
1391 got->SetLocations(nullptr);
1392}
1393
1394void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1395 HandleGoto(got, got->GetSuccessor());
1396}
1397
1398void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1399 try_boundary->SetLocations(nullptr);
1400}
1401
1402void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1403 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1404 if (!successor->IsExitBlock()) {
1405 HandleGoto(try_boundary, successor);
1406 }
1407}
1408
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001411}
1412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001413void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001414}
1415
Mark Mendell152408f2015-12-31 12:28:50 -05001416template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001417void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001418 LabelType* true_label,
1419 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 if (cond->IsFPConditionTrueIfNaN()) {
1421 __ j(kUnordered, true_label);
1422 } else if (cond->IsFPConditionFalseIfNaN()) {
1423 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001424 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001426}
1427
Mark Mendell152408f2015-12-31 12:28:50 -05001428template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001429void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001430 LabelType* true_label,
1431 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001432 LocationSummary* locations = cond->GetLocations();
1433 Location left = locations->InAt(0);
1434 Location right = locations->InAt(1);
1435 IfCondition if_cond = cond->GetCondition();
1436
Mark Mendellc4701932015-04-10 13:18:51 -04001437 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001439 IfCondition true_high_cond = if_cond;
1440 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001441 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001442
1443 // Set the conditions for the test, remembering that == needs to be
1444 // decided using the low words.
1445 switch (if_cond) {
1446 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001447 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001449 break;
1450 case kCondLT:
1451 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001452 break;
1453 case kCondLE:
1454 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001455 break;
1456 case kCondGT:
1457 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001458 break;
1459 case kCondGE:
1460 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001461 break;
Aart Bike9f37602015-10-09 11:15:55 -07001462 case kCondB:
1463 false_high_cond = kCondA;
1464 break;
1465 case kCondBE:
1466 true_high_cond = kCondB;
1467 break;
1468 case kCondA:
1469 false_high_cond = kCondB;
1470 break;
1471 case kCondAE:
1472 true_high_cond = kCondA;
1473 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001474 }
1475
1476 if (right.IsConstant()) {
1477 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001478 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001479 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001480
Aart Bika19616e2016-02-01 18:57:58 -08001481 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001484 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001485 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(true_high_cond), true_label);
1488 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001489 }
1490 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001491 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001492 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001493 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001494 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001495
1496 __ cmpl(left_high, right_high);
1497 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001498 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001499 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001500 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001501 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001502 __ j(X86Condition(true_high_cond), true_label);
1503 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001504 }
1505 // Must be equal high, so compare the lows.
1506 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001507 } else {
1508 DCHECK(right.IsDoubleStackSlot());
1509 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1510 if (if_cond == kCondNE) {
1511 __ j(X86Condition(true_high_cond), true_label);
1512 } else if (if_cond == kCondEQ) {
1513 __ j(X86Condition(false_high_cond), false_label);
1514 } else {
1515 __ j(X86Condition(true_high_cond), true_label);
1516 __ j(X86Condition(false_high_cond), false_label);
1517 }
1518 // Must be equal high, so compare the lows.
1519 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001520 }
1521 // The last comparison might be unsigned.
1522 __ j(final_condition, true_label);
1523}
1524
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001525void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1526 Location rhs,
1527 HInstruction* insn,
1528 bool is_double) {
1529 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1530 if (is_double) {
1531 if (rhs.IsFpuRegister()) {
1532 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1533 } else if (const_area != nullptr) {
1534 DCHECK(const_area->IsEmittedAtUseSite());
1535 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1536 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001537 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1538 const_area->GetBaseMethodAddress(),
1539 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001540 } else {
1541 DCHECK(rhs.IsDoubleStackSlot());
1542 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1543 }
1544 } else {
1545 if (rhs.IsFpuRegister()) {
1546 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1547 } else if (const_area != nullptr) {
1548 DCHECK(const_area->IsEmittedAtUseSite());
1549 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1550 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001551 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1552 const_area->GetBaseMethodAddress(),
1553 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001554 } else {
1555 DCHECK(rhs.IsStackSlot());
1556 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1557 }
1558 }
1559}
1560
Mark Mendell152408f2015-12-31 12:28:50 -05001561template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001562void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001563 LabelType* true_target_in,
1564 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001565 // Generated branching requires both targets to be explicit. If either of the
1566 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001567 LabelType fallthrough_target;
1568 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1569 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001570
Mark Mendellc4701932015-04-10 13:18:51 -04001571 LocationSummary* locations = condition->GetLocations();
1572 Location left = locations->InAt(0);
1573 Location right = locations->InAt(1);
1574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001575 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001576 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001578 GenerateLongComparesAndJumps(condition, true_target, false_target);
1579 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001580 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001581 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001582 GenerateFPJumps(condition, true_target, false_target);
1583 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001585 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001586 GenerateFPJumps(condition, true_target, false_target);
1587 break;
1588 default:
1589 LOG(FATAL) << "Unexpected compare type " << type;
1590 }
1591
David Brazdil0debae72015-11-12 18:37:00 +00001592 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001593 __ jmp(false_target);
1594 }
David Brazdil0debae72015-11-12 18:37:00 +00001595
1596 if (fallthrough_target.IsLinked()) {
1597 __ Bind(&fallthrough_target);
1598 }
Mark Mendellc4701932015-04-10 13:18:51 -04001599}
1600
David Brazdil0debae72015-11-12 18:37:00 +00001601static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1602 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1603 // are set only strictly before `branch`. We can't use the eflags on long/FP
1604 // conditions if they are materialized due to the complex branching.
1605 return cond->IsCondition() &&
1606 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1608 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001609}
1610
Mark Mendell152408f2015-12-31 12:28:50 -05001611template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001612void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001613 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001614 LabelType* true_target,
1615 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001616 HInstruction* cond = instruction->InputAt(condition_input_index);
1617
1618 if (true_target == nullptr && false_target == nullptr) {
1619 // Nothing to do. The code always falls through.
1620 return;
1621 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001622 // Constant condition, statically compared against "true" (integer value 1).
1623 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001624 if (true_target != nullptr) {
1625 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001626 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001627 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001628 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001629 if (false_target != nullptr) {
1630 __ jmp(false_target);
1631 }
1632 }
1633 return;
1634 }
1635
1636 // The following code generates these patterns:
1637 // (1) true_target == nullptr && false_target != nullptr
1638 // - opposite condition true => branch to false_target
1639 // (2) true_target != nullptr && false_target == nullptr
1640 // - condition true => branch to true_target
1641 // (3) true_target != nullptr && false_target != nullptr
1642 // - condition true => branch to true_target
1643 // - branch to false_target
1644 if (IsBooleanValueOrMaterializedCondition(cond)) {
1645 if (AreEflagsSetFrom(cond, instruction)) {
1646 if (true_target == nullptr) {
1647 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1648 } else {
1649 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1650 }
1651 } else {
1652 // Materialized condition, compare against 0.
1653 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1654 if (lhs.IsRegister()) {
1655 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1656 } else {
1657 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1658 }
1659 if (true_target == nullptr) {
1660 __ j(kEqual, false_target);
1661 } else {
1662 __ j(kNotEqual, true_target);
1663 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001664 }
1665 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001666 // Condition has not been materialized, use its inputs as the comparison and
1667 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001668 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001669
1670 // If this is a long or FP comparison that has been folded into
1671 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 DataType::Type type = condition->InputAt(0)->GetType();
1673 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001674 GenerateCompareTestAndBranch(condition, true_target, false_target);
1675 return;
1676 }
1677
1678 Location lhs = condition->GetLocations()->InAt(0);
1679 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001680 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001681 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001682 if (true_target == nullptr) {
1683 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1684 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001685 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001686 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001687 }
David Brazdil0debae72015-11-12 18:37:00 +00001688
1689 // If neither branch falls through (case 3), the conditional branch to `true_target`
1690 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1691 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001692 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001693 }
1694}
1695
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001697 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001698 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001699 locations->SetInAt(0, Location::Any());
1700 }
1701}
1702
1703void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001704 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1705 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1706 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1707 nullptr : codegen_->GetLabelOf(true_successor);
1708 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1709 nullptr : codegen_->GetLabelOf(false_successor);
1710 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001711}
1712
1713void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001714 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001716 InvokeRuntimeCallingConvention calling_convention;
1717 RegisterSet caller_saves = RegisterSet::Empty();
1718 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1719 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001720 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001721 locations->SetInAt(0, Location::Any());
1722 }
1723}
1724
1725void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001726 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001727 GenerateTestAndBranch<Label>(deoptimize,
1728 /* condition_input_index */ 0,
1729 slow_path->GetEntryLabel(),
1730 /* false_target */ nullptr);
1731}
1732
Mingyao Yang063fc772016-08-02 11:02:54 -07001733void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001734 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001735 LocationSummary(flag, LocationSummary::kNoCall);
1736 locations->SetOut(Location::RequiresRegister());
1737}
1738
1739void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1740 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1741 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1742}
1743
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744static bool SelectCanUseCMOV(HSelect* select) {
1745 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001747 return false;
1748 }
1749
1750 // A FP condition doesn't generate the single CC that we need.
1751 // In 32 bit mode, a long condition doesn't generate a single CC either.
1752 HInstruction* condition = select->GetCondition();
1753 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 DataType::Type compare_type = condition->InputAt(0)->GetType();
1755 if (compare_type == DataType::Type::kInt64 ||
1756 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001757 return false;
1758 }
1759 }
1760
1761 // We can generate a CMOV for this Select.
1762 return true;
1763}
1764
David Brazdil74eb1b22015-12-14 11:44:01 +00001765void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001766 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001768 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001769 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001770 } else {
1771 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772 if (SelectCanUseCMOV(select)) {
1773 if (select->InputAt(1)->IsConstant()) {
1774 // Cmov can't handle a constant value.
1775 locations->SetInAt(1, Location::RequiresRegister());
1776 } else {
1777 locations->SetInAt(1, Location::Any());
1778 }
1779 } else {
1780 locations->SetInAt(1, Location::Any());
1781 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001782 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001783 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1784 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001785 }
1786 locations->SetOut(Location::SameAsFirstInput());
1787}
1788
1789void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1790 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001791 DCHECK(locations->InAt(0).Equals(locations->Out()));
1792 if (SelectCanUseCMOV(select)) {
1793 // If both the condition and the source types are integer, we can generate
1794 // a CMOV to implement Select.
1795
1796 HInstruction* select_condition = select->GetCondition();
1797 Condition cond = kNotEqual;
1798
1799 // Figure out how to test the 'condition'.
1800 if (select_condition->IsCondition()) {
1801 HCondition* condition = select_condition->AsCondition();
1802 if (!condition->IsEmittedAtUseSite()) {
1803 // This was a previously materialized condition.
1804 // Can we use the existing condition code?
1805 if (AreEflagsSetFrom(condition, select)) {
1806 // Materialization was the previous instruction. Condition codes are right.
1807 cond = X86Condition(condition->GetCondition());
1808 } else {
1809 // No, we have to recreate the condition code.
1810 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1811 __ testl(cond_reg, cond_reg);
1812 }
1813 } else {
1814 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1816 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001817 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001818 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001819 cond = X86Condition(condition->GetCondition());
1820 }
1821 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001822 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001823 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1824 __ testl(cond_reg, cond_reg);
1825 }
1826
1827 // If the condition is true, overwrite the output, which already contains false.
1828 Location false_loc = locations->InAt(0);
1829 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001831 // 64 bit conditional move.
1832 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1833 Register false_low = false_loc.AsRegisterPairLow<Register>();
1834 if (true_loc.IsRegisterPair()) {
1835 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1836 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1837 } else {
1838 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1839 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1840 }
1841 } else {
1842 // 32 bit conditional move.
1843 Register false_reg = false_loc.AsRegister<Register>();
1844 if (true_loc.IsRegister()) {
1845 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1846 } else {
1847 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1848 }
1849 }
1850 } else {
1851 NearLabel false_target;
1852 GenerateTestAndBranch<NearLabel>(
1853 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1854 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1855 __ Bind(&false_target);
1856 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001857}
1858
David Srbecky0cf44932015-12-09 14:09:59 +00001859void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001860 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001861}
1862
David Srbeckyd28f4a02016-03-14 17:14:24 +00001863void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1864 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001865}
1866
1867void CodeGeneratorX86::GenerateNop() {
1868 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001869}
1870
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001871void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001873 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001874 // Handle the long/FP comparisons made in instruction simplification.
1875 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001877 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001878 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001879 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001880 locations->SetOut(Location::RequiresRegister());
1881 }
1882 break;
1883 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001884 case DataType::Type::kFloat32:
1885 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001886 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001887 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1888 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1889 } else if (cond->InputAt(1)->IsConstant()) {
1890 locations->SetInAt(1, Location::RequiresFpuRegister());
1891 } else {
1892 locations->SetInAt(1, Location::Any());
1893 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001894 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001895 locations->SetOut(Location::RequiresRegister());
1896 }
1897 break;
1898 }
1899 default:
1900 locations->SetInAt(0, Location::RequiresRegister());
1901 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001902 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001903 // We need a byte register.
1904 locations->SetOut(Location::RegisterLocation(ECX));
1905 }
1906 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908}
1909
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001910void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001911 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001913 }
Mark Mendellc4701932015-04-10 13:18:51 -04001914
1915 LocationSummary* locations = cond->GetLocations();
1916 Location lhs = locations->InAt(0);
1917 Location rhs = locations->InAt(1);
1918 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001919 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001920
1921 switch (cond->InputAt(0)->GetType()) {
1922 default: {
1923 // Integer case.
1924
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001925 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001926 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001927 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001928 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001929 return;
1930 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001932 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1933 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001935 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001936 GenerateFPJumps(cond, &true_label, &false_label);
1937 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001939 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001940 GenerateFPJumps(cond, &true_label, &false_label);
1941 break;
1942 }
1943
1944 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001945 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001946
Roland Levillain4fa13f62015-07-06 18:11:54 +01001947 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001948 __ Bind(&false_label);
1949 __ xorl(reg, reg);
1950 __ jmp(&done_label);
1951
Roland Levillain4fa13f62015-07-06 18:11:54 +01001952 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001953 __ Bind(&true_label);
1954 __ movl(reg, Immediate(1));
1955 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001956}
1957
1958void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004}
2005
Aart Bike9f37602015-10-09 11:15:55 -07002006void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002008}
2009
2010void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002039 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002040 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002041 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002042}
2043
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002044void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002045 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002046}
2047
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002048void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2049 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002050 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002051 locations->SetOut(Location::ConstantLocation(constant));
2052}
2053
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002054void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002055 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002056}
2057
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002059 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002060 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002061 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062}
2063
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002064void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 // Will be generated at use site.
2066}
2067
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2069 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002070 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071 locations->SetOut(Location::ConstantLocation(constant));
2072}
2073
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002074void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 // Will be generated at use site.
2076}
2077
2078void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002080 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081 locations->SetOut(Location::ConstantLocation(constant));
2082}
2083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002084void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 // Will be generated at use site.
2086}
2087
Igor Murashkind01745e2017-04-05 16:40:31 -07002088void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2089 constructor_fence->SetLocations(nullptr);
2090}
2091
2092void InstructionCodeGeneratorX86::VisitConstructorFence(
2093 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2094 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2095}
2096
Calin Juravle27df7582015-04-17 19:12:31 +01002097void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2098 memory_barrier->SetLocations(nullptr);
2099}
2100
2101void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002102 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002103}
2104
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002105void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002106 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002107}
2108
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002109void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002111}
2112
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002113void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002114 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002115 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002116 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002117 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002119 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kInt8:
2121 case DataType::Type::kUint16:
2122 case DataType::Type::kInt16:
2123 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 break;
2126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002128 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002130 break;
2131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 case DataType::Type::kFloat32:
2133 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002134 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002136 break;
2137
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002139 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141}
2142
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002143void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002144 if (kIsDebugBuild) {
2145 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002146 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002148 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 case DataType::Type::kInt8:
2150 case DataType::Type::kUint16:
2151 case DataType::Type::kInt16:
2152 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002154 break;
2155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002156 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002157 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2158 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002159 break;
2160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 case DataType::Type::kFloat32:
2162 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002164 break;
2165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002166 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002167 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168 }
2169 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002170 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002171}
2172
Calin Juravle175dc732015-08-25 15:42:32 +01002173void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2174 // The trampoline uses the same calling convention as dex calling conventions,
2175 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2176 // the method_idx.
2177 HandleInvoke(invoke);
2178}
2179
2180void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2181 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2182}
2183
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002184void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002185 // Explicit clinit checks triggered by static invokes must have been pruned by
2186 // art::PrepareForRegisterAllocation.
2187 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002188
Mark Mendellfb8d2792015-03-31 22:16:59 -04002189 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002190 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002191 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002192 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002193 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002194 return;
2195 }
2196
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002197 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002198
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002199 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002200 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002201 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002202 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002203}
2204
Mark Mendell09ed1a32015-03-25 08:30:06 -04002205static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2206 if (invoke->GetLocations()->Intrinsified()) {
2207 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2208 intrinsic.Dispatch(invoke);
2209 return true;
2210 }
2211 return false;
2212}
2213
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002214void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002215 // Explicit clinit checks triggered by static invokes must have been pruned by
2216 // art::PrepareForRegisterAllocation.
2217 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002218
Mark Mendell09ed1a32015-03-25 08:30:06 -04002219 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2220 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002221 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002222
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002223 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002224 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002225 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002226}
2227
2228void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002229 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2230 if (intrinsic.TryDispatch(invoke)) {
2231 return;
2232 }
2233
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002234 HandleInvoke(invoke);
2235}
2236
2237void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002238 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002239 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002240}
2241
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002242void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002243 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2244 return;
2245 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002246
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002247 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002248 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249}
2250
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002251void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002252 // This call to HandleInvoke allocates a temporary (core) register
2253 // which is also used to transfer the hidden argument from FP to
2254 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255 HandleInvoke(invoke);
2256 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002257 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258}
2259
2260void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2261 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 LocationSummary* locations = invoke->GetLocations();
2263 Register temp = locations->GetTemp(0).AsRegister<Register>();
2264 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002265 Location receiver = locations->InAt(0);
2266 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2267
Roland Levillain0d5a2812015-11-13 10:07:31 +00002268 // Set the hidden argument. This is safe to do this here, as XMM7
2269 // won't be modified thereafter, before the `call` instruction.
2270 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002272 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 if (receiver.IsStackSlot()) {
2275 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002276 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 __ movl(temp, Address(temp, class_offset));
2278 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002279 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002280 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281 }
Roland Levillain4d027112015-07-01 15:41:14 +01002282 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002283 // Instead of simply (possibly) unpoisoning `temp` here, we should
2284 // emit a read barrier for the previous class reference load.
2285 // However this is not required in practice, as this is an
2286 // intermediate/temporary reference and because the current
2287 // concurrent copying collector keeps the from-space memory
2288 // intact/accessible until the end of the marking phase (the
2289 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002290 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002291 // temp = temp->GetAddressOfIMT()
2292 __ movl(temp,
2293 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002294 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002295 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002296 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297 __ movl(temp, Address(temp, method_offset));
2298 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002299 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002300 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002301
2302 DCHECK(!codegen_->IsLeafMethod());
2303 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2304}
2305
Orion Hodsonac141392017-01-13 11:53:47 +00002306void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2307 HandleInvoke(invoke);
2308}
2309
2310void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2311 codegen_->GenerateInvokePolymorphicCall(invoke);
2312}
2313
Roland Levillain88cb1752014-10-20 16:36:47 +01002314void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2315 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002316 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 case DataType::Type::kInt32:
2319 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002320 locations->SetInAt(0, Location::RequiresRegister());
2321 locations->SetOut(Location::SameAsFirstInput());
2322 break;
2323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002325 locations->SetInAt(0, Location::RequiresFpuRegister());
2326 locations->SetOut(Location::SameAsFirstInput());
2327 locations->AddTemp(Location::RequiresRegister());
2328 locations->AddTemp(Location::RequiresFpuRegister());
2329 break;
2330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002332 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002333 locations->SetOut(Location::SameAsFirstInput());
2334 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002335 break;
2336
2337 default:
2338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2339 }
2340}
2341
2342void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2343 LocationSummary* locations = neg->GetLocations();
2344 Location out = locations->Out();
2345 Location in = locations->InAt(0);
2346 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002351 break;
2352
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002354 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002355 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002356 __ negl(out.AsRegisterPairLow<Register>());
2357 // Negation is similar to subtraction from zero. The least
2358 // significant byte triggers a borrow when it is different from
2359 // zero; to take it into account, add 1 to the most significant
2360 // byte if the carry flag (CF) is set to 1 after the first NEGL
2361 // operation.
2362 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2363 __ negl(out.AsRegisterPairHigh<Register>());
2364 break;
2365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002367 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 Register constant = locations->GetTemp(0).AsRegister<Register>();
2369 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002370 // Implement float negation with an exclusive or with value
2371 // 0x80000000 (mask for bit 31, representing the sign of a
2372 // single-precision floating-point number).
2373 __ movl(constant, Immediate(INT32_C(0x80000000)));
2374 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002376 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002377 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002380 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002382 // Implement double negation with an exclusive or with value
2383 // 0x8000000000000000 (mask for bit 63, representing the sign of
2384 // a double-precision floating-point number).
2385 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002386 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002387 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002388 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002389
2390 default:
2391 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2392 }
2393}
2394
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002395void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2396 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002397 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002399 locations->SetInAt(0, Location::RequiresFpuRegister());
2400 locations->SetInAt(1, Location::RequiresRegister());
2401 locations->SetOut(Location::SameAsFirstInput());
2402 locations->AddTemp(Location::RequiresFpuRegister());
2403}
2404
2405void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2406 LocationSummary* locations = neg->GetLocations();
2407 Location out = locations->Out();
2408 DCHECK(locations->InAt(0).Equals(out));
2409
2410 Register constant_area = locations->InAt(1).AsRegister<Register>();
2411 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002413 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2414 neg->GetBaseMethodAddress(),
2415 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002416 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2417 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002418 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2419 neg->GetBaseMethodAddress(),
2420 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002421 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2422 }
2423}
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 DataType::Type result_type = conversion->GetResultType();
2427 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002428 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2429 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002430
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002431 // The float-to-long and double-to-long type conversions rely on a
2432 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002433 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002434 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2435 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002436 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002437 : LocationSummary::kNoCall;
2438 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002439 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002440
Roland Levillaindff1f282014-11-05 14:15:05 +00002441 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002442 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002444 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002445 case DataType::Type::kUint8:
2446 case DataType::Type::kInt8:
2447 case DataType::Type::kUint16:
2448 case DataType::Type::kInt16:
2449 case DataType::Type::kInt32:
2450 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2451 // Make the output overlap to please the register allocator. This greatly simplifies
2452 // the validation of the linear scan implementation
2453 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2454 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002455 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002456 HInstruction* input = conversion->InputAt(0);
2457 Location input_location = input->IsConstant()
2458 ? Location::ConstantLocation(input->AsConstant())
2459 : Location::RegisterPairLocation(EAX, EDX);
2460 locations->SetInAt(0, input_location);
2461 // Make the output overlap to please the register allocator. This greatly simplifies
2462 // the validation of the linear scan implementation
2463 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2464 break;
2465 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002466
2467 default:
2468 LOG(FATAL) << "Unexpected type conversion from " << input_type
2469 << " to " << result_type;
2470 }
2471 break;
2472
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002473 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002475 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2476 locations->SetInAt(0, Location::Any());
2477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002478 break;
2479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002481 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488 locations->SetInAt(0, Location::RequiresFpuRegister());
2489 locations->SetOut(Location::RequiresRegister());
2490 locations->AddTemp(Location::RequiresFpuRegister());
2491 break;
2492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetOut(Location::RequiresRegister());
2496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002508 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002509 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002510 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 case DataType::Type::kInt16:
2512 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 locations->SetInAt(0, Location::RegisterLocation(EAX));
2514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2515 break;
2516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002517 case DataType::Type::kFloat32:
2518 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002519 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002520 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2521 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2522
Vladimir Marko949c91f2015-01-27 10:48:44 +00002523 // The runtime helper puts the result in EAX, EDX.
2524 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002525 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002526 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002535 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002537 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002539 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kInt16:
2541 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetOut(Location::RequiresFpuRegister());
2544 break;
2545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002547 locations->SetInAt(0, Location::Any());
2548 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002549 break;
2550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002552 locations->SetInAt(0, Location::RequiresFpuRegister());
2553 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002559 }
Roland Levillaincff13742014-11-17 14:32:17 +00002560 break;
2561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002563 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002565 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002567 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002568 case DataType::Type::kInt16:
2569 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002570 locations->SetInAt(0, Location::RequiresRegister());
2571 locations->SetOut(Location::RequiresFpuRegister());
2572 break;
2573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002574 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002575 locations->SetInAt(0, Location::Any());
2576 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002577 break;
2578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002580 locations->SetInAt(0, Location::RequiresFpuRegister());
2581 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002582 break;
2583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 break;
2589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
2594}
2595
2596void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2597 LocationSummary* locations = conversion->GetLocations();
2598 Location out = locations->Out();
2599 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 DataType::Type result_type = conversion->GetResultType();
2601 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002602 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2603 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002604 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002605 case DataType::Type::kUint8:
2606 switch (input_type) {
2607 case DataType::Type::kInt8:
2608 case DataType::Type::kUint16:
2609 case DataType::Type::kInt16:
2610 case DataType::Type::kInt32:
2611 if (in.IsRegister()) {
2612 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2613 } else {
2614 DCHECK(in.GetConstant()->IsIntConstant());
2615 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2616 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2617 }
2618 break;
2619 case DataType::Type::kInt64:
2620 if (in.IsRegisterPair()) {
2621 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2622 } else {
2623 DCHECK(in.GetConstant()->IsLongConstant());
2624 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2625 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2626 }
2627 break;
2628
2629 default:
2630 LOG(FATAL) << "Unexpected type conversion from " << input_type
2631 << " to " << result_type;
2632 }
2633 break;
2634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002636 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002637 case DataType::Type::kUint8:
2638 case DataType::Type::kUint16:
2639 case DataType::Type::kInt16:
2640 case DataType::Type::kInt32:
2641 if (in.IsRegister()) {
2642 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2643 } else {
2644 DCHECK(in.GetConstant()->IsIntConstant());
2645 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2646 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2647 }
2648 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002650 if (in.IsRegisterPair()) {
2651 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2652 } else {
2653 DCHECK(in.GetConstant()->IsLongConstant());
2654 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2655 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2656 }
2657 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663 break;
2664
2665 case DataType::Type::kUint16:
2666 switch (input_type) {
2667 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kInt16:
2669 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002670 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002671 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2672 } else if (in.IsStackSlot()) {
2673 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002674 } else {
2675 DCHECK(in.GetConstant()->IsIntConstant());
2676 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002677 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2678 }
2679 break;
2680 case DataType::Type::kInt64:
2681 if (in.IsRegisterPair()) {
2682 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2683 } else if (in.IsDoubleStackSlot()) {
2684 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2685 } else {
2686 DCHECK(in.GetConstant()->IsLongConstant());
2687 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2688 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002689 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002690 break;
2691
2692 default:
2693 LOG(FATAL) << "Unexpected type conversion from " << input_type
2694 << " to " << result_type;
2695 }
2696 break;
2697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002699 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002701 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002702 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002703 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002704 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002706 } else {
2707 DCHECK(in.GetConstant()->IsIntConstant());
2708 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002710 }
2711 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002712 case DataType::Type::kInt64:
2713 if (in.IsRegisterPair()) {
2714 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2715 } else if (in.IsDoubleStackSlot()) {
2716 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2717 } else {
2718 DCHECK(in.GetConstant()->IsLongConstant());
2719 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2720 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2721 }
2722 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002723
2724 default:
2725 LOG(FATAL) << "Unexpected type conversion from " << input_type
2726 << " to " << result_type;
2727 }
2728 break;
2729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002731 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002733 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002735 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002737 } else {
2738 DCHECK(in.IsConstant());
2739 DCHECK(in.GetConstant()->IsLongConstant());
2740 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002741 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002742 }
2743 break;
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002746 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2747 Register output = out.AsRegister<Register>();
2748 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002749 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002750
2751 __ movl(output, Immediate(kPrimIntMax));
2752 // temp = int-to-float(output)
2753 __ cvtsi2ss(temp, output);
2754 // if input >= temp goto done
2755 __ comiss(input, temp);
2756 __ j(kAboveEqual, &done);
2757 // if input == NaN goto nan
2758 __ j(kUnordered, &nan);
2759 // output = float-to-int-truncate(input)
2760 __ cvttss2si(output, input);
2761 __ jmp(&done);
2762 __ Bind(&nan);
2763 // output = 0
2764 __ xorl(output, output);
2765 __ Bind(&done);
2766 break;
2767 }
2768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002770 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2771 Register output = out.AsRegister<Register>();
2772 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002773 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002774
2775 __ movl(output, Immediate(kPrimIntMax));
2776 // temp = int-to-double(output)
2777 __ cvtsi2sd(temp, output);
2778 // if input >= temp goto done
2779 __ comisd(input, temp);
2780 __ j(kAboveEqual, &done);
2781 // if input == NaN goto nan
2782 __ j(kUnordered, &nan);
2783 // output = double-to-int-truncate(input)
2784 __ cvttsd2si(output, input);
2785 __ jmp(&done);
2786 __ Bind(&nan);
2787 // output = 0
2788 __ xorl(output, output);
2789 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002790 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002791 }
Roland Levillain946e1432014-11-11 17:35:19 +00002792
2793 default:
2794 LOG(FATAL) << "Unexpected type conversion from " << input_type
2795 << " to " << result_type;
2796 }
2797 break;
2798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002802 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002804 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kInt16:
2806 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002807 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2808 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002810 __ cdq();
2811 break;
2812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002814 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002815 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002816 break;
2817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002819 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002820 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 break;
2822
2823 default:
2824 LOG(FATAL) << "Unexpected type conversion from " << input_type
2825 << " to " << result_type;
2826 }
2827 break;
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002830 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002831 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002832 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002834 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kInt16:
2836 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002838 break;
2839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002841 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002842
Roland Levillain232ade02015-04-20 15:14:36 +01002843 // Create stack space for the call to
2844 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2845 // TODO: enhance register allocator to ask for stack temporaries.
2846 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002848 __ subl(ESP, Immediate(adjustment));
2849 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002850
Roland Levillain232ade02015-04-20 15:14:36 +01002851 // Load the value to the FP stack, using temporaries if needed.
2852 PushOntoFPStack(in, 0, adjustment, false, true);
2853
2854 if (out.IsStackSlot()) {
2855 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2856 } else {
2857 __ fstps(Address(ESP, 0));
2858 Location stack_temp = Location::StackSlot(0);
2859 codegen_->Move32(out, stack_temp);
2860 }
2861
2862 // Remove the temporary stack space we allocated.
2863 if (adjustment != 0) {
2864 __ addl(ESP, Immediate(adjustment));
2865 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002866 break;
2867 }
2868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002870 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002876 }
Roland Levillaincff13742014-11-17 14:32:17 +00002877 break;
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002880 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002881 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002882 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002884 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kInt16:
2886 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002888 break;
2889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002891 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002892
Roland Levillain232ade02015-04-20 15:14:36 +01002893 // Create stack space for the call to
2894 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2895 // TODO: enhance register allocator to ask for stack temporaries.
2896 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002898 __ subl(ESP, Immediate(adjustment));
2899 }
2900
2901 // Load the value to the FP stack, using temporaries if needed.
2902 PushOntoFPStack(in, 0, adjustment, false, true);
2903
2904 if (out.IsDoubleStackSlot()) {
2905 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2906 } else {
2907 __ fstpl(Address(ESP, 0));
2908 Location stack_temp = Location::DoubleStackSlot(0);
2909 codegen_->Move64(out, stack_temp);
2910 }
2911
2912 // Remove the temporary stack space we allocated.
2913 if (adjustment != 0) {
2914 __ addl(ESP, Immediate(adjustment));
2915 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002916 break;
2917 }
2918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002920 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002921 break;
2922
2923 default:
2924 LOG(FATAL) << "Unexpected type conversion from " << input_type
2925 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002926 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002927 break;
2928
2929 default:
2930 LOG(FATAL) << "Unexpected type conversion from " << input_type
2931 << " to " << result_type;
2932 }
2933}
2934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002936 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002937 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002940 locations->SetInAt(0, Location::RequiresRegister());
2941 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2943 break;
2944 }
2945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::Any());
2949 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002950 break;
2951 }
2952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kFloat32:
2954 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002956 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2957 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002958 } else if (add->InputAt(1)->IsConstant()) {
2959 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002960 } else {
2961 locations->SetInAt(1, Location::Any());
2962 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002964 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002966
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2969 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002970 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971}
2972
2973void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2974 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002975 Location first = locations->InAt(0);
2976 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002977 Location out = locations->Out();
2978
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002979 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002980 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002982 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2983 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002984 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2985 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002986 } else {
2987 __ leal(out.AsRegister<Register>(), Address(
2988 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2989 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002991 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2992 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2993 __ addl(out.AsRegister<Register>(), Immediate(value));
2994 } else {
2995 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2996 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002997 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002998 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002999 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003000 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003002 }
3003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003005 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003006 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3007 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003008 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003009 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3010 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003011 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003012 } else {
3013 DCHECK(second.IsConstant()) << second;
3014 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3015 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3016 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003017 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003018 break;
3019 }
3020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003021 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003022 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003024 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3025 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003026 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003027 __ addss(first.AsFpuRegister<XmmRegister>(),
3028 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003029 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3030 const_area->GetBaseMethodAddress(),
3031 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003032 } else {
3033 DCHECK(second.IsStackSlot());
3034 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003036 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003037 }
3038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003039 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003042 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3043 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003044 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 __ addsd(first.AsFpuRegister<XmmRegister>(),
3046 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003047 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3048 const_area->GetBaseMethodAddress(),
3049 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003050 } else {
3051 DCHECK(second.IsDoubleStackSlot());
3052 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003053 }
3054 break;
3055 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003056
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003057 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003059 }
3060}
3061
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003062void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003064 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003065 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kInt32:
3067 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003068 locations->SetInAt(0, Location::RequiresRegister());
3069 locations->SetInAt(1, Location::Any());
3070 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003071 break;
3072 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003073 case DataType::Type::kFloat32:
3074 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003075 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003076 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3077 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003078 } else if (sub->InputAt(1)->IsConstant()) {
3079 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003080 } else {
3081 locations->SetInAt(1, Location::Any());
3082 }
Calin Juravle11351682014-10-23 15:38:15 +01003083 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003084 break;
Calin Juravle11351682014-10-23 15:38:15 +01003085 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003086
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087 default:
Calin Juravle11351682014-10-23 15:38:15 +01003088 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003089 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003090}
3091
3092void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3093 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 Location first = locations->InAt(0);
3095 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003096 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003097 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003098 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003100 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003101 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003102 __ subl(first.AsRegister<Register>(),
3103 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003104 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003106 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003107 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003108 }
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003111 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003112 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3113 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003114 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003115 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003116 __ sbbl(first.AsRegisterPairHigh<Register>(),
3117 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003118 } else {
3119 DCHECK(second.IsConstant()) << second;
3120 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3121 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3122 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003123 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003124 break;
3125 }
3126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003128 if (second.IsFpuRegister()) {
3129 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3130 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3131 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003132 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003133 __ subss(first.AsFpuRegister<XmmRegister>(),
3134 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003135 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3136 const_area->GetBaseMethodAddress(),
3137 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003138 } else {
3139 DCHECK(second.IsStackSlot());
3140 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3141 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003142 break;
Calin Juravle11351682014-10-23 15:38:15 +01003143 }
3144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 if (second.IsFpuRegister()) {
3147 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3148 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3149 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003150 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003151 __ subsd(first.AsFpuRegister<XmmRegister>(),
3152 codegen_->LiteralDoubleAddress(
3153 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003154 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003155 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3156 } else {
3157 DCHECK(second.IsDoubleStackSlot());
3158 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3159 }
Calin Juravle11351682014-10-23 15:38:15 +01003160 break;
3161 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003162
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003163 default:
Calin Juravle11351682014-10-23 15:38:15 +01003164 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003165 }
3166}
3167
Calin Juravle34bacdf2014-10-07 20:23:36 +01003168void LocationsBuilderX86::VisitMul(HMul* mul) {
3169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003170 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003172 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 locations->SetInAt(0, Location::RequiresRegister());
3174 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003175 if (mul->InputAt(1)->IsIntConstant()) {
3176 // Can use 3 operand multiply.
3177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3178 } else {
3179 locations->SetOut(Location::SameAsFirstInput());
3180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003182 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 locations->SetInAt(1, Location::Any());
3185 locations->SetOut(Location::SameAsFirstInput());
3186 // Needed for imul on 32bits with 64bits output.
3187 locations->AddTemp(Location::RegisterLocation(EAX));
3188 locations->AddTemp(Location::RegisterLocation(EDX));
3189 break;
3190 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kFloat32:
3192 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003193 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003194 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3195 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003196 } else if (mul->InputAt(1)->IsConstant()) {
3197 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003198 } else {
3199 locations->SetInAt(1, Location::Any());
3200 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204
3205 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003206 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 }
3208}
3209
3210void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3211 LocationSummary* locations = mul->GetLocations();
3212 Location first = locations->InAt(0);
3213 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003214 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215
3216 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003218 // The constant may have ended up in a register, so test explicitly to avoid
3219 // problems where the output may not be the same as the first operand.
3220 if (mul->InputAt(1)->IsIntConstant()) {
3221 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3222 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3223 } else if (second.IsRegister()) {
3224 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226 } else {
3227 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003228 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 }
3231 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003234 Register in1_hi = first.AsRegisterPairHigh<Register>();
3235 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 Register eax = locations->GetTemp(0).AsRegister<Register>();
3237 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238
3239 DCHECK_EQ(EAX, eax);
3240 DCHECK_EQ(EDX, edx);
3241
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003242 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003243 // output: in1
3244 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3245 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3246 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003247 if (second.IsConstant()) {
3248 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003250 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3251 int32_t low_value = Low32Bits(value);
3252 int32_t high_value = High32Bits(value);
3253 Immediate low(low_value);
3254 Immediate high(high_value);
3255
3256 __ movl(eax, high);
3257 // eax <- in1.lo * in2.hi
3258 __ imull(eax, in1_lo);
3259 // in1.hi <- in1.hi * in2.lo
3260 __ imull(in1_hi, low);
3261 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3262 __ addl(in1_hi, eax);
3263 // move in2_lo to eax to prepare for double precision
3264 __ movl(eax, low);
3265 // edx:eax <- in1.lo * in2.lo
3266 __ mull(in1_lo);
3267 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3268 __ addl(in1_hi, edx);
3269 // in1.lo <- (in1.lo * in2.lo)[31:0];
3270 __ movl(in1_lo, eax);
3271 } else if (second.IsRegisterPair()) {
3272 Register in2_hi = second.AsRegisterPairHigh<Register>();
3273 Register in2_lo = second.AsRegisterPairLow<Register>();
3274
3275 __ movl(eax, in2_hi);
3276 // eax <- in1.lo * in2.hi
3277 __ imull(eax, in1_lo);
3278 // in1.hi <- in1.hi * in2.lo
3279 __ imull(in1_hi, in2_lo);
3280 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3281 __ addl(in1_hi, eax);
3282 // move in1_lo to eax to prepare for double precision
3283 __ movl(eax, in1_lo);
3284 // edx:eax <- in1.lo * in2.lo
3285 __ mull(in2_lo);
3286 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3287 __ addl(in1_hi, edx);
3288 // in1.lo <- (in1.lo * in2.lo)[31:0];
3289 __ movl(in1_lo, eax);
3290 } else {
3291 DCHECK(second.IsDoubleStackSlot()) << second;
3292 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3293 Address in2_lo(ESP, second.GetStackIndex());
3294
3295 __ movl(eax, in2_hi);
3296 // eax <- in1.lo * in2.hi
3297 __ imull(eax, in1_lo);
3298 // in1.hi <- in1.hi * in2.lo
3299 __ imull(in1_hi, in2_lo);
3300 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3301 __ addl(in1_hi, eax);
3302 // move in1_lo to eax to prepare for double precision
3303 __ movl(eax, in1_lo);
3304 // edx:eax <- in1.lo * in2.lo
3305 __ mull(in2_lo);
3306 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3307 __ addl(in1_hi, edx);
3308 // in1.lo <- (in1.lo * in2.lo)[31:0];
3309 __ movl(in1_lo, eax);
3310 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311
3312 break;
3313 }
3314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003316 DCHECK(first.Equals(locations->Out()));
3317 if (second.IsFpuRegister()) {
3318 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3319 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3320 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003321 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003322 __ mulss(first.AsFpuRegister<XmmRegister>(),
3323 codegen_->LiteralFloatAddress(
3324 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003325 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003326 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3327 } else {
3328 DCHECK(second.IsStackSlot());
3329 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3330 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003331 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003332 }
3333
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003334 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003335 DCHECK(first.Equals(locations->Out()));
3336 if (second.IsFpuRegister()) {
3337 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3338 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3339 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003340 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003341 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3342 codegen_->LiteralDoubleAddress(
3343 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003344 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3346 } else {
3347 DCHECK(second.IsDoubleStackSlot());
3348 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3349 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003350 break;
3351 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352
3353 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003354 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355 }
3356}
3357
Roland Levillain232ade02015-04-20 15:14:36 +01003358void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3359 uint32_t temp_offset,
3360 uint32_t stack_adjustment,
3361 bool is_fp,
3362 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003363 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003364 DCHECK(!is_wide);
3365 if (is_fp) {
3366 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3367 } else {
3368 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3369 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003370 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003371 DCHECK(is_wide);
3372 if (is_fp) {
3373 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3374 } else {
3375 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3376 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003377 } else {
3378 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003379 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003380 Location stack_temp = Location::StackSlot(temp_offset);
3381 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003382 if (is_fp) {
3383 __ flds(Address(ESP, temp_offset));
3384 } else {
3385 __ filds(Address(ESP, temp_offset));
3386 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003387 } else {
3388 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3389 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003390 if (is_fp) {
3391 __ fldl(Address(ESP, temp_offset));
3392 } else {
3393 __ fildl(Address(ESP, temp_offset));
3394 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003395 }
3396 }
3397}
3398
3399void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 DataType::Type type = rem->GetResultType();
3401 bool is_float = type == DataType::Type::kFloat32;
3402 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403 LocationSummary* locations = rem->GetLocations();
3404 Location first = locations->InAt(0);
3405 Location second = locations->InAt(1);
3406 Location out = locations->Out();
3407
3408 // Create stack space for 2 elements.
3409 // TODO: enhance register allocator to ask for stack temporaries.
3410 __ subl(ESP, Immediate(2 * elem_size));
3411
3412 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003413 const bool is_wide = !is_float;
3414 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3415 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003416
3417 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003418 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003419 __ Bind(&retry);
3420 __ fprem();
3421
3422 // Move FP status to AX.
3423 __ fstsw();
3424
3425 // And see if the argument reduction is complete. This is signaled by the
3426 // C2 FPU flag bit set to 0.
3427 __ andl(EAX, Immediate(kC2ConditionMask));
3428 __ j(kNotEqual, &retry);
3429
3430 // We have settled on the final value. Retrieve it into an XMM register.
3431 // Store FP top of stack to real stack.
3432 if (is_float) {
3433 __ fsts(Address(ESP, 0));
3434 } else {
3435 __ fstl(Address(ESP, 0));
3436 }
3437
3438 // Pop the 2 items from the FP stack.
3439 __ fucompp();
3440
3441 // Load the value from the stack into an XMM register.
3442 DCHECK(out.IsFpuRegister()) << out;
3443 if (is_float) {
3444 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3445 } else {
3446 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3447 }
3448
3449 // And remove the temporary stack space we allocated.
3450 __ addl(ESP, Immediate(2 * elem_size));
3451}
3452
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453
3454void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3455 DCHECK(instruction->IsDiv() || instruction->IsRem());
3456
3457 LocationSummary* locations = instruction->GetLocations();
3458 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003459 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003460
3461 Register out_register = locations->Out().AsRegister<Register>();
3462 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003463 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464
3465 DCHECK(imm == 1 || imm == -1);
3466
3467 if (instruction->IsRem()) {
3468 __ xorl(out_register, out_register);
3469 } else {
3470 __ movl(out_register, input_register);
3471 if (imm == -1) {
3472 __ negl(out_register);
3473 }
3474 }
3475}
3476
3477
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003478void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479 LocationSummary* locations = instruction->GetLocations();
3480
3481 Register out_register = locations->Out().AsRegister<Register>();
3482 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003483 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003484 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3485 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 Register num = locations->GetTemp(0).AsRegister<Register>();
3488
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003489 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 __ testl(input_register, input_register);
3491 __ cmovl(kGreaterEqual, num, input_register);
3492 int shift = CTZ(imm);
3493 __ sarl(num, Immediate(shift));
3494
3495 if (imm < 0) {
3496 __ negl(num);
3497 }
3498
3499 __ movl(out_register, num);
3500}
3501
3502void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3503 DCHECK(instruction->IsDiv() || instruction->IsRem());
3504
3505 LocationSummary* locations = instruction->GetLocations();
3506 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3507
3508 Register eax = locations->InAt(0).AsRegister<Register>();
3509 Register out = locations->Out().AsRegister<Register>();
3510 Register num;
3511 Register edx;
3512
3513 if (instruction->IsDiv()) {
3514 edx = locations->GetTemp(0).AsRegister<Register>();
3515 num = locations->GetTemp(1).AsRegister<Register>();
3516 } else {
3517 edx = locations->Out().AsRegister<Register>();
3518 num = locations->GetTemp(0).AsRegister<Register>();
3519 }
3520
3521 DCHECK_EQ(EAX, eax);
3522 DCHECK_EQ(EDX, edx);
3523 if (instruction->IsDiv()) {
3524 DCHECK_EQ(EAX, out);
3525 } else {
3526 DCHECK_EQ(EDX, out);
3527 }
3528
3529 int64_t magic;
3530 int shift;
3531 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3532
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533 // Save the numerator.
3534 __ movl(num, eax);
3535
3536 // EAX = magic
3537 __ movl(eax, Immediate(magic));
3538
3539 // EDX:EAX = magic * numerator
3540 __ imull(num);
3541
3542 if (imm > 0 && magic < 0) {
3543 // EDX += num
3544 __ addl(edx, num);
3545 } else if (imm < 0 && magic > 0) {
3546 __ subl(edx, num);
3547 }
3548
3549 // Shift if needed.
3550 if (shift != 0) {
3551 __ sarl(edx, Immediate(shift));
3552 }
3553
3554 // EDX += 1 if EDX < 0
3555 __ movl(eax, edx);
3556 __ shrl(edx, Immediate(31));
3557 __ addl(edx, eax);
3558
3559 if (instruction->IsRem()) {
3560 __ movl(eax, num);
3561 __ imull(edx, Immediate(imm));
3562 __ subl(eax, edx);
3563 __ movl(edx, eax);
3564 } else {
3565 __ movl(eax, edx);
3566 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003567}
3568
Calin Juravlebacfec32014-11-14 15:54:36 +00003569void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3570 DCHECK(instruction->IsDiv() || instruction->IsRem());
3571
3572 LocationSummary* locations = instruction->GetLocations();
3573 Location out = locations->Out();
3574 Location first = locations->InAt(0);
3575 Location second = locations->InAt(1);
3576 bool is_div = instruction->IsDiv();
3577
3578 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003579 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 DCHECK_EQ(EAX, first.AsRegister<Register>());
3581 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003582
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003583 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003584 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585
3586 if (imm == 0) {
3587 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3588 } else if (imm == 1 || imm == -1) {
3589 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003590 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003591 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 } else {
3593 DCHECK(imm <= -2 || imm >= 2);
3594 GenerateDivRemWithAnyConstant(instruction);
3595 }
3596 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003597 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003598 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003600
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601 Register second_reg = second.AsRegister<Register>();
3602 // 0x80000000/-1 triggers an arithmetic exception!
3603 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3604 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 __ cmpl(second_reg, Immediate(-1));
3607 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003608
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 // edx:eax <- sign-extended of eax
3610 __ cdq();
3611 // eax = quotient, edx = remainder
3612 __ idivl(second_reg);
3613 __ Bind(slow_path->GetExitLabel());
3614 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 break;
3616 }
3617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003619 InvokeRuntimeCallingConvention calling_convention;
3620 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3621 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3622 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3623 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3624 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3625 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3626
3627 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003628 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003629 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003631 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003632 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636
3637 default:
3638 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3639 }
3640}
3641
Calin Juravle7c4954d2014-10-28 16:57:40 +00003642void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003643 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003644 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003645 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003646 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003647
Calin Juravle7c4954d2014-10-28 16:57:40 +00003648 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003650 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003652 locations->SetOut(Location::SameAsFirstInput());
3653 // Intel uses edx:eax as the dividend.
3654 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003655 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3656 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3657 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003658 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003659 locations->AddTemp(Location::RequiresRegister());
3660 }
Calin Juravled0d48522014-11-04 16:40:20 +00003661 break;
3662 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 InvokeRuntimeCallingConvention calling_convention;
3665 locations->SetInAt(0, Location::RegisterPairLocation(
3666 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3667 locations->SetInAt(1, Location::RegisterPairLocation(
3668 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3669 // Runtime helper puts the result in EAX, EDX.
3670 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 break;
3672 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 case DataType::Type::kFloat32:
3674 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003675 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003676 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3677 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003678 } else if (div->InputAt(1)->IsConstant()) {
3679 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003680 } else {
3681 locations->SetInAt(1, Location::Any());
3682 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 locations->SetOut(Location::SameAsFirstInput());
3684 break;
3685 }
3686
3687 default:
3688 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3689 }
3690}
3691
3692void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3693 LocationSummary* locations = div->GetLocations();
3694 Location first = locations->InAt(0);
3695 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003696
3697 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003698 case DataType::Type::kInt32:
3699 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003700 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003701 break;
3702 }
3703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003705 if (second.IsFpuRegister()) {
3706 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3707 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3708 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003709 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003710 __ divss(first.AsFpuRegister<XmmRegister>(),
3711 codegen_->LiteralFloatAddress(
3712 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003713 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003714 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3715 } else {
3716 DCHECK(second.IsStackSlot());
3717 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3718 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719 break;
3720 }
3721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003723 if (second.IsFpuRegister()) {
3724 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3725 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3726 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003727 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 __ divsd(first.AsFpuRegister<XmmRegister>(),
3729 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003730 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3731 const_area->GetBaseMethodAddress(),
3732 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 } else {
3734 DCHECK(second.IsDoubleStackSlot());
3735 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3736 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003737 break;
3738 }
3739
3740 default:
3741 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3742 }
3743}
3744
Calin Juravlebacfec32014-11-14 15:54:36 +00003745void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003746 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003749 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003750 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003751 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003752
Calin Juravled2ec87d2014-12-08 14:24:46 +00003753 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003755 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003756 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003757 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003758 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3759 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3760 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003761 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003762 locations->AddTemp(Location::RequiresRegister());
3763 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 break;
3765 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003766 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003767 InvokeRuntimeCallingConvention calling_convention;
3768 locations->SetInAt(0, Location::RegisterPairLocation(
3769 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3770 locations->SetInAt(1, Location::RegisterPairLocation(
3771 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3772 // Runtime helper puts the result in EAX, EDX.
3773 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3774 break;
3775 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003776 case DataType::Type::kFloat64:
3777 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003778 locations->SetInAt(0, Location::Any());
3779 locations->SetInAt(1, Location::Any());
3780 locations->SetOut(Location::RequiresFpuRegister());
3781 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003782 break;
3783 }
3784
3785 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003786 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003787 }
3788}
3789
3790void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003792 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 case DataType::Type::kInt32:
3794 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003795 GenerateDivRemIntegral(rem);
3796 break;
3797 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kFloat32:
3799 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003800 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 break;
3802 }
3803 default:
3804 LOG(FATAL) << "Unexpected rem type " << type;
3805 }
3806}
3807
Aart Bik1f8d51b2018-02-15 10:42:37 -08003808static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3809 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3810 switch (minmax->GetResultType()) {
3811 case DataType::Type::kInt32:
3812 locations->SetInAt(0, Location::RequiresRegister());
3813 locations->SetInAt(1, Location::RequiresRegister());
3814 locations->SetOut(Location::SameAsFirstInput());
3815 break;
3816 case DataType::Type::kInt64:
3817 locations->SetInAt(0, Location::RequiresRegister());
3818 locations->SetInAt(1, Location::RequiresRegister());
3819 locations->SetOut(Location::SameAsFirstInput());
3820 // Register to use to perform a long subtract to set cc.
3821 locations->AddTemp(Location::RequiresRegister());
3822 break;
3823 case DataType::Type::kFloat32:
3824 locations->SetInAt(0, Location::RequiresFpuRegister());
3825 locations->SetInAt(1, Location::RequiresFpuRegister());
3826 locations->SetOut(Location::SameAsFirstInput());
3827 locations->AddTemp(Location::RequiresRegister());
3828 break;
3829 case DataType::Type::kFloat64:
3830 locations->SetInAt(0, Location::RequiresFpuRegister());
3831 locations->SetInAt(1, Location::RequiresFpuRegister());
3832 locations->SetOut(Location::SameAsFirstInput());
3833 break;
3834 default:
3835 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3836 }
3837}
3838
3839void InstructionCodeGeneratorX86::GenerateMinMax(LocationSummary* locations,
3840 bool is_min,
3841 DataType::Type type) {
3842 Location op1_loc = locations->InAt(0);
3843 Location op2_loc = locations->InAt(1);
3844
3845 // Shortcut for same input locations.
3846 if (op1_loc.Equals(op2_loc)) {
3847 // Can return immediately, as op1_loc == out_loc.
3848 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3849 // a copy here.
3850 DCHECK(locations->Out().Equals(op1_loc));
3851 return;
3852 }
3853
3854 if (type == DataType::Type::kInt64) {
3855 // Need to perform a subtract to get the sign right.
3856 // op1 is already in the same location as the output.
3857 Location output = locations->Out();
3858 Register output_lo = output.AsRegisterPairLow<Register>();
3859 Register output_hi = output.AsRegisterPairHigh<Register>();
3860
3861 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3862 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3863
3864 // The comparison is performed by subtracting the second operand from
3865 // the first operand and then setting the status flags in the same
3866 // manner as the SUB instruction."
3867 __ cmpl(output_lo, op2_lo);
3868
3869 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3870 Register temp = locations->GetTemp(0).AsRegister<Register>();
3871 __ movl(temp, output_hi);
3872 __ sbbl(temp, op2_hi);
3873
3874 // Now the condition code is correct.
3875 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3876 __ cmovl(cond, output_lo, op2_lo);
3877 __ cmovl(cond, output_hi, op2_hi);
3878 } else {
3879 DCHECK_EQ(type, DataType::Type::kInt32);
3880 Register out = locations->Out().AsRegister<Register>();
3881 Register op2 = op2_loc.AsRegister<Register>();
3882
3883 // (out := op1)
3884 // out <=? op2
3885 // if out is min jmp done
3886 // out := op2
3887 // done:
3888
3889 __ cmpl(out, op2);
3890 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3891 __ cmovl(cond, out, op2);
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3896 bool is_min,
3897 DataType::Type type) {
3898 Location op1_loc = locations->InAt(0);
3899 Location op2_loc = locations->InAt(1);
3900 Location out_loc = locations->Out();
3901 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3902
3903 // Shortcut for same input locations.
3904 if (op1_loc.Equals(op2_loc)) {
3905 DCHECK(out_loc.Equals(op1_loc));
3906 return;
3907 }
3908
3909 // (out := op1)
3910 // out <=? op2
3911 // if Nan jmp Nan_label
3912 // if out is min jmp done
3913 // if op2 is min jmp op2_label
3914 // handle -0/+0
3915 // jmp done
3916 // Nan_label:
3917 // out := NaN
3918 // op2_label:
3919 // out := op2
3920 // done:
3921 //
3922 // This removes one jmp, but needs to copy one input (op1) to out.
3923 //
3924 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3925
3926 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3927
3928 NearLabel nan, done, op2_label;
3929 if (type == DataType::Type::kFloat64) {
3930 __ ucomisd(out, op2);
3931 } else {
3932 DCHECK_EQ(type, DataType::Type::kFloat32);
3933 __ ucomiss(out, op2);
3934 }
3935
3936 __ j(Condition::kParityEven, &nan);
3937
3938 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3939 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3940
3941 // Handle 0.0/-0.0.
3942 if (is_min) {
3943 if (type == DataType::Type::kFloat64) {
3944 __ orpd(out, op2);
3945 } else {
3946 __ orps(out, op2);
3947 }
3948 } else {
3949 if (type == DataType::Type::kFloat64) {
3950 __ andpd(out, op2);
3951 } else {
3952 __ andps(out, op2);
3953 }
3954 }
3955 __ jmp(&done);
3956
3957 // NaN handling.
3958 __ Bind(&nan);
3959 if (type == DataType::Type::kFloat64) {
3960 // TODO: Use a constant from the constant table (requires extra input).
3961 __ LoadLongConstant(out, kDoubleNaN);
3962 } else {
3963 Register constant = locations->GetTemp(0).AsRegister<Register>();
3964 __ movl(constant, Immediate(kFloatNaN));
3965 __ movd(out, constant);
3966 }
3967 __ jmp(&done);
3968
3969 // out := op2;
3970 __ Bind(&op2_label);
3971 if (type == DataType::Type::kFloat64) {
3972 __ movsd(out, op2);
3973 } else {
3974 __ movss(out, op2);
3975 }
3976
3977 // Done.
3978 __ Bind(&done);
3979}
3980
3981void LocationsBuilderX86::VisitMin(HMin* min) {
3982 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
3983}
3984
3985void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
3986 switch (min->GetResultType()) {
3987 case DataType::Type::kInt32:
3988 case DataType::Type::kInt64:
3989 GenerateMinMax(min->GetLocations(), /*is_min*/ true, min->GetResultType());
3990 break;
3991 case DataType::Type::kFloat32:
3992 case DataType::Type::kFloat64:
3993 GenerateMinMaxFP(min->GetLocations(), /*is_min*/ true, min->GetResultType());
3994 break;
3995 default:
3996 LOG(FATAL) << "Unexpected type for HMin " << min->GetResultType();
3997 }
3998}
3999
4000void LocationsBuilderX86::VisitMax(HMax* max) {
4001 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4002}
4003
4004void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
4005 switch (max->GetResultType()) {
4006 case DataType::Type::kInt32:
4007 case DataType::Type::kInt64:
4008 GenerateMinMax(max->GetLocations(), /*is_min*/ false, max->GetResultType());
4009 break;
4010 case DataType::Type::kFloat32:
4011 case DataType::Type::kFloat64:
4012 GenerateMinMaxFP(max->GetLocations(), /*is_min*/ false, max->GetResultType());
4013 break;
4014 default:
4015 LOG(FATAL) << "Unexpected type for HMax " << max->GetResultType();
4016 }
4017}
4018
Aart Bik3dad3412018-02-28 12:01:46 -08004019void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4020 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4021 switch (abs->GetResultType()) {
4022 case DataType::Type::kInt32:
4023 locations->SetInAt(0, Location::RegisterLocation(EAX));
4024 locations->SetOut(Location::SameAsFirstInput());
4025 locations->AddTemp(Location::RegisterLocation(EDX));
4026 break;
4027 case DataType::Type::kInt64:
4028 locations->SetInAt(0, Location::RequiresRegister());
4029 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4030 locations->AddTemp(Location::RequiresRegister());
4031 break;
4032 case DataType::Type::kFloat32:
4033 locations->SetInAt(0, Location::RequiresFpuRegister());
4034 locations->SetOut(Location::SameAsFirstInput());
4035 locations->AddTemp(Location::RequiresFpuRegister());
4036 locations->AddTemp(Location::RequiresRegister());
4037 break;
4038 case DataType::Type::kFloat64:
4039 locations->SetInAt(0, Location::RequiresFpuRegister());
4040 locations->SetOut(Location::SameAsFirstInput());
4041 locations->AddTemp(Location::RequiresFpuRegister());
4042 break;
4043 default:
4044 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4045 }
4046}
4047
4048void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4049 LocationSummary* locations = abs->GetLocations();
4050 switch (abs->GetResultType()) {
4051 case DataType::Type::kInt32: {
4052 Register out = locations->Out().AsRegister<Register>();
4053 DCHECK_EQ(out, EAX);
4054 Register temp = locations->GetTemp(0).AsRegister<Register>();
4055 DCHECK_EQ(temp, EDX);
4056 // Sign extend EAX into EDX.
4057 __ cdq();
4058 // XOR EAX with sign.
4059 __ xorl(EAX, EDX);
4060 // Subtract out sign to correct.
4061 __ subl(EAX, EDX);
4062 // The result is in EAX.
4063 break;
4064 }
4065 case DataType::Type::kInt64: {
4066 Location input = locations->InAt(0);
4067 Register input_lo = input.AsRegisterPairLow<Register>();
4068 Register input_hi = input.AsRegisterPairHigh<Register>();
4069 Location output = locations->Out();
4070 Register output_lo = output.AsRegisterPairLow<Register>();
4071 Register output_hi = output.AsRegisterPairHigh<Register>();
4072 Register temp = locations->GetTemp(0).AsRegister<Register>();
4073 // Compute the sign into the temporary.
4074 __ movl(temp, input_hi);
4075 __ sarl(temp, Immediate(31));
4076 // Store the sign into the output.
4077 __ movl(output_lo, temp);
4078 __ movl(output_hi, temp);
4079 // XOR the input to the output.
4080 __ xorl(output_lo, input_lo);
4081 __ xorl(output_hi, input_hi);
4082 // Subtract the sign.
4083 __ subl(output_lo, temp);
4084 __ sbbl(output_hi, temp);
4085 break;
4086 }
4087 case DataType::Type::kFloat32: {
4088 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4089 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4090 Register constant = locations->GetTemp(1).AsRegister<Register>();
4091 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4092 __ movd(temp, constant);
4093 __ andps(out, temp);
4094 break;
4095 }
4096 case DataType::Type::kFloat64: {
4097 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4098 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4099 // TODO: Use a constant from the constant table (requires extra input).
4100 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4101 __ andpd(out, temp);
4102 break;
4103 }
4104 default:
4105 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4106 }
4107}
4108
Calin Juravled0d48522014-11-04 16:40:20 +00004109void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004110 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004111 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004112 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004113 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004114 case DataType::Type::kInt8:
4115 case DataType::Type::kUint16:
4116 case DataType::Type::kInt16:
4117 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004118 locations->SetInAt(0, Location::Any());
4119 break;
4120 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004122 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4123 if (!instruction->IsConstant()) {
4124 locations->AddTemp(Location::RequiresRegister());
4125 }
4126 break;
4127 }
4128 default:
4129 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4130 }
Calin Juravled0d48522014-11-04 16:40:20 +00004131}
4132
4133void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004134 SlowPathCode* slow_path =
4135 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004136 codegen_->AddSlowPath(slow_path);
4137
4138 LocationSummary* locations = instruction->GetLocations();
4139 Location value = locations->InAt(0);
4140
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004141 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004142 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004143 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 case DataType::Type::kInt8:
4145 case DataType::Type::kUint16:
4146 case DataType::Type::kInt16:
4147 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004148 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004149 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004150 __ j(kEqual, slow_path->GetEntryLabel());
4151 } else if (value.IsStackSlot()) {
4152 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4153 __ j(kEqual, slow_path->GetEntryLabel());
4154 } else {
4155 DCHECK(value.IsConstant()) << value;
4156 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004157 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004158 }
4159 }
4160 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004161 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004162 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004163 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004164 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004165 __ movl(temp, value.AsRegisterPairLow<Register>());
4166 __ orl(temp, value.AsRegisterPairHigh<Register>());
4167 __ j(kEqual, slow_path->GetEntryLabel());
4168 } else {
4169 DCHECK(value.IsConstant()) << value;
4170 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4171 __ jmp(slow_path->GetEntryLabel());
4172 }
4173 }
4174 break;
4175 }
4176 default:
4177 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004178 }
Calin Juravled0d48522014-11-04 16:40:20 +00004179}
4180
Calin Juravle9aec02f2014-11-18 23:06:35 +00004181void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4182 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4183
4184 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004185 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004186
4187 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004188 case DataType::Type::kInt32:
4189 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004190 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004191 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004192 // The shift count needs to be in CL or a constant.
4193 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004194 locations->SetOut(Location::SameAsFirstInput());
4195 break;
4196 }
4197 default:
4198 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4199 }
4200}
4201
4202void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4203 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4204
4205 LocationSummary* locations = op->GetLocations();
4206 Location first = locations->InAt(0);
4207 Location second = locations->InAt(1);
4208 DCHECK(first.Equals(locations->Out()));
4209
4210 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004211 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004212 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004213 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004214 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004215 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004216 DCHECK_EQ(ECX, second_reg);
4217 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004218 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004219 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004220 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004221 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004222 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004223 }
4224 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004225 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004226 if (shift == 0) {
4227 return;
4228 }
4229 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004230 if (op->IsShl()) {
4231 __ shll(first_reg, imm);
4232 } else if (op->IsShr()) {
4233 __ sarl(first_reg, imm);
4234 } else {
4235 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004236 }
4237 }
4238 break;
4239 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004241 if (second.IsRegister()) {
4242 Register second_reg = second.AsRegister<Register>();
4243 DCHECK_EQ(ECX, second_reg);
4244 if (op->IsShl()) {
4245 GenerateShlLong(first, second_reg);
4246 } else if (op->IsShr()) {
4247 GenerateShrLong(first, second_reg);
4248 } else {
4249 GenerateUShrLong(first, second_reg);
4250 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004251 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004252 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004253 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004254 // Nothing to do if the shift is 0, as the input is already the output.
4255 if (shift != 0) {
4256 if (op->IsShl()) {
4257 GenerateShlLong(first, shift);
4258 } else if (op->IsShr()) {
4259 GenerateShrLong(first, shift);
4260 } else {
4261 GenerateUShrLong(first, shift);
4262 }
4263 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004264 }
4265 break;
4266 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004267 default:
4268 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4269 }
4270}
4271
Mark P Mendell73945692015-04-29 14:56:17 +00004272void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4273 Register low = loc.AsRegisterPairLow<Register>();
4274 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004275 if (shift == 1) {
4276 // This is just an addition.
4277 __ addl(low, low);
4278 __ adcl(high, high);
4279 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004280 // Shift by 32 is easy. High gets low, and low gets 0.
4281 codegen_->EmitParallelMoves(
4282 loc.ToLow(),
4283 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004284 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004285 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4286 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004287 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004288 } else if (shift > 32) {
4289 // Low part becomes 0. High part is low part << (shift-32).
4290 __ movl(high, low);
4291 __ shll(high, Immediate(shift - 32));
4292 __ xorl(low, low);
4293 } else {
4294 // Between 1 and 31.
4295 __ shld(high, low, Immediate(shift));
4296 __ shll(low, Immediate(shift));
4297 }
4298}
4299
Calin Juravle9aec02f2014-11-18 23:06:35 +00004300void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004301 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004302 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4303 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4304 __ testl(shifter, Immediate(32));
4305 __ j(kEqual, &done);
4306 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4307 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4308 __ Bind(&done);
4309}
4310
Mark P Mendell73945692015-04-29 14:56:17 +00004311void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4312 Register low = loc.AsRegisterPairLow<Register>();
4313 Register high = loc.AsRegisterPairHigh<Register>();
4314 if (shift == 32) {
4315 // Need to copy the sign.
4316 DCHECK_NE(low, high);
4317 __ movl(low, high);
4318 __ sarl(high, Immediate(31));
4319 } else if (shift > 32) {
4320 DCHECK_NE(low, high);
4321 // High part becomes sign. Low part is shifted by shift - 32.
4322 __ movl(low, high);
4323 __ sarl(high, Immediate(31));
4324 __ sarl(low, Immediate(shift - 32));
4325 } else {
4326 // Between 1 and 31.
4327 __ shrd(low, high, Immediate(shift));
4328 __ sarl(high, Immediate(shift));
4329 }
4330}
4331
Calin Juravle9aec02f2014-11-18 23:06:35 +00004332void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004333 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004334 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4335 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4336 __ testl(shifter, Immediate(32));
4337 __ j(kEqual, &done);
4338 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4339 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4340 __ Bind(&done);
4341}
4342
Mark P Mendell73945692015-04-29 14:56:17 +00004343void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4344 Register low = loc.AsRegisterPairLow<Register>();
4345 Register high = loc.AsRegisterPairHigh<Register>();
4346 if (shift == 32) {
4347 // Shift by 32 is easy. Low gets high, and high gets 0.
4348 codegen_->EmitParallelMoves(
4349 loc.ToHigh(),
4350 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004351 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004352 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4353 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004354 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004355 } else if (shift > 32) {
4356 // Low part is high >> (shift - 32). High part becomes 0.
4357 __ movl(low, high);
4358 __ shrl(low, Immediate(shift - 32));
4359 __ xorl(high, high);
4360 } else {
4361 // Between 1 and 31.
4362 __ shrd(low, high, Immediate(shift));
4363 __ shrl(high, Immediate(shift));
4364 }
4365}
4366
Calin Juravle9aec02f2014-11-18 23:06:35 +00004367void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004368 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004369 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4370 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4371 __ testl(shifter, Immediate(32));
4372 __ j(kEqual, &done);
4373 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4374 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4375 __ Bind(&done);
4376}
4377
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004378void LocationsBuilderX86::VisitRor(HRor* ror) {
4379 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004380 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004381
4382 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004383 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004384 // Add the temporary needed.
4385 locations->AddTemp(Location::RequiresRegister());
4386 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004387 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004388 locations->SetInAt(0, Location::RequiresRegister());
4389 // The shift count needs to be in CL (unless it is a constant).
4390 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4391 locations->SetOut(Location::SameAsFirstInput());
4392 break;
4393 default:
4394 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4395 UNREACHABLE();
4396 }
4397}
4398
4399void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4400 LocationSummary* locations = ror->GetLocations();
4401 Location first = locations->InAt(0);
4402 Location second = locations->InAt(1);
4403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004404 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004405 Register first_reg = first.AsRegister<Register>();
4406 if (second.IsRegister()) {
4407 Register second_reg = second.AsRegister<Register>();
4408 __ rorl(first_reg, second_reg);
4409 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004410 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004411 __ rorl(first_reg, imm);
4412 }
4413 return;
4414 }
4415
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004416 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004417 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4418 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4419 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4420 if (second.IsRegister()) {
4421 Register second_reg = second.AsRegister<Register>();
4422 DCHECK_EQ(second_reg, ECX);
4423 __ movl(temp_reg, first_reg_hi);
4424 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4425 __ shrd(first_reg_lo, temp_reg, second_reg);
4426 __ movl(temp_reg, first_reg_hi);
4427 __ testl(second_reg, Immediate(32));
4428 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4429 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4430 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004431 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004432 if (shift_amt == 0) {
4433 // Already fine.
4434 return;
4435 }
4436 if (shift_amt == 32) {
4437 // Just swap.
4438 __ movl(temp_reg, first_reg_lo);
4439 __ movl(first_reg_lo, first_reg_hi);
4440 __ movl(first_reg_hi, temp_reg);
4441 return;
4442 }
4443
4444 Immediate imm(shift_amt);
4445 // Save the constents of the low value.
4446 __ movl(temp_reg, first_reg_lo);
4447
4448 // Shift right into low, feeding bits from high.
4449 __ shrd(first_reg_lo, first_reg_hi, imm);
4450
4451 // Shift right into high, feeding bits from the original low.
4452 __ shrd(first_reg_hi, temp_reg, imm);
4453
4454 // Swap if needed.
4455 if (shift_amt > 32) {
4456 __ movl(temp_reg, first_reg_lo);
4457 __ movl(first_reg_lo, first_reg_hi);
4458 __ movl(first_reg_hi, temp_reg);
4459 }
4460 }
4461}
4462
Calin Juravle9aec02f2014-11-18 23:06:35 +00004463void LocationsBuilderX86::VisitShl(HShl* shl) {
4464 HandleShift(shl);
4465}
4466
4467void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4468 HandleShift(shl);
4469}
4470
4471void LocationsBuilderX86::VisitShr(HShr* shr) {
4472 HandleShift(shr);
4473}
4474
4475void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4476 HandleShift(shr);
4477}
4478
4479void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4480 HandleShift(ushr);
4481}
4482
4483void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4484 HandleShift(ushr);
4485}
4486
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004487void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004488 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4489 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004490 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004491 if (instruction->IsStringAlloc()) {
4492 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4493 } else {
4494 InvokeRuntimeCallingConvention calling_convention;
4495 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004496 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004497}
4498
4499void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004500 // Note: if heap poisoning is enabled, the entry point takes cares
4501 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004502 if (instruction->IsStringAlloc()) {
4503 // String is allocated through StringFactory. Call NewEmptyString entry point.
4504 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004505 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004506 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4507 __ call(Address(temp, code_offset.Int32Value()));
4508 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4509 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004510 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004511 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004512 DCHECK(!codegen_->IsLeafMethod());
4513 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004514}
4515
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004516void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004517 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4518 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004519 locations->SetOut(Location::RegisterLocation(EAX));
4520 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004521 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4522 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004523}
4524
4525void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004526 // Note: if heap poisoning is enabled, the entry point takes cares
4527 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004528 QuickEntrypointEnum entrypoint =
4529 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4530 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004531 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004532 DCHECK(!codegen_->IsLeafMethod());
4533}
4534
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004535void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004536 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004537 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004538 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4539 if (location.IsStackSlot()) {
4540 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4541 } else if (location.IsDoubleStackSlot()) {
4542 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004543 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004544 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004545}
4546
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004547void InstructionCodeGeneratorX86::VisitParameterValue(
4548 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4549}
4550
4551void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4552 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004553 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004554 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4555}
4556
4557void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004558}
4559
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004560void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4561 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004562 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004563 locations->SetInAt(0, Location::RequiresRegister());
4564 locations->SetOut(Location::RequiresRegister());
4565}
4566
4567void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4568 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004569 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004570 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004571 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004572 __ movl(locations->Out().AsRegister<Register>(),
4573 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004574 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004575 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004576 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004577 __ movl(locations->Out().AsRegister<Register>(),
4578 Address(locations->InAt(0).AsRegister<Register>(),
4579 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4580 // temp = temp->GetImtEntryAt(method_offset);
4581 __ movl(locations->Out().AsRegister<Register>(),
4582 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004583 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004584}
4585
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004586void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004587 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004588 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004589 locations->SetInAt(0, Location::RequiresRegister());
4590 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004591}
4592
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004593void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4594 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004595 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004596 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004597 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004598 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004599 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004600 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004601 break;
4602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004603 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004604 __ notl(out.AsRegisterPairLow<Register>());
4605 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004606 break;
4607
4608 default:
4609 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4610 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004611}
4612
David Brazdil66d126e2015-04-03 16:02:44 +01004613void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4614 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004615 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004616 locations->SetInAt(0, Location::RequiresRegister());
4617 locations->SetOut(Location::SameAsFirstInput());
4618}
4619
4620void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004621 LocationSummary* locations = bool_not->GetLocations();
4622 Location in = locations->InAt(0);
4623 Location out = locations->Out();
4624 DCHECK(in.Equals(out));
4625 __ xorl(out.AsRegister<Register>(), Immediate(1));
4626}
4627
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004628void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004629 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004630 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004631 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004632 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004633 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004634 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004635 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004636 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kInt32:
4638 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004639 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004640 locations->SetInAt(1, Location::Any());
4641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4642 break;
4643 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004644 case DataType::Type::kFloat32:
4645 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004646 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004647 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4648 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4649 } else if (compare->InputAt(1)->IsConstant()) {
4650 locations->SetInAt(1, Location::RequiresFpuRegister());
4651 } else {
4652 locations->SetInAt(1, Location::Any());
4653 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004654 locations->SetOut(Location::RequiresRegister());
4655 break;
4656 }
4657 default:
4658 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4659 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004660}
4661
4662void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004663 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004664 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004665 Location left = locations->InAt(0);
4666 Location right = locations->InAt(1);
4667
Mark Mendell0c9497d2015-08-21 09:30:05 -04004668 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004669 Condition less_cond = kLess;
4670
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004671 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004675 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004676 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004678 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004679 break;
4680 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004681 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004682 Register left_low = left.AsRegisterPairLow<Register>();
4683 Register left_high = left.AsRegisterPairHigh<Register>();
4684 int32_t val_low = 0;
4685 int32_t val_high = 0;
4686 bool right_is_const = false;
4687
4688 if (right.IsConstant()) {
4689 DCHECK(right.GetConstant()->IsLongConstant());
4690 right_is_const = true;
4691 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4692 val_low = Low32Bits(val);
4693 val_high = High32Bits(val);
4694 }
4695
Calin Juravleddb7df22014-11-25 20:56:51 +00004696 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004697 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004698 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004699 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004700 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004701 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004702 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004703 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004704 __ j(kLess, &less); // Signed compare.
4705 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004706 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004707 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004708 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004709 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004710 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004711 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004712 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004713 }
Aart Bika19616e2016-02-01 18:57:58 -08004714 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004715 break;
4716 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004718 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004719 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004720 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004721 break;
4722 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004723 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004724 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004725 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004726 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004727 break;
4728 }
4729 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004730 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004731 }
Aart Bika19616e2016-02-01 18:57:58 -08004732
Calin Juravleddb7df22014-11-25 20:56:51 +00004733 __ movl(out, Immediate(0));
4734 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004735 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004736
4737 __ Bind(&greater);
4738 __ movl(out, Immediate(1));
4739 __ jmp(&done);
4740
4741 __ Bind(&less);
4742 __ movl(out, Immediate(-1));
4743
4744 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004745}
4746
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004747void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004748 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004749 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004750 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004751 locations->SetInAt(i, Location::Any());
4752 }
4753 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004754}
4755
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004756void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004757 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004758}
4759
Roland Levillain7c1559a2015-12-15 10:55:36 +00004760void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004761 /*
4762 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4763 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4764 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4765 */
4766 switch (kind) {
4767 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004768 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004769 break;
4770 }
4771 case MemBarrierKind::kAnyStore:
4772 case MemBarrierKind::kLoadAny:
4773 case MemBarrierKind::kStoreStore: {
4774 // nop
4775 break;
4776 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004777 case MemBarrierKind::kNTStoreStore:
4778 // Non-Temporal Store/Store needs an explicit fence.
4779 MemoryFence(/* non-temporal */ true);
4780 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004781 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782}
4783
Vladimir Markodc151b22015-10-15 18:02:30 +01004784HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4785 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004786 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004787 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004788}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004790Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4791 Register temp) {
4792 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004793 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004794 if (!invoke->GetLocations()->Intrinsified()) {
4795 return location.AsRegister<Register>();
4796 }
4797 // For intrinsics we allow any location, so it may be on the stack.
4798 if (!location.IsRegister()) {
4799 __ movl(temp, Address(ESP, location.GetStackIndex()));
4800 return temp;
4801 }
4802 // For register locations, check if the register was saved. If so, get it from the stack.
4803 // Note: There is a chance that the register was saved but not overwritten, so we could
4804 // save one load. However, since this is just an intrinsic slow path we prefer this
4805 // simple and more robust approach rather that trying to determine if that's the case.
4806 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004807 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4808 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4809 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4810 __ movl(temp, Address(ESP, stack_offset));
4811 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004812 }
4813 return location.AsRegister<Register>();
4814}
4815
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004816void CodeGeneratorX86::GenerateStaticOrDirectCall(
4817 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004818 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4819 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004820 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004821 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004822 uint32_t offset =
4823 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4824 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004825 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004826 }
Vladimir Marko58155012015-08-19 12:49:41 +00004827 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004828 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004829 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004830 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4831 DCHECK(GetCompilerOptions().IsBootImage());
4832 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4833 temp.AsRegister<Register>());
4834 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004835 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004836 break;
4837 }
Vladimir Marko58155012015-08-19 12:49:41 +00004838 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4839 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4840 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004841 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004842 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4843 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004844 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004845 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004846 break;
4847 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004848 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4849 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4850 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004851 }
Vladimir Marko58155012015-08-19 12:49:41 +00004852 }
4853
4854 switch (invoke->GetCodePtrLocation()) {
4855 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4856 __ call(GetFrameEntryLabel());
4857 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004858 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4859 // (callee_method + offset_of_quick_compiled_code)()
4860 __ call(Address(callee_method.AsRegister<Register>(),
4861 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004862 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004863 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004864 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004865 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004866
4867 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004868}
4869
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004870void CodeGeneratorX86::GenerateVirtualCall(
4871 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004872 Register temp = temp_in.AsRegister<Register>();
4873 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4874 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004875
4876 // Use the calling convention instead of the location of the receiver, as
4877 // intrinsics may have put the receiver in a different register. In the intrinsics
4878 // slow path, the arguments have been moved to the right place, so here we are
4879 // guaranteed that the receiver is the first register of the calling convention.
4880 InvokeDexCallingConvention calling_convention;
4881 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004882 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004883 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004884 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004885 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004886 // Instead of simply (possibly) unpoisoning `temp` here, we should
4887 // emit a read barrier for the previous class reference load.
4888 // However this is not required in practice, as this is an
4889 // intermediate/temporary reference and because the current
4890 // concurrent copying collector keeps the from-space memory
4891 // intact/accessible until the end of the marking phase (the
4892 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004893 __ MaybeUnpoisonHeapReference(temp);
4894 // temp = temp->GetMethodAt(method_offset);
4895 __ movl(temp, Address(temp, method_offset));
4896 // call temp->GetEntryPoint();
4897 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004898 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004899 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004900}
4901
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004902void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004903 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004904 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004905 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004906 boot_image_method_patches_.emplace_back(
4907 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004908 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004909}
4910
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004911void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4912 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4913 HX86ComputeBaseMethodAddress* method_address =
4914 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004915 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004916 method_bss_entry_patches_.emplace_back(
4917 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4918 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004919}
4920
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004921void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4922 HX86ComputeBaseMethodAddress* method_address =
4923 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4924 boot_image_type_patches_.emplace_back(
4925 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004926 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004927}
4928
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004929Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004930 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004931 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4932 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004933 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004934 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004935}
4936
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004937void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4938 HX86ComputeBaseMethodAddress* method_address =
4939 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4940 boot_image_string_patches_.emplace_back(
4941 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4942 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004943}
4944
Vladimir Markoaad75c62016-10-03 08:46:48 +00004945Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4946 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004947 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004948 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004949 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004950 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004951 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004952}
4953
Vladimir Markoaad75c62016-10-03 08:46:48 +00004954// The label points to the end of the "movl" or another instruction but the literal offset
4955// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4956constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4957
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004958template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004959inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004960 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004961 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004962 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004963 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004964 linker_patches->push_back(Factory(literal_offset,
4965 info.target_dex_file,
4966 GetMethodAddressOffset(info.method_address),
4967 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004968 }
4969}
4970
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004971void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004972 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004973 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004974 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004975 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004976 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004977 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004978 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004979 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004980 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004981 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004982 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4983 boot_image_method_patches_, linker_patches);
4984 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4985 boot_image_type_patches_, linker_patches);
4986 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004987 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004988 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004989 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004990 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4991 boot_image_type_patches_, linker_patches);
4992 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004993 boot_image_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004994 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004995 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4996 method_bss_entry_patches_, linker_patches);
4997 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4998 type_bss_entry_patches_, linker_patches);
4999 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5000 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005001 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005002}
5003
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005004void CodeGeneratorX86::MarkGCCard(Register temp,
5005 Register card,
5006 Register object,
5007 Register value,
5008 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005009 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005010 if (value_can_be_null) {
5011 __ testl(value, value);
5012 __ j(kEqual, &is_null);
5013 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005014 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 __ movl(temp, object);
5016 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005017 __ movb(Address(temp, card, TIMES_1, 0),
5018 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005019 if (value_can_be_null) {
5020 __ Bind(&is_null);
5021 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005022}
5023
Calin Juravle52c48962014-12-16 17:02:57 +00005024void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5025 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005026
5027 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005028 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005029 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005030 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5031 kEmitCompilerReadBarrier
5032 ? LocationSummary::kCallOnSlowPath
5033 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005034 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005035 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005036 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005037 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005039 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005040 locations->SetOut(Location::RequiresFpuRegister());
5041 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005042 // The output overlaps in case of long: we don't want the low move
5043 // to overwrite the object's location. Likewise, in the case of
5044 // an object field get with read barriers enabled, we do not want
5045 // the move to overwrite the object's location, as we need it to emit
5046 // the read barrier.
5047 locations->SetOut(
5048 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005049 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005050 Location::kOutputOverlap :
5051 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005052 }
Calin Juravle52c48962014-12-16 17:02:57 +00005053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005055 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005056 // So we use an XMM register as a temp to achieve atomicity (first
5057 // load the temp into the XMM and then copy the XMM into the
5058 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005059 locations->AddTemp(Location::RequiresFpuRegister());
5060 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005061}
5062
Calin Juravle52c48962014-12-16 17:02:57 +00005063void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5064 const FieldInfo& field_info) {
5065 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066
Calin Juravle52c48962014-12-16 17:02:57 +00005067 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005068 Location base_loc = locations->InAt(0);
5069 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005070 Location out = locations->Out();
5071 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005072 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5073 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005074 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5075
Vladimir Marko61b92282017-10-11 13:23:17 +01005076 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005077 case DataType::Type::kBool:
5078 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005079 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080 break;
5081 }
5082
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005083 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005084 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085 break;
5086 }
5087
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005088 case DataType::Type::kUint16: {
5089 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005090 break;
5091 }
5092
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005093 case DataType::Type::kInt16: {
5094 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 break;
5096 }
5097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005098 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005099 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005100 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005103 // /* HeapReference<Object> */ out = *(base + offset)
5104 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005105 // Note that a potential implicit null check is handled in this
5106 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5107 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005108 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005109 if (is_volatile) {
5110 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5111 }
5112 } else {
5113 __ movl(out.AsRegister<Register>(), Address(base, offset));
5114 codegen_->MaybeRecordImplicitNullCheck(instruction);
5115 if (is_volatile) {
5116 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5117 }
5118 // If read barriers are enabled, emit read barriers other than
5119 // Baker's using a slow path (and also unpoison the loaded
5120 // reference, if heap poisoning is enabled).
5121 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5122 }
5123 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005124 }
5125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005126 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005127 if (is_volatile) {
5128 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5129 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005130 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005131 __ movd(out.AsRegisterPairLow<Register>(), temp);
5132 __ psrlq(temp, Immediate(32));
5133 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5134 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005135 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005136 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005137 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005138 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5139 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140 break;
5141 }
5142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005143 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005144 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005145 break;
5146 }
5147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005148 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005149 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005150 break;
5151 }
5152
Aart Bik66c158e2018-01-31 12:55:04 -08005153 case DataType::Type::kUint32:
5154 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005155 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005156 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005157 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158 }
Calin Juravle52c48962014-12-16 17:02:57 +00005159
Vladimir Marko61b92282017-10-11 13:23:17 +01005160 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005161 // Potential implicit null checks, in the case of reference or
5162 // long fields, are handled in the previous switch statement.
5163 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 codegen_->MaybeRecordImplicitNullCheck(instruction);
5165 }
5166
Calin Juravle52c48962014-12-16 17:02:57 +00005167 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005168 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005169 // Memory barriers, in the case of references, are also handled
5170 // in the previous switch statement.
5171 } else {
5172 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5173 }
Roland Levillain4d027112015-07-01 15:41:14 +01005174 }
Calin Juravle52c48962014-12-16 17:02:57 +00005175}
5176
5177void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5178 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5179
5180 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005181 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005182 locations->SetInAt(0, Location::RequiresRegister());
5183 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005184 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005185 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005186
5187 // The register allocator does not support multiple
5188 // inputs that die at entry with one in a specific register.
5189 if (is_byte_type) {
5190 // Ensure the value is in a byte register.
5191 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005192 } else if (DataType::IsFloatingPointType(field_type)) {
5193 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005194 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5195 locations->SetInAt(1, Location::RequiresFpuRegister());
5196 } else {
5197 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5198 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005200 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005201 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005202
Calin Juravle52c48962014-12-16 17:02:57 +00005203 // 64bits value can be atomically written to an address with movsd and an XMM register.
5204 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5205 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5206 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5207 // isolated cases when we need this it isn't worth adding the extra complexity.
5208 locations->AddTemp(Location::RequiresFpuRegister());
5209 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005210 } else {
5211 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5212
5213 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5214 // Temporary registers for the write barrier.
5215 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5216 // Ensure the card is in a byte register.
5217 locations->AddTemp(Location::RegisterLocation(ECX));
5218 }
Calin Juravle52c48962014-12-16 17:02:57 +00005219 }
5220}
5221
5222void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005223 const FieldInfo& field_info,
5224 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005225 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5226
5227 LocationSummary* locations = instruction->GetLocations();
5228 Register base = locations->InAt(0).AsRegister<Register>();
5229 Location value = locations->InAt(1);
5230 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005231 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005232 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005233 bool needs_write_barrier =
5234 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005235
5236 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005237 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005238 }
5239
Mark Mendell81489372015-11-04 11:30:41 -05005240 bool maybe_record_implicit_null_check_done = false;
5241
Calin Juravle52c48962014-12-16 17:02:57 +00005242 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005244 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005245 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005246 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5247 break;
5248 }
5249
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005250 case DataType::Type::kUint16:
5251 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005252 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005253 __ movw(Address(base, offset),
5254 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005255 } else {
5256 __ movw(Address(base, offset), value.AsRegister<Register>());
5257 }
Calin Juravle52c48962014-12-16 17:02:57 +00005258 break;
5259 }
5260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005261 case DataType::Type::kInt32:
5262 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005263 if (kPoisonHeapReferences && needs_write_barrier) {
5264 // Note that in the case where `value` is a null reference,
5265 // we do not enter this block, as the reference does not
5266 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005267 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005268 Register temp = locations->GetTemp(0).AsRegister<Register>();
5269 __ movl(temp, value.AsRegister<Register>());
5270 __ PoisonHeapReference(temp);
5271 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005272 } else if (value.IsConstant()) {
5273 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5274 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005275 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005276 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005277 __ movl(Address(base, offset), value.AsRegister<Register>());
5278 }
Calin Juravle52c48962014-12-16 17:02:57 +00005279 break;
5280 }
5281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005283 if (is_volatile) {
5284 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5285 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5286 __ movd(temp1, value.AsRegisterPairLow<Register>());
5287 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5288 __ punpckldq(temp1, temp2);
5289 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005291 } else if (value.IsConstant()) {
5292 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5293 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5294 codegen_->MaybeRecordImplicitNullCheck(instruction);
5295 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005296 } else {
5297 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005299 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5300 }
Mark Mendell81489372015-11-04 11:30:41 -05005301 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005302 break;
5303 }
5304
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005305 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005306 if (value.IsConstant()) {
5307 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5308 __ movl(Address(base, offset), Immediate(v));
5309 } else {
5310 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5311 }
Calin Juravle52c48962014-12-16 17:02:57 +00005312 break;
5313 }
5314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005316 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)));
5321 maybe_record_implicit_null_check_done = true;
5322 } else {
5323 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5324 }
Calin Juravle52c48962014-12-16 17:02:57 +00005325 break;
5326 }
5327
Aart Bik66c158e2018-01-31 12:55:04 -08005328 case DataType::Type::kUint32:
5329 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005331 LOG(FATAL) << "Unreachable type " << field_type;
5332 UNREACHABLE();
5333 }
5334
Mark Mendell81489372015-11-04 11:30:41 -05005335 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005336 codegen_->MaybeRecordImplicitNullCheck(instruction);
5337 }
5338
Roland Levillain4d027112015-07-01 15:41:14 +01005339 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005340 Register temp = locations->GetTemp(0).AsRegister<Register>();
5341 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005342 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005343 }
5344
Calin Juravle52c48962014-12-16 17:02:57 +00005345 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005346 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005347 }
5348}
5349
5350void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5351 HandleFieldGet(instruction, instruction->GetFieldInfo());
5352}
5353
5354void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5355 HandleFieldGet(instruction, instruction->GetFieldInfo());
5356}
5357
5358void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5359 HandleFieldSet(instruction, instruction->GetFieldInfo());
5360}
5361
5362void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005363 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005364}
5365
5366void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5367 HandleFieldSet(instruction, instruction->GetFieldInfo());
5368}
5369
5370void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005371 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005372}
5373
5374void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5375 HandleFieldGet(instruction, instruction->GetFieldInfo());
5376}
5377
5378void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5379 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005380}
5381
Calin Juravlee460d1d2015-09-29 04:52:17 +01005382void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5383 HUnresolvedInstanceFieldGet* instruction) {
5384 FieldAccessCallingConventionX86 calling_convention;
5385 codegen_->CreateUnresolvedFieldLocationSummary(
5386 instruction, instruction->GetFieldType(), calling_convention);
5387}
5388
5389void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5390 HUnresolvedInstanceFieldGet* instruction) {
5391 FieldAccessCallingConventionX86 calling_convention;
5392 codegen_->GenerateUnresolvedFieldAccess(instruction,
5393 instruction->GetFieldType(),
5394 instruction->GetFieldIndex(),
5395 instruction->GetDexPc(),
5396 calling_convention);
5397}
5398
5399void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5400 HUnresolvedInstanceFieldSet* instruction) {
5401 FieldAccessCallingConventionX86 calling_convention;
5402 codegen_->CreateUnresolvedFieldLocationSummary(
5403 instruction, instruction->GetFieldType(), calling_convention);
5404}
5405
5406void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5407 HUnresolvedInstanceFieldSet* instruction) {
5408 FieldAccessCallingConventionX86 calling_convention;
5409 codegen_->GenerateUnresolvedFieldAccess(instruction,
5410 instruction->GetFieldType(),
5411 instruction->GetFieldIndex(),
5412 instruction->GetDexPc(),
5413 calling_convention);
5414}
5415
5416void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5417 HUnresolvedStaticFieldGet* instruction) {
5418 FieldAccessCallingConventionX86 calling_convention;
5419 codegen_->CreateUnresolvedFieldLocationSummary(
5420 instruction, instruction->GetFieldType(), calling_convention);
5421}
5422
5423void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5424 HUnresolvedStaticFieldGet* instruction) {
5425 FieldAccessCallingConventionX86 calling_convention;
5426 codegen_->GenerateUnresolvedFieldAccess(instruction,
5427 instruction->GetFieldType(),
5428 instruction->GetFieldIndex(),
5429 instruction->GetDexPc(),
5430 calling_convention);
5431}
5432
5433void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5434 HUnresolvedStaticFieldSet* instruction) {
5435 FieldAccessCallingConventionX86 calling_convention;
5436 codegen_->CreateUnresolvedFieldLocationSummary(
5437 instruction, instruction->GetFieldType(), calling_convention);
5438}
5439
5440void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5441 HUnresolvedStaticFieldSet* instruction) {
5442 FieldAccessCallingConventionX86 calling_convention;
5443 codegen_->GenerateUnresolvedFieldAccess(instruction,
5444 instruction->GetFieldType(),
5445 instruction->GetFieldIndex(),
5446 instruction->GetDexPc(),
5447 calling_convention);
5448}
5449
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005450void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005451 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5452 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5453 ? Location::RequiresRegister()
5454 : Location::Any();
5455 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005456}
5457
Calin Juravle2ae48182016-03-16 14:05:09 +00005458void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5459 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005460 return;
5461 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005462 LocationSummary* locations = instruction->GetLocations();
5463 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005464
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005465 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005466 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005467}
5468
Calin Juravle2ae48182016-03-16 14:05:09 +00005469void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005470 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005471 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005472
5473 LocationSummary* locations = instruction->GetLocations();
5474 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005475
5476 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005477 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005478 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005479 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 } else {
5481 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005482 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005483 __ jmp(slow_path->GetEntryLabel());
5484 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005485 }
5486 __ j(kEqual, slow_path->GetEntryLabel());
5487}
5488
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005489void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005490 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005491}
5492
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005493void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005494 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005495 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005496 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005497 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5498 object_array_get_with_read_barrier
5499 ? LocationSummary::kCallOnSlowPath
5500 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005501 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005502 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005503 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005504 locations->SetInAt(0, Location::RequiresRegister());
5505 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005506 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5508 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005509 // The output overlaps in case of long: we don't want the low move
5510 // to overwrite the array's location. Likewise, in the case of an
5511 // object array get with read barriers enabled, we do not want the
5512 // move to overwrite the array's location, as we need it to emit
5513 // the read barrier.
5514 locations->SetOut(
5515 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005516 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5517 ? Location::kOutputOverlap
5518 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005519 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520}
5521
5522void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5523 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005524 Location obj_loc = locations->InAt(0);
5525 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005526 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005527 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005528 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005529
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005531 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005532 case DataType::Type::kBool:
5533 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005534 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005535 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536 break;
5537 }
5538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005539 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005540 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005541 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542 break;
5543 }
5544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005546 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005547 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5548 // Branch cases into compressed and uncompressed for each index's type.
5549 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5550 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005551 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005553 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5554 "Expecting 0=compressed, 1=uncompressed");
5555 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005556 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5557 __ jmp(&done);
5558 __ Bind(&not_compressed);
5559 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5560 __ Bind(&done);
5561 } else {
5562 // Common case for charAt of array of char or when string compression's
5563 // feature is turned off.
5564 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5565 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566 break;
5567 }
5568
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005569 case DataType::Type::kInt16: {
5570 Register out = out_loc.AsRegister<Register>();
5571 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5572 break;
5573 }
5574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005575 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005576 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005577 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578 break;
5579 }
5580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005582 static_assert(
5583 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5584 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005585 // /* HeapReference<Object> */ out =
5586 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5587 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005588 // Note that a potential implicit null check is handled in this
5589 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5590 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005591 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005592 } else {
5593 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005594 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5595 codegen_->MaybeRecordImplicitNullCheck(instruction);
5596 // If read barriers are enabled, emit read barriers other than
5597 // Baker's using a slow path (and also unpoison the loaded
5598 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005599 if (index.IsConstant()) {
5600 uint32_t offset =
5601 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005602 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5603 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005604 codegen_->MaybeGenerateReadBarrierSlow(
5605 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5606 }
5607 }
5608 break;
5609 }
5610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005611 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005612 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005613 __ movl(out_loc.AsRegisterPairLow<Register>(),
5614 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5615 codegen_->MaybeRecordImplicitNullCheck(instruction);
5616 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5617 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618 break;
5619 }
5620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005622 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005623 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005624 break;
5625 }
5626
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005627 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005628 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005629 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005630 break;
5631 }
5632
Aart Bik66c158e2018-01-31 12:55:04 -08005633 case DataType::Type::kUint32:
5634 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005635 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005636 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005637 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005640 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005641 // Potential implicit null checks, in the case of reference or
5642 // long arrays, are handled in the previous switch statement.
5643 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005644 codegen_->MaybeRecordImplicitNullCheck(instruction);
5645 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005646}
5647
5648void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005649 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005650
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005651 bool needs_write_barrier =
5652 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005653 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005654
Vladimir Markoca6fff82017-10-03 14:49:14 +01005655 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005656 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005657 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005658 LocationSummary::kCallOnSlowPath :
5659 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005660
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005661 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005662 // We need the inputs to be different than the output in case of long operation.
5663 // In case of a byte operation, the register allocator does not support multiple
5664 // inputs that die at entry with one in a specific register.
5665 locations->SetInAt(0, Location::RequiresRegister());
5666 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5667 if (is_byte_type) {
5668 // Ensure the value is in a byte register.
5669 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005670 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005671 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005672 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005673 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5674 }
5675 if (needs_write_barrier) {
5676 // Temporary registers for the write barrier.
5677 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5678 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005679 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005680 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005681}
5682
5683void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5684 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005685 Location array_loc = locations->InAt(0);
5686 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005687 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005688 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005689 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005690 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5691 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5692 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005694 bool needs_write_barrier =
5695 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005696
5697 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005698 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005699 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005700 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005701 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005702 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005703 if (value.IsRegister()) {
5704 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005705 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005706 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005707 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005709 break;
5710 }
5711
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005712 case DataType::Type::kUint16:
5713 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005714 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005715 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005716 if (value.IsRegister()) {
5717 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005718 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005719 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005720 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005721 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722 break;
5723 }
5724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005726 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005727 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005729 if (!value.IsRegister()) {
5730 // Just setting null.
5731 DCHECK(instruction->InputAt(2)->IsNullConstant());
5732 DCHECK(value.IsConstant()) << value;
5733 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005734 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005735 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005736 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005737 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005738 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005739
5740 DCHECK(needs_write_barrier);
5741 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005742 // We cannot use a NearLabel for `done`, as its range may be too
5743 // short when Baker read barriers are enabled.
5744 Label done;
5745 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005746 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005747 Location temp_loc = locations->GetTemp(0);
5748 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005750 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005751 codegen_->AddSlowPath(slow_path);
5752 if (instruction->GetValueCanBeNull()) {
5753 __ testl(register_value, register_value);
5754 __ j(kNotEqual, &not_null);
5755 __ movl(address, Immediate(0));
5756 codegen_->MaybeRecordImplicitNullCheck(instruction);
5757 __ jmp(&done);
5758 __ Bind(&not_null);
5759 }
5760
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005761 // Note that when Baker read barriers are enabled, the type
5762 // checks are performed without read barriers. This is fine,
5763 // even in the case where a class object is in the from-space
5764 // after the flip, as a comparison involving such a type would
5765 // not produce a false positive; it may of course produce a
5766 // false negative, in which case we would take the ArraySet
5767 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005768
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005769 // /* HeapReference<Class> */ temp = array->klass_
5770 __ movl(temp, Address(array, class_offset));
5771 codegen_->MaybeRecordImplicitNullCheck(instruction);
5772 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005773
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005774 // /* HeapReference<Class> */ temp = temp->component_type_
5775 __ movl(temp, Address(temp, component_offset));
5776 // If heap poisoning is enabled, no need to unpoison `temp`
5777 // nor the object reference in `register_value->klass`, as
5778 // we are comparing two poisoned references.
5779 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005780
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005781 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5782 __ j(kEqual, &do_put);
5783 // If heap poisoning is enabled, the `temp` reference has
5784 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 __ MaybeUnpoisonHeapReference(temp);
5786
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005787 // If heap poisoning is enabled, no need to unpoison the
5788 // heap reference loaded below, as it is only used for a
5789 // comparison with null.
5790 __ cmpl(Address(temp, super_offset), Immediate(0));
5791 __ j(kNotEqual, slow_path->GetEntryLabel());
5792 __ Bind(&do_put);
5793 } else {
5794 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005795 }
5796 }
5797
5798 if (kPoisonHeapReferences) {
5799 __ movl(temp, register_value);
5800 __ PoisonHeapReference(temp);
5801 __ movl(address, temp);
5802 } else {
5803 __ movl(address, register_value);
5804 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005806 codegen_->MaybeRecordImplicitNullCheck(instruction);
5807 }
5808
5809 Register card = locations->GetTemp(1).AsRegister<Register>();
5810 codegen_->MarkGCCard(
5811 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5812 __ Bind(&done);
5813
5814 if (slow_path != nullptr) {
5815 __ Bind(slow_path->GetExitLabel());
5816 }
5817
5818 break;
5819 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005822 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005823 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005824 if (value.IsRegister()) {
5825 __ movl(address, value.AsRegister<Register>());
5826 } else {
5827 DCHECK(value.IsConstant()) << value;
5828 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5829 __ movl(address, Immediate(v));
5830 }
5831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005832 break;
5833 }
5834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005837 if (value.IsRegisterPair()) {
5838 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5839 value.AsRegisterPairLow<Register>());
5840 codegen_->MaybeRecordImplicitNullCheck(instruction);
5841 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5842 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005843 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005844 DCHECK(value.IsConstant());
5845 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5846 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5847 Immediate(Low32Bits(val)));
5848 codegen_->MaybeRecordImplicitNullCheck(instruction);
5849 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5850 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005851 }
5852 break;
5853 }
5854
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005855 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005856 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005857 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005858 if (value.IsFpuRegister()) {
5859 __ movss(address, value.AsFpuRegister<XmmRegister>());
5860 } else {
5861 DCHECK(value.IsConstant());
5862 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5863 __ movl(address, Immediate(v));
5864 }
5865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005866 break;
5867 }
5868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005869 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005870 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005871 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005872 if (value.IsFpuRegister()) {
5873 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5874 } else {
5875 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005876 Address address_hi =
5877 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005878 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5879 __ movl(address, Immediate(Low32Bits(v)));
5880 codegen_->MaybeRecordImplicitNullCheck(instruction);
5881 __ movl(address_hi, Immediate(High32Bits(v)));
5882 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005883 break;
5884 }
5885
Aart Bik66c158e2018-01-31 12:55:04 -08005886 case DataType::Type::kUint32:
5887 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005889 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005890 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005891 }
5892}
5893
5894void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005895 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005896 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005897 if (!instruction->IsEmittedAtUseSite()) {
5898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5899 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005900}
5901
5902void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005903 if (instruction->IsEmittedAtUseSite()) {
5904 return;
5905 }
5906
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005907 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005908 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005909 Register obj = locations->InAt(0).AsRegister<Register>();
5910 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005911 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005912 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005913 // Mask out most significant bit in case the array is String's array of char.
5914 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005915 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005916 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005917}
5918
5919void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005920 RegisterSet caller_saves = RegisterSet::Empty();
5921 InvokeRuntimeCallingConvention calling_convention;
5922 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5923 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5924 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005925 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005926 HInstruction* length = instruction->InputAt(1);
5927 if (!length->IsEmittedAtUseSite()) {
5928 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5929 }
jessicahandojo4877b792016-09-08 19:49:13 -07005930 // Need register to see array's length.
5931 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5932 locations->AddTemp(Location::RequiresRegister());
5933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005934}
5935
5936void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005937 const bool is_string_compressed_char_at =
5938 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005939 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005940 Location index_loc = locations->InAt(0);
5941 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005942 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005943 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005944
Mark Mendell99dbd682015-04-22 16:18:52 -04005945 if (length_loc.IsConstant()) {
5946 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5947 if (index_loc.IsConstant()) {
5948 // BCE will remove the bounds check if we are guarenteed to pass.
5949 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5950 if (index < 0 || index >= length) {
5951 codegen_->AddSlowPath(slow_path);
5952 __ jmp(slow_path->GetEntryLabel());
5953 } else {
5954 // Some optimization after BCE may have generated this, and we should not
5955 // generate a bounds check if it is a valid range.
5956 }
5957 return;
5958 }
5959
5960 // We have to reverse the jump condition because the length is the constant.
5961 Register index_reg = index_loc.AsRegister<Register>();
5962 __ cmpl(index_reg, Immediate(length));
5963 codegen_->AddSlowPath(slow_path);
5964 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005965 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005966 HInstruction* array_length = instruction->InputAt(1);
5967 if (array_length->IsEmittedAtUseSite()) {
5968 // Address the length field in the array.
5969 DCHECK(array_length->IsArrayLength());
5970 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5971 Location array_loc = array_length->GetLocations()->InAt(0);
5972 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005973 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005974 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5975 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005976 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5977 __ movl(length_reg, array_len);
5978 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005979 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005980 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005981 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005982 // Checking bounds for general case:
5983 // Array of char or string's array with feature compression off.
5984 if (index_loc.IsConstant()) {
5985 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5986 __ cmpl(array_len, Immediate(value));
5987 } else {
5988 __ cmpl(array_len, index_loc.AsRegister<Register>());
5989 }
5990 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005991 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005992 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005993 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005994 }
5995 codegen_->AddSlowPath(slow_path);
5996 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005997 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005998}
5999
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006000void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006001 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006002}
6003
6004void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006005 if (instruction->GetNext()->IsSuspendCheck() &&
6006 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6007 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6008 // The back edge will generate the suspend check.
6009 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6010 }
6011
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006012 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6013}
6014
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006015void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006016 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6017 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006018 // In suspend check slow path, usually there are no caller-save registers at all.
6019 // If SIMD instructions are present, however, we force spilling all live SIMD
6020 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006021 locations->SetCustomSlowPathCallerSaves(
6022 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006023}
6024
6025void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006026 HBasicBlock* block = instruction->GetBlock();
6027 if (block->GetLoopInformation() != nullptr) {
6028 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6029 // The back edge will generate the suspend check.
6030 return;
6031 }
6032 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6033 // The goto will generate the suspend check.
6034 return;
6035 }
6036 GenerateSuspendCheck(instruction, nullptr);
6037}
6038
6039void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6040 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006041 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006042 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6043 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006044 slow_path =
6045 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006046 instruction->SetSlowPath(slow_path);
6047 codegen_->AddSlowPath(slow_path);
6048 if (successor != nullptr) {
6049 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006050 }
6051 } else {
6052 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6053 }
6054
Andreas Gampe542451c2016-07-26 09:02:02 -07006055 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006056 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006057 if (successor == nullptr) {
6058 __ j(kNotEqual, slow_path->GetEntryLabel());
6059 __ Bind(slow_path->GetReturnLabel());
6060 } else {
6061 __ j(kEqual, codegen_->GetLabelOf(successor));
6062 __ jmp(slow_path->GetEntryLabel());
6063 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006064}
6065
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006066X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6067 return codegen_->GetAssembler();
6068}
6069
Aart Bikcfe50bb2017-12-12 14:54:12 -08006070void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006071 ScratchRegisterScope ensure_scratch(
6072 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6073 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6074 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006075
Aart Bikcfe50bb2017-12-12 14:54:12 -08006076 // Now that temp register is available (possibly spilled), move blocks of memory.
6077 for (int i = 0; i < number_of_words; i++) {
6078 __ movl(temp_reg, Address(ESP, src + stack_offset));
6079 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6080 stack_offset += kX86WordSize;
6081 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006082}
6083
6084void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006085 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006086 Location source = move->GetSource();
6087 Location destination = move->GetDestination();
6088
6089 if (source.IsRegister()) {
6090 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006091 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006092 } else if (destination.IsFpuRegister()) {
6093 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006094 } else {
6095 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006096 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006097 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006098 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006099 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006100 // Create stack space for 2 elements.
6101 __ subl(ESP, Immediate(2 * elem_size));
6102 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6103 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6104 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6105 // And remove the temporary stack space we allocated.
6106 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006107 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006108 if (destination.IsRegister()) {
6109 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6110 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006111 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006112 } else if (destination.IsRegisterPair()) {
6113 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6114 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6115 __ psrlq(src_reg, Immediate(32));
6116 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006117 } else if (destination.IsStackSlot()) {
6118 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006119 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006120 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006121 } else {
6122 DCHECK(destination.IsSIMDStackSlot());
6123 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006124 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006125 } else if (source.IsStackSlot()) {
6126 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006127 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006128 } else if (destination.IsFpuRegister()) {
6129 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006130 } else {
6131 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006132 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006133 }
6134 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006135 if (destination.IsRegisterPair()) {
6136 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6137 __ movl(destination.AsRegisterPairHigh<Register>(),
6138 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6139 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006140 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6141 } else {
6142 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006143 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006144 }
Aart Bik5576f372017-03-23 16:17:37 -07006145 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006146 if (destination.IsFpuRegister()) {
6147 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6148 } else {
6149 DCHECK(destination.IsSIMDStackSlot());
6150 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6151 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006152 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006153 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006154 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006155 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006156 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006157 if (value == 0) {
6158 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6159 } else {
6160 __ movl(destination.AsRegister<Register>(), Immediate(value));
6161 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006162 } else {
6163 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006164 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006165 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006166 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006167 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006168 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006169 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006170 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006171 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6172 if (value == 0) {
6173 // Easy handling of 0.0.
6174 __ xorps(dest, dest);
6175 } else {
6176 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006177 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6178 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6179 __ movl(temp, Immediate(value));
6180 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006181 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006182 } else {
6183 DCHECK(destination.IsStackSlot()) << destination;
6184 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6185 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006186 } else if (constant->IsLongConstant()) {
6187 int64_t value = constant->AsLongConstant()->GetValue();
6188 int32_t low_value = Low32Bits(value);
6189 int32_t high_value = High32Bits(value);
6190 Immediate low(low_value);
6191 Immediate high(high_value);
6192 if (destination.IsDoubleStackSlot()) {
6193 __ movl(Address(ESP, destination.GetStackIndex()), low);
6194 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6195 } else {
6196 __ movl(destination.AsRegisterPairLow<Register>(), low);
6197 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6198 }
6199 } else {
6200 DCHECK(constant->IsDoubleConstant());
6201 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006202 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006203 int32_t low_value = Low32Bits(value);
6204 int32_t high_value = High32Bits(value);
6205 Immediate low(low_value);
6206 Immediate high(high_value);
6207 if (destination.IsFpuRegister()) {
6208 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6209 if (value == 0) {
6210 // Easy handling of 0.0.
6211 __ xorpd(dest, dest);
6212 } else {
6213 __ pushl(high);
6214 __ pushl(low);
6215 __ movsd(dest, Address(ESP, 0));
6216 __ addl(ESP, Immediate(8));
6217 }
6218 } else {
6219 DCHECK(destination.IsDoubleStackSlot()) << destination;
6220 __ movl(Address(ESP, destination.GetStackIndex()), low);
6221 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6222 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006223 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006224 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006225 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006226 }
6227}
6228
Mark Mendella5c19ce2015-04-01 12:51:05 -04006229void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006230 Register suggested_scratch = reg == EAX ? EBX : EAX;
6231 ScratchRegisterScope ensure_scratch(
6232 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6233
6234 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6235 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6236 __ movl(Address(ESP, mem + stack_offset), reg);
6237 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006238}
6239
Mark Mendell7c8d0092015-01-26 11:21:33 -05006240void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006241 ScratchRegisterScope ensure_scratch(
6242 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6243
6244 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6245 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6246 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6247 __ movss(Address(ESP, mem + stack_offset), reg);
6248 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006249}
6250
Aart Bikcfe50bb2017-12-12 14:54:12 -08006251void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6252 size_t extra_slot = 4 * kX86WordSize;
6253 __ subl(ESP, Immediate(extra_slot));
6254 __ movups(Address(ESP, 0), XmmRegister(reg));
6255 ExchangeMemory(0, mem + extra_slot, 4);
6256 __ movups(XmmRegister(reg), Address(ESP, 0));
6257 __ addl(ESP, Immediate(extra_slot));
6258}
6259
6260void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006261 ScratchRegisterScope ensure_scratch1(
6262 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006263
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006264 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6265 ScratchRegisterScope ensure_scratch2(
6266 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006267
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006268 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6269 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006270
6271 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6272 for (int i = 0; i < number_of_words; i++) {
6273 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6274 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6275 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6276 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6277 stack_offset += kX86WordSize;
6278 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006279}
6280
6281void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006282 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006283 Location source = move->GetSource();
6284 Location destination = move->GetDestination();
6285
6286 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006287 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6288 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6289 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6290 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6291 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006292 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006293 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006294 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006295 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006296 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006297 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006298 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6299 // Use XOR Swap algorithm to avoid a temporary.
6300 DCHECK_NE(source.reg(), destination.reg());
6301 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6302 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6303 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6304 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6305 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6306 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6307 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006308 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6309 // Take advantage of the 16 bytes in the XMM register.
6310 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6311 Address stack(ESP, destination.GetStackIndex());
6312 // Load the double into the high doubleword.
6313 __ movhpd(reg, stack);
6314
6315 // Store the low double into the destination.
6316 __ movsd(stack, reg);
6317
6318 // Move the high double to the low double.
6319 __ psrldq(reg, Immediate(8));
6320 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6321 // Take advantage of the 16 bytes in the XMM register.
6322 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6323 Address stack(ESP, source.GetStackIndex());
6324 // Load the double into the high doubleword.
6325 __ movhpd(reg, stack);
6326
6327 // Store the low double into the destination.
6328 __ movsd(stack, reg);
6329
6330 // Move the high double to the low double.
6331 __ psrldq(reg, Immediate(8));
6332 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006333 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6334 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6335 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6336 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6337 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6338 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6339 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006340 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006341 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006342 }
6343}
6344
6345void ParallelMoveResolverX86::SpillScratch(int reg) {
6346 __ pushl(static_cast<Register>(reg));
6347}
6348
6349void ParallelMoveResolverX86::RestoreScratch(int reg) {
6350 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006351}
6352
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006353HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6354 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006355 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006356 case HLoadClass::LoadKind::kInvalid:
6357 LOG(FATAL) << "UNREACHABLE";
6358 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006359 case HLoadClass::LoadKind::kReferrersClass:
6360 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006361 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006362 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006363 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006364 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006365 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006366 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006367 DCHECK(Runtime::Current()->UseJitCompilation());
6368 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006369 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006370 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006371 break;
6372 }
6373 return desired_class_load_kind;
6374}
6375
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006376void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006377 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006378 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006379 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006380 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006381 cls,
6382 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006383 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006384 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006385 return;
6386 }
Vladimir Marko41559982017-01-06 14:04:23 +00006387 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006388
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006389 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6390 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006391 ? LocationSummary::kCallOnSlowPath
6392 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006393 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006394 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006395 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006396 }
6397
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006398 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006399 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006400 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006401 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006402 locations->SetInAt(0, Location::RequiresRegister());
6403 }
6404 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006405 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6406 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6407 // Rely on the type resolution and/or initialization to save everything.
6408 RegisterSet caller_saves = RegisterSet::Empty();
6409 InvokeRuntimeCallingConvention calling_convention;
6410 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6411 locations->SetCustomSlowPathCallerSaves(caller_saves);
6412 } else {
6413 // For non-Baker read barrier we have a temp-clobbering call.
6414 }
6415 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006416}
6417
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006418Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006419 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006420 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006421 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006422 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006423 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006424 PatchInfo<Label>* info = &jit_class_patches_.back();
6425 return &info->label;
6426}
6427
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006428// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6429// move.
6430void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006431 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006432 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006433 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006434 return;
6435 }
Vladimir Marko41559982017-01-06 14:04:23 +00006436 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006437
Vladimir Marko41559982017-01-06 14:04:23 +00006438 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 Location out_loc = locations->Out();
6440 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006442 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006443 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6444 ? kWithoutReadBarrier
6445 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006446 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006447 case HLoadClass::LoadKind::kReferrersClass: {
6448 DCHECK(!cls->CanCallRuntime());
6449 DCHECK(!cls->MustGenerateClinitCheck());
6450 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6451 Register current_method = locations->InAt(0).AsRegister<Register>();
6452 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006453 cls,
6454 out_loc,
6455 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006456 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006457 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006458 break;
6459 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006460 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006461 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006462 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006463 Register method_address = locations->InAt(0).AsRegister<Register>();
6464 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006465 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006466 break;
6467 }
6468 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006469 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006470 uint32_t address = dchecked_integral_cast<uint32_t>(
6471 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6472 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006474 break;
6475 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006476 case HLoadClass::LoadKind::kBootImageClassTable: {
6477 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6478 Register method_address = locations->InAt(0).AsRegister<Register>();
6479 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006480 codegen_->RecordBootImageTypePatch(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006481 // Extract the reference from the slot data, i.e. clear the hash bits.
6482 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6483 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6484 if (masked_hash != 0) {
6485 __ subl(out, Immediate(masked_hash));
6486 }
6487 break;
6488 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006489 case HLoadClass::LoadKind::kBssEntry: {
6490 Register method_address = locations->InAt(0).AsRegister<Register>();
6491 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6492 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6493 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6494 generate_null_check = true;
6495 break;
6496 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006497 case HLoadClass::LoadKind::kJitTableAddress: {
6498 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6499 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006500 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006501 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006502 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006503 break;
6504 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006505 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006506 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006507 LOG(FATAL) << "UNREACHABLE";
6508 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006509 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006510
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006511 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6512 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006513 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006514 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6515 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006516
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006517 if (generate_null_check) {
6518 __ testl(out, out);
6519 __ j(kEqual, slow_path->GetEntryLabel());
6520 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006521
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006522 if (cls->MustGenerateClinitCheck()) {
6523 GenerateClassInitializationCheck(slow_path, out);
6524 } else {
6525 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006526 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006527 }
6528}
6529
6530void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6531 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006532 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006533 locations->SetInAt(0, Location::RequiresRegister());
6534 if (check->HasUses()) {
6535 locations->SetOut(Location::SameAsFirstInput());
6536 }
6537}
6538
6539void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006540 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006541 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006542 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006543 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006544 GenerateClassInitializationCheck(slow_path,
6545 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006546}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006547
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006548void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006549 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006550 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6551 const size_t status_byte_offset =
6552 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6553 constexpr uint32_t shifted_initialized_value =
6554 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6555
6556 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006557 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006558 __ Bind(slow_path->GetExitLabel());
6559 // No need for memory fence, thanks to the X86 memory model.
6560}
6561
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006562HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6563 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006564 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006565 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006566 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006567 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006568 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006569 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006570 case HLoadString::LoadKind::kJitTableAddress:
6571 DCHECK(Runtime::Current()->UseJitCompilation());
6572 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006573 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006574 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006575 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006576 }
6577 return desired_string_load_kind;
6578}
6579
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006580void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006581 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006582 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006583 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006584 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006585 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006586 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006587 locations->SetInAt(0, Location::RequiresRegister());
6588 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006589 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006590 locations->SetOut(Location::RegisterLocation(EAX));
6591 } else {
6592 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006593 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6594 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006595 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006596 RegisterSet caller_saves = RegisterSet::Empty();
6597 InvokeRuntimeCallingConvention calling_convention;
6598 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6599 locations->SetCustomSlowPathCallerSaves(caller_saves);
6600 } else {
6601 // For non-Baker read barrier we have a temp-clobbering call.
6602 }
6603 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006604 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006605}
6606
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006607Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006608 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006609 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006610 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006611 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006612 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006613 PatchInfo<Label>* info = &jit_string_patches_.back();
6614 return &info->label;
6615}
6616
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006617// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6618// move.
6619void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006620 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006621 Location out_loc = locations->Out();
6622 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006625 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006626 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006627 Register method_address = locations->InAt(0).AsRegister<Register>();
6628 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006629 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006630 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006631 }
6632 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006633 uint32_t address = dchecked_integral_cast<uint32_t>(
6634 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6635 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006636 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006637 return;
6638 }
6639 case HLoadString::LoadKind::kBootImageInternTable: {
6640 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6641 Register method_address = locations->InAt(0).AsRegister<Register>();
6642 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006643 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006644 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006645 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006646 case HLoadString::LoadKind::kBssEntry: {
6647 Register method_address = locations->InAt(0).AsRegister<Register>();
6648 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6649 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006650 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006651 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006652 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006653 codegen_->AddSlowPath(slow_path);
6654 __ testl(out, out);
6655 __ j(kEqual, slow_path->GetEntryLabel());
6656 __ Bind(slow_path->GetExitLabel());
6657 return;
6658 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006659 case HLoadString::LoadKind::kJitTableAddress: {
6660 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6661 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006662 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006663 // /* GcRoot<mirror::String> */ out = *address
6664 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6665 return;
6666 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006667 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006668 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006671 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006672 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006673 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006674 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006675 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6676 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006677}
6678
David Brazdilcb1c0552015-08-04 16:22:25 +01006679static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006680 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006681}
6682
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006683void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6684 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006685 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006686 locations->SetOut(Location::RequiresRegister());
6687}
6688
6689void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006690 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6691}
6692
6693void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006694 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006695}
6696
6697void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6698 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006699}
6700
6701void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006702 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6703 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006704 InvokeRuntimeCallingConvention calling_convention;
6705 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6706}
6707
6708void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006709 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006710 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006711}
6712
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006713// Temp is used for read barrier.
6714static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6715 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006716 !kUseBakerReadBarrier &&
6717 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006718 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006719 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6720 return 1;
6721 }
6722 return 0;
6723}
6724
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006725// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006726// interface pointer, one for loading the current interface.
6727// The other checks have one temp for loading the object's class.
6728static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006729 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006730 return 2;
6731 }
6732 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006733}
6734
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006735void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006736 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006738 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740 case TypeCheckKind::kExactCheck:
6741 case TypeCheckKind::kAbstractClassCheck:
6742 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006743 case TypeCheckKind::kArrayObjectCheck: {
6744 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6745 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6746 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006748 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750 case TypeCheckKind::kUnresolvedCheck:
6751 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006752 call_kind = LocationSummary::kCallOnSlowPath;
6753 break;
6754 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006755
Vladimir Markoca6fff82017-10-03 14:49:14 +01006756 LocationSummary* locations =
6757 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006758 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006759 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006760 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006762 locations->SetInAt(1, Location::Any());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6764 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006765 // When read barriers are enabled, we need a temporary register for some cases.
6766 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006767}
6768
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006769void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006770 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006771 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006772 Location obj_loc = locations->InAt(0);
6773 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006774 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006775 Location out_loc = locations->Out();
6776 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006777 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6778 DCHECK_LE(num_temps, 1u);
6779 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006780 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006781 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6782 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6783 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006784 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006786
6787 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006788 // Avoid null check if we know obj is not null.
6789 if (instruction->MustDoNullCheck()) {
6790 __ testl(obj, obj);
6791 __ j(kEqual, &zero);
6792 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006793
Roland Levillain7c1559a2015-12-15 10:55:36 +00006794 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006796 ReadBarrierOption read_barrier_option =
6797 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006798 // /* HeapReference<Class> */ out = obj->klass_
6799 GenerateReferenceLoadTwoRegisters(instruction,
6800 out_loc,
6801 obj_loc,
6802 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006803 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006804 if (cls.IsRegister()) {
6805 __ cmpl(out, cls.AsRegister<Register>());
6806 } else {
6807 DCHECK(cls.IsStackSlot()) << cls;
6808 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6809 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006810
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006811 // Classes must be equal for the instanceof to succeed.
6812 __ j(kNotEqual, &zero);
6813 __ movl(out, Immediate(1));
6814 __ jmp(&done);
6815 break;
6816 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006817
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006818 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006819 ReadBarrierOption read_barrier_option =
6820 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006821 // /* HeapReference<Class> */ out = obj->klass_
6822 GenerateReferenceLoadTwoRegisters(instruction,
6823 out_loc,
6824 obj_loc,
6825 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006826 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006827 // If the class is abstract, we eagerly fetch the super class of the
6828 // object to avoid doing a comparison we know will fail.
6829 NearLabel loop;
6830 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006831 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006832 GenerateReferenceLoadOneRegister(instruction,
6833 out_loc,
6834 super_offset,
6835 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006836 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006837 __ testl(out, out);
6838 // If `out` is null, we use it for the result, and jump to `done`.
6839 __ j(kEqual, &done);
6840 if (cls.IsRegister()) {
6841 __ cmpl(out, cls.AsRegister<Register>());
6842 } else {
6843 DCHECK(cls.IsStackSlot()) << cls;
6844 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6845 }
6846 __ j(kNotEqual, &loop);
6847 __ movl(out, Immediate(1));
6848 if (zero.IsLinked()) {
6849 __ jmp(&done);
6850 }
6851 break;
6852 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006853
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006854 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006855 ReadBarrierOption read_barrier_option =
6856 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006857 // /* HeapReference<Class> */ out = obj->klass_
6858 GenerateReferenceLoadTwoRegisters(instruction,
6859 out_loc,
6860 obj_loc,
6861 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006862 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006863 // Walk over the class hierarchy to find a match.
6864 NearLabel loop, success;
6865 __ Bind(&loop);
6866 if (cls.IsRegister()) {
6867 __ cmpl(out, cls.AsRegister<Register>());
6868 } else {
6869 DCHECK(cls.IsStackSlot()) << cls;
6870 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6871 }
6872 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006873 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006874 GenerateReferenceLoadOneRegister(instruction,
6875 out_loc,
6876 super_offset,
6877 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006878 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006879 __ testl(out, out);
6880 __ j(kNotEqual, &loop);
6881 // If `out` is null, we use it for the result, and jump to `done`.
6882 __ jmp(&done);
6883 __ Bind(&success);
6884 __ movl(out, Immediate(1));
6885 if (zero.IsLinked()) {
6886 __ jmp(&done);
6887 }
6888 break;
6889 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006890
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006891 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006892 ReadBarrierOption read_barrier_option =
6893 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006894 // /* HeapReference<Class> */ out = obj->klass_
6895 GenerateReferenceLoadTwoRegisters(instruction,
6896 out_loc,
6897 obj_loc,
6898 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006899 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006900 // Do an exact check.
6901 NearLabel exact_check;
6902 if (cls.IsRegister()) {
6903 __ cmpl(out, cls.AsRegister<Register>());
6904 } else {
6905 DCHECK(cls.IsStackSlot()) << cls;
6906 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6907 }
6908 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006909 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006910 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006911 GenerateReferenceLoadOneRegister(instruction,
6912 out_loc,
6913 component_offset,
6914 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006915 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006916 __ testl(out, out);
6917 // If `out` is null, we use it for the result, and jump to `done`.
6918 __ j(kEqual, &done);
6919 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6920 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006921 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006922 __ movl(out, Immediate(1));
6923 __ jmp(&done);
6924 break;
6925 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006926
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006927 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006928 // No read barrier since the slow path will retry upon failure.
6929 // /* HeapReference<Class> */ out = obj->klass_
6930 GenerateReferenceLoadTwoRegisters(instruction,
6931 out_loc,
6932 obj_loc,
6933 class_offset,
6934 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006935 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 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006942 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6943 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006944 codegen_->AddSlowPath(slow_path);
6945 __ j(kNotEqual, slow_path->GetEntryLabel());
6946 __ movl(out, Immediate(1));
6947 if (zero.IsLinked()) {
6948 __ jmp(&done);
6949 }
6950 break;
6951 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006952
Calin Juravle98893e12015-10-02 21:05:03 +01006953 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006954 case TypeCheckKind::kInterfaceCheck: {
6955 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006956 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006957 // cases.
6958 //
6959 // We cannot directly call the InstanceofNonTrivial runtime
6960 // entry point without resorting to a type checking slow path
6961 // here (i.e. by calling InvokeRuntime directly), as it would
6962 // require to assign fixed registers for the inputs of this
6963 // HInstanceOf instruction (following the runtime calling
6964 // convention), which might be cluttered by the potential first
6965 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006966 //
6967 // TODO: Introduce a new runtime entry point taking the object
6968 // to test (instead of its class) as argument, and let it deal
6969 // with the read barrier issues. This will let us refactor this
6970 // case of the `switch` code as it was previously (with a direct
6971 // call to the runtime not using a type checking slow path).
6972 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006973 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006974 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6975 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006976 codegen_->AddSlowPath(slow_path);
6977 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006978 if (zero.IsLinked()) {
6979 __ jmp(&done);
6980 }
6981 break;
6982 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006983 }
6984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006985 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006986 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006987 __ xorl(out, out);
6988 }
6989
6990 if (done.IsLinked()) {
6991 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006992 }
6993
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006994 if (slow_path != nullptr) {
6995 __ Bind(slow_path->GetExitLabel());
6996 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006997}
6998
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006999void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007000 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007001 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007002 LocationSummary* locations =
7003 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007004 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007005 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7006 // Require a register for the interface check since there is a loop that compares the class to
7007 // a memory address.
7008 locations->SetInAt(1, Location::RequiresRegister());
7009 } else {
7010 locations->SetInAt(1, Location::Any());
7011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007012 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
7013 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007014 // When read barriers are enabled, we need an additional temporary register for some cases.
7015 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7016}
7017
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007018void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007020 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007021 Location obj_loc = locations->InAt(0);
7022 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007023 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007024 Location temp_loc = locations->GetTemp(0);
7025 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007026 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7027 DCHECK_GE(num_temps, 1u);
7028 DCHECK_LE(num_temps, 2u);
7029 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7030 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7031 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7032 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7033 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7034 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7035 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7036 const uint32_t object_array_data_offset =
7037 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007038
Vladimir Marko87584542017-12-12 17:47:52 +00007039 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007040 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007041 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7042 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007043 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007044
Roland Levillain0d5a2812015-11-13 10:07:31 +00007045 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007046 // Avoid null check if we know obj is not null.
7047 if (instruction->MustDoNullCheck()) {
7048 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007049 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007050 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007051
Roland Levillain0d5a2812015-11-13 10:07:31 +00007052 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007053 case TypeCheckKind::kExactCheck:
7054 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007055 // /* HeapReference<Class> */ temp = obj->klass_
7056 GenerateReferenceLoadTwoRegisters(instruction,
7057 temp_loc,
7058 obj_loc,
7059 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007060 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007061
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007062 if (cls.IsRegister()) {
7063 __ cmpl(temp, cls.AsRegister<Register>());
7064 } else {
7065 DCHECK(cls.IsStackSlot()) << cls;
7066 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7067 }
7068 // Jump to slow path for throwing the exception or doing a
7069 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007070 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007071 break;
7072 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007073
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007074 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007075 // /* HeapReference<Class> */ temp = obj->klass_
7076 GenerateReferenceLoadTwoRegisters(instruction,
7077 temp_loc,
7078 obj_loc,
7079 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007080 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007081
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007082 // If the class is abstract, we eagerly fetch the super class of the
7083 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007084 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007085 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007086 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007087 GenerateReferenceLoadOneRegister(instruction,
7088 temp_loc,
7089 super_offset,
7090 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007091 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007092
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007093 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7094 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007095 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007096 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007097
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007098 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007099 if (cls.IsRegister()) {
7100 __ cmpl(temp, cls.AsRegister<Register>());
7101 } else {
7102 DCHECK(cls.IsStackSlot()) << cls;
7103 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7104 }
7105 __ j(kNotEqual, &loop);
7106 break;
7107 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007108
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007109 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007110 // /* HeapReference<Class> */ temp = obj->klass_
7111 GenerateReferenceLoadTwoRegisters(instruction,
7112 temp_loc,
7113 obj_loc,
7114 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007115 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007116
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007117 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007118 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007119 __ Bind(&loop);
7120 if (cls.IsRegister()) {
7121 __ cmpl(temp, cls.AsRegister<Register>());
7122 } else {
7123 DCHECK(cls.IsStackSlot()) << cls;
7124 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7125 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007126 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007127
Roland Levillain0d5a2812015-11-13 10:07:31 +00007128 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007129 GenerateReferenceLoadOneRegister(instruction,
7130 temp_loc,
7131 super_offset,
7132 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007133 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007134
7135 // If the class reference currently in `temp` is not null, jump
7136 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007137 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007138 __ j(kNotZero, &loop);
7139 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007140 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007141 break;
7142 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007143
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007144 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007145 // /* HeapReference<Class> */ temp = obj->klass_
7146 GenerateReferenceLoadTwoRegisters(instruction,
7147 temp_loc,
7148 obj_loc,
7149 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007150 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007151
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007152 // Do an exact check.
7153 if (cls.IsRegister()) {
7154 __ cmpl(temp, cls.AsRegister<Register>());
7155 } else {
7156 DCHECK(cls.IsStackSlot()) << cls;
7157 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7158 }
7159 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007160
7161 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007162 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007163 GenerateReferenceLoadOneRegister(instruction,
7164 temp_loc,
7165 component_offset,
7166 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007167 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007168
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007169 // If the component type is null (i.e. the object not an array), jump to the slow path to
7170 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007171 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007172 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007173
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007174 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007175 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007176 break;
7177 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007178
Calin Juravle98893e12015-10-02 21:05:03 +01007179 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007180 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007181 // We cannot directly call the CheckCast runtime entry point
7182 // without resorting to a type checking slow path here (i.e. by
7183 // calling InvokeRuntime directly), as it would require to
7184 // assign fixed registers for the inputs of this HInstanceOf
7185 // instruction (following the runtime calling convention), which
7186 // might be cluttered by the potential first read barrier
7187 // emission at the beginning of this method.
7188 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007189 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007190
7191 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007192 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7193 // We can not get false positives by doing this.
7194 // /* HeapReference<Class> */ temp = obj->klass_
7195 GenerateReferenceLoadTwoRegisters(instruction,
7196 temp_loc,
7197 obj_loc,
7198 class_offset,
7199 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007200
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007201 // /* HeapReference<Class> */ temp = temp->iftable_
7202 GenerateReferenceLoadTwoRegisters(instruction,
7203 temp_loc,
7204 temp_loc,
7205 iftable_offset,
7206 kWithoutReadBarrier);
7207 // Iftable is never null.
7208 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7209 // Maybe poison the `cls` for direct comparison with memory.
7210 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7211 // Loop through the iftable and check if any class matches.
7212 NearLabel start_loop;
7213 __ Bind(&start_loop);
7214 // Need to subtract first to handle the empty array case.
7215 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7216 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7217 // Go to next interface if the classes do not match.
7218 __ cmpl(cls.AsRegister<Register>(),
7219 CodeGeneratorX86::ArrayAddress(temp,
7220 maybe_temp2_loc,
7221 TIMES_4,
7222 object_array_data_offset));
7223 __ j(kNotEqual, &start_loop);
7224 // If `cls` was poisoned above, unpoison it.
7225 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007226 break;
7227 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007228 }
7229 __ Bind(&done);
7230
Roland Levillain0d5a2812015-11-13 10:07:31 +00007231 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007232}
7233
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007234void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007235 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7236 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007237 InvokeRuntimeCallingConvention calling_convention;
7238 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7239}
7240
7241void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007242 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7243 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007244 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007245 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007246 if (instruction->IsEnter()) {
7247 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7248 } else {
7249 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7250 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007251}
7252
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007253void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7254void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7255void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7256
7257void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7258 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007259 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007260 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7261 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007262 locations->SetInAt(0, Location::RequiresRegister());
7263 locations->SetInAt(1, Location::Any());
7264 locations->SetOut(Location::SameAsFirstInput());
7265}
7266
7267void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7268 HandleBitwiseOperation(instruction);
7269}
7270
7271void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7272 HandleBitwiseOperation(instruction);
7273}
7274
7275void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7276 HandleBitwiseOperation(instruction);
7277}
7278
7279void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7280 LocationSummary* locations = instruction->GetLocations();
7281 Location first = locations->InAt(0);
7282 Location second = locations->InAt(1);
7283 DCHECK(first.Equals(locations->Out()));
7284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007285 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007286 if (second.IsRegister()) {
7287 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007288 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007289 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007290 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007291 } else {
7292 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007293 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007294 }
7295 } else if (second.IsConstant()) {
7296 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007297 __ andl(first.AsRegister<Register>(),
7298 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007299 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007300 __ orl(first.AsRegister<Register>(),
7301 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007302 } else {
7303 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007304 __ xorl(first.AsRegister<Register>(),
7305 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007306 }
7307 } else {
7308 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007309 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007310 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007311 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007312 } else {
7313 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007314 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007315 }
7316 }
7317 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007318 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007319 if (second.IsRegisterPair()) {
7320 if (instruction->IsAnd()) {
7321 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7322 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7323 } else if (instruction->IsOr()) {
7324 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7325 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7326 } else {
7327 DCHECK(instruction->IsXor());
7328 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7329 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7330 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007331 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007332 if (instruction->IsAnd()) {
7333 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7334 __ andl(first.AsRegisterPairHigh<Register>(),
7335 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7336 } else if (instruction->IsOr()) {
7337 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7338 __ orl(first.AsRegisterPairHigh<Register>(),
7339 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7340 } else {
7341 DCHECK(instruction->IsXor());
7342 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7343 __ xorl(first.AsRegisterPairHigh<Register>(),
7344 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7345 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007346 } else {
7347 DCHECK(second.IsConstant()) << second;
7348 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007349 int32_t low_value = Low32Bits(value);
7350 int32_t high_value = High32Bits(value);
7351 Immediate low(low_value);
7352 Immediate high(high_value);
7353 Register first_low = first.AsRegisterPairLow<Register>();
7354 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007355 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007356 if (low_value == 0) {
7357 __ xorl(first_low, first_low);
7358 } else if (low_value != -1) {
7359 __ andl(first_low, low);
7360 }
7361 if (high_value == 0) {
7362 __ xorl(first_high, first_high);
7363 } else if (high_value != -1) {
7364 __ andl(first_high, high);
7365 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007366 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007367 if (low_value != 0) {
7368 __ orl(first_low, low);
7369 }
7370 if (high_value != 0) {
7371 __ orl(first_high, high);
7372 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007373 } else {
7374 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007375 if (low_value != 0) {
7376 __ xorl(first_low, low);
7377 }
7378 if (high_value != 0) {
7379 __ xorl(first_high, high);
7380 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007381 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007382 }
7383 }
7384}
7385
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007386void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7387 HInstruction* instruction,
7388 Location out,
7389 uint32_t offset,
7390 Location maybe_temp,
7391 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007392 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007393 if (read_barrier_option == kWithReadBarrier) {
7394 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007395 if (kUseBakerReadBarrier) {
7396 // Load with fast path based Baker's read barrier.
7397 // /* HeapReference<Object> */ out = *(out + offset)
7398 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007399 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007400 } else {
7401 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007402 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007403 // in the following move operation, as we will need it for the
7404 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007405 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007406 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007407 // /* HeapReference<Object> */ out = *(out + offset)
7408 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007409 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007410 }
7411 } else {
7412 // Plain load with no read barrier.
7413 // /* HeapReference<Object> */ out = *(out + offset)
7414 __ movl(out_reg, Address(out_reg, offset));
7415 __ MaybeUnpoisonHeapReference(out_reg);
7416 }
7417}
7418
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007419void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7420 HInstruction* instruction,
7421 Location out,
7422 Location obj,
7423 uint32_t offset,
7424 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007425 Register out_reg = out.AsRegister<Register>();
7426 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007427 if (read_barrier_option == kWithReadBarrier) {
7428 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007429 if (kUseBakerReadBarrier) {
7430 // Load with fast path based Baker's read barrier.
7431 // /* HeapReference<Object> */ out = *(obj + offset)
7432 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007433 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007434 } else {
7435 // Load with slow path based read barrier.
7436 // /* HeapReference<Object> */ out = *(obj + offset)
7437 __ movl(out_reg, Address(obj_reg, offset));
7438 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7439 }
7440 } else {
7441 // Plain load with no read barrier.
7442 // /* HeapReference<Object> */ out = *(obj + offset)
7443 __ movl(out_reg, Address(obj_reg, offset));
7444 __ MaybeUnpoisonHeapReference(out_reg);
7445 }
7446}
7447
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007448void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7449 HInstruction* instruction,
7450 Location root,
7451 const Address& address,
7452 Label* fixup_label,
7453 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007454 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007455 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007456 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007457 if (kUseBakerReadBarrier) {
7458 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7459 // Baker's read barrier are used:
7460 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007461 // root = obj.field;
7462 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7463 // if (temp != null) {
7464 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007465 // }
7466
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007467 // /* GcRoot<mirror::Object> */ root = *address
7468 __ movl(root_reg, address);
7469 if (fixup_label != nullptr) {
7470 __ Bind(fixup_label);
7471 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007472 static_assert(
7473 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7474 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7475 "have different sizes.");
7476 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7477 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7478 "have different sizes.");
7479
Vladimir Marko953437b2016-08-24 08:30:46 +00007480 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007481 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007482 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007483 codegen_->AddSlowPath(slow_path);
7484
Roland Levillaind966ce72017-02-09 16:20:14 +00007485 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7486 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007487 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007488 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7489 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007490 __ j(kNotEqual, slow_path->GetEntryLabel());
7491 __ Bind(slow_path->GetExitLabel());
7492 } else {
7493 // GC root loaded through a slow path for read barriers other
7494 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007495 // /* GcRoot<mirror::Object>* */ root = address
7496 __ leal(root_reg, address);
7497 if (fixup_label != nullptr) {
7498 __ Bind(fixup_label);
7499 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007500 // /* mirror::Object* */ root = root->Read()
7501 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7502 }
7503 } else {
7504 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007505 // /* GcRoot<mirror::Object> */ root = *address
7506 __ movl(root_reg, address);
7507 if (fixup_label != nullptr) {
7508 __ Bind(fixup_label);
7509 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007510 // Note that GC roots are not affected by heap poisoning, thus we
7511 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007512 }
7513}
7514
7515void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7516 Location ref,
7517 Register obj,
7518 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007519 bool needs_null_check) {
7520 DCHECK(kEmitCompilerReadBarrier);
7521 DCHECK(kUseBakerReadBarrier);
7522
7523 // /* HeapReference<Object> */ ref = *(obj + offset)
7524 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007525 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007526}
7527
7528void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7529 Location ref,
7530 Register obj,
7531 uint32_t data_offset,
7532 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007533 bool needs_null_check) {
7534 DCHECK(kEmitCompilerReadBarrier);
7535 DCHECK(kUseBakerReadBarrier);
7536
Roland Levillain3d312422016-06-23 13:53:42 +01007537 static_assert(
7538 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7539 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007540 // /* HeapReference<Object> */ ref =
7541 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007542 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007543 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007544}
7545
7546void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7547 Location ref,
7548 Register obj,
7549 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007550 bool needs_null_check,
7551 bool always_update_field,
7552 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007553 DCHECK(kEmitCompilerReadBarrier);
7554 DCHECK(kUseBakerReadBarrier);
7555
7556 // In slow path based read barriers, the read barrier call is
7557 // inserted after the original load. However, in fast path based
7558 // Baker's read barriers, we need to perform the load of
7559 // mirror::Object::monitor_ *before* the original reference load.
7560 // This load-load ordering is required by the read barrier.
7561 // The fast path/slow path (for Baker's algorithm) should look like:
7562 //
7563 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7564 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7565 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007566 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007567 // if (is_gray) {
7568 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7569 // }
7570 //
7571 // Note: the original implementation in ReadBarrier::Barrier is
7572 // slightly more complex as:
7573 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007574 // the high-bits of rb_state, which are expected to be all zeroes
7575 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7576 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007577 // - it performs additional checks that we do not do here for
7578 // performance reasons.
7579
7580 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007581 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7582
Vladimir Marko953437b2016-08-24 08:30:46 +00007583 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007584 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7585 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007586 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7587 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7588 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7589
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007590 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007591 // ref = ReadBarrier::Mark(ref);
7592 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7593 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007594 if (needs_null_check) {
7595 MaybeRecordImplicitNullCheck(instruction);
7596 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007597
7598 // Load fence to prevent load-load reordering.
7599 // Note that this is a no-op, thanks to the x86 memory model.
7600 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7601
7602 // The actual reference load.
7603 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007604 __ movl(ref_reg, src); // Flags are unaffected.
7605
7606 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7607 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007608 SlowPathCode* slow_path;
7609 if (always_update_field) {
7610 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007611 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007612 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7613 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007614 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007615 instruction, ref, /* unpoison_ref_before_marking */ true);
7616 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007617 AddSlowPath(slow_path);
7618
7619 // We have done the "if" of the gray bit check above, now branch based on the flags.
7620 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007621
7622 // Object* ref = ref_addr->AsMirrorPtr()
7623 __ MaybeUnpoisonHeapReference(ref_reg);
7624
Roland Levillain7c1559a2015-12-15 10:55:36 +00007625 __ Bind(slow_path->GetExitLabel());
7626}
7627
7628void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7629 Location out,
7630 Location ref,
7631 Location obj,
7632 uint32_t offset,
7633 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007634 DCHECK(kEmitCompilerReadBarrier);
7635
Roland Levillain7c1559a2015-12-15 10:55:36 +00007636 // Insert a slow path based read barrier *after* the reference load.
7637 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007638 // If heap poisoning is enabled, the unpoisoning of the loaded
7639 // reference will be carried out by the runtime within the slow
7640 // path.
7641 //
7642 // Note that `ref` currently does not get unpoisoned (when heap
7643 // poisoning is enabled), which is alright as the `ref` argument is
7644 // not used by the artReadBarrierSlow entry point.
7645 //
7646 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007647 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007648 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7649 AddSlowPath(slow_path);
7650
Roland Levillain0d5a2812015-11-13 10:07:31 +00007651 __ jmp(slow_path->GetEntryLabel());
7652 __ Bind(slow_path->GetExitLabel());
7653}
7654
Roland Levillain7c1559a2015-12-15 10:55:36 +00007655void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7656 Location out,
7657 Location ref,
7658 Location obj,
7659 uint32_t offset,
7660 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007661 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007662 // Baker's read barriers shall be handled by the fast path
7663 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7664 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007665 // If heap poisoning is enabled, unpoisoning will be taken care of
7666 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007667 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007668 } else if (kPoisonHeapReferences) {
7669 __ UnpoisonHeapReference(out.AsRegister<Register>());
7670 }
7671}
7672
Roland Levillain7c1559a2015-12-15 10:55:36 +00007673void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7674 Location out,
7675 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007676 DCHECK(kEmitCompilerReadBarrier);
7677
Roland Levillain7c1559a2015-12-15 10:55:36 +00007678 // Insert a slow path based read barrier *after* the GC root load.
7679 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007680 // Note that GC roots are not affected by heap poisoning, so we do
7681 // not need to do anything special for this here.
7682 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007683 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007684 AddSlowPath(slow_path);
7685
Roland Levillain0d5a2812015-11-13 10:07:31 +00007686 __ jmp(slow_path->GetEntryLabel());
7687 __ Bind(slow_path->GetExitLabel());
7688}
7689
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007690void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007691 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007692 LOG(FATAL) << "Unreachable";
7693}
7694
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007695void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007696 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007697 LOG(FATAL) << "Unreachable";
7698}
7699
Mark Mendellfe57faa2015-09-18 09:26:15 -04007700// Simple implementation of packed switch - generate cascaded compare/jumps.
7701void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7702 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007703 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007704 locations->SetInAt(0, Location::RequiresRegister());
7705}
7706
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007707void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7708 int32_t lower_bound,
7709 uint32_t num_entries,
7710 HBasicBlock* switch_block,
7711 HBasicBlock* default_block) {
7712 // Figure out the correct compare values and jump conditions.
7713 // Handle the first compare/branch as a special case because it might
7714 // jump to the default case.
7715 DCHECK_GT(num_entries, 2u);
7716 Condition first_condition;
7717 uint32_t index;
7718 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7719 if (lower_bound != 0) {
7720 first_condition = kLess;
7721 __ cmpl(value_reg, Immediate(lower_bound));
7722 __ j(first_condition, codegen_->GetLabelOf(default_block));
7723 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007724
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007725 index = 1;
7726 } else {
7727 // Handle all the compare/jumps below.
7728 first_condition = kBelow;
7729 index = 0;
7730 }
7731
7732 // Handle the rest of the compare/jumps.
7733 for (; index + 1 < num_entries; index += 2) {
7734 int32_t compare_to_value = lower_bound + index + 1;
7735 __ cmpl(value_reg, Immediate(compare_to_value));
7736 // Jump to successors[index] if value < case_value[index].
7737 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7738 // Jump to successors[index + 1] if value == case_value[index + 1].
7739 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7740 }
7741
7742 if (index != num_entries) {
7743 // There are an odd number of entries. Handle the last one.
7744 DCHECK_EQ(index + 1, num_entries);
7745 __ cmpl(value_reg, Immediate(lower_bound + index));
7746 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007747 }
7748
7749 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007750 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7751 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007752 }
7753}
7754
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007755void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7756 int32_t lower_bound = switch_instr->GetStartValue();
7757 uint32_t num_entries = switch_instr->GetNumEntries();
7758 LocationSummary* locations = switch_instr->GetLocations();
7759 Register value_reg = locations->InAt(0).AsRegister<Register>();
7760
7761 GenPackedSwitchWithCompares(value_reg,
7762 lower_bound,
7763 num_entries,
7764 switch_instr->GetBlock(),
7765 switch_instr->GetDefaultBlock());
7766}
7767
Mark Mendell805b3b52015-09-18 14:10:29 -04007768void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7769 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007770 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007771 locations->SetInAt(0, Location::RequiresRegister());
7772
7773 // Constant area pointer.
7774 locations->SetInAt(1, Location::RequiresRegister());
7775
7776 // And the temporary we need.
7777 locations->AddTemp(Location::RequiresRegister());
7778}
7779
7780void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7781 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007782 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007783 LocationSummary* locations = switch_instr->GetLocations();
7784 Register value_reg = locations->InAt(0).AsRegister<Register>();
7785 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7786
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007787 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7788 GenPackedSwitchWithCompares(value_reg,
7789 lower_bound,
7790 num_entries,
7791 switch_instr->GetBlock(),
7792 default_block);
7793 return;
7794 }
7795
Mark Mendell805b3b52015-09-18 14:10:29 -04007796 // Optimizing has a jump area.
7797 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7798 Register constant_area = locations->InAt(1).AsRegister<Register>();
7799
7800 // Remove the bias, if needed.
7801 if (lower_bound != 0) {
7802 __ leal(temp_reg, Address(value_reg, -lower_bound));
7803 value_reg = temp_reg;
7804 }
7805
7806 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007807 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007808 __ cmpl(value_reg, Immediate(num_entries - 1));
7809 __ j(kAbove, codegen_->GetLabelOf(default_block));
7810
7811 // We are in the range of the table.
7812 // Load (target-constant_area) from the jump table, indexing by the value.
7813 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7814
7815 // Compute the actual target address by adding in constant_area.
7816 __ addl(temp_reg, constant_area);
7817
7818 // And jump.
7819 __ jmp(temp_reg);
7820}
7821
Mark Mendell0616ae02015-04-17 12:49:27 -04007822void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7823 HX86ComputeBaseMethodAddress* insn) {
7824 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007825 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007826 locations->SetOut(Location::RequiresRegister());
7827}
7828
7829void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7830 HX86ComputeBaseMethodAddress* insn) {
7831 LocationSummary* locations = insn->GetLocations();
7832 Register reg = locations->Out().AsRegister<Register>();
7833
7834 // Generate call to next instruction.
7835 Label next_instruction;
7836 __ call(&next_instruction);
7837 __ Bind(&next_instruction);
7838
7839 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007840 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007841
7842 // Grab the return address off the stack.
7843 __ popl(reg);
7844}
7845
7846void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7847 HX86LoadFromConstantTable* insn) {
7848 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007849 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007850
7851 locations->SetInAt(0, Location::RequiresRegister());
7852 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7853
7854 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007855 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007856 return;
7857 }
7858
7859 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007860 case DataType::Type::kFloat32:
7861 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007862 locations->SetOut(Location::RequiresFpuRegister());
7863 break;
7864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007865 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007866 locations->SetOut(Location::RequiresRegister());
7867 break;
7868
7869 default:
7870 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7871 }
7872}
7873
7874void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007875 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007876 return;
7877 }
7878
7879 LocationSummary* locations = insn->GetLocations();
7880 Location out = locations->Out();
7881 Register const_area = locations->InAt(0).AsRegister<Register>();
7882 HConstant *value = insn->GetConstant();
7883
7884 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007885 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007886 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007887 codegen_->LiteralFloatAddress(
7888 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007889 break;
7890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007891 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007892 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007893 codegen_->LiteralDoubleAddress(
7894 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007895 break;
7896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007897 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007898 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007899 codegen_->LiteralInt32Address(
7900 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007901 break;
7902
7903 default:
7904 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7905 }
7906}
7907
Mark Mendell0616ae02015-04-17 12:49:27 -04007908/**
7909 * Class to handle late fixup of offsets into constant area.
7910 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007911class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007912 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007913 RIPFixup(CodeGeneratorX86& codegen,
7914 HX86ComputeBaseMethodAddress* base_method_address,
7915 size_t offset)
7916 : codegen_(&codegen),
7917 base_method_address_(base_method_address),
7918 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007919
7920 protected:
7921 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7922
7923 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007924 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007925
7926 private:
7927 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7928 // Patch the correct offset for the instruction. The place to patch is the
7929 // last 4 bytes of the instruction.
7930 // The value to patch is the distance from the offset in the constant area
7931 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007932 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007933 int32_t relative_position =
7934 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007935
7936 // Patch in the right value.
7937 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7938 }
7939
Mark Mendell0616ae02015-04-17 12:49:27 -04007940 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007941 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007942};
7943
Mark Mendell805b3b52015-09-18 14:10:29 -04007944/**
7945 * Class to handle late fixup of offsets to a jump table that will be created in the
7946 * constant area.
7947 */
7948class JumpTableRIPFixup : public RIPFixup {
7949 public:
7950 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007951 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7952 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007953
7954 void CreateJumpTable() {
7955 X86Assembler* assembler = codegen_->GetAssembler();
7956
7957 // Ensure that the reference to the jump table has the correct offset.
7958 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7959 SetOffset(offset_in_constant_table);
7960
7961 // The label values in the jump table are computed relative to the
7962 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007963 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007964
7965 // Populate the jump table with the correct values for the jump table.
7966 int32_t num_entries = switch_instr_->GetNumEntries();
7967 HBasicBlock* block = switch_instr_->GetBlock();
7968 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7969 // The value that we want is the target offset - the position of the table.
7970 for (int32_t i = 0; i < num_entries; i++) {
7971 HBasicBlock* b = successors[i];
7972 Label* l = codegen_->GetLabelOf(b);
7973 DCHECK(l->IsBound());
7974 int32_t offset_to_block = l->Position() - relative_offset;
7975 assembler->AppendInt32(offset_to_block);
7976 }
7977 }
7978
7979 private:
7980 const HX86PackedSwitch* switch_instr_;
7981};
7982
7983void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7984 // Generate the constant area if needed.
7985 X86Assembler* assembler = GetAssembler();
7986 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7987 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7988 // byte values.
7989 assembler->Align(4, 0);
7990 constant_area_start_ = assembler->CodeSize();
7991
7992 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007993 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007994 jump_table->CreateJumpTable();
7995 }
7996
7997 // And now add the constant area to the generated code.
7998 assembler->AddConstantArea();
7999 }
8000
8001 // And finish up.
8002 CodeGenerator::Finalize(allocator);
8003}
8004
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008005Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8006 HX86ComputeBaseMethodAddress* method_base,
8007 Register reg) {
8008 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008009 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008010 return Address(reg, kDummy32BitOffset, fixup);
8011}
8012
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008013Address CodeGeneratorX86::LiteralFloatAddress(float v,
8014 HX86ComputeBaseMethodAddress* method_base,
8015 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008016 AssemblerFixup* fixup =
8017 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008018 return Address(reg, kDummy32BitOffset, fixup);
8019}
8020
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008021Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8022 HX86ComputeBaseMethodAddress* method_base,
8023 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008024 AssemblerFixup* fixup =
8025 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008026 return Address(reg, kDummy32BitOffset, fixup);
8027}
8028
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008029Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8030 HX86ComputeBaseMethodAddress* method_base,
8031 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008032 AssemblerFixup* fixup =
8033 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008034 return Address(reg, kDummy32BitOffset, fixup);
8035}
8036
Aart Bika19616e2016-02-01 18:57:58 -08008037void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8038 if (value == 0) {
8039 __ xorl(dest, dest);
8040 } else {
8041 __ movl(dest, Immediate(value));
8042 }
8043}
8044
8045void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8046 if (value == 0) {
8047 __ testl(dest, dest);
8048 } else {
8049 __ cmpl(dest, Immediate(value));
8050 }
8051}
8052
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008053void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8054 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008055 GenerateIntCompare(lhs_reg, rhs);
8056}
8057
8058void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008059 if (rhs.IsConstant()) {
8060 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008061 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008062 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008063 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008064 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008065 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008066 }
8067}
8068
8069Address CodeGeneratorX86::ArrayAddress(Register obj,
8070 Location index,
8071 ScaleFactor scale,
8072 uint32_t data_offset) {
8073 return index.IsConstant() ?
8074 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8075 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8076}
8077
Mark Mendell805b3b52015-09-18 14:10:29 -04008078Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8079 Register reg,
8080 Register value) {
8081 // Create a fixup to be used to create and address the jump table.
8082 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008083 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008084
8085 // We have to populate the jump tables.
8086 fixups_to_jump_tables_.push_back(table_fixup);
8087
8088 // We want a scaled address, as we are extracting the correct offset from the table.
8089 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8090}
8091
Andreas Gampe85b62f22015-09-09 13:15:38 -07008092// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008093void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008094 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008095 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008096 return;
8097 }
8098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008099 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008100
8101 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8102 if (target.Equals(return_loc)) {
8103 return;
8104 }
8105
8106 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8107 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008108 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008109 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008110 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8111 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008112 GetMoveResolver()->EmitNativeCode(&parallel_move);
8113 } else {
8114 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008115 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008116 parallel_move.AddMove(return_loc, target, type, nullptr);
8117 GetMoveResolver()->EmitNativeCode(&parallel_move);
8118 }
8119}
8120
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008121void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8122 const uint8_t* roots_data,
8123 const PatchInfo<Label>& info,
8124 uint64_t index_in_table) const {
8125 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8126 uintptr_t address =
8127 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8128 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8129 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8130 dchecked_integral_cast<uint32_t>(address);
8131}
8132
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008133void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8134 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008135 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008136 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008137 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008138 }
8139
8140 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008141 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008142 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008143 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008144 }
8145}
8146
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008147void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8148 ATTRIBUTE_UNUSED) {
8149 LOG(FATAL) << "Unreachable";
8150}
8151
8152void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8153 ATTRIBUTE_UNUSED) {
8154 LOG(FATAL) << "Unreachable";
8155}
8156
Roland Levillain4d027112015-07-01 15:41:14 +01008157#undef __
8158
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008159} // namespace x86
8160} // namespace art