blob: b03d72c0f3f5baf0e100aea86f11af28158b1f40 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "intrinsics.h"
29#include "intrinsics_x86.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070032#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010034#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010038#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041
Roland Levillain0d5a2812015-11-13 10:07:31 +000042template<class MirrorType>
43class GcRoot;
44
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000045namespace x86 {
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050049static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Mark Mendell24f2dfa2015-01-14 19:51:45 -050051static constexpr int kC2ConditionMask = 0x400;
52
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000053static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000054
Aart Bik1f8d51b2018-02-15 10:42:37 -080055static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
56static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
57
Roland Levillain7cbd27f2016-08-11 23:53:33 +010058// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
59#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000064 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames2ed20af2015-03-06 13:55:35 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010067 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010073 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010074 instruction_,
75 instruction_->GetDexPc(),
76 this);
Roland Levillain888d0672015-11-23 18:53:50 +000077 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 }
79
Alexandre Rames8158f282015-08-07 10:26:17 +010080 bool IsFatal() const OVERRIDE { return true; }
81
Alexandre Rames9931f312015-06-19 14:47:01 +010082 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
83
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Andreas Gampe85b62f22015-09-09 13:15:38 -070088class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000089 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000090 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000091
Alexandre Rames2ed20af2015-03-06 13:55:35 +000092 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010093 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000094 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010095 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000109 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
110 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
125 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Andreas Gampe85b62f22015-09-09 13:15:38 -0700130class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000132 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100135 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000140 if (instruction_->CanThrowIntoCatchBlock()) {
141 // Live registers will be restored in the catch block if caught.
142 SaveLiveRegisters(codegen, instruction_->GetLocations());
143 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400144
145 // Are we using an array length from memory?
146 HInstruction* array_length = instruction_->InputAt(1);
147 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400149 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
150 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100151 HArrayLength* length = array_length->AsArrayLength();
152 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400153 Location array_loc = array_length->GetLocations()->InAt(0);
154 Address array_len(array_loc.AsRegister<Register>(), len_offset);
155 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
156 // Check for conflicts with index.
157 if (length_loc.Equals(locations->InAt(0))) {
158 // We know we aren't using parameter 2.
159 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
160 }
161 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100162 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100163 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700164 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000166 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100167 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100169 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400170 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100172 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100173 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? kQuickThrowStringBounds
175 : kQuickThrowArrayBounds;
176 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100177 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000178 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 }
180
Alexandre Rames8158f282015-08-07 10:26:17 +0100181 bool IsFatal() const OVERRIDE { return true; }
182
Alexandre Rames9931f312015-06-19 14:47:01 +0100183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
187};
188
Andreas Gampe85b62f22015-09-09 13:15:38 -0700189class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000191 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700195 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100196 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000197 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700198 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100199 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000200 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700201 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 if (successor_ == nullptr) {
203 __ jmp(GetReturnLabel());
204 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100205 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 }
208
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 Label* GetReturnLabel() {
210 DCHECK(successor_ == nullptr);
211 return &return_label_;
212 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100214 HBasicBlock* GetSuccessor() const {
215 return successor_;
216 }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
219
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000220 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000222 Label return_label_;
223
224 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
225};
226
Vladimir Markoaad75c62016-10-03 08:46:48 +0000227class LoadStringSlowPathX86 : public SlowPathCode {
228 public:
229 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 LocationSummary* locations = instruction_->GetLocations();
233 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
234
235 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
236 __ Bind(GetEntryLabel());
237 SaveLiveRegisters(codegen, locations);
238
239 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
241 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000242 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
243 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
244 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
245 RestoreLiveRegisters(codegen, locations);
246
Vladimir Markoaad75c62016-10-03 08:46:48 +0000247 __ jmp(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
251
252 private:
253 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
254};
255
Andreas Gampe85b62f22015-09-09 13:15:38 -0700256class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257 public:
258 LoadClassSlowPathX86(HLoadClass* cls,
259 HInstruction* at,
260 uint32_t dex_pc,
261 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
264 }
265
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000266 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000267 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
269 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000270 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271
272 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000273 dex::TypeIndex type_index = cls_->GetTypeIndex();
274 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100275 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
276 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 instruction_,
278 dex_pc_,
279 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 if (do_clinit_) {
281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
282 } else {
283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
284 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000292 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000293 __ jmp(GetExitLabel());
294 }
295
Alexandre Rames9931f312015-06-19 14:47:01 +0100296 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
297
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000298 private:
299 // The class this slow path will load.
300 HLoadClass* const cls_;
301
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000302 // The dex PC of `at_`.
303 const uint32_t dex_pc_;
304
305 // Whether to initialize the class.
306 const bool do_clinit_;
307
308 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
309};
310
Andreas Gampe85b62f22015-09-09 13:15:38 -0700311class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000314 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000316 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 DCHECK(instruction_->IsCheckCast()
319 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
322 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000323
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000324 if (kPoisonHeapReferences &&
325 instruction_->IsCheckCast() &&
326 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
327 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
328 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
329 }
330
Vladimir Marko87584542017-12-12 17:47:52 +0000331 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 SaveLiveRegisters(codegen, locations);
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 // We're moving two locations to locations that could overlap, so we need a parallel
336 // move resolver.
337 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800338 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800339 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100340 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800341 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800342 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100343 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100345 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100346 instruction_,
347 instruction_->GetDexPc(),
348 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800349 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000350 } else {
351 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800352 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
353 instruction_,
354 instruction_->GetDexPc(),
355 this);
356 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000357 }
358
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 if (!is_fatal_) {
360 if (instruction_->IsInstanceOf()) {
361 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
362 }
363 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 __ jmp(GetExitLabel());
366 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000367 }
368
Alexandre Rames9931f312015-06-19 14:47:01 +0100369 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100371
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000374
375 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
376};
377
Andreas Gampe85b62f22015-09-09 13:15:38 -0700378class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 public:
Aart Bik42249c32016-01-07 15:33:50 -0800380 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000381 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382
383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100384 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100386 LocationSummary* locations = instruction_->GetLocations();
387 SaveLiveRegisters(codegen, locations);
388 InvokeRuntimeCallingConvention calling_convention;
389 x86_codegen->Load32BitValue(
390 calling_convention.GetRegisterAt(0),
391 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100392 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100393 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
397
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
400};
401
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402class ArraySetSlowPathX86 : public SlowPathCode {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405
406 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
407 LocationSummary* locations = instruction_->GetLocations();
408 __ Bind(GetEntryLabel());
409 SaveLiveRegisters(codegen, locations);
410
411 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100412 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413 parallel_move.AddMove(
414 locations->InAt(0),
415 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100416 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417 nullptr);
418 parallel_move.AddMove(
419 locations->InAt(1),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100421 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(2),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100426 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 nullptr);
428 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
429
430 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100431 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 RestoreLiveRegisters(codegen, locations);
434 __ jmp(GetExitLabel());
435 }
436
437 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
438
439 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100440 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
441};
442
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100443// Slow path marking an object reference `ref` during a read
444// barrier. The field `obj.field` in the object `obj` holding this
445// reference does not get updated by this slow path after marking (see
446// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
447//
448// This means that after the execution of this slow path, `ref` will
449// always be up-to-date, but `obj.field` may not; i.e., after the
450// flip, `ref` will be a to-space reference, but `obj.field` will
451// probably still be a from-space reference (unless it gets updated by
452// another thread, or if another thread installed another object
453// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
455 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100456 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
457 Location ref,
458 bool unpoison_ref_before_marking)
459 : SlowPathCode(instruction),
460 ref_(ref),
461 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(kEmitCompilerReadBarrier);
463 }
464
465 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100469 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000470 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100471 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000472 DCHECK(instruction_->IsInstanceFieldGet() ||
473 instruction_->IsStaticFieldGet() ||
474 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100475 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000476 instruction_->IsLoadClass() ||
477 instruction_->IsLoadString() ||
478 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100479 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100480 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
481 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 << "Unexpected instruction in read barrier marking slow path: "
483 << instruction_->DebugName();
484
485 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000487 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100488 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000489 }
Roland Levillain4359e612016-07-20 11:32:19 +0100490 // No need to save live registers; it's taken care of by the
491 // entrypoint. Also, there is no need to update the stack mask,
492 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100494 DCHECK_NE(ref_reg, ESP);
495 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100496 // "Compact" slow path, saving two moves.
497 //
498 // Instead of using the standard runtime calling convention (input
499 // and output in EAX):
500 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100501 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100502 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100504 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100506 // of a dedicated entrypoint:
507 //
508 // rX <- ReadBarrierMarkRegX(rX)
509 //
Roland Levillain97c46462017-05-11 14:04:03 +0100510 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100511 // This runtime call does not require a stack map.
512 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000513 __ jmp(GetExitLabel());
514 }
515
516 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // The location (register) of the marked object reference.
518 const Location ref_;
519 // Should the reference in `ref_` be unpoisoned prior to marking it?
520 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000521
522 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
523};
524
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100525// Slow path marking an object reference `ref` during a read barrier,
526// and if needed, atomically updating the field `obj.field` in the
527// object `obj` holding this reference after marking (contrary to
528// ReadBarrierMarkSlowPathX86 above, which never tries to update
529// `obj.field`).
530//
531// This means that after the execution of this slow path, both `ref`
532// and `obj.field` will be up-to-date; i.e., after the flip, both will
533// hold the same to-space reference (unless another thread installed
534// another object reference (different from `ref`) in `obj.field`).
535class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
536 public:
537 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
538 Location ref,
539 Register obj,
540 const Address& field_addr,
541 bool unpoison_ref_before_marking,
542 Register temp)
543 : SlowPathCode(instruction),
544 ref_(ref),
545 obj_(obj),
546 field_addr_(field_addr),
547 unpoison_ref_before_marking_(unpoison_ref_before_marking),
548 temp_(temp) {
549 DCHECK(kEmitCompilerReadBarrier);
550 }
551
552 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
553
554 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
555 LocationSummary* locations = instruction_->GetLocations();
556 Register ref_reg = ref_.AsRegister<Register>();
557 DCHECK(locations->CanCall());
558 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
559 // This slow path is only used by the UnsafeCASObject intrinsic.
560 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
561 << "Unexpected instruction in read barrier marking and field updating slow path: "
562 << instruction_->DebugName();
563 DCHECK(instruction_->GetLocations()->Intrinsified());
564 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
565
566 __ Bind(GetEntryLabel());
567 if (unpoison_ref_before_marking_) {
568 // Object* ref = ref_addr->AsMirrorPtr()
569 __ MaybeUnpoisonHeapReference(ref_reg);
570 }
571
572 // Save the old (unpoisoned) reference.
573 __ movl(temp_, ref_reg);
574
575 // No need to save live registers; it's taken care of by the
576 // entrypoint. Also, there is no need to update the stack mask,
577 // as this runtime call will not trigger a garbage collection.
578 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
579 DCHECK_NE(ref_reg, ESP);
580 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
581 // "Compact" slow path, saving two moves.
582 //
583 // Instead of using the standard runtime calling convention (input
584 // and output in EAX):
585 //
586 // EAX <- ref
587 // EAX <- ReadBarrierMark(EAX)
588 // ref <- EAX
589 //
590 // we just use rX (the register containing `ref`) as input and output
591 // of a dedicated entrypoint:
592 //
593 // rX <- ReadBarrierMarkRegX(rX)
594 //
Roland Levillain97c46462017-05-11 14:04:03 +0100595 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100596 // This runtime call does not require a stack map.
597 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
598
599 // If the new reference is different from the old reference,
600 // update the field in the holder (`*field_addr`).
601 //
602 // Note that this field could also hold a different object, if
603 // another thread had concurrently changed it. In that case, the
604 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
605 // operation below would abort the CAS, leaving the field as-is.
606 NearLabel done;
607 __ cmpl(temp_, ref_reg);
608 __ j(kEqual, &done);
609
610 // Update the the holder's field atomically. This may fail if
611 // mutator updates before us, but it's OK. This is achieved
612 // using a strong compare-and-set (CAS) operation with relaxed
613 // memory synchronization ordering, where the expected value is
614 // the old reference and the desired value is the new reference.
615 // This operation is implemented with a 32-bit LOCK CMPXLCHG
616 // instruction, which requires the expected value (the old
617 // reference) to be in EAX. Save EAX beforehand, and move the
618 // expected value (stored in `temp_`) into EAX.
619 __ pushl(EAX);
620 __ movl(EAX, temp_);
621
622 // Convenience aliases.
623 Register base = obj_;
624 Register expected = EAX;
625 Register value = ref_reg;
626
627 bool base_equals_value = (base == value);
628 if (kPoisonHeapReferences) {
629 if (base_equals_value) {
630 // If `base` and `value` are the same register location, move
631 // `value` to a temporary register. This way, poisoning
632 // `value` won't invalidate `base`.
633 value = temp_;
634 __ movl(value, base);
635 }
636
637 // Check that the register allocator did not assign the location
638 // of `expected` (EAX) to `value` nor to `base`, so that heap
639 // poisoning (when enabled) works as intended below.
640 // - If `value` were equal to `expected`, both references would
641 // be poisoned twice, meaning they would not be poisoned at
642 // all, as heap poisoning uses address negation.
643 // - If `base` were equal to `expected`, poisoning `expected`
644 // would invalidate `base`.
645 DCHECK_NE(value, expected);
646 DCHECK_NE(base, expected);
647
648 __ PoisonHeapReference(expected);
649 __ PoisonHeapReference(value);
650 }
651
652 __ LockCmpxchgl(field_addr_, value);
653
654 // If heap poisoning is enabled, we need to unpoison the values
655 // that were poisoned earlier.
656 if (kPoisonHeapReferences) {
657 if (base_equals_value) {
658 // `value` has been moved to a temporary register, no need
659 // to unpoison it.
660 } else {
661 __ UnpoisonHeapReference(value);
662 }
663 // No need to unpoison `expected` (EAX), as it is be overwritten below.
664 }
665
666 // Restore EAX.
667 __ popl(EAX);
668
669 __ Bind(&done);
670 __ jmp(GetExitLabel());
671 }
672
673 private:
674 // The location (register) of the marked object reference.
675 const Location ref_;
676 // The register containing the object holding the marked object reference field.
677 const Register obj_;
678 // The address of the marked reference field. The base of this address must be `obj_`.
679 const Address field_addr_;
680
681 // Should the reference in `ref_` be unpoisoned prior to marking it?
682 const bool unpoison_ref_before_marking_;
683
684 const Register temp_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
687};
688
Roland Levillain0d5a2812015-11-13 10:07:31 +0000689// Slow path generating a read barrier for a heap reference.
690class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
691 public:
692 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
693 Location out,
694 Location ref,
695 Location obj,
696 uint32_t offset,
697 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000698 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000699 out_(out),
700 ref_(ref),
701 obj_(obj),
702 offset_(offset),
703 index_(index) {
704 DCHECK(kEmitCompilerReadBarrier);
705 // If `obj` is equal to `out` or `ref`, it means the initial object
706 // has been overwritten by (or after) the heap object reference load
707 // to be instrumented, e.g.:
708 //
709 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000710 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711 //
712 // In that case, we have lost the information about the original
713 // object, and the emitted read barrier cannot work properly.
714 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
715 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
720 LocationSummary* locations = instruction_->GetLocations();
721 Register reg_out = out_.AsRegister<Register>();
722 DCHECK(locations->CanCall());
723 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100724 DCHECK(instruction_->IsInstanceFieldGet() ||
725 instruction_->IsStaticFieldGet() ||
726 instruction_->IsArrayGet() ||
727 instruction_->IsInstanceOf() ||
728 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700729 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000730 << "Unexpected instruction in read barrier for heap reference slow path: "
731 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000732
733 __ Bind(GetEntryLabel());
734 SaveLiveRegisters(codegen, locations);
735
736 // We may have to change the index's value, but as `index_` is a
737 // constant member (like other "inputs" of this slow path),
738 // introduce a copy of it, `index`.
739 Location index = index_;
740 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100741 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000742 if (instruction_->IsArrayGet()) {
743 // Compute the actual memory offset and store it in `index`.
744 Register index_reg = index_.AsRegister<Register>();
745 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
746 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
747 // We are about to change the value of `index_reg` (see the
748 // calls to art::x86::X86Assembler::shll and
749 // art::x86::X86Assembler::AddImmediate below), but it has
750 // not been saved by the previous call to
751 // art::SlowPathCode::SaveLiveRegisters, as it is a
752 // callee-save register --
753 // art::SlowPathCode::SaveLiveRegisters does not consider
754 // callee-save registers, as it has been designed with the
755 // assumption that callee-save registers are supposed to be
756 // handled by the called function. So, as a callee-save
757 // register, `index_reg` _would_ eventually be saved onto
758 // the stack, but it would be too late: we would have
759 // changed its value earlier. Therefore, we manually save
760 // it here into another freely available register,
761 // `free_reg`, chosen of course among the caller-save
762 // registers (as a callee-save `free_reg` register would
763 // exhibit the same problem).
764 //
765 // Note we could have requested a temporary register from
766 // the register allocator instead; but we prefer not to, as
767 // this is a slow path, and we know we can find a
768 // caller-save register that is available.
769 Register free_reg = FindAvailableCallerSaveRegister(codegen);
770 __ movl(free_reg, index_reg);
771 index_reg = free_reg;
772 index = Location::RegisterLocation(index_reg);
773 } else {
774 // The initial register stored in `index_` has already been
775 // saved in the call to art::SlowPathCode::SaveLiveRegisters
776 // (as it is not a callee-save register), so we can freely
777 // use it.
778 }
779 // Shifting the index value contained in `index_reg` by the scale
780 // factor (2) cannot overflow in practice, as the runtime is
781 // unable to allocate object arrays with a size larger than
782 // 2^26 - 1 (that is, 2^28 - 4 bytes).
783 __ shll(index_reg, Immediate(TIMES_4));
784 static_assert(
785 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
786 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
787 __ AddImmediate(index_reg, Immediate(offset_));
788 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100789 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
790 // intrinsics, `index_` is not shifted by a scale factor of 2
791 // (as in the case of ArrayGet), as it is actually an offset
792 // to an object field within an object.
793 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000794 DCHECK(instruction_->GetLocations()->Intrinsified());
795 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
796 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
797 << instruction_->AsInvoke()->GetIntrinsic();
798 DCHECK_EQ(offset_, 0U);
799 DCHECK(index_.IsRegisterPair());
800 // UnsafeGet's offset location is a register pair, the low
801 // part contains the correct offset.
802 index = index_.ToLow();
803 }
804 }
805
806 // We're moving two or three locations to locations that could
807 // overlap, so we need a parallel move resolver.
808 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100809 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000810 parallel_move.AddMove(ref_,
811 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100812 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000813 nullptr);
814 parallel_move.AddMove(obj_,
815 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100816 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000817 nullptr);
818 if (index.IsValid()) {
819 parallel_move.AddMove(index,
820 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100821 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 nullptr);
823 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
824 } else {
825 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
826 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
827 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100828 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 CheckEntrypointTypes<
830 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
831 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
832
833 RestoreLiveRegisters(codegen, locations);
834 __ jmp(GetExitLabel());
835 }
836
837 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
838
839 private:
840 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
841 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
842 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
843 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
844 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
845 return static_cast<Register>(i);
846 }
847 }
848 // We shall never fail to find a free caller-save register, as
849 // there are more than two core caller-save registers on x86
850 // (meaning it is possible to find one which is different from
851 // `ref` and `obj`).
852 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
853 LOG(FATAL) << "Could not find a free caller-save register";
854 UNREACHABLE();
855 }
856
Roland Levillain0d5a2812015-11-13 10:07:31 +0000857 const Location out_;
858 const Location ref_;
859 const Location obj_;
860 const uint32_t offset_;
861 // An additional location containing an index to an array.
862 // Only used for HArrayGet and the UnsafeGetObject &
863 // UnsafeGetObjectVolatile intrinsics.
864 const Location index_;
865
866 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
867};
868
869// Slow path generating a read barrier for a GC root.
870class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
871 public:
872 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000873 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000874 DCHECK(kEmitCompilerReadBarrier);
875 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000876
877 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
878 LocationSummary* locations = instruction_->GetLocations();
879 Register reg_out = out_.AsRegister<Register>();
880 DCHECK(locations->CanCall());
881 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000882 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
883 << "Unexpected instruction in read barrier for GC root slow path: "
884 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000885
886 __ Bind(GetEntryLabel());
887 SaveLiveRegisters(codegen, locations);
888
889 InvokeRuntimeCallingConvention calling_convention;
890 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
891 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100892 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000893 instruction_,
894 instruction_->GetDexPc(),
895 this);
896 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
897 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
898
899 RestoreLiveRegisters(codegen, locations);
900 __ jmp(GetExitLabel());
901 }
902
903 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
904
905 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000906 const Location out_;
907 const Location root_;
908
909 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
910};
911
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100912#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100913// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
914#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100915
Aart Bike9f37602015-10-09 11:15:55 -0700916inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700917 switch (cond) {
918 case kCondEQ: return kEqual;
919 case kCondNE: return kNotEqual;
920 case kCondLT: return kLess;
921 case kCondLE: return kLessEqual;
922 case kCondGT: return kGreater;
923 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700924 case kCondB: return kBelow;
925 case kCondBE: return kBelowEqual;
926 case kCondA: return kAbove;
927 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700928 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100929 LOG(FATAL) << "Unreachable";
930 UNREACHABLE();
931}
932
Aart Bike9f37602015-10-09 11:15:55 -0700933// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100934inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
935 switch (cond) {
936 case kCondEQ: return kEqual;
937 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700938 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100939 case kCondLT: return kBelow;
940 case kCondLE: return kBelowEqual;
941 case kCondGT: return kAbove;
942 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700943 // Unsigned remain unchanged.
944 case kCondB: return kBelow;
945 case kCondBE: return kBelowEqual;
946 case kCondA: return kAbove;
947 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 }
949 LOG(FATAL) << "Unreachable";
950 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700951}
952
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100953void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100954 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100955}
956
957void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959}
960
Vladimir Markoa0431112018-06-25 09:32:54 +0100961const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
962 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
967 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100968}
969
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100970size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
971 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
972 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100973}
974
Mark Mendell7c8d0092015-01-26 11:21:33 -0500975size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700976 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700977 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700978 } else {
979 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
980 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500981 return GetFloatingPointSpillSlotSize();
982}
983
984size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700985 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700986 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 } else {
988 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
989 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500990 return GetFloatingPointSpillSlotSize();
991}
992
Calin Juravle175dc732015-08-25 15:42:32 +0100993void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
994 HInstruction* instruction,
995 uint32_t dex_pc,
996 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100997 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100998 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
999 if (EntrypointRequiresStackMap(entrypoint)) {
1000 RecordPcInfo(instruction, dex_pc, slow_path);
1001 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001002}
1003
Roland Levillaindec8f632016-07-22 17:10:06 +01001004void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1005 HInstruction* instruction,
1006 SlowPathCode* slow_path) {
1007 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001008 GenerateInvokeRuntime(entry_point_offset);
1009}
1010
1011void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001012 __ fs()->call(Address::Absolute(entry_point_offset));
1013}
1014
Mark Mendellfb8d2792015-03-31 22:16:59 -04001015CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001016 const CompilerOptions& compiler_options,
1017 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001018 : CodeGenerator(graph,
1019 kNumberOfCpuRegisters,
1020 kNumberOfXmmRegisters,
1021 kNumberOfRegisterPairs,
1022 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1023 arraysize(kCoreCalleeSaves))
1024 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001025 0,
1026 compiler_options,
1027 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001028 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001030 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001031 move_resolver_(graph->GetAllocator(), this),
1032 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001033 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1034 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1035 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1036 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001037 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001038 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1039 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1040 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001041 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001043 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001045 // Use a fake return address register to mimic Quick.
1046 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001047}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048
David Brazdil58282f42016-01-14 12:45:10 +00001049void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001050 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001051 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052}
1053
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001054InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001055 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001056 assembler_(codegen->GetAssembler()),
1057 codegen_(codegen) {}
1058
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001059static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001060 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061}
1062
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001063void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001064 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001065 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001066 bool skip_overflow_check =
1067 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001068 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001069
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001070 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1071 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1072 Immediate(1));
1073 }
1074
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001075 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001076 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1077 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001078 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001079 }
1080
Mark Mendell5f874182015-03-04 15:42:45 -05001081 if (HasEmptyFrame()) {
1082 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001083 }
Mark Mendell5f874182015-03-04 15:42:45 -05001084
1085 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1086 Register reg = kCoreCalleeSaves[i];
1087 if (allocated_registers_.ContainsCoreRegister(reg)) {
1088 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001089 __ cfi().AdjustCFAOffset(kX86WordSize);
1090 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001091 }
1092 }
1093
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001094 int adjust = GetFrameSize() - FrameEntrySpillSize();
1095 __ subl(ESP, Immediate(adjust));
1096 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001097 // Save the current method if we need it. Note that we do not
1098 // do this in HCurrentMethod, as the instruction might have been removed
1099 // in the SSA graph.
1100 if (RequiresCurrentMethod()) {
1101 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1102 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001103
1104 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1105 // Initialize should_deoptimize flag to 0.
1106 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1107 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001108}
1109
1110void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001111 __ cfi().RememberState();
1112 if (!HasEmptyFrame()) {
1113 int adjust = GetFrameSize() - FrameEntrySpillSize();
1114 __ addl(ESP, Immediate(adjust));
1115 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001116
David Srbeckyc34dc932015-04-12 09:27:43 +01001117 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1118 Register reg = kCoreCalleeSaves[i];
1119 if (allocated_registers_.ContainsCoreRegister(reg)) {
1120 __ popl(reg);
1121 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1122 __ cfi().Restore(DWARFReg(reg));
1123 }
Mark Mendell5f874182015-03-04 15:42:45 -05001124 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001125 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001126 __ ret();
1127 __ cfi().RestoreState();
1128 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001129}
1130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001131void CodeGeneratorX86::Bind(HBasicBlock* block) {
1132 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001133}
1134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001135Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001136 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001137 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001139 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001140 case DataType::Type::kInt8:
1141 case DataType::Type::kUint16:
1142 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001143 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001144 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001145 return Location::RegisterLocation(EAX);
1146
Aart Bik66c158e2018-01-31 12:55:04 -08001147 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001149 return Location::RegisterPairLocation(EAX, EDX);
1150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001152 return Location::NoLocation();
1153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001154 case DataType::Type::kFloat64:
1155 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001156 return Location::FpuRegisterLocation(XMM0);
1157 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001158
1159 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001160}
1161
1162Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1163 return Location::RegisterLocation(kMethodRegisterArgument);
1164}
1165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001166Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001167 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001168 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001169 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001170 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001171 case DataType::Type::kInt8:
1172 case DataType::Type::kUint16:
1173 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001174 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001180 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001181 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001182 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001184 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 uint32_t index = gp_index_;
1186 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001187 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001188 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001189 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1190 calling_convention.GetRegisterPairAt(index));
1191 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001192 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001193 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1194 }
1195 }
1196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001198 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001199 stack_index_++;
1200 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1201 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1202 } else {
1203 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1204 }
1205 }
1206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001207 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001208 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001209 stack_index_ += 2;
1210 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1211 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1212 } else {
1213 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001214 }
1215 }
1216
Aart Bik66c158e2018-01-31 12:55:04 -08001217 case DataType::Type::kUint32:
1218 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001219 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001220 LOG(FATAL) << "Unexpected parameter type " << type;
1221 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001222 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001223 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001224}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001225
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226void CodeGeneratorX86::Move32(Location destination, Location source) {
1227 if (source.Equals(destination)) {
1228 return;
1229 }
1230 if (destination.IsRegister()) {
1231 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 } else {
1236 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 } else if (destination.IsFpuRegister()) {
1240 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 } else {
1245 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001246 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001247 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001249 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001253 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001254 } else if (source.IsConstant()) {
1255 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001256 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001257 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001258 } else {
1259 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001260 __ pushl(Address(ESP, source.GetStackIndex()));
1261 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 }
1263 }
1264}
1265
1266void CodeGeneratorX86::Move64(Location destination, Location source) {
1267 if (source.Equals(destination)) {
1268 return;
1269 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001270 if (destination.IsRegisterPair()) {
1271 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001272 EmitParallelMoves(
1273 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1274 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001275 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001276 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001277 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001278 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1281 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1282 __ psrlq(src_reg, Immediate(32));
1283 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001284 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001285 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001287 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1288 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1290 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001291 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001292 if (source.IsFpuRegister()) {
1293 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1294 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001295 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001296 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001297 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001298 // Create stack space for 2 elements.
1299 __ subl(ESP, Immediate(2 * elem_size));
1300 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1301 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1302 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1303 // And remove the temporary stack space we allocated.
1304 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001305 } else {
1306 LOG(FATAL) << "Unimplemented";
1307 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001309 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001311 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001312 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001313 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001314 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001315 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001317 } else if (source.IsConstant()) {
1318 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001319 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1320 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001321 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001322 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1323 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001324 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001325 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001326 EmitParallelMoves(
1327 Location::StackSlot(source.GetStackIndex()),
1328 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001330 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001331 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001332 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001333 }
1334 }
1335}
1336
Calin Juravle175dc732015-08-25 15:42:32 +01001337void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1338 DCHECK(location.IsRegister());
1339 __ movl(location.AsRegister<Register>(), Immediate(value));
1340}
1341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001343 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001344 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1345 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1346 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001347 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001348 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001349 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001350 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001351}
1352
1353void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1354 if (location.IsRegister()) {
1355 locations->AddTemp(location);
1356 } else if (location.IsRegisterPair()) {
1357 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1358 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1359 } else {
1360 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1361 }
1362}
1363
David Brazdilfc6a86a2015-06-26 10:33:45 +00001364void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001365 if (successor->IsExitBlock()) {
1366 DCHECK(got->GetPrevious()->AlwaysThrows());
1367 return; // no code needed
1368 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001369
1370 HBasicBlock* block = got->GetBlock();
1371 HInstruction* previous = got->GetPrevious();
1372
1373 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001374 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001375 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1376 __ pushl(EAX);
1377 __ movl(EAX, Address(ESP, kX86WordSize));
1378 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1379 __ popl(EAX);
1380 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001381 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1382 return;
1383 }
1384
1385 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1386 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1387 }
1388 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001389 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001390 }
1391}
1392
David Brazdilfc6a86a2015-06-26 10:33:45 +00001393void LocationsBuilderX86::VisitGoto(HGoto* got) {
1394 got->SetLocations(nullptr);
1395}
1396
1397void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1398 HandleGoto(got, got->GetSuccessor());
1399}
1400
1401void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1402 try_boundary->SetLocations(nullptr);
1403}
1404
1405void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1406 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1407 if (!successor->IsExitBlock()) {
1408 HandleGoto(try_boundary, successor);
1409 }
1410}
1411
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001413 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001414}
1415
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001416void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001417}
1418
Mark Mendell152408f2015-12-31 12:28:50 -05001419template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001420void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001421 LabelType* true_label,
1422 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423 if (cond->IsFPConditionTrueIfNaN()) {
1424 __ j(kUnordered, true_label);
1425 } else if (cond->IsFPConditionFalseIfNaN()) {
1426 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001427 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001428 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001429}
1430
Mark Mendell152408f2015-12-31 12:28:50 -05001431template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001432void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001433 LabelType* true_label,
1434 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001435 LocationSummary* locations = cond->GetLocations();
1436 Location left = locations->InAt(0);
1437 Location right = locations->InAt(1);
1438 IfCondition if_cond = cond->GetCondition();
1439
Mark Mendellc4701932015-04-10 13:18:51 -04001440 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001441 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001442 IfCondition true_high_cond = if_cond;
1443 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001444 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001445
1446 // Set the conditions for the test, remembering that == needs to be
1447 // decided using the low words.
1448 switch (if_cond) {
1449 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001450 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001451 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001452 break;
1453 case kCondLT:
1454 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001455 break;
1456 case kCondLE:
1457 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001458 break;
1459 case kCondGT:
1460 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001461 break;
1462 case kCondGE:
1463 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001464 break;
Aart Bike9f37602015-10-09 11:15:55 -07001465 case kCondB:
1466 false_high_cond = kCondA;
1467 break;
1468 case kCondBE:
1469 true_high_cond = kCondB;
1470 break;
1471 case kCondA:
1472 false_high_cond = kCondB;
1473 break;
1474 case kCondAE:
1475 true_high_cond = kCondA;
1476 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001477 }
1478
1479 if (right.IsConstant()) {
1480 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001481 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001482 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001483
Aart Bika19616e2016-02-01 18:57:58 -08001484 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001485 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001486 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001487 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001488 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001489 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001490 __ j(X86Condition(true_high_cond), true_label);
1491 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001492 }
1493 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001494 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001495 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001496 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001497 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001498
1499 __ cmpl(left_high, right_high);
1500 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001501 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001502 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001503 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001504 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001505 __ j(X86Condition(true_high_cond), true_label);
1506 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001507 }
1508 // Must be equal high, so compare the lows.
1509 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001510 } else {
1511 DCHECK(right.IsDoubleStackSlot());
1512 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1513 if (if_cond == kCondNE) {
1514 __ j(X86Condition(true_high_cond), true_label);
1515 } else if (if_cond == kCondEQ) {
1516 __ j(X86Condition(false_high_cond), false_label);
1517 } else {
1518 __ j(X86Condition(true_high_cond), true_label);
1519 __ j(X86Condition(false_high_cond), false_label);
1520 }
1521 // Must be equal high, so compare the lows.
1522 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001523 }
1524 // The last comparison might be unsigned.
1525 __ j(final_condition, true_label);
1526}
1527
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001528void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1529 Location rhs,
1530 HInstruction* insn,
1531 bool is_double) {
1532 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1533 if (is_double) {
1534 if (rhs.IsFpuRegister()) {
1535 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1536 } else if (const_area != nullptr) {
1537 DCHECK(const_area->IsEmittedAtUseSite());
1538 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1539 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001540 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1541 const_area->GetBaseMethodAddress(),
1542 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001543 } else {
1544 DCHECK(rhs.IsDoubleStackSlot());
1545 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1546 }
1547 } else {
1548 if (rhs.IsFpuRegister()) {
1549 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1550 } else if (const_area != nullptr) {
1551 DCHECK(const_area->IsEmittedAtUseSite());
1552 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1553 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001554 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1555 const_area->GetBaseMethodAddress(),
1556 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001557 } else {
1558 DCHECK(rhs.IsStackSlot());
1559 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1560 }
1561 }
1562}
1563
Mark Mendell152408f2015-12-31 12:28:50 -05001564template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001565void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001566 LabelType* true_target_in,
1567 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001568 // Generated branching requires both targets to be explicit. If either of the
1569 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001570 LabelType fallthrough_target;
1571 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1572 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001573
Mark Mendellc4701932015-04-10 13:18:51 -04001574 LocationSummary* locations = condition->GetLocations();
1575 Location left = locations->InAt(0);
1576 Location right = locations->InAt(1);
1577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001578 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001579 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001580 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001581 GenerateLongComparesAndJumps(condition, true_target, false_target);
1582 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001583 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001584 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001585 GenerateFPJumps(condition, true_target, false_target);
1586 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001587 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001588 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001589 GenerateFPJumps(condition, true_target, false_target);
1590 break;
1591 default:
1592 LOG(FATAL) << "Unexpected compare type " << type;
1593 }
1594
David Brazdil0debae72015-11-12 18:37:00 +00001595 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001596 __ jmp(false_target);
1597 }
David Brazdil0debae72015-11-12 18:37:00 +00001598
1599 if (fallthrough_target.IsLinked()) {
1600 __ Bind(&fallthrough_target);
1601 }
Mark Mendellc4701932015-04-10 13:18:51 -04001602}
1603
David Brazdil0debae72015-11-12 18:37:00 +00001604static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1605 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1606 // are set only strictly before `branch`. We can't use the eflags on long/FP
1607 // conditions if they are materialized due to the complex branching.
1608 return cond->IsCondition() &&
1609 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1611 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001612}
1613
Mark Mendell152408f2015-12-31 12:28:50 -05001614template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001615void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001616 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001617 LabelType* true_target,
1618 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001619 HInstruction* cond = instruction->InputAt(condition_input_index);
1620
1621 if (true_target == nullptr && false_target == nullptr) {
1622 // Nothing to do. The code always falls through.
1623 return;
1624 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001625 // Constant condition, statically compared against "true" (integer value 1).
1626 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001627 if (true_target != nullptr) {
1628 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001629 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001630 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001631 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001632 if (false_target != nullptr) {
1633 __ jmp(false_target);
1634 }
1635 }
1636 return;
1637 }
1638
1639 // The following code generates these patterns:
1640 // (1) true_target == nullptr && false_target != nullptr
1641 // - opposite condition true => branch to false_target
1642 // (2) true_target != nullptr && false_target == nullptr
1643 // - condition true => branch to true_target
1644 // (3) true_target != nullptr && false_target != nullptr
1645 // - condition true => branch to true_target
1646 // - branch to false_target
1647 if (IsBooleanValueOrMaterializedCondition(cond)) {
1648 if (AreEflagsSetFrom(cond, instruction)) {
1649 if (true_target == nullptr) {
1650 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1651 } else {
1652 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1653 }
1654 } else {
1655 // Materialized condition, compare against 0.
1656 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1657 if (lhs.IsRegister()) {
1658 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1659 } else {
1660 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1661 }
1662 if (true_target == nullptr) {
1663 __ j(kEqual, false_target);
1664 } else {
1665 __ j(kNotEqual, true_target);
1666 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001667 }
1668 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001669 // Condition has not been materialized, use its inputs as the comparison and
1670 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001671 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001672
1673 // If this is a long or FP comparison that has been folded into
1674 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001675 DataType::Type type = condition->InputAt(0)->GetType();
1676 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001677 GenerateCompareTestAndBranch(condition, true_target, false_target);
1678 return;
1679 }
1680
1681 Location lhs = condition->GetLocations()->InAt(0);
1682 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001683 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001684 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001685 if (true_target == nullptr) {
1686 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1687 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001688 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001689 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001690 }
David Brazdil0debae72015-11-12 18:37:00 +00001691
1692 // If neither branch falls through (case 3), the conditional branch to `true_target`
1693 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1694 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001695 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001696 }
1697}
1698
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001699void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001701 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001702 locations->SetInAt(0, Location::Any());
1703 }
1704}
1705
1706void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001707 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1708 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1709 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1710 nullptr : codegen_->GetLabelOf(true_successor);
1711 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1712 nullptr : codegen_->GetLabelOf(false_successor);
1713 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001714}
1715
1716void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001717 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001718 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001719 InvokeRuntimeCallingConvention calling_convention;
1720 RegisterSet caller_saves = RegisterSet::Empty();
1721 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1722 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001723 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001724 locations->SetInAt(0, Location::Any());
1725 }
1726}
1727
1728void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001729 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001730 GenerateTestAndBranch<Label>(deoptimize,
1731 /* condition_input_index */ 0,
1732 slow_path->GetEntryLabel(),
1733 /* false_target */ nullptr);
1734}
1735
Mingyao Yang063fc772016-08-02 11:02:54 -07001736void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001737 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001738 LocationSummary(flag, LocationSummary::kNoCall);
1739 locations->SetOut(Location::RequiresRegister());
1740}
1741
1742void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1743 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1744 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1745}
1746
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001747static bool SelectCanUseCMOV(HSelect* select) {
1748 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001750 return false;
1751 }
1752
1753 // A FP condition doesn't generate the single CC that we need.
1754 // In 32 bit mode, a long condition doesn't generate a single CC either.
1755 HInstruction* condition = select->GetCondition();
1756 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 DataType::Type compare_type = condition->InputAt(0)->GetType();
1758 if (compare_type == DataType::Type::kInt64 ||
1759 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001760 return false;
1761 }
1762 }
1763
1764 // We can generate a CMOV for this Select.
1765 return true;
1766}
1767
David Brazdil74eb1b22015-12-14 11:44:01 +00001768void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001769 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001770 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001771 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001773 } else {
1774 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001775 if (SelectCanUseCMOV(select)) {
1776 if (select->InputAt(1)->IsConstant()) {
1777 // Cmov can't handle a constant value.
1778 locations->SetInAt(1, Location::RequiresRegister());
1779 } else {
1780 locations->SetInAt(1, Location::Any());
1781 }
1782 } else {
1783 locations->SetInAt(1, Location::Any());
1784 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001785 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001786 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1787 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001788 }
1789 locations->SetOut(Location::SameAsFirstInput());
1790}
1791
1792void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1793 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001794 DCHECK(locations->InAt(0).Equals(locations->Out()));
1795 if (SelectCanUseCMOV(select)) {
1796 // If both the condition and the source types are integer, we can generate
1797 // a CMOV to implement Select.
1798
1799 HInstruction* select_condition = select->GetCondition();
1800 Condition cond = kNotEqual;
1801
1802 // Figure out how to test the 'condition'.
1803 if (select_condition->IsCondition()) {
1804 HCondition* condition = select_condition->AsCondition();
1805 if (!condition->IsEmittedAtUseSite()) {
1806 // This was a previously materialized condition.
1807 // Can we use the existing condition code?
1808 if (AreEflagsSetFrom(condition, select)) {
1809 // Materialization was the previous instruction. Condition codes are right.
1810 cond = X86Condition(condition->GetCondition());
1811 } else {
1812 // No, we have to recreate the condition code.
1813 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1814 __ testl(cond_reg, cond_reg);
1815 }
1816 } else {
1817 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1819 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001820 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001821 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001822 cond = X86Condition(condition->GetCondition());
1823 }
1824 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001825 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001826 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1827 __ testl(cond_reg, cond_reg);
1828 }
1829
1830 // If the condition is true, overwrite the output, which already contains false.
1831 Location false_loc = locations->InAt(0);
1832 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001834 // 64 bit conditional move.
1835 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1836 Register false_low = false_loc.AsRegisterPairLow<Register>();
1837 if (true_loc.IsRegisterPair()) {
1838 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1839 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1840 } else {
1841 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1842 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1843 }
1844 } else {
1845 // 32 bit conditional move.
1846 Register false_reg = false_loc.AsRegister<Register>();
1847 if (true_loc.IsRegister()) {
1848 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1849 } else {
1850 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1851 }
1852 }
1853 } else {
1854 NearLabel false_target;
1855 GenerateTestAndBranch<NearLabel>(
1856 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1857 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1858 __ Bind(&false_target);
1859 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001860}
1861
David Srbecky0cf44932015-12-09 14:09:59 +00001862void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001864}
1865
David Srbeckyd28f4a02016-03-14 17:14:24 +00001866void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1867 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001868}
1869
1870void CodeGeneratorX86::GenerateNop() {
1871 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001872}
1873
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001874void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001875 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001876 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001877 // Handle the long/FP comparisons made in instruction simplification.
1878 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001879 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001880 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001881 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001882 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001883 locations->SetOut(Location::RequiresRegister());
1884 }
1885 break;
1886 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001887 case DataType::Type::kFloat32:
1888 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001889 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001890 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1891 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1892 } else if (cond->InputAt(1)->IsConstant()) {
1893 locations->SetInAt(1, Location::RequiresFpuRegister());
1894 } else {
1895 locations->SetInAt(1, Location::Any());
1896 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001897 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001898 locations->SetOut(Location::RequiresRegister());
1899 }
1900 break;
1901 }
1902 default:
1903 locations->SetInAt(0, Location::RequiresRegister());
1904 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001905 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001906 // We need a byte register.
1907 locations->SetOut(Location::RegisterLocation(ECX));
1908 }
1909 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001910 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001911}
1912
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001913void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001914 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001915 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001916 }
Mark Mendellc4701932015-04-10 13:18:51 -04001917
1918 LocationSummary* locations = cond->GetLocations();
1919 Location lhs = locations->InAt(0);
1920 Location rhs = locations->InAt(1);
1921 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001922 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001923
1924 switch (cond->InputAt(0)->GetType()) {
1925 default: {
1926 // Integer case.
1927
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001928 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001929 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001930 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001931 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001932 return;
1933 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001935 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1936 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001937 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001938 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001939 GenerateFPJumps(cond, &true_label, &false_label);
1940 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001942 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001943 GenerateFPJumps(cond, &true_label, &false_label);
1944 break;
1945 }
1946
1947 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001948 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001949
Roland Levillain4fa13f62015-07-06 18:11:54 +01001950 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001951 __ Bind(&false_label);
1952 __ xorl(reg, reg);
1953 __ jmp(&done_label);
1954
Roland Levillain4fa13f62015-07-06 18:11:54 +01001955 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001956 __ Bind(&true_label);
1957 __ movl(reg, Immediate(1));
1958 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001987}
1988
1989void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001991}
1992
1993void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001995}
1996
1997void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001999}
2000
2001void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002003}
2004
2005void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002007}
2008
Aart Bike9f37602015-10-09 11:15:55 -07002009void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
2021void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002022 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002023}
2024
2025void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002026 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002027}
2028
2029void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002030 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002031}
2032
2033void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002035}
2036
2037void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002039}
2040
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002041void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002042 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002044 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002045}
2046
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002047void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002048 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002049}
2050
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002051void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2052 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002053 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002054 locations->SetOut(Location::ConstantLocation(constant));
2055}
2056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002057void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002058 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059}
2060
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002061void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002062 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002063 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002064 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065}
2066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002067void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002068 // Will be generated at use site.
2069}
2070
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002073 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002074 locations->SetOut(Location::ConstantLocation(constant));
2075}
2076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002078 // Will be generated at use site.
2079}
2080
2081void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2082 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002083 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002084 locations->SetOut(Location::ConstantLocation(constant));
2085}
2086
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002087void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002088 // Will be generated at use site.
2089}
2090
Igor Murashkind01745e2017-04-05 16:40:31 -07002091void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2092 constructor_fence->SetLocations(nullptr);
2093}
2094
2095void InstructionCodeGeneratorX86::VisitConstructorFence(
2096 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2097 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2098}
2099
Calin Juravle27df7582015-04-17 19:12:31 +01002100void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2101 memory_barrier->SetLocations(nullptr);
2102}
2103
2104void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002105 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002106}
2107
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002108void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002109 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110}
2111
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002112void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002113 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002114}
2115
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002116void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002117 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002118 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002120 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002122 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 case DataType::Type::kInt8:
2124 case DataType::Type::kUint16:
2125 case DataType::Type::kInt16:
2126 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002128 break;
2129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002130 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133 break;
2134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 case DataType::Type::kFloat32:
2136 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002137 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002139 break;
2140
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002142 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144}
2145
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002146void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 if (kIsDebugBuild) {
2148 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002149 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002151 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 case DataType::Type::kInt8:
2153 case DataType::Type::kUint16:
2154 case DataType::Type::kInt16:
2155 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002156 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002157 break;
2158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002160 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2161 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002162 break;
2163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 case DataType::Type::kFloat32:
2165 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002166 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002167 break;
2168
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002169 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002170 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171 }
2172 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002173 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002174}
2175
Calin Juravle175dc732015-08-25 15:42:32 +01002176void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2177 // The trampoline uses the same calling convention as dex calling conventions,
2178 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2179 // the method_idx.
2180 HandleInvoke(invoke);
2181}
2182
2183void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2184 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2185}
2186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002187void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002188 // Explicit clinit checks triggered by static invokes must have been pruned by
2189 // art::PrepareForRegisterAllocation.
2190 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002191
Mark Mendellfb8d2792015-03-31 22:16:59 -04002192 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002193 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002194 if (invoke->GetLocations()->CanCall() &&
2195 invoke->HasPcRelativeMethodLoadKind() &&
2196 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002197 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002198 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002199 return;
2200 }
2201
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002202 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002203
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002204 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002205 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002206 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002207 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002208}
2209
Mark Mendell09ed1a32015-03-25 08:30:06 -04002210static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2211 if (invoke->GetLocations()->Intrinsified()) {
2212 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2213 intrinsic.Dispatch(invoke);
2214 return true;
2215 }
2216 return false;
2217}
2218
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002219void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002220 // Explicit clinit checks triggered by static invokes must have been pruned by
2221 // art::PrepareForRegisterAllocation.
2222 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002223
Mark Mendell09ed1a32015-03-25 08:30:06 -04002224 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2225 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002226 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002227
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002228 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002229 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002230 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002231}
2232
2233void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002234 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2235 if (intrinsic.TryDispatch(invoke)) {
2236 return;
2237 }
2238
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002239 HandleInvoke(invoke);
2240}
2241
2242void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002243 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002244 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002245}
2246
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002247void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002248 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2249 return;
2250 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002251
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002252 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002253 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002254}
2255
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 // This call to HandleInvoke allocates a temporary (core) register
2258 // which is also used to transfer the hidden argument from FP to
2259 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 HandleInvoke(invoke);
2261 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002262 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002263}
2264
2265void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2266 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002267 LocationSummary* locations = invoke->GetLocations();
2268 Register temp = locations->GetTemp(0).AsRegister<Register>();
2269 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 Location receiver = locations->InAt(0);
2271 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2272
Roland Levillain0d5a2812015-11-13 10:07:31 +00002273 // Set the hidden argument. This is safe to do this here, as XMM7
2274 // won't be modified thereafter, before the `call` instruction.
2275 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002277 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002278
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279 if (receiver.IsStackSlot()) {
2280 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002281 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002282 __ movl(temp, Address(temp, class_offset));
2283 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002284 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002285 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002286 }
Roland Levillain4d027112015-07-01 15:41:14 +01002287 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002288 // Instead of simply (possibly) unpoisoning `temp` here, we should
2289 // emit a read barrier for the previous class reference load.
2290 // However this is not required in practice, as this is an
2291 // intermediate/temporary reference and because the current
2292 // concurrent copying collector keeps the from-space memory
2293 // intact/accessible until the end of the marking phase (the
2294 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002295 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002296 // temp = temp->GetAddressOfIMT()
2297 __ movl(temp,
2298 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002299 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002300 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002301 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002302 __ movl(temp, Address(temp, method_offset));
2303 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002304 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002305 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002306
2307 DCHECK(!codegen_->IsLeafMethod());
2308 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2309}
2310
Orion Hodsonac141392017-01-13 11:53:47 +00002311void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2312 HandleInvoke(invoke);
2313}
2314
2315void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2316 codegen_->GenerateInvokePolymorphicCall(invoke);
2317}
2318
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002319void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2320 HandleInvoke(invoke);
2321}
2322
2323void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2324 codegen_->GenerateInvokeCustomCall(invoke);
2325}
2326
Roland Levillain88cb1752014-10-20 16:36:47 +01002327void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2328 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002329 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002330 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 case DataType::Type::kInt32:
2332 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002333 locations->SetInAt(0, Location::RequiresRegister());
2334 locations->SetOut(Location::SameAsFirstInput());
2335 break;
2336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002338 locations->SetInAt(0, Location::RequiresFpuRegister());
2339 locations->SetOut(Location::SameAsFirstInput());
2340 locations->AddTemp(Location::RequiresRegister());
2341 locations->AddTemp(Location::RequiresFpuRegister());
2342 break;
2343
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002345 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002346 locations->SetOut(Location::SameAsFirstInput());
2347 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2352 }
2353}
2354
2355void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2356 LocationSummary* locations = neg->GetLocations();
2357 Location out = locations->Out();
2358 Location in = locations->InAt(0);
2359 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002361 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002362 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002364 break;
2365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002367 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002368 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002369 __ negl(out.AsRegisterPairLow<Register>());
2370 // Negation is similar to subtraction from zero. The least
2371 // significant byte triggers a borrow when it is different from
2372 // zero; to take it into account, add 1 to the most significant
2373 // byte if the carry flag (CF) is set to 1 after the first NEGL
2374 // operation.
2375 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2376 __ negl(out.AsRegisterPairHigh<Register>());
2377 break;
2378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002380 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 Register constant = locations->GetTemp(0).AsRegister<Register>();
2382 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002383 // Implement float negation with an exclusive or with value
2384 // 0x80000000 (mask for bit 31, representing the sign of a
2385 // single-precision floating-point number).
2386 __ movl(constant, Immediate(INT32_C(0x80000000)));
2387 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002388 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002389 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002390 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002392 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002393 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002394 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002395 // Implement double negation with an exclusive or with value
2396 // 0x8000000000000000 (mask for bit 63, representing the sign of
2397 // a double-precision floating-point number).
2398 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002399 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002400 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002401 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002402
2403 default:
2404 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2405 }
2406}
2407
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002408void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2409 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002410 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002412 locations->SetInAt(0, Location::RequiresFpuRegister());
2413 locations->SetInAt(1, Location::RequiresRegister());
2414 locations->SetOut(Location::SameAsFirstInput());
2415 locations->AddTemp(Location::RequiresFpuRegister());
2416}
2417
2418void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2419 LocationSummary* locations = neg->GetLocations();
2420 Location out = locations->Out();
2421 DCHECK(locations->InAt(0).Equals(out));
2422
2423 Register constant_area = locations->InAt(1).AsRegister<Register>();
2424 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002425 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002426 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2427 neg->GetBaseMethodAddress(),
2428 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002429 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2430 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002431 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2432 neg->GetBaseMethodAddress(),
2433 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002434 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2435 }
2436}
2437
Roland Levillaindff1f282014-11-05 14:15:05 +00002438void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 DataType::Type result_type = conversion->GetResultType();
2440 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002441 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2442 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002443
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002444 // The float-to-long and double-to-long type conversions rely on a
2445 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002446 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002447 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2448 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002449 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002450 : LocationSummary::kNoCall;
2451 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002452 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002453
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002455 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002457 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002458 case DataType::Type::kUint8:
2459 case DataType::Type::kInt8:
2460 case DataType::Type::kUint16:
2461 case DataType::Type::kInt16:
2462 case DataType::Type::kInt32:
2463 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2464 // Make the output overlap to please the register allocator. This greatly simplifies
2465 // the validation of the linear scan implementation
2466 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2467 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002469 HInstruction* input = conversion->InputAt(0);
2470 Location input_location = input->IsConstant()
2471 ? Location::ConstantLocation(input->AsConstant())
2472 : Location::RegisterPairLocation(EAX, EDX);
2473 locations->SetInAt(0, input_location);
2474 // Make the output overlap to please the register allocator. This greatly simplifies
2475 // the validation of the linear scan implementation
2476 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2477 break;
2478 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002479
2480 default:
2481 LOG(FATAL) << "Unexpected type conversion from " << input_type
2482 << " to " << result_type;
2483 }
2484 break;
2485
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002486 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002488 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2489 locations->SetInAt(0, Location::Any());
2490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002491 break;
2492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002494 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002496 locations->SetInAt(0, Location::Any());
2497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2498 break;
2499
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002501 locations->SetInAt(0, Location::RequiresFpuRegister());
2502 locations->SetOut(Location::RequiresRegister());
2503 locations->AddTemp(Location::RequiresFpuRegister());
2504 break;
2505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002507 locations->SetInAt(0, Location::RequiresFpuRegister());
2508 locations->SetOut(Location::RequiresRegister());
2509 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002510 break;
2511
2512 default:
2513 LOG(FATAL) << "Unexpected type conversion from " << input_type
2514 << " to " << result_type;
2515 }
2516 break;
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002519 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002521 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002523 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 case DataType::Type::kInt16:
2525 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 locations->SetInAt(0, Location::RegisterLocation(EAX));
2527 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2528 break;
2529
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002530 case DataType::Type::kFloat32:
2531 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002532 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002533 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2534 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2535
Vladimir Marko949c91f2015-01-27 10:48:44 +00002536 // The runtime helper puts the result in EAX, EDX.
2537 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002538 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002539 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002540
2541 default:
2542 LOG(FATAL) << "Unexpected type conversion from " << input_type
2543 << " to " << result_type;
2544 }
2545 break;
2546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002548 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002550 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002552 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kInt16:
2554 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002555 locations->SetInAt(0, Location::RequiresRegister());
2556 locations->SetOut(Location::RequiresFpuRegister());
2557 break;
2558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002560 locations->SetInAt(0, Location::Any());
2561 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002562 break;
2563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002565 locations->SetInAt(0, Location::RequiresFpuRegister());
2566 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002567 break;
2568
2569 default:
2570 LOG(FATAL) << "Unexpected type conversion from " << input_type
2571 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002572 }
Roland Levillaincff13742014-11-17 14:32:17 +00002573 break;
2574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002576 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002578 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002580 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002581 case DataType::Type::kInt16:
2582 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002583 locations->SetInAt(0, Location::RequiresRegister());
2584 locations->SetOut(Location::RequiresFpuRegister());
2585 break;
2586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002588 locations->SetInAt(0, Location::Any());
2589 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002590 break;
2591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002593 locations->SetInAt(0, Location::RequiresFpuRegister());
2594 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002595 break;
2596
2597 default:
2598 LOG(FATAL) << "Unexpected type conversion from " << input_type
2599 << " to " << result_type;
2600 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 break;
2602
2603 default:
2604 LOG(FATAL) << "Unexpected type conversion from " << input_type
2605 << " to " << result_type;
2606 }
2607}
2608
2609void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2610 LocationSummary* locations = conversion->GetLocations();
2611 Location out = locations->Out();
2612 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 DataType::Type result_type = conversion->GetResultType();
2614 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002615 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2616 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002617 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002618 case DataType::Type::kUint8:
2619 switch (input_type) {
2620 case DataType::Type::kInt8:
2621 case DataType::Type::kUint16:
2622 case DataType::Type::kInt16:
2623 case DataType::Type::kInt32:
2624 if (in.IsRegister()) {
2625 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2626 } else {
2627 DCHECK(in.GetConstant()->IsIntConstant());
2628 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2629 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2630 }
2631 break;
2632 case DataType::Type::kInt64:
2633 if (in.IsRegisterPair()) {
2634 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2635 } else {
2636 DCHECK(in.GetConstant()->IsLongConstant());
2637 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2638 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2639 }
2640 break;
2641
2642 default:
2643 LOG(FATAL) << "Unexpected type conversion from " << input_type
2644 << " to " << result_type;
2645 }
2646 break;
2647
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002649 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002650 case DataType::Type::kUint8:
2651 case DataType::Type::kUint16:
2652 case DataType::Type::kInt16:
2653 case DataType::Type::kInt32:
2654 if (in.IsRegister()) {
2655 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2656 } else {
2657 DCHECK(in.GetConstant()->IsIntConstant());
2658 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2659 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2660 }
2661 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002663 if (in.IsRegisterPair()) {
2664 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2665 } else {
2666 DCHECK(in.GetConstant()->IsLongConstant());
2667 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2668 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2669 }
2670 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 }
2676 break;
2677
2678 case DataType::Type::kUint16:
2679 switch (input_type) {
2680 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kInt16:
2682 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002683 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002684 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2685 } else if (in.IsStackSlot()) {
2686 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002687 } else {
2688 DCHECK(in.GetConstant()->IsIntConstant());
2689 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002690 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2691 }
2692 break;
2693 case DataType::Type::kInt64:
2694 if (in.IsRegisterPair()) {
2695 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2696 } else if (in.IsDoubleStackSlot()) {
2697 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2698 } else {
2699 DCHECK(in.GetConstant()->IsLongConstant());
2700 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2701 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002702 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 }
2709 break;
2710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002712 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002713 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002714 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002715 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002717 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002718 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002719 } else {
2720 DCHECK(in.GetConstant()->IsIntConstant());
2721 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002722 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002723 }
2724 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002725 case DataType::Type::kInt64:
2726 if (in.IsRegisterPair()) {
2727 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2728 } else if (in.IsDoubleStackSlot()) {
2729 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2730 } else {
2731 DCHECK(in.GetConstant()->IsLongConstant());
2732 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2733 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2734 }
2735 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002736
2737 default:
2738 LOG(FATAL) << "Unexpected type conversion from " << input_type
2739 << " to " << result_type;
2740 }
2741 break;
2742
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002744 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002746 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002748 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002750 } else {
2751 DCHECK(in.IsConstant());
2752 DCHECK(in.GetConstant()->IsLongConstant());
2753 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002754 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002755 }
2756 break;
2757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002759 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2760 Register output = out.AsRegister<Register>();
2761 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002762 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002763
2764 __ movl(output, Immediate(kPrimIntMax));
2765 // temp = int-to-float(output)
2766 __ cvtsi2ss(temp, output);
2767 // if input >= temp goto done
2768 __ comiss(input, temp);
2769 __ j(kAboveEqual, &done);
2770 // if input == NaN goto nan
2771 __ j(kUnordered, &nan);
2772 // output = float-to-int-truncate(input)
2773 __ cvttss2si(output, input);
2774 __ jmp(&done);
2775 __ Bind(&nan);
2776 // output = 0
2777 __ xorl(output, output);
2778 __ Bind(&done);
2779 break;
2780 }
2781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002783 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2784 Register output = out.AsRegister<Register>();
2785 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002786 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002787
2788 __ movl(output, Immediate(kPrimIntMax));
2789 // temp = int-to-double(output)
2790 __ cvtsi2sd(temp, output);
2791 // if input >= temp goto done
2792 __ comisd(input, temp);
2793 __ j(kAboveEqual, &done);
2794 // if input == NaN goto nan
2795 __ j(kUnordered, &nan);
2796 // output = double-to-int-truncate(input)
2797 __ cvttsd2si(output, input);
2798 __ jmp(&done);
2799 __ Bind(&nan);
2800 // output = 0
2801 __ xorl(output, output);
2802 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002803 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002804 }
Roland Levillain946e1432014-11-11 17:35:19 +00002805
2806 default:
2807 LOG(FATAL) << "Unexpected type conversion from " << input_type
2808 << " to " << result_type;
2809 }
2810 break;
2811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002813 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002815 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002817 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 case DataType::Type::kInt16:
2819 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002820 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2821 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002822 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002823 __ cdq();
2824 break;
2825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002827 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002828 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002829 break;
2830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002831 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002832 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002833 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002834 break;
2835
2836 default:
2837 LOG(FATAL) << "Unexpected type conversion from " << input_type
2838 << " to " << result_type;
2839 }
2840 break;
2841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002843 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002845 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002847 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 case DataType::Type::kInt16:
2849 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002851 break;
2852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002854 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002855
Roland Levillain232ade02015-04-20 15:14:36 +01002856 // Create stack space for the call to
2857 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2858 // TODO: enhance register allocator to ask for stack temporaries.
2859 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002860 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002861 __ subl(ESP, Immediate(adjustment));
2862 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002863
Roland Levillain232ade02015-04-20 15:14:36 +01002864 // Load the value to the FP stack, using temporaries if needed.
2865 PushOntoFPStack(in, 0, adjustment, false, true);
2866
2867 if (out.IsStackSlot()) {
2868 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2869 } else {
2870 __ fstps(Address(ESP, 0));
2871 Location stack_temp = Location::StackSlot(0);
2872 codegen_->Move32(out, stack_temp);
2873 }
2874
2875 // Remove the temporary stack space we allocated.
2876 if (adjustment != 0) {
2877 __ addl(ESP, Immediate(adjustment));
2878 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002879 break;
2880 }
2881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002883 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002884 break;
2885
2886 default:
2887 LOG(FATAL) << "Unexpected type conversion from " << input_type
2888 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002889 }
Roland Levillaincff13742014-11-17 14:32:17 +00002890 break;
2891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002893 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002895 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002897 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kInt16:
2899 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002900 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002901 break;
2902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002904 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002905
Roland Levillain232ade02015-04-20 15:14:36 +01002906 // Create stack space for the call to
2907 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2908 // TODO: enhance register allocator to ask for stack temporaries.
2909 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002910 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002911 __ subl(ESP, Immediate(adjustment));
2912 }
2913
2914 // Load the value to the FP stack, using temporaries if needed.
2915 PushOntoFPStack(in, 0, adjustment, false, true);
2916
2917 if (out.IsDoubleStackSlot()) {
2918 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2919 } else {
2920 __ fstpl(Address(ESP, 0));
2921 Location stack_temp = Location::DoubleStackSlot(0);
2922 codegen_->Move64(out, stack_temp);
2923 }
2924
2925 // Remove the temporary stack space we allocated.
2926 if (adjustment != 0) {
2927 __ addl(ESP, Immediate(adjustment));
2928 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002929 break;
2930 }
2931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002933 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002934 break;
2935
2936 default:
2937 LOG(FATAL) << "Unexpected type conversion from " << input_type
2938 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002939 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002940 break;
2941
2942 default:
2943 LOG(FATAL) << "Unexpected type conversion from " << input_type
2944 << " to " << result_type;
2945 }
2946}
2947
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002948void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002949 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002950 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002951 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002953 locations->SetInAt(0, Location::RequiresRegister());
2954 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2956 break;
2957 }
2958
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002960 locations->SetInAt(0, Location::RequiresRegister());
2961 locations->SetInAt(1, Location::Any());
2962 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002963 break;
2964 }
2965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kFloat32:
2967 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002969 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2970 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002971 } else if (add->InputAt(1)->IsConstant()) {
2972 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002973 } else {
2974 locations->SetInAt(1, Location::Any());
2975 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002976 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002977 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002979
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002980 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2982 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002983 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002984}
2985
2986void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2987 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 Location first = locations->InAt(0);
2989 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002990 Location out = locations->Out();
2991
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002994 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002995 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2996 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002997 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2998 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002999 } else {
3000 __ leal(out.AsRegister<Register>(), Address(
3001 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3002 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003004 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3005 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<Register>(), Immediate(value));
3007 } else {
3008 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3009 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003010 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003011 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003012 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003013 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003014 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003015 }
3016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003018 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003019 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3020 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003021 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003022 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3023 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003024 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003025 } else {
3026 DCHECK(second.IsConstant()) << second;
3027 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3028 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3029 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003030 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003031 break;
3032 }
3033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003035 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003037 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3038 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003039 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003040 __ addss(first.AsFpuRegister<XmmRegister>(),
3041 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003042 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3043 const_area->GetBaseMethodAddress(),
3044 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 } else {
3046 DCHECK(second.IsStackSlot());
3047 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003048 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003049 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003050 }
3051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003052 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003053 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003054 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003055 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3056 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003057 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003058 __ addsd(first.AsFpuRegister<XmmRegister>(),
3059 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003060 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3061 const_area->GetBaseMethodAddress(),
3062 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003063 } else {
3064 DCHECK(second.IsDoubleStackSlot());
3065 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003066 }
3067 break;
3068 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003069
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003070 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003071 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003072 }
3073}
3074
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003075void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003076 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003077 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003078 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kInt32:
3080 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003081 locations->SetInAt(0, Location::RequiresRegister());
3082 locations->SetInAt(1, Location::Any());
3083 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003084 break;
3085 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kFloat32:
3087 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003088 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003089 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3090 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003091 } else if (sub->InputAt(1)->IsConstant()) {
3092 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003093 } else {
3094 locations->SetInAt(1, Location::Any());
3095 }
Calin Juravle11351682014-10-23 15:38:15 +01003096 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003097 break;
Calin Juravle11351682014-10-23 15:38:15 +01003098 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003099
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003100 default:
Calin Juravle11351682014-10-23 15:38:15 +01003101 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003102 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003103}
3104
3105void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3106 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003107 Location first = locations->InAt(0);
3108 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003109 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003112 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003114 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003115 __ subl(first.AsRegister<Register>(),
3116 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003117 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003118 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003119 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003121 }
3122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003124 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003125 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3126 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003127 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003128 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003129 __ sbbl(first.AsRegisterPairHigh<Register>(),
3130 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003131 } else {
3132 DCHECK(second.IsConstant()) << second;
3133 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3134 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3135 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003136 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003137 break;
3138 }
3139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003140 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003141 if (second.IsFpuRegister()) {
3142 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3143 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3144 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003145 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 __ subss(first.AsFpuRegister<XmmRegister>(),
3147 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003148 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3149 const_area->GetBaseMethodAddress(),
3150 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003151 } else {
3152 DCHECK(second.IsStackSlot());
3153 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3154 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003155 break;
Calin Juravle11351682014-10-23 15:38:15 +01003156 }
3157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003158 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003159 if (second.IsFpuRegister()) {
3160 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3161 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3162 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003163 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003164 __ subsd(first.AsFpuRegister<XmmRegister>(),
3165 codegen_->LiteralDoubleAddress(
3166 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003167 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003168 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3169 } else {
3170 DCHECK(second.IsDoubleStackSlot());
3171 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3172 }
Calin Juravle11351682014-10-23 15:38:15 +01003173 break;
3174 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003175
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003176 default:
Calin Juravle11351682014-10-23 15:38:15 +01003177 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003178 }
3179}
3180
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181void LocationsBuilderX86::VisitMul(HMul* mul) {
3182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003183 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003185 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003186 locations->SetInAt(0, Location::RequiresRegister());
3187 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003188 if (mul->InputAt(1)->IsIntConstant()) {
3189 // Can use 3 operand multiply.
3190 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3191 } else {
3192 locations->SetOut(Location::SameAsFirstInput());
3193 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 locations->SetInAt(1, Location::Any());
3198 locations->SetOut(Location::SameAsFirstInput());
3199 // Needed for imul on 32bits with 64bits output.
3200 locations->AddTemp(Location::RegisterLocation(EAX));
3201 locations->AddTemp(Location::RegisterLocation(EDX));
3202 break;
3203 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 case DataType::Type::kFloat32:
3205 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003206 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003207 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3208 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003209 } else if (mul->InputAt(1)->IsConstant()) {
3210 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003211 } else {
3212 locations->SetInAt(1, Location::Any());
3213 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003214 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003216 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003217
3218 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003219 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003220 }
3221}
3222
3223void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3224 LocationSummary* locations = mul->GetLocations();
3225 Location first = locations->InAt(0);
3226 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003227 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003228
3229 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003231 // The constant may have ended up in a register, so test explicitly to avoid
3232 // problems where the output may not be the same as the first operand.
3233 if (mul->InputAt(1)->IsIntConstant()) {
3234 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3235 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3236 } else if (second.IsRegister()) {
3237 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003238 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003239 } else {
3240 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003241 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003243 }
3244 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003246 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003247 Register in1_hi = first.AsRegisterPairHigh<Register>();
3248 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 Register eax = locations->GetTemp(0).AsRegister<Register>();
3250 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251
3252 DCHECK_EQ(EAX, eax);
3253 DCHECK_EQ(EDX, edx);
3254
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003255 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003256 // output: in1
3257 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3258 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3259 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003260 if (second.IsConstant()) {
3261 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003262
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003263 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3264 int32_t low_value = Low32Bits(value);
3265 int32_t high_value = High32Bits(value);
3266 Immediate low(low_value);
3267 Immediate high(high_value);
3268
3269 __ movl(eax, high);
3270 // eax <- in1.lo * in2.hi
3271 __ imull(eax, in1_lo);
3272 // in1.hi <- in1.hi * in2.lo
3273 __ imull(in1_hi, low);
3274 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3275 __ addl(in1_hi, eax);
3276 // move in2_lo to eax to prepare for double precision
3277 __ movl(eax, low);
3278 // edx:eax <- in1.lo * in2.lo
3279 __ mull(in1_lo);
3280 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3281 __ addl(in1_hi, edx);
3282 // in1.lo <- (in1.lo * in2.lo)[31:0];
3283 __ movl(in1_lo, eax);
3284 } else if (second.IsRegisterPair()) {
3285 Register in2_hi = second.AsRegisterPairHigh<Register>();
3286 Register in2_lo = second.AsRegisterPairLow<Register>();
3287
3288 __ movl(eax, in2_hi);
3289 // eax <- in1.lo * in2.hi
3290 __ imull(eax, in1_lo);
3291 // in1.hi <- in1.hi * in2.lo
3292 __ imull(in1_hi, in2_lo);
3293 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3294 __ addl(in1_hi, eax);
3295 // move in1_lo to eax to prepare for double precision
3296 __ movl(eax, in1_lo);
3297 // edx:eax <- in1.lo * in2.lo
3298 __ mull(in2_lo);
3299 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3300 __ addl(in1_hi, edx);
3301 // in1.lo <- (in1.lo * in2.lo)[31:0];
3302 __ movl(in1_lo, eax);
3303 } else {
3304 DCHECK(second.IsDoubleStackSlot()) << second;
3305 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3306 Address in2_lo(ESP, second.GetStackIndex());
3307
3308 __ movl(eax, in2_hi);
3309 // eax <- in1.lo * in2.hi
3310 __ imull(eax, in1_lo);
3311 // in1.hi <- in1.hi * in2.lo
3312 __ imull(in1_hi, in2_lo);
3313 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3314 __ addl(in1_hi, eax);
3315 // move in1_lo to eax to prepare for double precision
3316 __ movl(eax, in1_lo);
3317 // edx:eax <- in1.lo * in2.lo
3318 __ mull(in2_lo);
3319 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3320 __ addl(in1_hi, edx);
3321 // in1.lo <- (in1.lo * in2.lo)[31:0];
3322 __ movl(in1_lo, eax);
3323 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324
3325 break;
3326 }
3327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003328 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003329 DCHECK(first.Equals(locations->Out()));
3330 if (second.IsFpuRegister()) {
3331 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3332 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3333 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003334 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003335 __ mulss(first.AsFpuRegister<XmmRegister>(),
3336 codegen_->LiteralFloatAddress(
3337 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003338 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003339 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3340 } else {
3341 DCHECK(second.IsStackSlot());
3342 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3343 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003344 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003345 }
3346
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003347 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003348 DCHECK(first.Equals(locations->Out()));
3349 if (second.IsFpuRegister()) {
3350 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3351 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3352 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003353 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003354 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3355 codegen_->LiteralDoubleAddress(
3356 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003357 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003358 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3359 } else {
3360 DCHECK(second.IsDoubleStackSlot());
3361 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3362 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003363 break;
3364 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003365
3366 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003367 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003368 }
3369}
3370
Roland Levillain232ade02015-04-20 15:14:36 +01003371void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3372 uint32_t temp_offset,
3373 uint32_t stack_adjustment,
3374 bool is_fp,
3375 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003376 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003377 DCHECK(!is_wide);
3378 if (is_fp) {
3379 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3380 } else {
3381 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3382 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003383 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003384 DCHECK(is_wide);
3385 if (is_fp) {
3386 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3387 } else {
3388 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3389 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003390 } else {
3391 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003392 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003393 Location stack_temp = Location::StackSlot(temp_offset);
3394 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003395 if (is_fp) {
3396 __ flds(Address(ESP, temp_offset));
3397 } else {
3398 __ filds(Address(ESP, temp_offset));
3399 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003400 } else {
3401 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3402 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003403 if (is_fp) {
3404 __ fldl(Address(ESP, temp_offset));
3405 } else {
3406 __ fildl(Address(ESP, temp_offset));
3407 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003408 }
3409 }
3410}
3411
3412void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 DataType::Type type = rem->GetResultType();
3414 bool is_float = type == DataType::Type::kFloat32;
3415 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003416 LocationSummary* locations = rem->GetLocations();
3417 Location first = locations->InAt(0);
3418 Location second = locations->InAt(1);
3419 Location out = locations->Out();
3420
3421 // Create stack space for 2 elements.
3422 // TODO: enhance register allocator to ask for stack temporaries.
3423 __ subl(ESP, Immediate(2 * elem_size));
3424
3425 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003426 const bool is_wide = !is_float;
3427 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3428 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003429
3430 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003431 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003432 __ Bind(&retry);
3433 __ fprem();
3434
3435 // Move FP status to AX.
3436 __ fstsw();
3437
3438 // And see if the argument reduction is complete. This is signaled by the
3439 // C2 FPU flag bit set to 0.
3440 __ andl(EAX, Immediate(kC2ConditionMask));
3441 __ j(kNotEqual, &retry);
3442
3443 // We have settled on the final value. Retrieve it into an XMM register.
3444 // Store FP top of stack to real stack.
3445 if (is_float) {
3446 __ fsts(Address(ESP, 0));
3447 } else {
3448 __ fstl(Address(ESP, 0));
3449 }
3450
3451 // Pop the 2 items from the FP stack.
3452 __ fucompp();
3453
3454 // Load the value from the stack into an XMM register.
3455 DCHECK(out.IsFpuRegister()) << out;
3456 if (is_float) {
3457 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3458 } else {
3459 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3460 }
3461
3462 // And remove the temporary stack space we allocated.
3463 __ addl(ESP, Immediate(2 * elem_size));
3464}
3465
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003466
3467void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3468 DCHECK(instruction->IsDiv() || instruction->IsRem());
3469
3470 LocationSummary* locations = instruction->GetLocations();
3471 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003472 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003473
3474 Register out_register = locations->Out().AsRegister<Register>();
3475 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003476 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003477
3478 DCHECK(imm == 1 || imm == -1);
3479
3480 if (instruction->IsRem()) {
3481 __ xorl(out_register, out_register);
3482 } else {
3483 __ movl(out_register, input_register);
3484 if (imm == -1) {
3485 __ negl(out_register);
3486 }
3487 }
3488}
3489
3490
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003491void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003492 LocationSummary* locations = instruction->GetLocations();
3493
3494 Register out_register = locations->Out().AsRegister<Register>();
3495 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003496 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003497 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3498 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003499
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 Register num = locations->GetTemp(0).AsRegister<Register>();
3501
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003502 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 __ testl(input_register, input_register);
3504 __ cmovl(kGreaterEqual, num, input_register);
3505 int shift = CTZ(imm);
3506 __ sarl(num, Immediate(shift));
3507
3508 if (imm < 0) {
3509 __ negl(num);
3510 }
3511
3512 __ movl(out_register, num);
3513}
3514
3515void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3516 DCHECK(instruction->IsDiv() || instruction->IsRem());
3517
3518 LocationSummary* locations = instruction->GetLocations();
3519 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3520
3521 Register eax = locations->InAt(0).AsRegister<Register>();
3522 Register out = locations->Out().AsRegister<Register>();
3523 Register num;
3524 Register edx;
3525
3526 if (instruction->IsDiv()) {
3527 edx = locations->GetTemp(0).AsRegister<Register>();
3528 num = locations->GetTemp(1).AsRegister<Register>();
3529 } else {
3530 edx = locations->Out().AsRegister<Register>();
3531 num = locations->GetTemp(0).AsRegister<Register>();
3532 }
3533
3534 DCHECK_EQ(EAX, eax);
3535 DCHECK_EQ(EDX, edx);
3536 if (instruction->IsDiv()) {
3537 DCHECK_EQ(EAX, out);
3538 } else {
3539 DCHECK_EQ(EDX, out);
3540 }
3541
3542 int64_t magic;
3543 int shift;
3544 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3545
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 // Save the numerator.
3547 __ movl(num, eax);
3548
3549 // EAX = magic
3550 __ movl(eax, Immediate(magic));
3551
3552 // EDX:EAX = magic * numerator
3553 __ imull(num);
3554
3555 if (imm > 0 && magic < 0) {
3556 // EDX += num
3557 __ addl(edx, num);
3558 } else if (imm < 0 && magic > 0) {
3559 __ subl(edx, num);
3560 }
3561
3562 // Shift if needed.
3563 if (shift != 0) {
3564 __ sarl(edx, Immediate(shift));
3565 }
3566
3567 // EDX += 1 if EDX < 0
3568 __ movl(eax, edx);
3569 __ shrl(edx, Immediate(31));
3570 __ addl(edx, eax);
3571
3572 if (instruction->IsRem()) {
3573 __ movl(eax, num);
3574 __ imull(edx, Immediate(imm));
3575 __ subl(eax, edx);
3576 __ movl(edx, eax);
3577 } else {
3578 __ movl(eax, edx);
3579 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580}
3581
Calin Juravlebacfec32014-11-14 15:54:36 +00003582void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3583 DCHECK(instruction->IsDiv() || instruction->IsRem());
3584
3585 LocationSummary* locations = instruction->GetLocations();
3586 Location out = locations->Out();
3587 Location first = locations->InAt(0);
3588 Location second = locations->InAt(1);
3589 bool is_div = instruction->IsDiv();
3590
3591 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003592 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003593 DCHECK_EQ(EAX, first.AsRegister<Register>());
3594 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003595
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003596 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003597 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598
3599 if (imm == 0) {
3600 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3601 } else if (imm == 1 || imm == -1) {
3602 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003603 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003604 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003605 } else {
3606 DCHECK(imm <= -2 || imm >= 2);
3607 GenerateDivRemWithAnyConstant(instruction);
3608 }
3609 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003610 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003611 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003612 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003613
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 Register second_reg = second.AsRegister<Register>();
3615 // 0x80000000/-1 triggers an arithmetic exception!
3616 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3617 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003618
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003619 __ cmpl(second_reg, Immediate(-1));
3620 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003621
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 // edx:eax <- sign-extended of eax
3623 __ cdq();
3624 // eax = quotient, edx = remainder
3625 __ idivl(second_reg);
3626 __ Bind(slow_path->GetExitLabel());
3627 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003628 break;
3629 }
3630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003632 InvokeRuntimeCallingConvention calling_convention;
3633 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3634 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3635 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3636 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3637 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3638 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3639
3640 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003641 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003642 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003643 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003644 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003645 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003646 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003647 break;
3648 }
3649
3650 default:
3651 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3652 }
3653}
3654
Calin Juravle7c4954d2014-10-28 16:57:40 +00003655void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003657 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003658 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003659 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003660
Calin Juravle7c4954d2014-10-28 16:57:40 +00003661 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003663 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003664 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003665 locations->SetOut(Location::SameAsFirstInput());
3666 // Intel uses edx:eax as the dividend.
3667 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003668 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3669 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3670 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003671 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003672 locations->AddTemp(Location::RequiresRegister());
3673 }
Calin Juravled0d48522014-11-04 16:40:20 +00003674 break;
3675 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003677 InvokeRuntimeCallingConvention calling_convention;
3678 locations->SetInAt(0, Location::RegisterPairLocation(
3679 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3680 locations->SetInAt(1, Location::RegisterPairLocation(
3681 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3682 // Runtime helper puts the result in EAX, EDX.
3683 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003684 break;
3685 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kFloat32:
3687 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003688 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003689 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3690 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003691 } else if (div->InputAt(1)->IsConstant()) {
3692 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003693 } else {
3694 locations->SetInAt(1, Location::Any());
3695 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003696 locations->SetOut(Location::SameAsFirstInput());
3697 break;
3698 }
3699
3700 default:
3701 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3702 }
3703}
3704
3705void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3706 LocationSummary* locations = div->GetLocations();
3707 Location first = locations->InAt(0);
3708 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003709
3710 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kInt32:
3712 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003713 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003714 break;
3715 }
3716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003717 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003718 if (second.IsFpuRegister()) {
3719 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3720 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3721 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003722 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003723 __ divss(first.AsFpuRegister<XmmRegister>(),
3724 codegen_->LiteralFloatAddress(
3725 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003726 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003727 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3728 } else {
3729 DCHECK(second.IsStackSlot());
3730 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3731 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003732 break;
3733 }
3734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003735 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003736 if (second.IsFpuRegister()) {
3737 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3738 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3739 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003740 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003741 __ divsd(first.AsFpuRegister<XmmRegister>(),
3742 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003743 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3744 const_area->GetBaseMethodAddress(),
3745 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003746 } else {
3747 DCHECK(second.IsDoubleStackSlot());
3748 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3749 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003750 break;
3751 }
3752
3753 default:
3754 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3755 }
3756}
3757
Calin Juravlebacfec32014-11-14 15:54:36 +00003758void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003759 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003762 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003763 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003764 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003765
Calin Juravled2ec87d2014-12-08 14:24:46 +00003766 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003768 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003769 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003770 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003771 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3772 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3773 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003774 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003775 locations->AddTemp(Location::RequiresRegister());
3776 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003777 break;
3778 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003779 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 InvokeRuntimeCallingConvention calling_convention;
3781 locations->SetInAt(0, Location::RegisterPairLocation(
3782 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3783 locations->SetInAt(1, Location::RegisterPairLocation(
3784 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3785 // Runtime helper puts the result in EAX, EDX.
3786 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3787 break;
3788 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003789 case DataType::Type::kFloat64:
3790 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003791 locations->SetInAt(0, Location::Any());
3792 locations->SetInAt(1, Location::Any());
3793 locations->SetOut(Location::RequiresFpuRegister());
3794 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003795 break;
3796 }
3797
3798 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003799 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003800 }
3801}
3802
3803void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003805 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 case DataType::Type::kInt32:
3807 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003808 GenerateDivRemIntegral(rem);
3809 break;
3810 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kFloat32:
3812 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003813 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003814 break;
3815 }
3816 default:
3817 LOG(FATAL) << "Unexpected rem type " << type;
3818 }
3819}
3820
Aart Bik1f8d51b2018-02-15 10:42:37 -08003821static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3822 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3823 switch (minmax->GetResultType()) {
3824 case DataType::Type::kInt32:
3825 locations->SetInAt(0, Location::RequiresRegister());
3826 locations->SetInAt(1, Location::RequiresRegister());
3827 locations->SetOut(Location::SameAsFirstInput());
3828 break;
3829 case DataType::Type::kInt64:
3830 locations->SetInAt(0, Location::RequiresRegister());
3831 locations->SetInAt(1, Location::RequiresRegister());
3832 locations->SetOut(Location::SameAsFirstInput());
3833 // Register to use to perform a long subtract to set cc.
3834 locations->AddTemp(Location::RequiresRegister());
3835 break;
3836 case DataType::Type::kFloat32:
3837 locations->SetInAt(0, Location::RequiresFpuRegister());
3838 locations->SetInAt(1, Location::RequiresFpuRegister());
3839 locations->SetOut(Location::SameAsFirstInput());
3840 locations->AddTemp(Location::RequiresRegister());
3841 break;
3842 case DataType::Type::kFloat64:
3843 locations->SetInAt(0, Location::RequiresFpuRegister());
3844 locations->SetInAt(1, Location::RequiresFpuRegister());
3845 locations->SetOut(Location::SameAsFirstInput());
3846 break;
3847 default:
3848 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3849 }
3850}
3851
Aart Bik351df3e2018-03-07 11:54:57 -08003852void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3853 bool is_min,
3854 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003855 Location op1_loc = locations->InAt(0);
3856 Location op2_loc = locations->InAt(1);
3857
3858 // Shortcut for same input locations.
3859 if (op1_loc.Equals(op2_loc)) {
3860 // Can return immediately, as op1_loc == out_loc.
3861 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3862 // a copy here.
3863 DCHECK(locations->Out().Equals(op1_loc));
3864 return;
3865 }
3866
3867 if (type == DataType::Type::kInt64) {
3868 // Need to perform a subtract to get the sign right.
3869 // op1 is already in the same location as the output.
3870 Location output = locations->Out();
3871 Register output_lo = output.AsRegisterPairLow<Register>();
3872 Register output_hi = output.AsRegisterPairHigh<Register>();
3873
3874 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3875 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3876
3877 // The comparison is performed by subtracting the second operand from
3878 // the first operand and then setting the status flags in the same
3879 // manner as the SUB instruction."
3880 __ cmpl(output_lo, op2_lo);
3881
3882 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3883 Register temp = locations->GetTemp(0).AsRegister<Register>();
3884 __ movl(temp, output_hi);
3885 __ sbbl(temp, op2_hi);
3886
3887 // Now the condition code is correct.
3888 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3889 __ cmovl(cond, output_lo, op2_lo);
3890 __ cmovl(cond, output_hi, op2_hi);
3891 } else {
3892 DCHECK_EQ(type, DataType::Type::kInt32);
3893 Register out = locations->Out().AsRegister<Register>();
3894 Register op2 = op2_loc.AsRegister<Register>();
3895
3896 // (out := op1)
3897 // out <=? op2
3898 // if out is min jmp done
3899 // out := op2
3900 // done:
3901
3902 __ cmpl(out, op2);
3903 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3904 __ cmovl(cond, out, op2);
3905 }
3906}
3907
3908void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3909 bool is_min,
3910 DataType::Type type) {
3911 Location op1_loc = locations->InAt(0);
3912 Location op2_loc = locations->InAt(1);
3913 Location out_loc = locations->Out();
3914 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3915
3916 // Shortcut for same input locations.
3917 if (op1_loc.Equals(op2_loc)) {
3918 DCHECK(out_loc.Equals(op1_loc));
3919 return;
3920 }
3921
3922 // (out := op1)
3923 // out <=? op2
3924 // if Nan jmp Nan_label
3925 // if out is min jmp done
3926 // if op2 is min jmp op2_label
3927 // handle -0/+0
3928 // jmp done
3929 // Nan_label:
3930 // out := NaN
3931 // op2_label:
3932 // out := op2
3933 // done:
3934 //
3935 // This removes one jmp, but needs to copy one input (op1) to out.
3936 //
3937 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3938
3939 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3940
3941 NearLabel nan, done, op2_label;
3942 if (type == DataType::Type::kFloat64) {
3943 __ ucomisd(out, op2);
3944 } else {
3945 DCHECK_EQ(type, DataType::Type::kFloat32);
3946 __ ucomiss(out, op2);
3947 }
3948
3949 __ j(Condition::kParityEven, &nan);
3950
3951 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3952 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3953
3954 // Handle 0.0/-0.0.
3955 if (is_min) {
3956 if (type == DataType::Type::kFloat64) {
3957 __ orpd(out, op2);
3958 } else {
3959 __ orps(out, op2);
3960 }
3961 } else {
3962 if (type == DataType::Type::kFloat64) {
3963 __ andpd(out, op2);
3964 } else {
3965 __ andps(out, op2);
3966 }
3967 }
3968 __ jmp(&done);
3969
3970 // NaN handling.
3971 __ Bind(&nan);
3972 if (type == DataType::Type::kFloat64) {
3973 // TODO: Use a constant from the constant table (requires extra input).
3974 __ LoadLongConstant(out, kDoubleNaN);
3975 } else {
3976 Register constant = locations->GetTemp(0).AsRegister<Register>();
3977 __ movl(constant, Immediate(kFloatNaN));
3978 __ movd(out, constant);
3979 }
3980 __ jmp(&done);
3981
3982 // out := op2;
3983 __ Bind(&op2_label);
3984 if (type == DataType::Type::kFloat64) {
3985 __ movsd(out, op2);
3986 } else {
3987 __ movss(out, op2);
3988 }
3989
3990 // Done.
3991 __ Bind(&done);
3992}
3993
Aart Bik351df3e2018-03-07 11:54:57 -08003994void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3995 DataType::Type type = minmax->GetResultType();
3996 switch (type) {
3997 case DataType::Type::kInt32:
3998 case DataType::Type::kInt64:
3999 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4000 break;
4001 case DataType::Type::kFloat32:
4002 case DataType::Type::kFloat64:
4003 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4004 break;
4005 default:
4006 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4007 }
4008}
4009
Aart Bik1f8d51b2018-02-15 10:42:37 -08004010void LocationsBuilderX86::VisitMin(HMin* min) {
4011 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4012}
4013
4014void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004015 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004016}
4017
4018void LocationsBuilderX86::VisitMax(HMax* max) {
4019 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4020}
4021
4022void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004023 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004024}
4025
Aart Bik3dad3412018-02-28 12:01:46 -08004026void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4027 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4028 switch (abs->GetResultType()) {
4029 case DataType::Type::kInt32:
4030 locations->SetInAt(0, Location::RegisterLocation(EAX));
4031 locations->SetOut(Location::SameAsFirstInput());
4032 locations->AddTemp(Location::RegisterLocation(EDX));
4033 break;
4034 case DataType::Type::kInt64:
4035 locations->SetInAt(0, Location::RequiresRegister());
4036 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4037 locations->AddTemp(Location::RequiresRegister());
4038 break;
4039 case DataType::Type::kFloat32:
4040 locations->SetInAt(0, Location::RequiresFpuRegister());
4041 locations->SetOut(Location::SameAsFirstInput());
4042 locations->AddTemp(Location::RequiresFpuRegister());
4043 locations->AddTemp(Location::RequiresRegister());
4044 break;
4045 case DataType::Type::kFloat64:
4046 locations->SetInAt(0, Location::RequiresFpuRegister());
4047 locations->SetOut(Location::SameAsFirstInput());
4048 locations->AddTemp(Location::RequiresFpuRegister());
4049 break;
4050 default:
4051 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4052 }
4053}
4054
4055void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4056 LocationSummary* locations = abs->GetLocations();
4057 switch (abs->GetResultType()) {
4058 case DataType::Type::kInt32: {
4059 Register out = locations->Out().AsRegister<Register>();
4060 DCHECK_EQ(out, EAX);
4061 Register temp = locations->GetTemp(0).AsRegister<Register>();
4062 DCHECK_EQ(temp, EDX);
4063 // Sign extend EAX into EDX.
4064 __ cdq();
4065 // XOR EAX with sign.
4066 __ xorl(EAX, EDX);
4067 // Subtract out sign to correct.
4068 __ subl(EAX, EDX);
4069 // The result is in EAX.
4070 break;
4071 }
4072 case DataType::Type::kInt64: {
4073 Location input = locations->InAt(0);
4074 Register input_lo = input.AsRegisterPairLow<Register>();
4075 Register input_hi = input.AsRegisterPairHigh<Register>();
4076 Location output = locations->Out();
4077 Register output_lo = output.AsRegisterPairLow<Register>();
4078 Register output_hi = output.AsRegisterPairHigh<Register>();
4079 Register temp = locations->GetTemp(0).AsRegister<Register>();
4080 // Compute the sign into the temporary.
4081 __ movl(temp, input_hi);
4082 __ sarl(temp, Immediate(31));
4083 // Store the sign into the output.
4084 __ movl(output_lo, temp);
4085 __ movl(output_hi, temp);
4086 // XOR the input to the output.
4087 __ xorl(output_lo, input_lo);
4088 __ xorl(output_hi, input_hi);
4089 // Subtract the sign.
4090 __ subl(output_lo, temp);
4091 __ sbbl(output_hi, temp);
4092 break;
4093 }
4094 case DataType::Type::kFloat32: {
4095 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4096 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4097 Register constant = locations->GetTemp(1).AsRegister<Register>();
4098 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4099 __ movd(temp, constant);
4100 __ andps(out, temp);
4101 break;
4102 }
4103 case DataType::Type::kFloat64: {
4104 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4105 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4106 // TODO: Use a constant from the constant table (requires extra input).
4107 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4108 __ andpd(out, temp);
4109 break;
4110 }
4111 default:
4112 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4113 }
4114}
4115
Calin Juravled0d48522014-11-04 16:40:20 +00004116void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004117 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004118 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004120 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 case DataType::Type::kInt8:
4122 case DataType::Type::kUint16:
4123 case DataType::Type::kInt16:
4124 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004125 locations->SetInAt(0, Location::Any());
4126 break;
4127 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004129 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4130 if (!instruction->IsConstant()) {
4131 locations->AddTemp(Location::RequiresRegister());
4132 }
4133 break;
4134 }
4135 default:
4136 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4137 }
Calin Juravled0d48522014-11-04 16:40:20 +00004138}
4139
4140void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004141 SlowPathCode* slow_path =
4142 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004143 codegen_->AddSlowPath(slow_path);
4144
4145 LocationSummary* locations = instruction->GetLocations();
4146 Location value = locations->InAt(0);
4147
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004148 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004150 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 case DataType::Type::kInt8:
4152 case DataType::Type::kUint16:
4153 case DataType::Type::kInt16:
4154 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004155 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004156 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004157 __ j(kEqual, slow_path->GetEntryLabel());
4158 } else if (value.IsStackSlot()) {
4159 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4160 __ j(kEqual, slow_path->GetEntryLabel());
4161 } else {
4162 DCHECK(value.IsConstant()) << value;
4163 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004164 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004165 }
4166 }
4167 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004168 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004169 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004170 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004172 __ movl(temp, value.AsRegisterPairLow<Register>());
4173 __ orl(temp, value.AsRegisterPairHigh<Register>());
4174 __ j(kEqual, slow_path->GetEntryLabel());
4175 } else {
4176 DCHECK(value.IsConstant()) << value;
4177 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4178 __ jmp(slow_path->GetEntryLabel());
4179 }
4180 }
4181 break;
4182 }
4183 default:
4184 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004185 }
Calin Juravled0d48522014-11-04 16:40:20 +00004186}
4187
Calin Juravle9aec02f2014-11-18 23:06:35 +00004188void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4189 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4190
4191 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004192 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004193
4194 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 case DataType::Type::kInt32:
4196 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004197 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004198 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004199 // The shift count needs to be in CL or a constant.
4200 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004201 locations->SetOut(Location::SameAsFirstInput());
4202 break;
4203 }
4204 default:
4205 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4206 }
4207}
4208
4209void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4210 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4211
4212 LocationSummary* locations = op->GetLocations();
4213 Location first = locations->InAt(0);
4214 Location second = locations->InAt(1);
4215 DCHECK(first.Equals(locations->Out()));
4216
4217 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004218 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004219 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004220 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004221 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004222 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004223 DCHECK_EQ(ECX, second_reg);
4224 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004225 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004226 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004227 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004228 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004229 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004230 }
4231 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004232 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004233 if (shift == 0) {
4234 return;
4235 }
4236 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004237 if (op->IsShl()) {
4238 __ shll(first_reg, imm);
4239 } else if (op->IsShr()) {
4240 __ sarl(first_reg, imm);
4241 } else {
4242 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004243 }
4244 }
4245 break;
4246 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004247 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004248 if (second.IsRegister()) {
4249 Register second_reg = second.AsRegister<Register>();
4250 DCHECK_EQ(ECX, second_reg);
4251 if (op->IsShl()) {
4252 GenerateShlLong(first, second_reg);
4253 } else if (op->IsShr()) {
4254 GenerateShrLong(first, second_reg);
4255 } else {
4256 GenerateUShrLong(first, second_reg);
4257 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004258 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004259 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004260 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004261 // Nothing to do if the shift is 0, as the input is already the output.
4262 if (shift != 0) {
4263 if (op->IsShl()) {
4264 GenerateShlLong(first, shift);
4265 } else if (op->IsShr()) {
4266 GenerateShrLong(first, shift);
4267 } else {
4268 GenerateUShrLong(first, shift);
4269 }
4270 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004271 }
4272 break;
4273 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004274 default:
4275 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4276 }
4277}
4278
Mark P Mendell73945692015-04-29 14:56:17 +00004279void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4280 Register low = loc.AsRegisterPairLow<Register>();
4281 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004282 if (shift == 1) {
4283 // This is just an addition.
4284 __ addl(low, low);
4285 __ adcl(high, high);
4286 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004287 // Shift by 32 is easy. High gets low, and low gets 0.
4288 codegen_->EmitParallelMoves(
4289 loc.ToLow(),
4290 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004292 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4293 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004294 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004295 } else if (shift > 32) {
4296 // Low part becomes 0. High part is low part << (shift-32).
4297 __ movl(high, low);
4298 __ shll(high, Immediate(shift - 32));
4299 __ xorl(low, low);
4300 } else {
4301 // Between 1 and 31.
4302 __ shld(high, low, Immediate(shift));
4303 __ shll(low, Immediate(shift));
4304 }
4305}
4306
Calin Juravle9aec02f2014-11-18 23:06:35 +00004307void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004308 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004309 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4310 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4311 __ testl(shifter, Immediate(32));
4312 __ j(kEqual, &done);
4313 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4314 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4315 __ Bind(&done);
4316}
4317
Mark P Mendell73945692015-04-29 14:56:17 +00004318void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4319 Register low = loc.AsRegisterPairLow<Register>();
4320 Register high = loc.AsRegisterPairHigh<Register>();
4321 if (shift == 32) {
4322 // Need to copy the sign.
4323 DCHECK_NE(low, high);
4324 __ movl(low, high);
4325 __ sarl(high, Immediate(31));
4326 } else if (shift > 32) {
4327 DCHECK_NE(low, high);
4328 // High part becomes sign. Low part is shifted by shift - 32.
4329 __ movl(low, high);
4330 __ sarl(high, Immediate(31));
4331 __ sarl(low, Immediate(shift - 32));
4332 } else {
4333 // Between 1 and 31.
4334 __ shrd(low, high, Immediate(shift));
4335 __ sarl(high, Immediate(shift));
4336 }
4337}
4338
Calin Juravle9aec02f2014-11-18 23:06:35 +00004339void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004340 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004341 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4342 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4343 __ testl(shifter, Immediate(32));
4344 __ j(kEqual, &done);
4345 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4346 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4347 __ Bind(&done);
4348}
4349
Mark P Mendell73945692015-04-29 14:56:17 +00004350void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4351 Register low = loc.AsRegisterPairLow<Register>();
4352 Register high = loc.AsRegisterPairHigh<Register>();
4353 if (shift == 32) {
4354 // Shift by 32 is easy. Low gets high, and high gets 0.
4355 codegen_->EmitParallelMoves(
4356 loc.ToHigh(),
4357 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004358 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004359 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4360 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004361 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004362 } else if (shift > 32) {
4363 // Low part is high >> (shift - 32). High part becomes 0.
4364 __ movl(low, high);
4365 __ shrl(low, Immediate(shift - 32));
4366 __ xorl(high, high);
4367 } else {
4368 // Between 1 and 31.
4369 __ shrd(low, high, Immediate(shift));
4370 __ shrl(high, Immediate(shift));
4371 }
4372}
4373
Calin Juravle9aec02f2014-11-18 23:06:35 +00004374void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004375 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004376 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4377 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4378 __ testl(shifter, Immediate(32));
4379 __ j(kEqual, &done);
4380 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4381 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4382 __ Bind(&done);
4383}
4384
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004385void LocationsBuilderX86::VisitRor(HRor* ror) {
4386 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004387 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004388
4389 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004390 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004391 // Add the temporary needed.
4392 locations->AddTemp(Location::RequiresRegister());
4393 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004395 locations->SetInAt(0, Location::RequiresRegister());
4396 // The shift count needs to be in CL (unless it is a constant).
4397 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4398 locations->SetOut(Location::SameAsFirstInput());
4399 break;
4400 default:
4401 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4402 UNREACHABLE();
4403 }
4404}
4405
4406void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4407 LocationSummary* locations = ror->GetLocations();
4408 Location first = locations->InAt(0);
4409 Location second = locations->InAt(1);
4410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004411 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004412 Register first_reg = first.AsRegister<Register>();
4413 if (second.IsRegister()) {
4414 Register second_reg = second.AsRegister<Register>();
4415 __ rorl(first_reg, second_reg);
4416 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004417 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004418 __ rorl(first_reg, imm);
4419 }
4420 return;
4421 }
4422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004423 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004424 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4425 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4426 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4427 if (second.IsRegister()) {
4428 Register second_reg = second.AsRegister<Register>();
4429 DCHECK_EQ(second_reg, ECX);
4430 __ movl(temp_reg, first_reg_hi);
4431 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4432 __ shrd(first_reg_lo, temp_reg, second_reg);
4433 __ movl(temp_reg, first_reg_hi);
4434 __ testl(second_reg, Immediate(32));
4435 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4436 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4437 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004438 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004439 if (shift_amt == 0) {
4440 // Already fine.
4441 return;
4442 }
4443 if (shift_amt == 32) {
4444 // Just swap.
4445 __ movl(temp_reg, first_reg_lo);
4446 __ movl(first_reg_lo, first_reg_hi);
4447 __ movl(first_reg_hi, temp_reg);
4448 return;
4449 }
4450
4451 Immediate imm(shift_amt);
4452 // Save the constents of the low value.
4453 __ movl(temp_reg, first_reg_lo);
4454
4455 // Shift right into low, feeding bits from high.
4456 __ shrd(first_reg_lo, first_reg_hi, imm);
4457
4458 // Shift right into high, feeding bits from the original low.
4459 __ shrd(first_reg_hi, temp_reg, imm);
4460
4461 // Swap if needed.
4462 if (shift_amt > 32) {
4463 __ movl(temp_reg, first_reg_lo);
4464 __ movl(first_reg_lo, first_reg_hi);
4465 __ movl(first_reg_hi, temp_reg);
4466 }
4467 }
4468}
4469
Calin Juravle9aec02f2014-11-18 23:06:35 +00004470void LocationsBuilderX86::VisitShl(HShl* shl) {
4471 HandleShift(shl);
4472}
4473
4474void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4475 HandleShift(shl);
4476}
4477
4478void LocationsBuilderX86::VisitShr(HShr* shr) {
4479 HandleShift(shr);
4480}
4481
4482void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4483 HandleShift(shr);
4484}
4485
4486void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4487 HandleShift(ushr);
4488}
4489
4490void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4491 HandleShift(ushr);
4492}
4493
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004494void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004495 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4496 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004497 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004498 if (instruction->IsStringAlloc()) {
4499 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4500 } else {
4501 InvokeRuntimeCallingConvention calling_convention;
4502 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004503 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004504}
4505
4506void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004507 // Note: if heap poisoning is enabled, the entry point takes cares
4508 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004509 if (instruction->IsStringAlloc()) {
4510 // String is allocated through StringFactory. Call NewEmptyString entry point.
4511 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004512 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004513 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4514 __ call(Address(temp, code_offset.Int32Value()));
4515 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4516 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004517 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004518 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004519 DCHECK(!codegen_->IsLeafMethod());
4520 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004521}
4522
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004523void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004524 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4525 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004526 locations->SetOut(Location::RegisterLocation(EAX));
4527 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004528 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4529 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004530}
4531
4532void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004533 // Note: if heap poisoning is enabled, the entry point takes cares
4534 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004535 QuickEntrypointEnum entrypoint =
4536 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4537 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004538 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004539 DCHECK(!codegen_->IsLeafMethod());
4540}
4541
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004542void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004543 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004544 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004545 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4546 if (location.IsStackSlot()) {
4547 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4548 } else if (location.IsDoubleStackSlot()) {
4549 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004550 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004551 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004552}
4553
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004554void InstructionCodeGeneratorX86::VisitParameterValue(
4555 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4556}
4557
4558void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4559 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004560 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004561 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4562}
4563
4564void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004565}
4566
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004567void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4568 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004569 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004570 locations->SetInAt(0, Location::RequiresRegister());
4571 locations->SetOut(Location::RequiresRegister());
4572}
4573
4574void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4575 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004576 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004577 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004578 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004579 __ movl(locations->Out().AsRegister<Register>(),
4580 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004581 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004582 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004583 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004584 __ movl(locations->Out().AsRegister<Register>(),
4585 Address(locations->InAt(0).AsRegister<Register>(),
4586 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4587 // temp = temp->GetImtEntryAt(method_offset);
4588 __ movl(locations->Out().AsRegister<Register>(),
4589 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004590 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004591}
4592
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004593void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004594 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004595 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004596 locations->SetInAt(0, Location::RequiresRegister());
4597 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004598}
4599
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004600void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4601 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004602 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004603 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004604 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004605 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004606 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004607 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004608 break;
4609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004611 __ notl(out.AsRegisterPairLow<Register>());
4612 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004613 break;
4614
4615 default:
4616 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4617 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004618}
4619
David Brazdil66d126e2015-04-03 16:02:44 +01004620void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4621 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004622 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004623 locations->SetInAt(0, Location::RequiresRegister());
4624 locations->SetOut(Location::SameAsFirstInput());
4625}
4626
4627void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004628 LocationSummary* locations = bool_not->GetLocations();
4629 Location in = locations->InAt(0);
4630 Location out = locations->Out();
4631 DCHECK(in.Equals(out));
4632 __ xorl(out.AsRegister<Register>(), Immediate(1));
4633}
4634
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004635void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004636 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004637 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004638 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004640 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004643 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004644 case DataType::Type::kInt32:
4645 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004646 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004647 locations->SetInAt(1, Location::Any());
4648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4649 break;
4650 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 case DataType::Type::kFloat32:
4652 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004653 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004654 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4655 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4656 } else if (compare->InputAt(1)->IsConstant()) {
4657 locations->SetInAt(1, Location::RequiresFpuRegister());
4658 } else {
4659 locations->SetInAt(1, Location::Any());
4660 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004661 locations->SetOut(Location::RequiresRegister());
4662 break;
4663 }
4664 default:
4665 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4666 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004667}
4668
4669void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004670 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004671 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004672 Location left = locations->InAt(0);
4673 Location right = locations->InAt(1);
4674
Mark Mendell0c9497d2015-08-21 09:30:05 -04004675 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004676 Condition less_cond = kLess;
4677
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004678 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004679 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004680 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004681 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004683 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004684 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004685 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004686 break;
4687 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004688 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004689 Register left_low = left.AsRegisterPairLow<Register>();
4690 Register left_high = left.AsRegisterPairHigh<Register>();
4691 int32_t val_low = 0;
4692 int32_t val_high = 0;
4693 bool right_is_const = false;
4694
4695 if (right.IsConstant()) {
4696 DCHECK(right.GetConstant()->IsLongConstant());
4697 right_is_const = true;
4698 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4699 val_low = Low32Bits(val);
4700 val_high = High32Bits(val);
4701 }
4702
Calin Juravleddb7df22014-11-25 20:56:51 +00004703 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004704 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004705 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004706 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004707 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004708 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004709 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004710 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004711 __ j(kLess, &less); // Signed compare.
4712 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004713 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004714 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004715 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004716 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004717 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004718 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004719 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004720 }
Aart Bika19616e2016-02-01 18:57:58 -08004721 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004722 break;
4723 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004724 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004725 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004726 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004727 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004728 break;
4729 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004730 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004731 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004732 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004733 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004734 break;
4735 }
4736 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004737 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004738 }
Aart Bika19616e2016-02-01 18:57:58 -08004739
Calin Juravleddb7df22014-11-25 20:56:51 +00004740 __ movl(out, Immediate(0));
4741 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004742 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004743
4744 __ Bind(&greater);
4745 __ movl(out, Immediate(1));
4746 __ jmp(&done);
4747
4748 __ Bind(&less);
4749 __ movl(out, Immediate(-1));
4750
4751 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004752}
4753
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004754void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004755 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004756 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004757 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004758 locations->SetInAt(i, Location::Any());
4759 }
4760 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004761}
4762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004763void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004764 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004765}
4766
Roland Levillain7c1559a2015-12-15 10:55:36 +00004767void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004768 /*
4769 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4770 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4771 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4772 */
4773 switch (kind) {
4774 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004775 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004776 break;
4777 }
4778 case MemBarrierKind::kAnyStore:
4779 case MemBarrierKind::kLoadAny:
4780 case MemBarrierKind::kStoreStore: {
4781 // nop
4782 break;
4783 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004784 case MemBarrierKind::kNTStoreStore:
4785 // Non-Temporal Store/Store needs an explicit fence.
4786 MemoryFence(/* non-temporal */ true);
4787 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004788 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789}
4790
Vladimir Markodc151b22015-10-15 18:02:30 +01004791HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4792 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004793 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004794 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004795}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004796
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004797Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4798 Register temp) {
4799 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004800 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004801 if (!invoke->GetLocations()->Intrinsified()) {
4802 return location.AsRegister<Register>();
4803 }
4804 // For intrinsics we allow any location, so it may be on the stack.
4805 if (!location.IsRegister()) {
4806 __ movl(temp, Address(ESP, location.GetStackIndex()));
4807 return temp;
4808 }
4809 // For register locations, check if the register was saved. If so, get it from the stack.
4810 // Note: There is a chance that the register was saved but not overwritten, so we could
4811 // save one load. However, since this is just an intrinsic slow path we prefer this
4812 // simple and more robust approach rather that trying to determine if that's the case.
4813 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004814 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4815 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4816 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4817 __ movl(temp, Address(ESP, stack_offset));
4818 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004819 }
4820 return location.AsRegister<Register>();
4821}
4822
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004823void CodeGeneratorX86::GenerateStaticOrDirectCall(
4824 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004825 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4826 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004827 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004828 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004829 uint32_t offset =
4830 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4831 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004832 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004833 }
Vladimir Marko58155012015-08-19 12:49:41 +00004834 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004835 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004836 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004837 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4838 DCHECK(GetCompilerOptions().IsBootImage());
4839 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4840 temp.AsRegister<Register>());
4841 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004842 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004843 break;
4844 }
Vladimir Marko58155012015-08-19 12:49:41 +00004845 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4846 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4847 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004848 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4849 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4850 temp.AsRegister<Register>());
4851 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4852 RecordBootImageRelRoPatch(
4853 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004854 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004855 break;
4856 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004857 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004858 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4859 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004860 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004861 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004862 break;
4863 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004864 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4865 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4866 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004867 }
Vladimir Marko58155012015-08-19 12:49:41 +00004868 }
4869
4870 switch (invoke->GetCodePtrLocation()) {
4871 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4872 __ call(GetFrameEntryLabel());
4873 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004874 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4875 // (callee_method + offset_of_quick_compiled_code)()
4876 __ call(Address(callee_method.AsRegister<Register>(),
4877 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004878 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004879 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004880 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004881 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004882
4883 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004884}
4885
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004886void CodeGeneratorX86::GenerateVirtualCall(
4887 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004888 Register temp = temp_in.AsRegister<Register>();
4889 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4890 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004891
4892 // Use the calling convention instead of the location of the receiver, as
4893 // intrinsics may have put the receiver in a different register. In the intrinsics
4894 // slow path, the arguments have been moved to the right place, so here we are
4895 // guaranteed that the receiver is the first register of the calling convention.
4896 InvokeDexCallingConvention calling_convention;
4897 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004898 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004899 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004900 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004901 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004902 // Instead of simply (possibly) unpoisoning `temp` here, we should
4903 // emit a read barrier for the previous class reference load.
4904 // However this is not required in practice, as this is an
4905 // intermediate/temporary reference and because the current
4906 // concurrent copying collector keeps the from-space memory
4907 // intact/accessible until the end of the marking phase (the
4908 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004909 __ MaybeUnpoisonHeapReference(temp);
4910 // temp = temp->GetMethodAt(method_offset);
4911 __ movl(temp, Address(temp, method_offset));
4912 // call temp->GetEntryPoint();
4913 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004914 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004915 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004916}
4917
Vladimir Markob066d432018-01-03 13:14:37 +00004918void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4919 uint32_t boot_image_offset) {
4920 boot_image_method_patches_.emplace_back(
4921 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4922 __ Bind(&boot_image_method_patches_.back().label);
4923}
4924
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004925void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004926 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004927 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004928 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004929 boot_image_method_patches_.emplace_back(
4930 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004931 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004932}
4933
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004934void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4935 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4936 HX86ComputeBaseMethodAddress* method_address =
4937 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004938 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004939 method_bss_entry_patches_.emplace_back(
4940 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4941 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004942}
4943
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004944void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4945 HX86ComputeBaseMethodAddress* method_address =
4946 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4947 boot_image_type_patches_.emplace_back(
4948 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004949 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950}
4951
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004952Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004953 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004954 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4955 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004956 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004957 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004958}
4959
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004960void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4961 HX86ComputeBaseMethodAddress* method_address =
4962 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4963 boot_image_string_patches_.emplace_back(
4964 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4965 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004966}
4967
Vladimir Markoaad75c62016-10-03 08:46:48 +00004968Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4969 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004970 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004971 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004972 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004973 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004974 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004975}
4976
Vladimir Markoeebb8212018-06-05 14:57:24 +01004977void CodeGeneratorX86::LoadBootImageAddress(Register reg,
4978 uint32_t boot_image_offset,
4979 HInvokeStaticOrDirect* invoke) {
4980 DCHECK(!GetCompilerOptions().IsBootImage());
4981 if (GetCompilerOptions().GetCompilePic()) {
4982 DCHECK(Runtime::Current()->IsAotCompiler());
4983 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4984 HX86ComputeBaseMethodAddress* method_address =
4985 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4986 DCHECK(method_address != nullptr);
4987 Register method_address_reg =
4988 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4989 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
4990 RecordBootImageRelRoPatch(method_address, boot_image_offset);
4991 } else {
4992 gc::Heap* heap = Runtime::Current()->GetHeap();
4993 DCHECK(!heap->GetBootImageSpaces().empty());
4994 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_offset;
4995 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
4996 }
4997}
4998
Vladimir Markoaad75c62016-10-03 08:46:48 +00004999// The label points to the end of the "movl" or another instruction but the literal offset
5000// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5001constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5002
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005003template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005004inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005005 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005006 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005007 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005008 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005009 linker_patches->push_back(Factory(literal_offset,
5010 info.target_dex_file,
5011 GetMethodAddressOffset(info.method_address),
5012 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005013 }
5014}
5015
Vladimir Markob066d432018-01-03 13:14:37 +00005016linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
5017 const DexFile* target_dex_file,
5018 uint32_t pc_insn_offset,
5019 uint32_t boot_image_offset) {
5020 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
5021 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
5022}
5023
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005024void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005025 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005026 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005027 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005028 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005029 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005030 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005031 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005032 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005033 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005034 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005035 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5036 boot_image_method_patches_, linker_patches);
5037 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5038 boot_image_type_patches_, linker_patches);
5039 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005040 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005041 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00005042 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
5043 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005044 DCHECK(boot_image_type_patches_.empty());
5045 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005046 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005047 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5048 method_bss_entry_patches_, linker_patches);
5049 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5050 type_bss_entry_patches_, linker_patches);
5051 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5052 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005053 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005054}
5055
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005056void CodeGeneratorX86::MarkGCCard(Register temp,
5057 Register card,
5058 Register object,
5059 Register value,
5060 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005061 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005062 if (value_can_be_null) {
5063 __ testl(value, value);
5064 __ j(kEqual, &is_null);
5065 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005066 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067 __ movl(temp, object);
5068 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005069 __ movb(Address(temp, card, TIMES_1, 0),
5070 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005071 if (value_can_be_null) {
5072 __ Bind(&is_null);
5073 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074}
5075
Calin Juravle52c48962014-12-16 17:02:57 +00005076void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5077 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005078
5079 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005080 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005081 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005082 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5083 kEmitCompilerReadBarrier
5084 ? LocationSummary::kCallOnSlowPath
5085 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005086 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005087 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005088 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005089 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005091 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005092 locations->SetOut(Location::RequiresFpuRegister());
5093 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005094 // The output overlaps in case of long: we don't want the low move
5095 // to overwrite the object's location. Likewise, in the case of
5096 // an object field get with read barriers enabled, we do not want
5097 // the move to overwrite the object's location, as we need it to emit
5098 // the read barrier.
5099 locations->SetOut(
5100 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005101 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005102 Location::kOutputOverlap :
5103 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005104 }
Calin Juravle52c48962014-12-16 17:02:57 +00005105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005106 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005107 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005108 // So we use an XMM register as a temp to achieve atomicity (first
5109 // load the temp into the XMM and then copy the XMM into the
5110 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005111 locations->AddTemp(Location::RequiresFpuRegister());
5112 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005113}
5114
Calin Juravle52c48962014-12-16 17:02:57 +00005115void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5116 const FieldInfo& field_info) {
5117 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118
Calin Juravle52c48962014-12-16 17:02:57 +00005119 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005120 Location base_loc = locations->InAt(0);
5121 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005122 Location out = locations->Out();
5123 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005124 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5125 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005126 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5127
Vladimir Marko61b92282017-10-11 13:23:17 +01005128 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005129 case DataType::Type::kBool:
5130 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005131 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132 break;
5133 }
5134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005136 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005137 break;
5138 }
5139
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005140 case DataType::Type::kUint16: {
5141 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005142 break;
5143 }
5144
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005145 case DataType::Type::kInt16: {
5146 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005147 break;
5148 }
5149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005150 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005151 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005152 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005154 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005155 // /* HeapReference<Object> */ out = *(base + offset)
5156 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005157 // Note that a potential implicit null check is handled in this
5158 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5159 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005160 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005161 if (is_volatile) {
5162 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5163 }
5164 } else {
5165 __ movl(out.AsRegister<Register>(), Address(base, offset));
5166 codegen_->MaybeRecordImplicitNullCheck(instruction);
5167 if (is_volatile) {
5168 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5169 }
5170 // If read barriers are enabled, emit read barriers other than
5171 // Baker's using a slow path (and also unpoison the loaded
5172 // reference, if heap poisoning is enabled).
5173 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5174 }
5175 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005176 }
5177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005179 if (is_volatile) {
5180 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5181 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005182 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005183 __ movd(out.AsRegisterPairLow<Register>(), temp);
5184 __ psrlq(temp, Immediate(32));
5185 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5186 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005187 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005188 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005189 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005190 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5191 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005192 break;
5193 }
5194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005195 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005196 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005197 break;
5198 }
5199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005200 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005201 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005202 break;
5203 }
5204
Aart Bik66c158e2018-01-31 12:55:04 -08005205 case DataType::Type::kUint32:
5206 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005207 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005208 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005209 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005210 }
Calin Juravle52c48962014-12-16 17:02:57 +00005211
Vladimir Marko61b92282017-10-11 13:23:17 +01005212 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005213 // Potential implicit null checks, in the case of reference or
5214 // long fields, are handled in the previous switch statement.
5215 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005216 codegen_->MaybeRecordImplicitNullCheck(instruction);
5217 }
5218
Calin Juravle52c48962014-12-16 17:02:57 +00005219 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005220 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005221 // Memory barriers, in the case of references, are also handled
5222 // in the previous switch statement.
5223 } else {
5224 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5225 }
Roland Levillain4d027112015-07-01 15:41:14 +01005226 }
Calin Juravle52c48962014-12-16 17:02:57 +00005227}
5228
5229void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5230 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5231
5232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005233 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005234 locations->SetInAt(0, Location::RequiresRegister());
5235 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005237 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005238
5239 // The register allocator does not support multiple
5240 // inputs that die at entry with one in a specific register.
5241 if (is_byte_type) {
5242 // Ensure the value is in a byte register.
5243 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 } else if (DataType::IsFloatingPointType(field_type)) {
5245 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005246 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5247 locations->SetInAt(1, Location::RequiresFpuRegister());
5248 } else {
5249 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5250 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005251 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005252 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005253 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005254
Calin Juravle52c48962014-12-16 17:02:57 +00005255 // 64bits value can be atomically written to an address with movsd and an XMM register.
5256 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5257 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5258 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5259 // isolated cases when we need this it isn't worth adding the extra complexity.
5260 locations->AddTemp(Location::RequiresFpuRegister());
5261 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005262 } else {
5263 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5264
5265 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5266 // Temporary registers for the write barrier.
5267 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5268 // Ensure the card is in a byte register.
5269 locations->AddTemp(Location::RegisterLocation(ECX));
5270 }
Calin Juravle52c48962014-12-16 17:02:57 +00005271 }
5272}
5273
5274void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005275 const FieldInfo& field_info,
5276 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005277 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5278
5279 LocationSummary* locations = instruction->GetLocations();
5280 Register base = locations->InAt(0).AsRegister<Register>();
5281 Location value = locations->InAt(1);
5282 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005283 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005284 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005285 bool needs_write_barrier =
5286 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005287
5288 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005290 }
5291
Mark Mendell81489372015-11-04 11:30:41 -05005292 bool maybe_record_implicit_null_check_done = false;
5293
Calin Juravle52c48962014-12-16 17:02:57 +00005294 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005295 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005296 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005298 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5299 break;
5300 }
5301
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005302 case DataType::Type::kUint16:
5303 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005304 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005305 __ movw(Address(base, offset),
5306 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005307 } else {
5308 __ movw(Address(base, offset), value.AsRegister<Register>());
5309 }
Calin Juravle52c48962014-12-16 17:02:57 +00005310 break;
5311 }
5312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005313 case DataType::Type::kInt32:
5314 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005315 if (kPoisonHeapReferences && needs_write_barrier) {
5316 // Note that in the case where `value` is a null reference,
5317 // we do not enter this block, as the reference does not
5318 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005319 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005320 Register temp = locations->GetTemp(0).AsRegister<Register>();
5321 __ movl(temp, value.AsRegister<Register>());
5322 __ PoisonHeapReference(temp);
5323 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005324 } else if (value.IsConstant()) {
5325 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5326 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005327 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005328 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005329 __ movl(Address(base, offset), value.AsRegister<Register>());
5330 }
Calin Juravle52c48962014-12-16 17:02:57 +00005331 break;
5332 }
5333
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005334 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005335 if (is_volatile) {
5336 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5337 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5338 __ movd(temp1, value.AsRegisterPairLow<Register>());
5339 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5340 __ punpckldq(temp1, temp2);
5341 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005343 } else if (value.IsConstant()) {
5344 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5345 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5346 codegen_->MaybeRecordImplicitNullCheck(instruction);
5347 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005348 } else {
5349 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005350 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005351 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5352 }
Mark Mendell81489372015-11-04 11:30:41 -05005353 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005354 break;
5355 }
5356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005357 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005358 if (value.IsConstant()) {
5359 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5360 __ movl(Address(base, offset), Immediate(v));
5361 } else {
5362 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5363 }
Calin Juravle52c48962014-12-16 17:02:57 +00005364 break;
5365 }
5366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005367 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005368 if (value.IsConstant()) {
5369 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5370 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5371 codegen_->MaybeRecordImplicitNullCheck(instruction);
5372 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5373 maybe_record_implicit_null_check_done = true;
5374 } else {
5375 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5376 }
Calin Juravle52c48962014-12-16 17:02:57 +00005377 break;
5378 }
5379
Aart Bik66c158e2018-01-31 12:55:04 -08005380 case DataType::Type::kUint32:
5381 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005383 LOG(FATAL) << "Unreachable type " << field_type;
5384 UNREACHABLE();
5385 }
5386
Mark Mendell81489372015-11-04 11:30:41 -05005387 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005388 codegen_->MaybeRecordImplicitNullCheck(instruction);
5389 }
5390
Roland Levillain4d027112015-07-01 15:41:14 +01005391 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005392 Register temp = locations->GetTemp(0).AsRegister<Register>();
5393 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005394 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005395 }
5396
Calin Juravle52c48962014-12-16 17:02:57 +00005397 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005398 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005399 }
5400}
5401
5402void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5403 HandleFieldGet(instruction, instruction->GetFieldInfo());
5404}
5405
5406void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5407 HandleFieldGet(instruction, instruction->GetFieldInfo());
5408}
5409
5410void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5411 HandleFieldSet(instruction, instruction->GetFieldInfo());
5412}
5413
5414void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005415 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005416}
5417
5418void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5419 HandleFieldSet(instruction, instruction->GetFieldInfo());
5420}
5421
5422void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005423 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005424}
5425
5426void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5427 HandleFieldGet(instruction, instruction->GetFieldInfo());
5428}
5429
5430void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5431 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005432}
5433
Calin Juravlee460d1d2015-09-29 04:52:17 +01005434void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5435 HUnresolvedInstanceFieldGet* instruction) {
5436 FieldAccessCallingConventionX86 calling_convention;
5437 codegen_->CreateUnresolvedFieldLocationSummary(
5438 instruction, instruction->GetFieldType(), calling_convention);
5439}
5440
5441void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5442 HUnresolvedInstanceFieldGet* instruction) {
5443 FieldAccessCallingConventionX86 calling_convention;
5444 codegen_->GenerateUnresolvedFieldAccess(instruction,
5445 instruction->GetFieldType(),
5446 instruction->GetFieldIndex(),
5447 instruction->GetDexPc(),
5448 calling_convention);
5449}
5450
5451void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5452 HUnresolvedInstanceFieldSet* instruction) {
5453 FieldAccessCallingConventionX86 calling_convention;
5454 codegen_->CreateUnresolvedFieldLocationSummary(
5455 instruction, instruction->GetFieldType(), calling_convention);
5456}
5457
5458void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5459 HUnresolvedInstanceFieldSet* instruction) {
5460 FieldAccessCallingConventionX86 calling_convention;
5461 codegen_->GenerateUnresolvedFieldAccess(instruction,
5462 instruction->GetFieldType(),
5463 instruction->GetFieldIndex(),
5464 instruction->GetDexPc(),
5465 calling_convention);
5466}
5467
5468void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5469 HUnresolvedStaticFieldGet* instruction) {
5470 FieldAccessCallingConventionX86 calling_convention;
5471 codegen_->CreateUnresolvedFieldLocationSummary(
5472 instruction, instruction->GetFieldType(), calling_convention);
5473}
5474
5475void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5476 HUnresolvedStaticFieldGet* instruction) {
5477 FieldAccessCallingConventionX86 calling_convention;
5478 codegen_->GenerateUnresolvedFieldAccess(instruction,
5479 instruction->GetFieldType(),
5480 instruction->GetFieldIndex(),
5481 instruction->GetDexPc(),
5482 calling_convention);
5483}
5484
5485void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5486 HUnresolvedStaticFieldSet* instruction) {
5487 FieldAccessCallingConventionX86 calling_convention;
5488 codegen_->CreateUnresolvedFieldLocationSummary(
5489 instruction, instruction->GetFieldType(), calling_convention);
5490}
5491
5492void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5493 HUnresolvedStaticFieldSet* instruction) {
5494 FieldAccessCallingConventionX86 calling_convention;
5495 codegen_->GenerateUnresolvedFieldAccess(instruction,
5496 instruction->GetFieldType(),
5497 instruction->GetFieldIndex(),
5498 instruction->GetDexPc(),
5499 calling_convention);
5500}
5501
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005502void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005503 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5504 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5505 ? Location::RequiresRegister()
5506 : Location::Any();
5507 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005508}
5509
Calin Juravle2ae48182016-03-16 14:05:09 +00005510void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5511 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005512 return;
5513 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005514 LocationSummary* locations = instruction->GetLocations();
5515 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005516
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005517 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005518 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005519}
5520
Calin Juravle2ae48182016-03-16 14:05:09 +00005521void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005522 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005523 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005524
5525 LocationSummary* locations = instruction->GetLocations();
5526 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005527
5528 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005529 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005530 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005531 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005532 } else {
5533 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005534 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005535 __ jmp(slow_path->GetEntryLabel());
5536 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005537 }
5538 __ j(kEqual, slow_path->GetEntryLabel());
5539}
5540
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005541void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005542 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005543}
5544
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005546 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005548 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005549 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5550 object_array_get_with_read_barrier
5551 ? LocationSummary::kCallOnSlowPath
5552 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005553 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005554 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005555 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005556 locations->SetInAt(0, Location::RequiresRegister());
5557 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005558 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5560 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005561 // The output overlaps in case of long: we don't want the low move
5562 // to overwrite the array's location. Likewise, in the case of an
5563 // object array get with read barriers enabled, we do not want the
5564 // move to overwrite the array's location, as we need it to emit
5565 // the read barrier.
5566 locations->SetOut(
5567 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005568 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5569 ? Location::kOutputOverlap
5570 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005571 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005572}
5573
5574void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5575 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005576 Location obj_loc = locations->InAt(0);
5577 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005579 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005580 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005583 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005584 case DataType::Type::kBool:
5585 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005586 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005587 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005588 break;
5589 }
5590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005591 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005592 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005593 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 break;
5595 }
5596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005598 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005599 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5600 // Branch cases into compressed and uncompressed for each index's type.
5601 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5602 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005603 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005604 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005605 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5606 "Expecting 0=compressed, 1=uncompressed");
5607 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005608 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5609 __ jmp(&done);
5610 __ Bind(&not_compressed);
5611 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5612 __ Bind(&done);
5613 } else {
5614 // Common case for charAt of array of char or when string compression's
5615 // feature is turned off.
5616 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618 break;
5619 }
5620
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005621 case DataType::Type::kInt16: {
5622 Register out = out_loc.AsRegister<Register>();
5623 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5624 break;
5625 }
5626
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005627 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005628 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005629 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630 break;
5631 }
5632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005633 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005634 static_assert(
5635 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5636 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005637 // /* HeapReference<Object> */ out =
5638 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5639 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005640 // Note that a potential implicit null check is handled in this
5641 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5642 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005643 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005644 } else {
5645 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005646 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5647 codegen_->MaybeRecordImplicitNullCheck(instruction);
5648 // If read barriers are enabled, emit read barriers other than
5649 // Baker's using a slow path (and also unpoison the loaded
5650 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005651 if (index.IsConstant()) {
5652 uint32_t offset =
5653 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005654 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5655 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005656 codegen_->MaybeGenerateReadBarrierSlow(
5657 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5658 }
5659 }
5660 break;
5661 }
5662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005663 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005664 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005665 __ movl(out_loc.AsRegisterPairLow<Register>(),
5666 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5667 codegen_->MaybeRecordImplicitNullCheck(instruction);
5668 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5669 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005670 break;
5671 }
5672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005673 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005674 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005675 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005676 break;
5677 }
5678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005680 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005681 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005682 break;
5683 }
5684
Aart Bik66c158e2018-01-31 12:55:04 -08005685 case DataType::Type::kUint32:
5686 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005687 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005688 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005689 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005690 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005692 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005693 // Potential implicit null checks, in the case of reference or
5694 // long arrays, are handled in the previous switch statement.
5695 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005696 codegen_->MaybeRecordImplicitNullCheck(instruction);
5697 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005698}
5699
5700void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005701 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005702
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005703 bool needs_write_barrier =
5704 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005705 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005706
Vladimir Markoca6fff82017-10-03 14:49:14 +01005707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005708 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005709 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710 LocationSummary::kCallOnSlowPath :
5711 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005712
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005713 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005714 // We need the inputs to be different than the output in case of long operation.
5715 // In case of a byte operation, the register allocator does not support multiple
5716 // inputs that die at entry with one in a specific register.
5717 locations->SetInAt(0, Location::RequiresRegister());
5718 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5719 if (is_byte_type) {
5720 // Ensure the value is in a byte register.
5721 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005723 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005724 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005725 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5726 }
5727 if (needs_write_barrier) {
5728 // Temporary registers for the write barrier.
5729 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5730 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005731 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005732 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005733}
5734
5735void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5736 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 Location array_loc = locations->InAt(0);
5738 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005739 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005740 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005742 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5743 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5744 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005746 bool needs_write_barrier =
5747 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005748
5749 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005750 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005751 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005753 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005754 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005755 if (value.IsRegister()) {
5756 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005757 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005758 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005759 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005760 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005761 break;
5762 }
5763
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005764 case DataType::Type::kUint16:
5765 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005766 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005767 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005768 if (value.IsRegister()) {
5769 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005770 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005771 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005772 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005773 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005774 break;
5775 }
5776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005778 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005779 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005781 if (!value.IsRegister()) {
5782 // Just setting null.
5783 DCHECK(instruction->InputAt(2)->IsNullConstant());
5784 DCHECK(value.IsConstant()) << value;
5785 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005786 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005787 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005789 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005790 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005791
5792 DCHECK(needs_write_barrier);
5793 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005794 // We cannot use a NearLabel for `done`, as its range may be too
5795 // short when Baker read barriers are enabled.
5796 Label done;
5797 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005798 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005799 Location temp_loc = locations->GetTemp(0);
5800 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005802 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005803 codegen_->AddSlowPath(slow_path);
5804 if (instruction->GetValueCanBeNull()) {
5805 __ testl(register_value, register_value);
5806 __ j(kNotEqual, &not_null);
5807 __ movl(address, Immediate(0));
5808 codegen_->MaybeRecordImplicitNullCheck(instruction);
5809 __ jmp(&done);
5810 __ Bind(&not_null);
5811 }
5812
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005813 // Note that when Baker read barriers are enabled, the type
5814 // checks are performed without read barriers. This is fine,
5815 // even in the case where a class object is in the from-space
5816 // after the flip, as a comparison involving such a type would
5817 // not produce a false positive; it may of course produce a
5818 // false negative, in which case we would take the ArraySet
5819 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005820
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005821 // /* HeapReference<Class> */ temp = array->klass_
5822 __ movl(temp, Address(array, class_offset));
5823 codegen_->MaybeRecordImplicitNullCheck(instruction);
5824 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005825
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005826 // /* HeapReference<Class> */ temp = temp->component_type_
5827 __ movl(temp, Address(temp, component_offset));
5828 // If heap poisoning is enabled, no need to unpoison `temp`
5829 // nor the object reference in `register_value->klass`, as
5830 // we are comparing two poisoned references.
5831 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005832
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005833 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5834 __ j(kEqual, &do_put);
5835 // If heap poisoning is enabled, the `temp` reference has
5836 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837 __ MaybeUnpoisonHeapReference(temp);
5838
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005839 // If heap poisoning is enabled, no need to unpoison the
5840 // heap reference loaded below, as it is only used for a
5841 // comparison with null.
5842 __ cmpl(Address(temp, super_offset), Immediate(0));
5843 __ j(kNotEqual, slow_path->GetEntryLabel());
5844 __ Bind(&do_put);
5845 } else {
5846 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005847 }
5848 }
5849
5850 if (kPoisonHeapReferences) {
5851 __ movl(temp, register_value);
5852 __ PoisonHeapReference(temp);
5853 __ movl(address, temp);
5854 } else {
5855 __ movl(address, register_value);
5856 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005857 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005858 codegen_->MaybeRecordImplicitNullCheck(instruction);
5859 }
5860
5861 Register card = locations->GetTemp(1).AsRegister<Register>();
5862 codegen_->MarkGCCard(
5863 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5864 __ Bind(&done);
5865
5866 if (slow_path != nullptr) {
5867 __ Bind(slow_path->GetExitLabel());
5868 }
5869
5870 break;
5871 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005874 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005875 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005876 if (value.IsRegister()) {
5877 __ movl(address, value.AsRegister<Register>());
5878 } else {
5879 DCHECK(value.IsConstant()) << value;
5880 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5881 __ movl(address, Immediate(v));
5882 }
5883 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005884 break;
5885 }
5886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005887 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005888 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005889 if (value.IsRegisterPair()) {
5890 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5891 value.AsRegisterPairLow<Register>());
5892 codegen_->MaybeRecordImplicitNullCheck(instruction);
5893 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5894 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005895 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005896 DCHECK(value.IsConstant());
5897 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5898 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5899 Immediate(Low32Bits(val)));
5900 codegen_->MaybeRecordImplicitNullCheck(instruction);
5901 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5902 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005903 }
5904 break;
5905 }
5906
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005908 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005909 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005910 if (value.IsFpuRegister()) {
5911 __ movss(address, value.AsFpuRegister<XmmRegister>());
5912 } else {
5913 DCHECK(value.IsConstant());
5914 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5915 __ movl(address, Immediate(v));
5916 }
5917 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005918 break;
5919 }
5920
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005921 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005922 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005923 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005924 if (value.IsFpuRegister()) {
5925 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5926 } else {
5927 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005928 Address address_hi =
5929 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005930 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5931 __ movl(address, Immediate(Low32Bits(v)));
5932 codegen_->MaybeRecordImplicitNullCheck(instruction);
5933 __ movl(address_hi, Immediate(High32Bits(v)));
5934 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005935 break;
5936 }
5937
Aart Bik66c158e2018-01-31 12:55:04 -08005938 case DataType::Type::kUint32:
5939 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005941 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005942 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005943 }
5944}
5945
5946void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005947 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005948 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005949 if (!instruction->IsEmittedAtUseSite()) {
5950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5951 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005952}
5953
5954void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005955 if (instruction->IsEmittedAtUseSite()) {
5956 return;
5957 }
5958
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005959 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005960 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005961 Register obj = locations->InAt(0).AsRegister<Register>();
5962 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005963 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005964 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005965 // Mask out most significant bit in case the array is String's array of char.
5966 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005967 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005969}
5970
5971void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005972 RegisterSet caller_saves = RegisterSet::Empty();
5973 InvokeRuntimeCallingConvention calling_convention;
5974 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5975 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5976 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005977 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005978 HInstruction* length = instruction->InputAt(1);
5979 if (!length->IsEmittedAtUseSite()) {
5980 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5981 }
jessicahandojo4877b792016-09-08 19:49:13 -07005982 // Need register to see array's length.
5983 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5984 locations->AddTemp(Location::RequiresRegister());
5985 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005986}
5987
5988void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005989 const bool is_string_compressed_char_at =
5990 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005991 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005992 Location index_loc = locations->InAt(0);
5993 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005994 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005995 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005996
Mark Mendell99dbd682015-04-22 16:18:52 -04005997 if (length_loc.IsConstant()) {
5998 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5999 if (index_loc.IsConstant()) {
6000 // BCE will remove the bounds check if we are guarenteed to pass.
6001 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6002 if (index < 0 || index >= length) {
6003 codegen_->AddSlowPath(slow_path);
6004 __ jmp(slow_path->GetEntryLabel());
6005 } else {
6006 // Some optimization after BCE may have generated this, and we should not
6007 // generate a bounds check if it is a valid range.
6008 }
6009 return;
6010 }
6011
6012 // We have to reverse the jump condition because the length is the constant.
6013 Register index_reg = index_loc.AsRegister<Register>();
6014 __ cmpl(index_reg, Immediate(length));
6015 codegen_->AddSlowPath(slow_path);
6016 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006017 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006018 HInstruction* array_length = instruction->InputAt(1);
6019 if (array_length->IsEmittedAtUseSite()) {
6020 // Address the length field in the array.
6021 DCHECK(array_length->IsArrayLength());
6022 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6023 Location array_loc = array_length->GetLocations()->InAt(0);
6024 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006025 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006026 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6027 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006028 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6029 __ movl(length_reg, array_len);
6030 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006031 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006032 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006033 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006034 // Checking bounds for general case:
6035 // Array of char or string's array with feature compression off.
6036 if (index_loc.IsConstant()) {
6037 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6038 __ cmpl(array_len, Immediate(value));
6039 } else {
6040 __ cmpl(array_len, index_loc.AsRegister<Register>());
6041 }
6042 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006043 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006044 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006045 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006046 }
6047 codegen_->AddSlowPath(slow_path);
6048 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006049 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050}
6051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006052void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006053 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006054}
6055
6056void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006057 if (instruction->GetNext()->IsSuspendCheck() &&
6058 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6059 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6060 // The back edge will generate the suspend check.
6061 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6062 }
6063
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006064 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6065}
6066
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006067void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006068 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6069 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006070 // In suspend check slow path, usually there are no caller-save registers at all.
6071 // If SIMD instructions are present, however, we force spilling all live SIMD
6072 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006073 locations->SetCustomSlowPathCallerSaves(
6074 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006075}
6076
6077void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006078 HBasicBlock* block = instruction->GetBlock();
6079 if (block->GetLoopInformation() != nullptr) {
6080 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6081 // The back edge will generate the suspend check.
6082 return;
6083 }
6084 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6085 // The goto will generate the suspend check.
6086 return;
6087 }
6088 GenerateSuspendCheck(instruction, nullptr);
6089}
6090
6091void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6092 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006093 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006094 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6095 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006096 slow_path =
6097 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006098 instruction->SetSlowPath(slow_path);
6099 codegen_->AddSlowPath(slow_path);
6100 if (successor != nullptr) {
6101 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006102 }
6103 } else {
6104 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6105 }
6106
Andreas Gampe542451c2016-07-26 09:02:02 -07006107 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006108 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006109 if (successor == nullptr) {
6110 __ j(kNotEqual, slow_path->GetEntryLabel());
6111 __ Bind(slow_path->GetReturnLabel());
6112 } else {
6113 __ j(kEqual, codegen_->GetLabelOf(successor));
6114 __ jmp(slow_path->GetEntryLabel());
6115 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006116}
6117
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006118X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6119 return codegen_->GetAssembler();
6120}
6121
Aart Bikcfe50bb2017-12-12 14:54:12 -08006122void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006123 ScratchRegisterScope ensure_scratch(
6124 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6125 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6126 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006127
Aart Bikcfe50bb2017-12-12 14:54:12 -08006128 // Now that temp register is available (possibly spilled), move blocks of memory.
6129 for (int i = 0; i < number_of_words; i++) {
6130 __ movl(temp_reg, Address(ESP, src + stack_offset));
6131 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6132 stack_offset += kX86WordSize;
6133 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006134}
6135
6136void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006137 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006138 Location source = move->GetSource();
6139 Location destination = move->GetDestination();
6140
6141 if (source.IsRegister()) {
6142 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006143 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006144 } else if (destination.IsFpuRegister()) {
6145 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006146 } else {
6147 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006148 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006149 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006150 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006152 // Create stack space for 2 elements.
6153 __ subl(ESP, Immediate(2 * elem_size));
6154 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6155 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6156 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6157 // And remove the temporary stack space we allocated.
6158 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006159 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006160 if (destination.IsRegister()) {
6161 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6162 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006163 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006164 } else if (destination.IsRegisterPair()) {
6165 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6166 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6167 __ psrlq(src_reg, Immediate(32));
6168 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006169 } else if (destination.IsStackSlot()) {
6170 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006171 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006172 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006173 } else {
6174 DCHECK(destination.IsSIMDStackSlot());
6175 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006176 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006177 } else if (source.IsStackSlot()) {
6178 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006179 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006180 } else if (destination.IsFpuRegister()) {
6181 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006182 } else {
6183 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006184 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006185 }
6186 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006187 if (destination.IsRegisterPair()) {
6188 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6189 __ movl(destination.AsRegisterPairHigh<Register>(),
6190 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6191 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006192 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6193 } else {
6194 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006195 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006196 }
Aart Bik5576f372017-03-23 16:17:37 -07006197 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006198 if (destination.IsFpuRegister()) {
6199 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6200 } else {
6201 DCHECK(destination.IsSIMDStackSlot());
6202 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6203 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006204 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006205 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006206 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006207 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006208 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006209 if (value == 0) {
6210 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6211 } else {
6212 __ movl(destination.AsRegister<Register>(), Immediate(value));
6213 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006214 } else {
6215 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006216 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006217 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006218 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006219 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006220 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006221 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006222 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006223 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6224 if (value == 0) {
6225 // Easy handling of 0.0.
6226 __ xorps(dest, dest);
6227 } else {
6228 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006229 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6230 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6231 __ movl(temp, Immediate(value));
6232 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006233 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006234 } else {
6235 DCHECK(destination.IsStackSlot()) << destination;
6236 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6237 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006238 } else if (constant->IsLongConstant()) {
6239 int64_t value = constant->AsLongConstant()->GetValue();
6240 int32_t low_value = Low32Bits(value);
6241 int32_t high_value = High32Bits(value);
6242 Immediate low(low_value);
6243 Immediate high(high_value);
6244 if (destination.IsDoubleStackSlot()) {
6245 __ movl(Address(ESP, destination.GetStackIndex()), low);
6246 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6247 } else {
6248 __ movl(destination.AsRegisterPairLow<Register>(), low);
6249 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6250 }
6251 } else {
6252 DCHECK(constant->IsDoubleConstant());
6253 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006254 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006255 int32_t low_value = Low32Bits(value);
6256 int32_t high_value = High32Bits(value);
6257 Immediate low(low_value);
6258 Immediate high(high_value);
6259 if (destination.IsFpuRegister()) {
6260 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6261 if (value == 0) {
6262 // Easy handling of 0.0.
6263 __ xorpd(dest, dest);
6264 } else {
6265 __ pushl(high);
6266 __ pushl(low);
6267 __ movsd(dest, Address(ESP, 0));
6268 __ addl(ESP, Immediate(8));
6269 }
6270 } else {
6271 DCHECK(destination.IsDoubleStackSlot()) << destination;
6272 __ movl(Address(ESP, destination.GetStackIndex()), low);
6273 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6274 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006275 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006276 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006277 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006278 }
6279}
6280
Mark Mendella5c19ce2015-04-01 12:51:05 -04006281void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006282 Register suggested_scratch = reg == EAX ? EBX : EAX;
6283 ScratchRegisterScope ensure_scratch(
6284 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6285
6286 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6287 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6288 __ movl(Address(ESP, mem + stack_offset), reg);
6289 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006290}
6291
Mark Mendell7c8d0092015-01-26 11:21:33 -05006292void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006293 ScratchRegisterScope ensure_scratch(
6294 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6295
6296 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6297 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6298 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6299 __ movss(Address(ESP, mem + stack_offset), reg);
6300 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006301}
6302
Aart Bikcfe50bb2017-12-12 14:54:12 -08006303void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6304 size_t extra_slot = 4 * kX86WordSize;
6305 __ subl(ESP, Immediate(extra_slot));
6306 __ movups(Address(ESP, 0), XmmRegister(reg));
6307 ExchangeMemory(0, mem + extra_slot, 4);
6308 __ movups(XmmRegister(reg), Address(ESP, 0));
6309 __ addl(ESP, Immediate(extra_slot));
6310}
6311
6312void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006313 ScratchRegisterScope ensure_scratch1(
6314 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006315
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006316 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6317 ScratchRegisterScope ensure_scratch2(
6318 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006319
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006320 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6321 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006322
6323 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6324 for (int i = 0; i < number_of_words; i++) {
6325 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6326 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6327 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6328 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6329 stack_offset += kX86WordSize;
6330 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006331}
6332
6333void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006334 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006335 Location source = move->GetSource();
6336 Location destination = move->GetDestination();
6337
6338 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006339 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6340 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6341 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6342 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6343 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006344 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006345 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006346 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006347 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006348 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006349 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006350 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6351 // Use XOR Swap algorithm to avoid a temporary.
6352 DCHECK_NE(source.reg(), destination.reg());
6353 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6354 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6355 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6356 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6357 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6358 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6359 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006360 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6361 // Take advantage of the 16 bytes in the XMM register.
6362 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6363 Address stack(ESP, destination.GetStackIndex());
6364 // Load the double into the high doubleword.
6365 __ movhpd(reg, stack);
6366
6367 // Store the low double into the destination.
6368 __ movsd(stack, reg);
6369
6370 // Move the high double to the low double.
6371 __ psrldq(reg, Immediate(8));
6372 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6373 // Take advantage of the 16 bytes in the XMM register.
6374 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6375 Address stack(ESP, source.GetStackIndex());
6376 // Load the double into the high doubleword.
6377 __ movhpd(reg, stack);
6378
6379 // Store the low double into the destination.
6380 __ movsd(stack, reg);
6381
6382 // Move the high double to the low double.
6383 __ psrldq(reg, Immediate(8));
6384 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006385 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6386 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6387 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6388 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6389 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6390 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6391 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006392 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006393 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006394 }
6395}
6396
6397void ParallelMoveResolverX86::SpillScratch(int reg) {
6398 __ pushl(static_cast<Register>(reg));
6399}
6400
6401void ParallelMoveResolverX86::RestoreScratch(int reg) {
6402 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006403}
6404
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006405HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6406 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006407 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006408 case HLoadClass::LoadKind::kInvalid:
6409 LOG(FATAL) << "UNREACHABLE";
6410 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411 case HLoadClass::LoadKind::kReferrersClass:
6412 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006413 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006414 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006415 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006416 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006417 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006418 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006419 DCHECK(Runtime::Current()->UseJitCompilation());
6420 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006421 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006422 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 break;
6424 }
6425 return desired_class_load_kind;
6426}
6427
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006428void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006429 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006430 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006431 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006432 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006433 cls,
6434 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006435 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006436 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006437 return;
6438 }
Vladimir Marko41559982017-01-06 14:04:23 +00006439 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006440
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006441 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6442 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 ? LocationSummary::kCallOnSlowPath
6444 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006445 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006446 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006447 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006448 }
6449
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006450 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006451 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006452 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006453 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006454 locations->SetInAt(0, Location::RequiresRegister());
6455 }
6456 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006457 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6458 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6459 // Rely on the type resolution and/or initialization to save everything.
6460 RegisterSet caller_saves = RegisterSet::Empty();
6461 InvokeRuntimeCallingConvention calling_convention;
6462 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6463 locations->SetCustomSlowPathCallerSaves(caller_saves);
6464 } else {
6465 // For non-Baker read barrier we have a temp-clobbering call.
6466 }
6467 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006468}
6469
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006470Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006471 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006472 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006473 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006474 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006475 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006476 PatchInfo<Label>* info = &jit_class_patches_.back();
6477 return &info->label;
6478}
6479
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006480// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6481// move.
6482void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006483 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006484 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006485 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006486 return;
6487 }
Vladimir Marko41559982017-01-06 14:04:23 +00006488 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006489
Vladimir Marko41559982017-01-06 14:04:23 +00006490 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 Location out_loc = locations->Out();
6492 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006493
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006494 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006495 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6496 ? kWithoutReadBarrier
6497 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006498 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006499 case HLoadClass::LoadKind::kReferrersClass: {
6500 DCHECK(!cls->CanCallRuntime());
6501 DCHECK(!cls->MustGenerateClinitCheck());
6502 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6503 Register current_method = locations->InAt(0).AsRegister<Register>();
6504 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006505 cls,
6506 out_loc,
6507 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006508 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006509 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006510 break;
6511 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006512 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006513 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006514 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006515 Register method_address = locations->InAt(0).AsRegister<Register>();
6516 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006517 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006518 break;
6519 }
6520 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006521 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006522 uint32_t address = dchecked_integral_cast<uint32_t>(
6523 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6524 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006525 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006526 break;
6527 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006528 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006529 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6530 Register method_address = locations->InAt(0).AsRegister<Register>();
6531 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006532 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6533 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006534 break;
6535 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006536 case HLoadClass::LoadKind::kBssEntry: {
6537 Register method_address = locations->InAt(0).AsRegister<Register>();
6538 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6539 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6540 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6541 generate_null_check = true;
6542 break;
6543 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006544 case HLoadClass::LoadKind::kJitTableAddress: {
6545 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6546 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006547 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006548 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006549 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006550 break;
6551 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006552 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006553 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006554 LOG(FATAL) << "UNREACHABLE";
6555 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006556 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006557
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006558 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6559 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006560 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006561 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6562 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006563
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006564 if (generate_null_check) {
6565 __ testl(out, out);
6566 __ j(kEqual, slow_path->GetEntryLabel());
6567 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006568
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006569 if (cls->MustGenerateClinitCheck()) {
6570 GenerateClassInitializationCheck(slow_path, out);
6571 } else {
6572 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006573 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006574 }
6575}
6576
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006577void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6578 InvokeRuntimeCallingConvention calling_convention;
6579 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6580 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6581}
6582
6583void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6584 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6585}
6586
Orion Hodson18259d72018-04-12 11:18:23 +01006587void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6588 InvokeRuntimeCallingConvention calling_convention;
6589 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6590 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6591}
6592
6593void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6594 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6595}
6596
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006597void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6598 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006599 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006600 locations->SetInAt(0, Location::RequiresRegister());
6601 if (check->HasUses()) {
6602 locations->SetOut(Location::SameAsFirstInput());
6603 }
6604}
6605
6606void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006607 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006608 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006609 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006610 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006611 GenerateClassInitializationCheck(slow_path,
6612 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006613}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006614
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006615void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006616 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006617 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6618 const size_t status_byte_offset =
6619 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6620 constexpr uint32_t shifted_initialized_value =
6621 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6622
6623 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006624 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006625 __ Bind(slow_path->GetExitLabel());
6626 // No need for memory fence, thanks to the X86 memory model.
6627}
6628
Vladimir Marko175e7862018-03-27 09:03:13 +00006629void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6630 Register temp) {
6631 uint32_t path_to_root = check->GetBitstringPathToRoot();
6632 uint32_t mask = check->GetBitstringMask();
6633 DCHECK(IsPowerOfTwo(mask + 1));
6634 size_t mask_bits = WhichPowerOf2(mask + 1);
6635
6636 if (mask_bits == 16u) {
6637 // Compare the bitstring in memory.
6638 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6639 } else {
6640 // /* uint32_t */ temp = temp->status_
6641 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6642 // Compare the bitstring bits using SUB.
6643 __ subl(temp, Immediate(path_to_root));
6644 // Shift out bits that do not contribute to the comparison.
6645 __ shll(temp, Immediate(32u - mask_bits));
6646 }
6647}
6648
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006649HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6650 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006651 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006652 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006653 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006654 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006655 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006656 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006657 case HLoadString::LoadKind::kJitTableAddress:
6658 DCHECK(Runtime::Current()->UseJitCompilation());
6659 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006660 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006661 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006662 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006663 }
6664 return desired_string_load_kind;
6665}
6666
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006667void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006668 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006670 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006671 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006672 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006673 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006674 locations->SetInAt(0, Location::RequiresRegister());
6675 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006676 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006677 locations->SetOut(Location::RegisterLocation(EAX));
6678 } else {
6679 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006680 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6681 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006682 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006683 RegisterSet caller_saves = RegisterSet::Empty();
6684 InvokeRuntimeCallingConvention calling_convention;
6685 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6686 locations->SetCustomSlowPathCallerSaves(caller_saves);
6687 } else {
6688 // For non-Baker read barrier we have a temp-clobbering call.
6689 }
6690 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006691 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006692}
6693
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006694Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006695 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006696 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006697 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006698 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006699 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006700 PatchInfo<Label>* info = &jit_string_patches_.back();
6701 return &info->label;
6702}
6703
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006704// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6705// move.
6706void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006707 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708 Location out_loc = locations->Out();
6709 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006711 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006712 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006713 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006714 Register method_address = locations->InAt(0).AsRegister<Register>();
6715 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006716 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006717 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006718 }
6719 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006720 uint32_t address = dchecked_integral_cast<uint32_t>(
6721 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6722 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006723 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006724 return;
6725 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006726 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006727 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6728 Register method_address = locations->InAt(0).AsRegister<Register>();
6729 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006730 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6731 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006732 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006733 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006734 case HLoadString::LoadKind::kBssEntry: {
6735 Register method_address = locations->InAt(0).AsRegister<Register>();
6736 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6737 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006738 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006739 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006740 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006741 codegen_->AddSlowPath(slow_path);
6742 __ testl(out, out);
6743 __ j(kEqual, slow_path->GetEntryLabel());
6744 __ Bind(slow_path->GetExitLabel());
6745 return;
6746 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006747 case HLoadString::LoadKind::kJitTableAddress: {
6748 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6749 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006750 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006751 // /* GcRoot<mirror::String> */ out = *address
6752 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6753 return;
6754 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006755 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006756 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006757 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006758
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006759 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006760 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006761 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006762 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006763 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6764 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006765}
6766
David Brazdilcb1c0552015-08-04 16:22:25 +01006767static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006768 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006769}
6770
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006771void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6772 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006773 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006774 locations->SetOut(Location::RequiresRegister());
6775}
6776
6777void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006778 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6779}
6780
6781void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006782 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006783}
6784
6785void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6786 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006787}
6788
6789void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006790 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6791 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006792 InvokeRuntimeCallingConvention calling_convention;
6793 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6794}
6795
6796void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006797 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006798 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006799}
6800
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006801// Temp is used for read barrier.
6802static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6803 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006804 !kUseBakerReadBarrier &&
6805 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006806 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006807 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6808 return 1;
6809 }
6810 return 0;
6811}
6812
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006813// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6814// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006815// The other checks have one temp for loading the object's class.
6816static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006817 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006818 return 2;
6819 }
6820 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006821}
6822
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006823void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006824 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006825 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006826 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006827 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006828 case TypeCheckKind::kExactCheck:
6829 case TypeCheckKind::kAbstractClassCheck:
6830 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006831 case TypeCheckKind::kArrayObjectCheck: {
6832 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6833 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6834 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006835 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006836 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006837 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006838 case TypeCheckKind::kUnresolvedCheck:
6839 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006840 call_kind = LocationSummary::kCallOnSlowPath;
6841 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006842 case TypeCheckKind::kBitstringCheck:
6843 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006844 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006845
Vladimir Markoca6fff82017-10-03 14:49:14 +01006846 LocationSummary* locations =
6847 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006848 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006849 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006850 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006851 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006852 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6853 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6854 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6855 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6856 } else {
6857 locations->SetInAt(1, Location::Any());
6858 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006859 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6860 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006861 // When read barriers are enabled, we need a temporary register for some cases.
6862 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006863}
6864
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006865void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006866 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006867 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006868 Location obj_loc = locations->InAt(0);
6869 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006870 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006871 Location out_loc = locations->Out();
6872 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006873 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6874 DCHECK_LE(num_temps, 1u);
6875 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006876 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006877 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6878 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6879 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006880 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006881 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006882
6883 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006884 // Avoid null check if we know obj is not null.
6885 if (instruction->MustDoNullCheck()) {
6886 __ testl(obj, obj);
6887 __ j(kEqual, &zero);
6888 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006889
Roland Levillain7c1559a2015-12-15 10:55:36 +00006890 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006891 case TypeCheckKind::kExactCheck: {
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 Geoffray85c7bab2015-09-18 13:40:46 +00006900 if (cls.IsRegister()) {
6901 __ cmpl(out, cls.AsRegister<Register>());
6902 } else {
6903 DCHECK(cls.IsStackSlot()) << cls;
6904 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6905 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006906
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006907 // Classes must be equal for the instanceof to succeed.
6908 __ j(kNotEqual, &zero);
6909 __ movl(out, Immediate(1));
6910 __ jmp(&done);
6911 break;
6912 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006913
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006914 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006915 ReadBarrierOption read_barrier_option =
6916 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006917 // /* HeapReference<Class> */ out = obj->klass_
6918 GenerateReferenceLoadTwoRegisters(instruction,
6919 out_loc,
6920 obj_loc,
6921 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006922 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006923 // If the class is abstract, we eagerly fetch the super class of the
6924 // object to avoid doing a comparison we know will fail.
6925 NearLabel loop;
6926 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006927 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006928 GenerateReferenceLoadOneRegister(instruction,
6929 out_loc,
6930 super_offset,
6931 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006932 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006933 __ testl(out, out);
6934 // If `out` is null, we use it for the result, and jump to `done`.
6935 __ j(kEqual, &done);
6936 if (cls.IsRegister()) {
6937 __ cmpl(out, cls.AsRegister<Register>());
6938 } else {
6939 DCHECK(cls.IsStackSlot()) << cls;
6940 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6941 }
6942 __ j(kNotEqual, &loop);
6943 __ movl(out, Immediate(1));
6944 if (zero.IsLinked()) {
6945 __ jmp(&done);
6946 }
6947 break;
6948 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006949
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006950 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006951 ReadBarrierOption read_barrier_option =
6952 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006953 // /* HeapReference<Class> */ out = obj->klass_
6954 GenerateReferenceLoadTwoRegisters(instruction,
6955 out_loc,
6956 obj_loc,
6957 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006958 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006959 // Walk over the class hierarchy to find a match.
6960 NearLabel loop, success;
6961 __ Bind(&loop);
6962 if (cls.IsRegister()) {
6963 __ cmpl(out, cls.AsRegister<Register>());
6964 } else {
6965 DCHECK(cls.IsStackSlot()) << cls;
6966 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6967 }
6968 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006969 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006970 GenerateReferenceLoadOneRegister(instruction,
6971 out_loc,
6972 super_offset,
6973 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006974 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006975 __ testl(out, out);
6976 __ j(kNotEqual, &loop);
6977 // If `out` is null, we use it for the result, and jump to `done`.
6978 __ jmp(&done);
6979 __ Bind(&success);
6980 __ movl(out, Immediate(1));
6981 if (zero.IsLinked()) {
6982 __ jmp(&done);
6983 }
6984 break;
6985 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006987 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006988 ReadBarrierOption read_barrier_option =
6989 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006990 // /* HeapReference<Class> */ out = obj->klass_
6991 GenerateReferenceLoadTwoRegisters(instruction,
6992 out_loc,
6993 obj_loc,
6994 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006995 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006996 // Do an exact check.
6997 NearLabel exact_check;
6998 if (cls.IsRegister()) {
6999 __ cmpl(out, cls.AsRegister<Register>());
7000 } else {
7001 DCHECK(cls.IsStackSlot()) << cls;
7002 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7003 }
7004 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007005 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007006 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007007 GenerateReferenceLoadOneRegister(instruction,
7008 out_loc,
7009 component_offset,
7010 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007011 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007012 __ testl(out, out);
7013 // If `out` is null, we use it for the result, and jump to `done`.
7014 __ j(kEqual, &done);
7015 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7016 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007017 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007018 __ movl(out, Immediate(1));
7019 __ jmp(&done);
7020 break;
7021 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007022
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007023 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007024 // No read barrier since the slow path will retry upon failure.
7025 // /* HeapReference<Class> */ out = obj->klass_
7026 GenerateReferenceLoadTwoRegisters(instruction,
7027 out_loc,
7028 obj_loc,
7029 class_offset,
7030 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007031 if (cls.IsRegister()) {
7032 __ cmpl(out, cls.AsRegister<Register>());
7033 } else {
7034 DCHECK(cls.IsStackSlot()) << cls;
7035 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7036 }
7037 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007038 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7039 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007040 codegen_->AddSlowPath(slow_path);
7041 __ j(kNotEqual, slow_path->GetEntryLabel());
7042 __ movl(out, Immediate(1));
7043 if (zero.IsLinked()) {
7044 __ jmp(&done);
7045 }
7046 break;
7047 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007048
Calin Juravle98893e12015-10-02 21:05:03 +01007049 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007050 case TypeCheckKind::kInterfaceCheck: {
7051 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007052 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007053 // cases.
7054 //
7055 // We cannot directly call the InstanceofNonTrivial runtime
7056 // entry point without resorting to a type checking slow path
7057 // here (i.e. by calling InvokeRuntime directly), as it would
7058 // require to assign fixed registers for the inputs of this
7059 // HInstanceOf instruction (following the runtime calling
7060 // convention), which might be cluttered by the potential first
7061 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007062 //
7063 // TODO: Introduce a new runtime entry point taking the object
7064 // to test (instead of its class) as argument, and let it deal
7065 // with the read barrier issues. This will let us refactor this
7066 // case of the `switch` code as it was previously (with a direct
7067 // call to the runtime not using a type checking slow path).
7068 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007069 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007070 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7071 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007072 codegen_->AddSlowPath(slow_path);
7073 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007074 if (zero.IsLinked()) {
7075 __ jmp(&done);
7076 }
7077 break;
7078 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007079
7080 case TypeCheckKind::kBitstringCheck: {
7081 // /* HeapReference<Class> */ temp = obj->klass_
7082 GenerateReferenceLoadTwoRegisters(instruction,
7083 out_loc,
7084 obj_loc,
7085 class_offset,
7086 kWithoutReadBarrier);
7087
7088 GenerateBitstringTypeCheckCompare(instruction, out);
7089 __ j(kNotEqual, &zero);
7090 __ movl(out, Immediate(1));
7091 __ jmp(&done);
7092 break;
7093 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007094 }
7095
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007096 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007097 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007098 __ xorl(out, out);
7099 }
7100
7101 if (done.IsLinked()) {
7102 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007103 }
7104
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007105 if (slow_path != nullptr) {
7106 __ Bind(slow_path->GetExitLabel());
7107 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007108}
7109
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007110void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007111 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007112 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007113 LocationSummary* locations =
7114 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007115 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007116 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7117 // Require a register for the interface check since there is a loop that compares the class to
7118 // a memory address.
7119 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007120 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7121 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7122 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7123 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007124 } else {
7125 locations->SetInAt(1, Location::Any());
7126 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007127 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007128 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7129}
7130
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007131void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007133 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007134 Location obj_loc = locations->InAt(0);
7135 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007136 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007137 Location temp_loc = locations->GetTemp(0);
7138 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007139 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7140 DCHECK_GE(num_temps, 1u);
7141 DCHECK_LE(num_temps, 2u);
7142 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7143 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7144 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7145 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7146 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7147 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7148 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7149 const uint32_t object_array_data_offset =
7150 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007151
Vladimir Marko87584542017-12-12 17:47:52 +00007152 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007153 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007154 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7155 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007156 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007157
Roland Levillain0d5a2812015-11-13 10:07:31 +00007158 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007159 // Avoid null check if we know obj is not null.
7160 if (instruction->MustDoNullCheck()) {
7161 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007162 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007163 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007164
Roland Levillain0d5a2812015-11-13 10:07:31 +00007165 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007166 case TypeCheckKind::kExactCheck:
7167 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007168 // /* HeapReference<Class> */ temp = obj->klass_
7169 GenerateReferenceLoadTwoRegisters(instruction,
7170 temp_loc,
7171 obj_loc,
7172 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007173 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007174
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007175 if (cls.IsRegister()) {
7176 __ cmpl(temp, cls.AsRegister<Register>());
7177 } else {
7178 DCHECK(cls.IsStackSlot()) << cls;
7179 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7180 }
7181 // Jump to slow path for throwing the exception or doing a
7182 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007183 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007184 break;
7185 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007187 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007188 // /* HeapReference<Class> */ temp = obj->klass_
7189 GenerateReferenceLoadTwoRegisters(instruction,
7190 temp_loc,
7191 obj_loc,
7192 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007193 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007195 // If the class is abstract, we eagerly fetch the super class of the
7196 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007197 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007198 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007199 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007200 GenerateReferenceLoadOneRegister(instruction,
7201 temp_loc,
7202 super_offset,
7203 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007204 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007205
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007206 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7207 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007208 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007209 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007210
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007211 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007212 if (cls.IsRegister()) {
7213 __ cmpl(temp, cls.AsRegister<Register>());
7214 } else {
7215 DCHECK(cls.IsStackSlot()) << cls;
7216 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7217 }
7218 __ j(kNotEqual, &loop);
7219 break;
7220 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007222 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007223 // /* HeapReference<Class> */ temp = obj->klass_
7224 GenerateReferenceLoadTwoRegisters(instruction,
7225 temp_loc,
7226 obj_loc,
7227 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007228 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007230 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007231 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 __ Bind(&loop);
7233 if (cls.IsRegister()) {
7234 __ cmpl(temp, cls.AsRegister<Register>());
7235 } else {
7236 DCHECK(cls.IsStackSlot()) << cls;
7237 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7238 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007239 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007240
Roland Levillain0d5a2812015-11-13 10:07:31 +00007241 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007242 GenerateReferenceLoadOneRegister(instruction,
7243 temp_loc,
7244 super_offset,
7245 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007246 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007247
7248 // If the class reference currently in `temp` is not null, jump
7249 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007250 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007251 __ j(kNotZero, &loop);
7252 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007253 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007254 break;
7255 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007256
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007257 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007258 // /* HeapReference<Class> */ temp = obj->klass_
7259 GenerateReferenceLoadTwoRegisters(instruction,
7260 temp_loc,
7261 obj_loc,
7262 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007263 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007264
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007265 // Do an exact check.
7266 if (cls.IsRegister()) {
7267 __ cmpl(temp, cls.AsRegister<Register>());
7268 } else {
7269 DCHECK(cls.IsStackSlot()) << cls;
7270 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7271 }
7272 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007273
7274 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007275 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007276 GenerateReferenceLoadOneRegister(instruction,
7277 temp_loc,
7278 component_offset,
7279 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007280 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007281
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007282 // If the component type is null (i.e. the object not an array), jump to the slow path to
7283 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007284 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007285 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007286
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007287 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007288 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007289 break;
7290 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007291
Calin Juravle98893e12015-10-02 21:05:03 +01007292 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007293 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007294 // We cannot directly call the CheckCast runtime entry point
7295 // without resorting to a type checking slow path here (i.e. by
7296 // calling InvokeRuntime directly), as it would require to
7297 // assign fixed registers for the inputs of this HInstanceOf
7298 // instruction (following the runtime calling convention), which
7299 // might be cluttered by the potential first read barrier
7300 // emission at the beginning of this method.
7301 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007302 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007303
7304 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007305 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7306 // We can not get false positives by doing this.
7307 // /* HeapReference<Class> */ temp = obj->klass_
7308 GenerateReferenceLoadTwoRegisters(instruction,
7309 temp_loc,
7310 obj_loc,
7311 class_offset,
7312 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007313
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007314 // /* HeapReference<Class> */ temp = temp->iftable_
7315 GenerateReferenceLoadTwoRegisters(instruction,
7316 temp_loc,
7317 temp_loc,
7318 iftable_offset,
7319 kWithoutReadBarrier);
7320 // Iftable is never null.
7321 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7322 // Maybe poison the `cls` for direct comparison with memory.
7323 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7324 // Loop through the iftable and check if any class matches.
7325 NearLabel start_loop;
7326 __ Bind(&start_loop);
7327 // Need to subtract first to handle the empty array case.
7328 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7329 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7330 // Go to next interface if the classes do not match.
7331 __ cmpl(cls.AsRegister<Register>(),
7332 CodeGeneratorX86::ArrayAddress(temp,
7333 maybe_temp2_loc,
7334 TIMES_4,
7335 object_array_data_offset));
7336 __ j(kNotEqual, &start_loop);
7337 // If `cls` was poisoned above, unpoison it.
7338 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007339 break;
7340 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007341
7342 case TypeCheckKind::kBitstringCheck: {
7343 // /* HeapReference<Class> */ temp = obj->klass_
7344 GenerateReferenceLoadTwoRegisters(instruction,
7345 temp_loc,
7346 obj_loc,
7347 class_offset,
7348 kWithoutReadBarrier);
7349
7350 GenerateBitstringTypeCheckCompare(instruction, temp);
7351 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7352 break;
7353 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007354 }
7355 __ Bind(&done);
7356
Roland Levillain0d5a2812015-11-13 10:07:31 +00007357 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007358}
7359
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007360void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007361 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7362 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007363 InvokeRuntimeCallingConvention calling_convention;
7364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7365}
7366
7367void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007368 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7369 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007370 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007371 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007372 if (instruction->IsEnter()) {
7373 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7374 } else {
7375 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7376 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007377}
7378
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007379void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7380void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7381void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7382
7383void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7384 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007385 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007386 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7387 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007388 locations->SetInAt(0, Location::RequiresRegister());
7389 locations->SetInAt(1, Location::Any());
7390 locations->SetOut(Location::SameAsFirstInput());
7391}
7392
7393void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7394 HandleBitwiseOperation(instruction);
7395}
7396
7397void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7398 HandleBitwiseOperation(instruction);
7399}
7400
7401void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7402 HandleBitwiseOperation(instruction);
7403}
7404
7405void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7406 LocationSummary* locations = instruction->GetLocations();
7407 Location first = locations->InAt(0);
7408 Location second = locations->InAt(1);
7409 DCHECK(first.Equals(locations->Out()));
7410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007411 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007412 if (second.IsRegister()) {
7413 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007414 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007415 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007416 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007417 } else {
7418 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007419 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007420 }
7421 } else if (second.IsConstant()) {
7422 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007423 __ andl(first.AsRegister<Register>(),
7424 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007425 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007426 __ orl(first.AsRegister<Register>(),
7427 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007428 } else {
7429 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007430 __ xorl(first.AsRegister<Register>(),
7431 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007432 }
7433 } else {
7434 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007435 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007436 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007437 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007438 } else {
7439 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007440 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007441 }
7442 }
7443 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007444 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007445 if (second.IsRegisterPair()) {
7446 if (instruction->IsAnd()) {
7447 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7448 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7449 } else if (instruction->IsOr()) {
7450 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7451 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7452 } else {
7453 DCHECK(instruction->IsXor());
7454 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7455 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7456 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007457 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007458 if (instruction->IsAnd()) {
7459 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7460 __ andl(first.AsRegisterPairHigh<Register>(),
7461 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7462 } else if (instruction->IsOr()) {
7463 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7464 __ orl(first.AsRegisterPairHigh<Register>(),
7465 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7466 } else {
7467 DCHECK(instruction->IsXor());
7468 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7469 __ xorl(first.AsRegisterPairHigh<Register>(),
7470 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7471 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007472 } else {
7473 DCHECK(second.IsConstant()) << second;
7474 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007475 int32_t low_value = Low32Bits(value);
7476 int32_t high_value = High32Bits(value);
7477 Immediate low(low_value);
7478 Immediate high(high_value);
7479 Register first_low = first.AsRegisterPairLow<Register>();
7480 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007481 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007482 if (low_value == 0) {
7483 __ xorl(first_low, first_low);
7484 } else if (low_value != -1) {
7485 __ andl(first_low, low);
7486 }
7487 if (high_value == 0) {
7488 __ xorl(first_high, first_high);
7489 } else if (high_value != -1) {
7490 __ andl(first_high, high);
7491 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007492 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007493 if (low_value != 0) {
7494 __ orl(first_low, low);
7495 }
7496 if (high_value != 0) {
7497 __ orl(first_high, high);
7498 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007499 } else {
7500 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007501 if (low_value != 0) {
7502 __ xorl(first_low, low);
7503 }
7504 if (high_value != 0) {
7505 __ xorl(first_high, high);
7506 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007507 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007508 }
7509 }
7510}
7511
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007512void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7513 HInstruction* instruction,
7514 Location out,
7515 uint32_t offset,
7516 Location maybe_temp,
7517 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007518 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007519 if (read_barrier_option == kWithReadBarrier) {
7520 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007521 if (kUseBakerReadBarrier) {
7522 // Load with fast path based Baker's read barrier.
7523 // /* HeapReference<Object> */ out = *(out + offset)
7524 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007525 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007526 } else {
7527 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007528 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007529 // in the following move operation, as we will need it for the
7530 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007531 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007532 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007533 // /* HeapReference<Object> */ out = *(out + offset)
7534 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007535 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007536 }
7537 } else {
7538 // Plain load with no read barrier.
7539 // /* HeapReference<Object> */ out = *(out + offset)
7540 __ movl(out_reg, Address(out_reg, offset));
7541 __ MaybeUnpoisonHeapReference(out_reg);
7542 }
7543}
7544
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007545void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7546 HInstruction* instruction,
7547 Location out,
7548 Location obj,
7549 uint32_t offset,
7550 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007551 Register out_reg = out.AsRegister<Register>();
7552 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007553 if (read_barrier_option == kWithReadBarrier) {
7554 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007555 if (kUseBakerReadBarrier) {
7556 // Load with fast path based Baker's read barrier.
7557 // /* HeapReference<Object> */ out = *(obj + offset)
7558 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007559 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007560 } else {
7561 // Load with slow path based read barrier.
7562 // /* HeapReference<Object> */ out = *(obj + offset)
7563 __ movl(out_reg, Address(obj_reg, offset));
7564 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7565 }
7566 } else {
7567 // Plain load with no read barrier.
7568 // /* HeapReference<Object> */ out = *(obj + offset)
7569 __ movl(out_reg, Address(obj_reg, offset));
7570 __ MaybeUnpoisonHeapReference(out_reg);
7571 }
7572}
7573
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007574void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7575 HInstruction* instruction,
7576 Location root,
7577 const Address& address,
7578 Label* fixup_label,
7579 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007580 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007581 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007582 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007583 if (kUseBakerReadBarrier) {
7584 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7585 // Baker's read barrier are used:
7586 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007587 // root = obj.field;
7588 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7589 // if (temp != null) {
7590 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007591 // }
7592
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007593 // /* GcRoot<mirror::Object> */ root = *address
7594 __ movl(root_reg, address);
7595 if (fixup_label != nullptr) {
7596 __ Bind(fixup_label);
7597 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007598 static_assert(
7599 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7600 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7601 "have different sizes.");
7602 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7603 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7604 "have different sizes.");
7605
Vladimir Marko953437b2016-08-24 08:30:46 +00007606 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007607 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007608 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007609 codegen_->AddSlowPath(slow_path);
7610
Roland Levillaind966ce72017-02-09 16:20:14 +00007611 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7612 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007613 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007614 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7615 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007616 __ j(kNotEqual, slow_path->GetEntryLabel());
7617 __ Bind(slow_path->GetExitLabel());
7618 } else {
7619 // GC root loaded through a slow path for read barriers other
7620 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007621 // /* GcRoot<mirror::Object>* */ root = address
7622 __ leal(root_reg, address);
7623 if (fixup_label != nullptr) {
7624 __ Bind(fixup_label);
7625 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007626 // /* mirror::Object* */ root = root->Read()
7627 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7628 }
7629 } else {
7630 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007631 // /* GcRoot<mirror::Object> */ root = *address
7632 __ movl(root_reg, address);
7633 if (fixup_label != nullptr) {
7634 __ Bind(fixup_label);
7635 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007636 // Note that GC roots are not affected by heap poisoning, thus we
7637 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007638 }
7639}
7640
7641void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7642 Location ref,
7643 Register obj,
7644 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007645 bool needs_null_check) {
7646 DCHECK(kEmitCompilerReadBarrier);
7647 DCHECK(kUseBakerReadBarrier);
7648
7649 // /* HeapReference<Object> */ ref = *(obj + offset)
7650 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007651 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007652}
7653
7654void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7655 Location ref,
7656 Register obj,
7657 uint32_t data_offset,
7658 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007659 bool needs_null_check) {
7660 DCHECK(kEmitCompilerReadBarrier);
7661 DCHECK(kUseBakerReadBarrier);
7662
Roland Levillain3d312422016-06-23 13:53:42 +01007663 static_assert(
7664 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7665 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007666 // /* HeapReference<Object> */ ref =
7667 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007668 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007669 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007670}
7671
7672void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7673 Location ref,
7674 Register obj,
7675 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007676 bool needs_null_check,
7677 bool always_update_field,
7678 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007679 DCHECK(kEmitCompilerReadBarrier);
7680 DCHECK(kUseBakerReadBarrier);
7681
7682 // In slow path based read barriers, the read barrier call is
7683 // inserted after the original load. However, in fast path based
7684 // Baker's read barriers, we need to perform the load of
7685 // mirror::Object::monitor_ *before* the original reference load.
7686 // This load-load ordering is required by the read barrier.
7687 // The fast path/slow path (for Baker's algorithm) should look like:
7688 //
7689 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7690 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7691 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007692 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007693 // if (is_gray) {
7694 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7695 // }
7696 //
7697 // Note: the original implementation in ReadBarrier::Barrier is
7698 // slightly more complex as:
7699 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007700 // the high-bits of rb_state, which are expected to be all zeroes
7701 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7702 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007703 // - it performs additional checks that we do not do here for
7704 // performance reasons.
7705
7706 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007707 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7708
Vladimir Marko953437b2016-08-24 08:30:46 +00007709 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007710 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7711 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007712 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7713 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7714 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7715
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007716 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007717 // ref = ReadBarrier::Mark(ref);
7718 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7719 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007720 if (needs_null_check) {
7721 MaybeRecordImplicitNullCheck(instruction);
7722 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007723
7724 // Load fence to prevent load-load reordering.
7725 // Note that this is a no-op, thanks to the x86 memory model.
7726 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7727
7728 // The actual reference load.
7729 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007730 __ movl(ref_reg, src); // Flags are unaffected.
7731
7732 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7733 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007734 SlowPathCode* slow_path;
7735 if (always_update_field) {
7736 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007737 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007738 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7739 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007740 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007741 instruction, ref, /* unpoison_ref_before_marking */ true);
7742 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007743 AddSlowPath(slow_path);
7744
7745 // We have done the "if" of the gray bit check above, now branch based on the flags.
7746 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007747
7748 // Object* ref = ref_addr->AsMirrorPtr()
7749 __ MaybeUnpoisonHeapReference(ref_reg);
7750
Roland Levillain7c1559a2015-12-15 10:55:36 +00007751 __ Bind(slow_path->GetExitLabel());
7752}
7753
7754void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7755 Location out,
7756 Location ref,
7757 Location obj,
7758 uint32_t offset,
7759 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007760 DCHECK(kEmitCompilerReadBarrier);
7761
Roland Levillain7c1559a2015-12-15 10:55:36 +00007762 // Insert a slow path based read barrier *after* the reference load.
7763 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007764 // If heap poisoning is enabled, the unpoisoning of the loaded
7765 // reference will be carried out by the runtime within the slow
7766 // path.
7767 //
7768 // Note that `ref` currently does not get unpoisoned (when heap
7769 // poisoning is enabled), which is alright as the `ref` argument is
7770 // not used by the artReadBarrierSlow entry point.
7771 //
7772 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007773 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007774 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7775 AddSlowPath(slow_path);
7776
Roland Levillain0d5a2812015-11-13 10:07:31 +00007777 __ jmp(slow_path->GetEntryLabel());
7778 __ Bind(slow_path->GetExitLabel());
7779}
7780
Roland Levillain7c1559a2015-12-15 10:55:36 +00007781void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7782 Location out,
7783 Location ref,
7784 Location obj,
7785 uint32_t offset,
7786 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007787 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007788 // Baker's read barriers shall be handled by the fast path
7789 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7790 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007791 // If heap poisoning is enabled, unpoisoning will be taken care of
7792 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007793 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007794 } else if (kPoisonHeapReferences) {
7795 __ UnpoisonHeapReference(out.AsRegister<Register>());
7796 }
7797}
7798
Roland Levillain7c1559a2015-12-15 10:55:36 +00007799void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7800 Location out,
7801 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007802 DCHECK(kEmitCompilerReadBarrier);
7803
Roland Levillain7c1559a2015-12-15 10:55:36 +00007804 // Insert a slow path based read barrier *after* the GC root load.
7805 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007806 // Note that GC roots are not affected by heap poisoning, so we do
7807 // not need to do anything special for this here.
7808 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007809 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007810 AddSlowPath(slow_path);
7811
Roland Levillain0d5a2812015-11-13 10:07:31 +00007812 __ jmp(slow_path->GetEntryLabel());
7813 __ Bind(slow_path->GetExitLabel());
7814}
7815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007816void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007817 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007818 LOG(FATAL) << "Unreachable";
7819}
7820
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007821void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007822 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007823 LOG(FATAL) << "Unreachable";
7824}
7825
Mark Mendellfe57faa2015-09-18 09:26:15 -04007826// Simple implementation of packed switch - generate cascaded compare/jumps.
7827void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7828 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007829 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007830 locations->SetInAt(0, Location::RequiresRegister());
7831}
7832
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007833void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7834 int32_t lower_bound,
7835 uint32_t num_entries,
7836 HBasicBlock* switch_block,
7837 HBasicBlock* default_block) {
7838 // Figure out the correct compare values and jump conditions.
7839 // Handle the first compare/branch as a special case because it might
7840 // jump to the default case.
7841 DCHECK_GT(num_entries, 2u);
7842 Condition first_condition;
7843 uint32_t index;
7844 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7845 if (lower_bound != 0) {
7846 first_condition = kLess;
7847 __ cmpl(value_reg, Immediate(lower_bound));
7848 __ j(first_condition, codegen_->GetLabelOf(default_block));
7849 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007850
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007851 index = 1;
7852 } else {
7853 // Handle all the compare/jumps below.
7854 first_condition = kBelow;
7855 index = 0;
7856 }
7857
7858 // Handle the rest of the compare/jumps.
7859 for (; index + 1 < num_entries; index += 2) {
7860 int32_t compare_to_value = lower_bound + index + 1;
7861 __ cmpl(value_reg, Immediate(compare_to_value));
7862 // Jump to successors[index] if value < case_value[index].
7863 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7864 // Jump to successors[index + 1] if value == case_value[index + 1].
7865 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7866 }
7867
7868 if (index != num_entries) {
7869 // There are an odd number of entries. Handle the last one.
7870 DCHECK_EQ(index + 1, num_entries);
7871 __ cmpl(value_reg, Immediate(lower_bound + index));
7872 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007873 }
7874
7875 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007876 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7877 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007878 }
7879}
7880
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007881void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7882 int32_t lower_bound = switch_instr->GetStartValue();
7883 uint32_t num_entries = switch_instr->GetNumEntries();
7884 LocationSummary* locations = switch_instr->GetLocations();
7885 Register value_reg = locations->InAt(0).AsRegister<Register>();
7886
7887 GenPackedSwitchWithCompares(value_reg,
7888 lower_bound,
7889 num_entries,
7890 switch_instr->GetBlock(),
7891 switch_instr->GetDefaultBlock());
7892}
7893
Mark Mendell805b3b52015-09-18 14:10:29 -04007894void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7895 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007896 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007897 locations->SetInAt(0, Location::RequiresRegister());
7898
7899 // Constant area pointer.
7900 locations->SetInAt(1, Location::RequiresRegister());
7901
7902 // And the temporary we need.
7903 locations->AddTemp(Location::RequiresRegister());
7904}
7905
7906void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7907 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007908 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007909 LocationSummary* locations = switch_instr->GetLocations();
7910 Register value_reg = locations->InAt(0).AsRegister<Register>();
7911 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7912
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007913 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7914 GenPackedSwitchWithCompares(value_reg,
7915 lower_bound,
7916 num_entries,
7917 switch_instr->GetBlock(),
7918 default_block);
7919 return;
7920 }
7921
Mark Mendell805b3b52015-09-18 14:10:29 -04007922 // Optimizing has a jump area.
7923 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7924 Register constant_area = locations->InAt(1).AsRegister<Register>();
7925
7926 // Remove the bias, if needed.
7927 if (lower_bound != 0) {
7928 __ leal(temp_reg, Address(value_reg, -lower_bound));
7929 value_reg = temp_reg;
7930 }
7931
7932 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007933 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007934 __ cmpl(value_reg, Immediate(num_entries - 1));
7935 __ j(kAbove, codegen_->GetLabelOf(default_block));
7936
7937 // We are in the range of the table.
7938 // Load (target-constant_area) from the jump table, indexing by the value.
7939 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7940
7941 // Compute the actual target address by adding in constant_area.
7942 __ addl(temp_reg, constant_area);
7943
7944 // And jump.
7945 __ jmp(temp_reg);
7946}
7947
Mark Mendell0616ae02015-04-17 12:49:27 -04007948void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7949 HX86ComputeBaseMethodAddress* insn) {
7950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007951 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007952 locations->SetOut(Location::RequiresRegister());
7953}
7954
7955void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7956 HX86ComputeBaseMethodAddress* insn) {
7957 LocationSummary* locations = insn->GetLocations();
7958 Register reg = locations->Out().AsRegister<Register>();
7959
7960 // Generate call to next instruction.
7961 Label next_instruction;
7962 __ call(&next_instruction);
7963 __ Bind(&next_instruction);
7964
7965 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007966 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007967
7968 // Grab the return address off the stack.
7969 __ popl(reg);
7970}
7971
7972void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7973 HX86LoadFromConstantTable* insn) {
7974 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007975 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007976
7977 locations->SetInAt(0, Location::RequiresRegister());
7978 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7979
7980 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007981 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007982 return;
7983 }
7984
7985 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007986 case DataType::Type::kFloat32:
7987 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007988 locations->SetOut(Location::RequiresFpuRegister());
7989 break;
7990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007991 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007992 locations->SetOut(Location::RequiresRegister());
7993 break;
7994
7995 default:
7996 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7997 }
7998}
7999
8000void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008001 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008002 return;
8003 }
8004
8005 LocationSummary* locations = insn->GetLocations();
8006 Location out = locations->Out();
8007 Register const_area = locations->InAt(0).AsRegister<Register>();
8008 HConstant *value = insn->GetConstant();
8009
8010 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008011 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008012 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008013 codegen_->LiteralFloatAddress(
8014 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008015 break;
8016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008017 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008018 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008019 codegen_->LiteralDoubleAddress(
8020 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008021 break;
8022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008023 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008024 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008025 codegen_->LiteralInt32Address(
8026 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008027 break;
8028
8029 default:
8030 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8031 }
8032}
8033
Mark Mendell0616ae02015-04-17 12:49:27 -04008034/**
8035 * Class to handle late fixup of offsets into constant area.
8036 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008037class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008038 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008039 RIPFixup(CodeGeneratorX86& codegen,
8040 HX86ComputeBaseMethodAddress* base_method_address,
8041 size_t offset)
8042 : codegen_(&codegen),
8043 base_method_address_(base_method_address),
8044 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008045
8046 protected:
8047 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8048
8049 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008050 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008051
8052 private:
8053 void Process(const MemoryRegion& region, int pos) OVERRIDE {
8054 // Patch the correct offset for the instruction. The place to patch is the
8055 // last 4 bytes of the instruction.
8056 // The value to patch is the distance from the offset in the constant area
8057 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008058 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008059 int32_t relative_position =
8060 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008061
8062 // Patch in the right value.
8063 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8064 }
8065
Mark Mendell0616ae02015-04-17 12:49:27 -04008066 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008067 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008068};
8069
Mark Mendell805b3b52015-09-18 14:10:29 -04008070/**
8071 * Class to handle late fixup of offsets to a jump table that will be created in the
8072 * constant area.
8073 */
8074class JumpTableRIPFixup : public RIPFixup {
8075 public:
8076 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008077 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8078 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008079
8080 void CreateJumpTable() {
8081 X86Assembler* assembler = codegen_->GetAssembler();
8082
8083 // Ensure that the reference to the jump table has the correct offset.
8084 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8085 SetOffset(offset_in_constant_table);
8086
8087 // The label values in the jump table are computed relative to the
8088 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008089 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008090
8091 // Populate the jump table with the correct values for the jump table.
8092 int32_t num_entries = switch_instr_->GetNumEntries();
8093 HBasicBlock* block = switch_instr_->GetBlock();
8094 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8095 // The value that we want is the target offset - the position of the table.
8096 for (int32_t i = 0; i < num_entries; i++) {
8097 HBasicBlock* b = successors[i];
8098 Label* l = codegen_->GetLabelOf(b);
8099 DCHECK(l->IsBound());
8100 int32_t offset_to_block = l->Position() - relative_offset;
8101 assembler->AppendInt32(offset_to_block);
8102 }
8103 }
8104
8105 private:
8106 const HX86PackedSwitch* switch_instr_;
8107};
8108
8109void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8110 // Generate the constant area if needed.
8111 X86Assembler* assembler = GetAssembler();
8112 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8113 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8114 // byte values.
8115 assembler->Align(4, 0);
8116 constant_area_start_ = assembler->CodeSize();
8117
8118 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008119 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008120 jump_table->CreateJumpTable();
8121 }
8122
8123 // And now add the constant area to the generated code.
8124 assembler->AddConstantArea();
8125 }
8126
8127 // And finish up.
8128 CodeGenerator::Finalize(allocator);
8129}
8130
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008131Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8132 HX86ComputeBaseMethodAddress* method_base,
8133 Register reg) {
8134 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008135 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008136 return Address(reg, kDummy32BitOffset, fixup);
8137}
8138
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008139Address CodeGeneratorX86::LiteralFloatAddress(float v,
8140 HX86ComputeBaseMethodAddress* method_base,
8141 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008142 AssemblerFixup* fixup =
8143 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008144 return Address(reg, kDummy32BitOffset, fixup);
8145}
8146
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008147Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8148 HX86ComputeBaseMethodAddress* method_base,
8149 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008150 AssemblerFixup* fixup =
8151 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008152 return Address(reg, kDummy32BitOffset, fixup);
8153}
8154
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008155Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8156 HX86ComputeBaseMethodAddress* method_base,
8157 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008158 AssemblerFixup* fixup =
8159 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008160 return Address(reg, kDummy32BitOffset, fixup);
8161}
8162
Aart Bika19616e2016-02-01 18:57:58 -08008163void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8164 if (value == 0) {
8165 __ xorl(dest, dest);
8166 } else {
8167 __ movl(dest, Immediate(value));
8168 }
8169}
8170
8171void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8172 if (value == 0) {
8173 __ testl(dest, dest);
8174 } else {
8175 __ cmpl(dest, Immediate(value));
8176 }
8177}
8178
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008179void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8180 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008181 GenerateIntCompare(lhs_reg, rhs);
8182}
8183
8184void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008185 if (rhs.IsConstant()) {
8186 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008187 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008188 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008189 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008190 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008191 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008192 }
8193}
8194
8195Address CodeGeneratorX86::ArrayAddress(Register obj,
8196 Location index,
8197 ScaleFactor scale,
8198 uint32_t data_offset) {
8199 return index.IsConstant() ?
8200 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8201 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8202}
8203
Mark Mendell805b3b52015-09-18 14:10:29 -04008204Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8205 Register reg,
8206 Register value) {
8207 // Create a fixup to be used to create and address the jump table.
8208 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008209 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008210
8211 // We have to populate the jump tables.
8212 fixups_to_jump_tables_.push_back(table_fixup);
8213
8214 // We want a scaled address, as we are extracting the correct offset from the table.
8215 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8216}
8217
Andreas Gampe85b62f22015-09-09 13:15:38 -07008218// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008219void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008220 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008221 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008222 return;
8223 }
8224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008225 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008226
8227 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8228 if (target.Equals(return_loc)) {
8229 return;
8230 }
8231
8232 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8233 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008234 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008235 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008236 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8237 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008238 GetMoveResolver()->EmitNativeCode(&parallel_move);
8239 } else {
8240 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008241 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008242 parallel_move.AddMove(return_loc, target, type, nullptr);
8243 GetMoveResolver()->EmitNativeCode(&parallel_move);
8244 }
8245}
8246
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008247void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8248 const uint8_t* roots_data,
8249 const PatchInfo<Label>& info,
8250 uint64_t index_in_table) const {
8251 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8252 uintptr_t address =
8253 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8254 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8255 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8256 dchecked_integral_cast<uint32_t>(address);
8257}
8258
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008259void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8260 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008261 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008262 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008263 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008264 }
8265
8266 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008267 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008268 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008269 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008270 }
8271}
8272
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008273void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8274 ATTRIBUTE_UNUSED) {
8275 LOG(FATAL) << "Unreachable";
8276}
8277
8278void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8279 ATTRIBUTE_UNUSED) {
8280 LOG(FATAL) << "Unreachable";
8281}
8282
Roland Levillain4d027112015-07-01 15:41:14 +01008283#undef __
8284
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008285} // namespace x86
8286} // namespace art