blob: 30436eef9c4711b6deef40c3a3d661f98930293b [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)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001039 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1041 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001042 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001043 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001044 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001045 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001046 // Use a fake return address register to mimic Quick.
1047 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049
David Brazdil58282f42016-01-14 12:45:10 +00001050void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001052 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001053}
1054
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001056 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001057 assembler_(codegen->GetAssembler()),
1058 codegen_(codegen) {}
1059
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001061 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062}
1063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001066 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001067 bool skip_overflow_check =
1068 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001070
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001071 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1072 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1073 Immediate(1));
1074 }
1075
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001076 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001077 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1078 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001079 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001080 }
1081
Mark Mendell5f874182015-03-04 15:42:45 -05001082 if (HasEmptyFrame()) {
1083 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001084 }
Mark Mendell5f874182015-03-04 15:42:45 -05001085
1086 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1087 Register reg = kCoreCalleeSaves[i];
1088 if (allocated_registers_.ContainsCoreRegister(reg)) {
1089 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001090 __ cfi().AdjustCFAOffset(kX86WordSize);
1091 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001092 }
1093 }
1094
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001095 int adjust = GetFrameSize() - FrameEntrySpillSize();
1096 __ subl(ESP, Immediate(adjust));
1097 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001098 // Save the current method if we need it. Note that we do not
1099 // do this in HCurrentMethod, as the instruction might have been removed
1100 // in the SSA graph.
1101 if (RequiresCurrentMethod()) {
1102 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1103 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001104
1105 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1106 // Initialize should_deoptimize flag to 0.
1107 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1108 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001109}
1110
1111void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001112 __ cfi().RememberState();
1113 if (!HasEmptyFrame()) {
1114 int adjust = GetFrameSize() - FrameEntrySpillSize();
1115 __ addl(ESP, Immediate(adjust));
1116 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001117
David Srbeckyc34dc932015-04-12 09:27:43 +01001118 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1119 Register reg = kCoreCalleeSaves[i];
1120 if (allocated_registers_.ContainsCoreRegister(reg)) {
1121 __ popl(reg);
1122 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1123 __ cfi().Restore(DWARFReg(reg));
1124 }
Mark Mendell5f874182015-03-04 15:42:45 -05001125 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001126 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001127 __ ret();
1128 __ cfi().RestoreState();
1129 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001130}
1131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001132void CodeGeneratorX86::Bind(HBasicBlock* block) {
1133 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001134}
1135
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001136Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001137 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001138 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001139 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001140 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 case DataType::Type::kInt8:
1142 case DataType::Type::kUint16:
1143 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001144 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::RegisterLocation(EAX);
1147
Aart Bik66c158e2018-01-31 12:55:04 -08001148 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001149 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150 return Location::RegisterPairLocation(EAX, EDX);
1151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001152 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001153 return Location::NoLocation();
1154
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001155 case DataType::Type::kFloat64:
1156 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001157 return Location::FpuRegisterLocation(XMM0);
1158 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001159
1160 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001161}
1162
1163Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1164 return Location::RegisterLocation(kMethodRegisterArgument);
1165}
1166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001169 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001170 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001171 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001172 case DataType::Type::kInt8:
1173 case DataType::Type::kUint16:
1174 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001175 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001181 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001182 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001183 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001185 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186 uint32_t index = gp_index_;
1187 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001188 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001190 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1191 calling_convention.GetRegisterPairAt(index));
1192 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001193 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001194 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1195 }
1196 }
1197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001198 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001199 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001200 stack_index_++;
1201 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1202 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1203 } else {
1204 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1205 }
1206 }
1207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001209 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001210 stack_index_ += 2;
1211 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1212 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1213 } else {
1214 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001215 }
1216 }
1217
Aart Bik66c158e2018-01-31 12:55:04 -08001218 case DataType::Type::kUint32:
1219 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001221 LOG(FATAL) << "Unexpected parameter type " << type;
1222 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001223 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001224 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001225}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001226
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227void CodeGeneratorX86::Move32(Location destination, Location source) {
1228 if (source.Equals(destination)) {
1229 return;
1230 }
1231 if (destination.IsRegister()) {
1232 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 } else {
1237 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (destination.IsFpuRegister()) {
1241 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 } else {
1246 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001250 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001252 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001254 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001255 } else if (source.IsConstant()) {
1256 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001257 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001258 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 } else {
1260 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001261 __ pushl(Address(ESP, source.GetStackIndex()));
1262 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 }
1264 }
1265}
1266
1267void CodeGeneratorX86::Move64(Location destination, Location source) {
1268 if (source.Equals(destination)) {
1269 return;
1270 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 if (destination.IsRegisterPair()) {
1272 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 EmitParallelMoves(
1274 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1275 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001276 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001277 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001278 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1282 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1283 __ psrlq(src_reg, Immediate(32));
1284 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001285 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001286 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001287 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001288 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1289 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001290 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1291 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001293 if (source.IsFpuRegister()) {
1294 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1295 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001296 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001297 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001298 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001299 // Create stack space for 2 elements.
1300 __ subl(ESP, Immediate(2 * elem_size));
1301 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1302 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1303 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1304 // And remove the temporary stack space we allocated.
1305 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else {
1307 LOG(FATAL) << "Unimplemented";
1308 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001309 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001310 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001311 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001312 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001315 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001316 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001317 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001318 } else if (source.IsConstant()) {
1319 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001320 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1321 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001322 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001323 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1324 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001325 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001326 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001327 EmitParallelMoves(
1328 Location::StackSlot(source.GetStackIndex()),
1329 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001330 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001331 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001332 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001334 }
1335 }
1336}
1337
Calin Juravle175dc732015-08-25 15:42:32 +01001338void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1339 DCHECK(location.IsRegister());
1340 __ movl(location.AsRegister<Register>(), Immediate(value));
1341}
1342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001343void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001344 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001345 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1346 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1347 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001348 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001349 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001350 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001351 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001352}
1353
1354void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1355 if (location.IsRegister()) {
1356 locations->AddTemp(location);
1357 } else if (location.IsRegisterPair()) {
1358 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1359 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1360 } else {
1361 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1362 }
1363}
1364
David Brazdilfc6a86a2015-06-26 10:33:45 +00001365void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001366 if (successor->IsExitBlock()) {
1367 DCHECK(got->GetPrevious()->AlwaysThrows());
1368 return; // no code needed
1369 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001370
1371 HBasicBlock* block = got->GetBlock();
1372 HInstruction* previous = got->GetPrevious();
1373
1374 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001375 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001376 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1377 __ pushl(EAX);
1378 __ movl(EAX, Address(ESP, kX86WordSize));
1379 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1380 __ popl(EAX);
1381 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001382 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1383 return;
1384 }
1385
1386 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1387 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1388 }
1389 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001390 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001391 }
1392}
1393
David Brazdilfc6a86a2015-06-26 10:33:45 +00001394void LocationsBuilderX86::VisitGoto(HGoto* got) {
1395 got->SetLocations(nullptr);
1396}
1397
1398void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1399 HandleGoto(got, got->GetSuccessor());
1400}
1401
1402void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1403 try_boundary->SetLocations(nullptr);
1404}
1405
1406void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1407 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1408 if (!successor->IsExitBlock()) {
1409 HandleGoto(try_boundary, successor);
1410 }
1411}
1412
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001413void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001414 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001415}
1416
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001417void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001418}
1419
Mark Mendell152408f2015-12-31 12:28:50 -05001420template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001421void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001422 LabelType* true_label,
1423 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 if (cond->IsFPConditionTrueIfNaN()) {
1425 __ j(kUnordered, true_label);
1426 } else if (cond->IsFPConditionFalseIfNaN()) {
1427 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001428 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001429 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001430}
1431
Mark Mendell152408f2015-12-31 12:28:50 -05001432template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001433void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001434 LabelType* true_label,
1435 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001436 LocationSummary* locations = cond->GetLocations();
1437 Location left = locations->InAt(0);
1438 Location right = locations->InAt(1);
1439 IfCondition if_cond = cond->GetCondition();
1440
Mark Mendellc4701932015-04-10 13:18:51 -04001441 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001442 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001443 IfCondition true_high_cond = if_cond;
1444 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001445 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001446
1447 // Set the conditions for the test, remembering that == needs to be
1448 // decided using the low words.
1449 switch (if_cond) {
1450 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001451 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001453 break;
1454 case kCondLT:
1455 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001456 break;
1457 case kCondLE:
1458 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001459 break;
1460 case kCondGT:
1461 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001462 break;
1463 case kCondGE:
1464 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001465 break;
Aart Bike9f37602015-10-09 11:15:55 -07001466 case kCondB:
1467 false_high_cond = kCondA;
1468 break;
1469 case kCondBE:
1470 true_high_cond = kCondB;
1471 break;
1472 case kCondA:
1473 false_high_cond = kCondB;
1474 break;
1475 case kCondAE:
1476 true_high_cond = kCondA;
1477 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001478 }
1479
1480 if (right.IsConstant()) {
1481 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001482 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001483 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001484
Aart Bika19616e2016-02-01 18:57:58 -08001485 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001488 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001489 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001490 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001491 __ j(X86Condition(true_high_cond), true_label);
1492 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001493 }
1494 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001495 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001496 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001497 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001498 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001499
1500 __ cmpl(left_high, right_high);
1501 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001502 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001503 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001504 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001505 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001506 __ j(X86Condition(true_high_cond), true_label);
1507 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001508 }
1509 // Must be equal high, so compare the lows.
1510 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001511 } else {
1512 DCHECK(right.IsDoubleStackSlot());
1513 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1514 if (if_cond == kCondNE) {
1515 __ j(X86Condition(true_high_cond), true_label);
1516 } else if (if_cond == kCondEQ) {
1517 __ j(X86Condition(false_high_cond), false_label);
1518 } else {
1519 __ j(X86Condition(true_high_cond), true_label);
1520 __ j(X86Condition(false_high_cond), false_label);
1521 }
1522 // Must be equal high, so compare the lows.
1523 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001524 }
1525 // The last comparison might be unsigned.
1526 __ j(final_condition, true_label);
1527}
1528
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001529void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1530 Location rhs,
1531 HInstruction* insn,
1532 bool is_double) {
1533 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1534 if (is_double) {
1535 if (rhs.IsFpuRegister()) {
1536 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1537 } else if (const_area != nullptr) {
1538 DCHECK(const_area->IsEmittedAtUseSite());
1539 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1540 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001541 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1542 const_area->GetBaseMethodAddress(),
1543 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001544 } else {
1545 DCHECK(rhs.IsDoubleStackSlot());
1546 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1547 }
1548 } else {
1549 if (rhs.IsFpuRegister()) {
1550 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1551 } else if (const_area != nullptr) {
1552 DCHECK(const_area->IsEmittedAtUseSite());
1553 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1554 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001555 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1556 const_area->GetBaseMethodAddress(),
1557 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001558 } else {
1559 DCHECK(rhs.IsStackSlot());
1560 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1561 }
1562 }
1563}
1564
Mark Mendell152408f2015-12-31 12:28:50 -05001565template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001566void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001567 LabelType* true_target_in,
1568 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001569 // Generated branching requires both targets to be explicit. If either of the
1570 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001571 LabelType fallthrough_target;
1572 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1573 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001574
Mark Mendellc4701932015-04-10 13:18:51 -04001575 LocationSummary* locations = condition->GetLocations();
1576 Location left = locations->InAt(0);
1577 Location right = locations->InAt(1);
1578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001579 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001580 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001581 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001582 GenerateLongComparesAndJumps(condition, true_target, false_target);
1583 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001585 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001586 GenerateFPJumps(condition, true_target, false_target);
1587 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001588 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001589 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001590 GenerateFPJumps(condition, true_target, false_target);
1591 break;
1592 default:
1593 LOG(FATAL) << "Unexpected compare type " << type;
1594 }
1595
David Brazdil0debae72015-11-12 18:37:00 +00001596 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001597 __ jmp(false_target);
1598 }
David Brazdil0debae72015-11-12 18:37:00 +00001599
1600 if (fallthrough_target.IsLinked()) {
1601 __ Bind(&fallthrough_target);
1602 }
Mark Mendellc4701932015-04-10 13:18:51 -04001603}
1604
David Brazdil0debae72015-11-12 18:37:00 +00001605static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1606 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1607 // are set only strictly before `branch`. We can't use the eflags on long/FP
1608 // conditions if they are materialized due to the complex branching.
1609 return cond->IsCondition() &&
1610 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1612 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001613}
1614
Mark Mendell152408f2015-12-31 12:28:50 -05001615template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001616void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001617 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001618 LabelType* true_target,
1619 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001620 HInstruction* cond = instruction->InputAt(condition_input_index);
1621
1622 if (true_target == nullptr && false_target == nullptr) {
1623 // Nothing to do. The code always falls through.
1624 return;
1625 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001626 // Constant condition, statically compared against "true" (integer value 1).
1627 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001628 if (true_target != nullptr) {
1629 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001630 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001631 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001632 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001633 if (false_target != nullptr) {
1634 __ jmp(false_target);
1635 }
1636 }
1637 return;
1638 }
1639
1640 // The following code generates these patterns:
1641 // (1) true_target == nullptr && false_target != nullptr
1642 // - opposite condition true => branch to false_target
1643 // (2) true_target != nullptr && false_target == nullptr
1644 // - condition true => branch to true_target
1645 // (3) true_target != nullptr && false_target != nullptr
1646 // - condition true => branch to true_target
1647 // - branch to false_target
1648 if (IsBooleanValueOrMaterializedCondition(cond)) {
1649 if (AreEflagsSetFrom(cond, instruction)) {
1650 if (true_target == nullptr) {
1651 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1652 } else {
1653 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1654 }
1655 } else {
1656 // Materialized condition, compare against 0.
1657 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1658 if (lhs.IsRegister()) {
1659 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1660 } else {
1661 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1662 }
1663 if (true_target == nullptr) {
1664 __ j(kEqual, false_target);
1665 } else {
1666 __ j(kNotEqual, true_target);
1667 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001668 }
1669 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001670 // Condition has not been materialized, use its inputs as the comparison and
1671 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001672 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001673
1674 // If this is a long or FP comparison that has been folded into
1675 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 DataType::Type type = condition->InputAt(0)->GetType();
1677 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001678 GenerateCompareTestAndBranch(condition, true_target, false_target);
1679 return;
1680 }
1681
1682 Location lhs = condition->GetLocations()->InAt(0);
1683 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001684 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001685 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001686 if (true_target == nullptr) {
1687 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1688 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001689 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001690 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001691 }
David Brazdil0debae72015-11-12 18:37:00 +00001692
1693 // If neither branch falls through (case 3), the conditional branch to `true_target`
1694 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1695 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697 }
1698}
1699
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001701 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001702 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001703 locations->SetInAt(0, Location::Any());
1704 }
1705}
1706
1707void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001708 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1709 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1710 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1711 nullptr : codegen_->GetLabelOf(true_successor);
1712 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1713 nullptr : codegen_->GetLabelOf(false_successor);
1714 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715}
1716
1717void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001718 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001719 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001720 InvokeRuntimeCallingConvention calling_convention;
1721 RegisterSet caller_saves = RegisterSet::Empty();
1722 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1723 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001724 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001725 locations->SetInAt(0, Location::Any());
1726 }
1727}
1728
1729void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001730 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001731 GenerateTestAndBranch<Label>(deoptimize,
1732 /* condition_input_index */ 0,
1733 slow_path->GetEntryLabel(),
1734 /* false_target */ nullptr);
1735}
1736
Mingyao Yang063fc772016-08-02 11:02:54 -07001737void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001738 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001739 LocationSummary(flag, LocationSummary::kNoCall);
1740 locations->SetOut(Location::RequiresRegister());
1741}
1742
1743void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1744 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1745 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1746}
1747
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001748static bool SelectCanUseCMOV(HSelect* select) {
1749 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001751 return false;
1752 }
1753
1754 // A FP condition doesn't generate the single CC that we need.
1755 // In 32 bit mode, a long condition doesn't generate a single CC either.
1756 HInstruction* condition = select->GetCondition();
1757 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 DataType::Type compare_type = condition->InputAt(0)->GetType();
1759 if (compare_type == DataType::Type::kInt64 ||
1760 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001761 return false;
1762 }
1763 }
1764
1765 // We can generate a CMOV for this Select.
1766 return true;
1767}
1768
David Brazdil74eb1b22015-12-14 11:44:01 +00001769void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001770 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001772 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001773 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001774 } else {
1775 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001776 if (SelectCanUseCMOV(select)) {
1777 if (select->InputAt(1)->IsConstant()) {
1778 // Cmov can't handle a constant value.
1779 locations->SetInAt(1, Location::RequiresRegister());
1780 } else {
1781 locations->SetInAt(1, Location::Any());
1782 }
1783 } else {
1784 locations->SetInAt(1, Location::Any());
1785 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001786 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001787 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1788 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001789 }
1790 locations->SetOut(Location::SameAsFirstInput());
1791}
1792
1793void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1794 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001795 DCHECK(locations->InAt(0).Equals(locations->Out()));
1796 if (SelectCanUseCMOV(select)) {
1797 // If both the condition and the source types are integer, we can generate
1798 // a CMOV to implement Select.
1799
1800 HInstruction* select_condition = select->GetCondition();
1801 Condition cond = kNotEqual;
1802
1803 // Figure out how to test the 'condition'.
1804 if (select_condition->IsCondition()) {
1805 HCondition* condition = select_condition->AsCondition();
1806 if (!condition->IsEmittedAtUseSite()) {
1807 // This was a previously materialized condition.
1808 // Can we use the existing condition code?
1809 if (AreEflagsSetFrom(condition, select)) {
1810 // Materialization was the previous instruction. Condition codes are right.
1811 cond = X86Condition(condition->GetCondition());
1812 } else {
1813 // No, we have to recreate the condition code.
1814 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1815 __ testl(cond_reg, cond_reg);
1816 }
1817 } else {
1818 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1820 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001821 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001822 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001823 cond = X86Condition(condition->GetCondition());
1824 }
1825 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001826 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001827 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1828 __ testl(cond_reg, cond_reg);
1829 }
1830
1831 // If the condition is true, overwrite the output, which already contains false.
1832 Location false_loc = locations->InAt(0);
1833 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001834 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001835 // 64 bit conditional move.
1836 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1837 Register false_low = false_loc.AsRegisterPairLow<Register>();
1838 if (true_loc.IsRegisterPair()) {
1839 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1840 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1841 } else {
1842 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1843 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1844 }
1845 } else {
1846 // 32 bit conditional move.
1847 Register false_reg = false_loc.AsRegister<Register>();
1848 if (true_loc.IsRegister()) {
1849 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1850 } else {
1851 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1852 }
1853 }
1854 } else {
1855 NearLabel false_target;
1856 GenerateTestAndBranch<NearLabel>(
1857 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1858 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1859 __ Bind(&false_target);
1860 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001861}
1862
David Srbecky0cf44932015-12-09 14:09:59 +00001863void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001864 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001865}
1866
David Srbeckyd28f4a02016-03-14 17:14:24 +00001867void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1868 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001869}
1870
1871void CodeGeneratorX86::GenerateNop() {
1872 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001873}
1874
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001875void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001876 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001877 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001878 // Handle the long/FP comparisons made in instruction simplification.
1879 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001881 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001882 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 locations->SetOut(Location::RequiresRegister());
1885 }
1886 break;
1887 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 case DataType::Type::kFloat32:
1889 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001890 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001891 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1892 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1893 } else if (cond->InputAt(1)->IsConstant()) {
1894 locations->SetInAt(1, Location::RequiresFpuRegister());
1895 } else {
1896 locations->SetInAt(1, Location::Any());
1897 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001898 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001899 locations->SetOut(Location::RequiresRegister());
1900 }
1901 break;
1902 }
1903 default:
1904 locations->SetInAt(0, Location::RequiresRegister());
1905 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001906 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001907 // We need a byte register.
1908 locations->SetOut(Location::RegisterLocation(ECX));
1909 }
1910 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001911 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001912}
1913
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001914void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001915 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001916 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001917 }
Mark Mendellc4701932015-04-10 13:18:51 -04001918
1919 LocationSummary* locations = cond->GetLocations();
1920 Location lhs = locations->InAt(0);
1921 Location rhs = locations->InAt(1);
1922 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001923 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001924
1925 switch (cond->InputAt(0)->GetType()) {
1926 default: {
1927 // Integer case.
1928
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001929 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001930 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001931 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001932 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001933 return;
1934 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001936 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1937 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001939 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001940 GenerateFPJumps(cond, &true_label, &false_label);
1941 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001942 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001943 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001944 GenerateFPJumps(cond, &true_label, &false_label);
1945 break;
1946 }
1947
1948 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001949 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001950
Roland Levillain4fa13f62015-07-06 18:11:54 +01001951 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001952 __ Bind(&false_label);
1953 __ xorl(reg, reg);
1954 __ jmp(&done_label);
1955
Roland Levillain4fa13f62015-07-06 18:11:54 +01001956 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001957 __ Bind(&true_label);
1958 __ movl(reg, Immediate(1));
1959 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002004}
2005
2006void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008}
2009
Aart Bike9f37602015-10-09 11:15:55 -07002010void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
2038void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002040}
2041
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002042void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002044 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002045 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002046}
2047
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002048void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002049 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002050}
2051
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002052void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2053 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002054 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002055 locations->SetOut(Location::ConstantLocation(constant));
2056}
2057
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002058void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002060}
2061
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002063 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002064 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002065 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066}
2067
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002068void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 // Will be generated at use site.
2070}
2071
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2073 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002074 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 locations->SetOut(Location::ConstantLocation(constant));
2076}
2077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002078void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002079 // Will be generated at use site.
2080}
2081
2082void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2083 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 locations->SetOut(Location::ConstantLocation(constant));
2086}
2087
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002088void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002089 // Will be generated at use site.
2090}
2091
Igor Murashkind01745e2017-04-05 16:40:31 -07002092void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2093 constructor_fence->SetLocations(nullptr);
2094}
2095
2096void InstructionCodeGeneratorX86::VisitConstructorFence(
2097 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2098 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2099}
2100
Calin Juravle27df7582015-04-17 19:12:31 +01002101void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2102 memory_barrier->SetLocations(nullptr);
2103}
2104
2105void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002106 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002107}
2108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002111}
2112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002113void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002114 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002115}
2116
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002118 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002119 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002121 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002123 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 case DataType::Type::kInt8:
2125 case DataType::Type::kUint16:
2126 case DataType::Type::kInt16:
2127 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 break;
2130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002132 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002133 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134 break;
2135
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 case DataType::Type::kFloat32:
2137 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002138 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 break;
2141
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002142 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002144 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002145}
2146
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002147void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 if (kIsDebugBuild) {
2149 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002150 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002151 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002152 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kInt8:
2154 case DataType::Type::kUint16:
2155 case DataType::Type::kInt16:
2156 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002157 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002158 break;
2159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002161 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2162 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163 break;
2164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002165 case DataType::Type::kFloat32:
2166 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002167 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002168 break;
2169
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002170 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002171 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002172 }
2173 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002174 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002175}
2176
Calin Juravle175dc732015-08-25 15:42:32 +01002177void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2178 // The trampoline uses the same calling convention as dex calling conventions,
2179 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2180 // the method_idx.
2181 HandleInvoke(invoke);
2182}
2183
2184void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2185 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2186}
2187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002188void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002189 // Explicit clinit checks triggered by static invokes must have been pruned by
2190 // art::PrepareForRegisterAllocation.
2191 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002192
Mark Mendellfb8d2792015-03-31 22:16:59 -04002193 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002194 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002195 if (invoke->GetLocations()->CanCall() &&
2196 invoke->HasPcRelativeMethodLoadKind() &&
2197 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002198 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002199 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002200 return;
2201 }
2202
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002203 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002204
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002205 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002206 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002207 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002208 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002209}
2210
Mark Mendell09ed1a32015-03-25 08:30:06 -04002211static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2212 if (invoke->GetLocations()->Intrinsified()) {
2213 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2214 intrinsic.Dispatch(invoke);
2215 return true;
2216 }
2217 return false;
2218}
2219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002220void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002221 // Explicit clinit checks triggered by static invokes must have been pruned by
2222 // art::PrepareForRegisterAllocation.
2223 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002224
Mark Mendell09ed1a32015-03-25 08:30:06 -04002225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2226 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002227 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002228
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002229 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002230 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002231 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002232}
2233
2234void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002235 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2236 if (intrinsic.TryDispatch(invoke)) {
2237 return;
2238 }
2239
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002240 HandleInvoke(invoke);
2241}
2242
2243void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002244 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002245 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002246}
2247
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002248void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002249 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2250 return;
2251 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002252
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002253 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002254 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002255}
2256
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // This call to HandleInvoke allocates a temporary (core) register
2259 // which is also used to transfer the hidden argument from FP to
2260 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261 HandleInvoke(invoke);
2262 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002263 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264}
2265
2266void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2267 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002268 LocationSummary* locations = invoke->GetLocations();
2269 Register temp = locations->GetTemp(0).AsRegister<Register>();
2270 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 Location receiver = locations->InAt(0);
2272 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2273
Roland Levillain0d5a2812015-11-13 10:07:31 +00002274 // Set the hidden argument. This is safe to do this here, as XMM7
2275 // won't be modified thereafter, before the `call` instruction.
2276 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002278 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 if (receiver.IsStackSlot()) {
2281 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002282 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283 __ movl(temp, Address(temp, class_offset));
2284 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002285 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002286 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002287 }
Roland Levillain4d027112015-07-01 15:41:14 +01002288 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002289 // Instead of simply (possibly) unpoisoning `temp` here, we should
2290 // emit a read barrier for the previous class reference load.
2291 // However this is not required in practice, as this is an
2292 // intermediate/temporary reference and because the current
2293 // concurrent copying collector keeps the from-space memory
2294 // intact/accessible until the end of the marking phase (the
2295 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002296 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002297 // temp = temp->GetAddressOfIMT()
2298 __ movl(temp,
2299 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002300 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002301 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002302 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002303 __ movl(temp, Address(temp, method_offset));
2304 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002305 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002306 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002307
2308 DCHECK(!codegen_->IsLeafMethod());
2309 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2310}
2311
Orion Hodsonac141392017-01-13 11:53:47 +00002312void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2313 HandleInvoke(invoke);
2314}
2315
2316void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2317 codegen_->GenerateInvokePolymorphicCall(invoke);
2318}
2319
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002320void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2321 HandleInvoke(invoke);
2322}
2323
2324void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2325 codegen_->GenerateInvokeCustomCall(invoke);
2326}
2327
Roland Levillain88cb1752014-10-20 16:36:47 +01002328void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2329 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002330 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002331 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 case DataType::Type::kInt32:
2333 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetOut(Location::SameAsFirstInput());
2336 break;
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002339 locations->SetInAt(0, Location::RequiresFpuRegister());
2340 locations->SetOut(Location::SameAsFirstInput());
2341 locations->AddTemp(Location::RequiresRegister());
2342 locations->AddTemp(Location::RequiresFpuRegister());
2343 break;
2344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002346 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002347 locations->SetOut(Location::SameAsFirstInput());
2348 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002349 break;
2350
2351 default:
2352 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2353 }
2354}
2355
2356void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2357 LocationSummary* locations = neg->GetLocations();
2358 Location out = locations->Out();
2359 Location in = locations->InAt(0);
2360 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002362 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002363 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002364 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002365 break;
2366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002368 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002369 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002370 __ negl(out.AsRegisterPairLow<Register>());
2371 // Negation is similar to subtraction from zero. The least
2372 // significant byte triggers a borrow when it is different from
2373 // zero; to take it into account, add 1 to the most significant
2374 // byte if the carry flag (CF) is set to 1 after the first NEGL
2375 // operation.
2376 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2377 __ negl(out.AsRegisterPairHigh<Register>());
2378 break;
2379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002380 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002381 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 Register constant = locations->GetTemp(0).AsRegister<Register>();
2383 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002384 // Implement float negation with an exclusive or with value
2385 // 0x80000000 (mask for bit 31, representing the sign of a
2386 // single-precision floating-point number).
2387 __ movl(constant, Immediate(INT32_C(0x80000000)));
2388 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002389 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002390 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002391 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002392
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002394 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002395 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002396 // Implement double negation with an exclusive or with value
2397 // 0x8000000000000000 (mask for bit 63, representing the sign of
2398 // a double-precision floating-point number).
2399 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002400 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002402 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002403
2404 default:
2405 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2406 }
2407}
2408
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002409void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2410 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002411 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002413 locations->SetInAt(0, Location::RequiresFpuRegister());
2414 locations->SetInAt(1, Location::RequiresRegister());
2415 locations->SetOut(Location::SameAsFirstInput());
2416 locations->AddTemp(Location::RequiresFpuRegister());
2417}
2418
2419void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2420 LocationSummary* locations = neg->GetLocations();
2421 Location out = locations->Out();
2422 DCHECK(locations->InAt(0).Equals(out));
2423
2424 Register constant_area = locations->InAt(1).AsRegister<Register>();
2425 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002427 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2428 neg->GetBaseMethodAddress(),
2429 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002430 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2431 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002432 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2433 neg->GetBaseMethodAddress(),
2434 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002435 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2436 }
2437}
2438
Roland Levillaindff1f282014-11-05 14:15:05 +00002439void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 DataType::Type result_type = conversion->GetResultType();
2441 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002442 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2443 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002444
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002445 // The float-to-long and double-to-long type conversions rely on a
2446 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002447 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002448 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2449 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002450 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002451 : LocationSummary::kNoCall;
2452 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002453 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002454
Roland Levillaindff1f282014-11-05 14:15:05 +00002455 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002456 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002458 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002459 case DataType::Type::kUint8:
2460 case DataType::Type::kInt8:
2461 case DataType::Type::kUint16:
2462 case DataType::Type::kInt16:
2463 case DataType::Type::kInt32:
2464 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2465 // Make the output overlap to please the register allocator. This greatly simplifies
2466 // the validation of the linear scan implementation
2467 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2468 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002470 HInstruction* input = conversion->InputAt(0);
2471 Location input_location = input->IsConstant()
2472 ? Location::ConstantLocation(input->AsConstant())
2473 : Location::RegisterPairLocation(EAX, EDX);
2474 locations->SetInAt(0, input_location);
2475 // Make the output overlap to please the register allocator. This greatly simplifies
2476 // the validation of the linear scan implementation
2477 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2478 break;
2479 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485 break;
2486
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002487 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002489 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2490 locations->SetInAt(0, Location::Any());
2491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002492 break;
2493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002495 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002497 locations->SetInAt(0, Location::Any());
2498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2499 break;
2500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002502 locations->SetInAt(0, Location::RequiresFpuRegister());
2503 locations->SetOut(Location::RequiresRegister());
2504 locations->AddTemp(Location::RequiresFpuRegister());
2505 break;
2506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002508 locations->SetInAt(0, Location::RequiresFpuRegister());
2509 locations->SetOut(Location::RequiresRegister());
2510 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected type conversion from " << input_type
2515 << " to " << result_type;
2516 }
2517 break;
2518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002520 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002522 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002524 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kInt16:
2526 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 locations->SetInAt(0, Location::RegisterLocation(EAX));
2528 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2529 break;
2530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 case DataType::Type::kFloat32:
2532 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002533 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002534 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2535 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2536
Vladimir Marko949c91f2015-01-27 10:48:44 +00002537 // The runtime helper puts the result in EAX, EDX.
2538 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002539 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002540 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002541
2542 default:
2543 LOG(FATAL) << "Unexpected type conversion from " << input_type
2544 << " to " << result_type;
2545 }
2546 break;
2547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002549 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002551 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002553 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt16:
2555 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002556 locations->SetInAt(0, Location::RequiresRegister());
2557 locations->SetOut(Location::RequiresFpuRegister());
2558 break;
2559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002561 locations->SetInAt(0, Location::Any());
2562 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002563 break;
2564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002566 locations->SetInAt(0, Location::RequiresFpuRegister());
2567 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002568 break;
2569
2570 default:
2571 LOG(FATAL) << "Unexpected type conversion from " << input_type
2572 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002573 }
Roland Levillaincff13742014-11-17 14:32:17 +00002574 break;
2575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002577 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002581 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kInt16:
2583 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002584 locations->SetInAt(0, Location::RequiresRegister());
2585 locations->SetOut(Location::RequiresFpuRegister());
2586 break;
2587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002589 locations->SetInAt(0, Location::Any());
2590 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002591 break;
2592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002594 locations->SetInAt(0, Location::RequiresFpuRegister());
2595 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002596 break;
2597
2598 default:
2599 LOG(FATAL) << "Unexpected type conversion from " << input_type
2600 << " to " << result_type;
2601 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 break;
2603
2604 default:
2605 LOG(FATAL) << "Unexpected type conversion from " << input_type
2606 << " to " << result_type;
2607 }
2608}
2609
2610void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2611 LocationSummary* locations = conversion->GetLocations();
2612 Location out = locations->Out();
2613 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DataType::Type result_type = conversion->GetResultType();
2615 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002616 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2617 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002618 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002619 case DataType::Type::kUint8:
2620 switch (input_type) {
2621 case DataType::Type::kInt8:
2622 case DataType::Type::kUint16:
2623 case DataType::Type::kInt16:
2624 case DataType::Type::kInt32:
2625 if (in.IsRegister()) {
2626 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2627 } else {
2628 DCHECK(in.GetConstant()->IsIntConstant());
2629 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2630 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2631 }
2632 break;
2633 case DataType::Type::kInt64:
2634 if (in.IsRegisterPair()) {
2635 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2636 } else {
2637 DCHECK(in.GetConstant()->IsLongConstant());
2638 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2640 }
2641 break;
2642
2643 default:
2644 LOG(FATAL) << "Unexpected type conversion from " << input_type
2645 << " to " << result_type;
2646 }
2647 break;
2648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002650 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002651 case DataType::Type::kUint8:
2652 case DataType::Type::kUint16:
2653 case DataType::Type::kInt16:
2654 case DataType::Type::kInt32:
2655 if (in.IsRegister()) {
2656 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2657 } else {
2658 DCHECK(in.GetConstant()->IsIntConstant());
2659 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2660 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2661 }
2662 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002664 if (in.IsRegisterPair()) {
2665 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2666 } else {
2667 DCHECK(in.GetConstant()->IsLongConstant());
2668 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2669 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2670 }
2671 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002672
2673 default:
2674 LOG(FATAL) << "Unexpected type conversion from " << input_type
2675 << " to " << result_type;
2676 }
2677 break;
2678
2679 case DataType::Type::kUint16:
2680 switch (input_type) {
2681 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 case DataType::Type::kInt16:
2683 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002684 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002685 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2686 } else if (in.IsStackSlot()) {
2687 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002688 } else {
2689 DCHECK(in.GetConstant()->IsIntConstant());
2690 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002691 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2692 }
2693 break;
2694 case DataType::Type::kInt64:
2695 if (in.IsRegisterPair()) {
2696 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2697 } else if (in.IsDoubleStackSlot()) {
2698 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2699 } else {
2700 DCHECK(in.GetConstant()->IsLongConstant());
2701 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2702 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002703 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002704 break;
2705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 }
2710 break;
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002713 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002715 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002716 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002718 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002719 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002720 } else {
2721 DCHECK(in.GetConstant()->IsIntConstant());
2722 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002724 }
2725 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002726 case DataType::Type::kInt64:
2727 if (in.IsRegisterPair()) {
2728 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2729 } else if (in.IsDoubleStackSlot()) {
2730 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2731 } else {
2732 DCHECK(in.GetConstant()->IsLongConstant());
2733 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2734 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2735 }
2736 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002737
2738 default:
2739 LOG(FATAL) << "Unexpected type conversion from " << input_type
2740 << " to " << result_type;
2741 }
2742 break;
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002745 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002747 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002749 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002750 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002751 } else {
2752 DCHECK(in.IsConstant());
2753 DCHECK(in.GetConstant()->IsLongConstant());
2754 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002756 }
2757 break;
2758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002760 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2761 Register output = out.AsRegister<Register>();
2762 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002763 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002764
2765 __ movl(output, Immediate(kPrimIntMax));
2766 // temp = int-to-float(output)
2767 __ cvtsi2ss(temp, output);
2768 // if input >= temp goto done
2769 __ comiss(input, temp);
2770 __ j(kAboveEqual, &done);
2771 // if input == NaN goto nan
2772 __ j(kUnordered, &nan);
2773 // output = float-to-int-truncate(input)
2774 __ cvttss2si(output, input);
2775 __ jmp(&done);
2776 __ Bind(&nan);
2777 // output = 0
2778 __ xorl(output, output);
2779 __ Bind(&done);
2780 break;
2781 }
2782
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002784 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2785 Register output = out.AsRegister<Register>();
2786 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002787 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002788
2789 __ movl(output, Immediate(kPrimIntMax));
2790 // temp = int-to-double(output)
2791 __ cvtsi2sd(temp, output);
2792 // if input >= temp goto done
2793 __ comisd(input, temp);
2794 __ j(kAboveEqual, &done);
2795 // if input == NaN goto nan
2796 __ j(kUnordered, &nan);
2797 // output = double-to-int-truncate(input)
2798 __ cvttsd2si(output, input);
2799 __ jmp(&done);
2800 __ Bind(&nan);
2801 // output = 0
2802 __ xorl(output, output);
2803 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002804 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002805 }
Roland Levillain946e1432014-11-11 17:35:19 +00002806
2807 default:
2808 LOG(FATAL) << "Unexpected type conversion from " << input_type
2809 << " to " << result_type;
2810 }
2811 break;
2812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002814 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002816 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002818 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 case DataType::Type::kInt16:
2820 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2822 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002823 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002824 __ cdq();
2825 break;
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002828 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002829 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002830 break;
2831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002833 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002834 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002835 break;
2836
2837 default:
2838 LOG(FATAL) << "Unexpected type conversion from " << input_type
2839 << " to " << result_type;
2840 }
2841 break;
2842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002844 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002846 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002848 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002849 case DataType::Type::kInt16:
2850 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002851 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002852 break;
2853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002854 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002855 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002856
Roland Levillain232ade02015-04-20 15:14:36 +01002857 // Create stack space for the call to
2858 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2859 // TODO: enhance register allocator to ask for stack temporaries.
2860 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002862 __ subl(ESP, Immediate(adjustment));
2863 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002864
Roland Levillain232ade02015-04-20 15:14:36 +01002865 // Load the value to the FP stack, using temporaries if needed.
2866 PushOntoFPStack(in, 0, adjustment, false, true);
2867
2868 if (out.IsStackSlot()) {
2869 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2870 } else {
2871 __ fstps(Address(ESP, 0));
2872 Location stack_temp = Location::StackSlot(0);
2873 codegen_->Move32(out, stack_temp);
2874 }
2875
2876 // Remove the temporary stack space we allocated.
2877 if (adjustment != 0) {
2878 __ addl(ESP, Immediate(adjustment));
2879 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002880 break;
2881 }
2882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002884 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002885 break;
2886
2887 default:
2888 LOG(FATAL) << "Unexpected type conversion from " << input_type
2889 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002890 }
Roland Levillaincff13742014-11-17 14:32:17 +00002891 break;
2892
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002894 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002896 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002898 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kInt16:
2900 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002901 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002902 break;
2903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002905 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002906
Roland Levillain232ade02015-04-20 15:14:36 +01002907 // Create stack space for the call to
2908 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2909 // TODO: enhance register allocator to ask for stack temporaries.
2910 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002912 __ subl(ESP, Immediate(adjustment));
2913 }
2914
2915 // Load the value to the FP stack, using temporaries if needed.
2916 PushOntoFPStack(in, 0, adjustment, false, true);
2917
2918 if (out.IsDoubleStackSlot()) {
2919 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2920 } else {
2921 __ fstpl(Address(ESP, 0));
2922 Location stack_temp = Location::DoubleStackSlot(0);
2923 codegen_->Move64(out, stack_temp);
2924 }
2925
2926 // Remove the temporary stack space we allocated.
2927 if (adjustment != 0) {
2928 __ addl(ESP, Immediate(adjustment));
2929 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002930 break;
2931 }
2932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002934 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002935 break;
2936
2937 default:
2938 LOG(FATAL) << "Unexpected type conversion from " << input_type
2939 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002940 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002941 break;
2942
2943 default:
2944 LOG(FATAL) << "Unexpected type conversion from " << input_type
2945 << " to " << result_type;
2946 }
2947}
2948
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002951 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002952 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002954 locations->SetInAt(0, Location::RequiresRegister());
2955 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2957 break;
2958 }
2959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002961 locations->SetInAt(0, Location::RequiresRegister());
2962 locations->SetInAt(1, Location::Any());
2963 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002964 break;
2965 }
2966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kFloat32:
2968 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002969 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002970 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2971 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002972 } else if (add->InputAt(1)->IsConstant()) {
2973 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002974 } else {
2975 locations->SetInAt(1, Location::Any());
2976 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002978 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002979 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002980
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002981 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2983 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002984 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002985}
2986
2987void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2988 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 Location first = locations->InAt(0);
2990 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002991 Location out = locations->Out();
2992
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002996 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2997 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002998 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2999 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003000 } else {
3001 __ leal(out.AsRegister<Register>(), Address(
3002 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3003 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003005 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3006 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3007 __ addl(out.AsRegister<Register>(), Immediate(value));
3008 } else {
3009 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3010 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003011 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003012 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003014 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003015 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003016 }
3017
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003019 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003020 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3021 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003022 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003023 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3024 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003025 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003026 } else {
3027 DCHECK(second.IsConstant()) << second;
3028 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3029 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3030 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003031 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003032 break;
3033 }
3034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003035 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003036 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003038 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3039 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003040 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003041 __ addss(first.AsFpuRegister<XmmRegister>(),
3042 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003043 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3044 const_area->GetBaseMethodAddress(),
3045 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003046 } else {
3047 DCHECK(second.IsStackSlot());
3048 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003049 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003050 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 }
3052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003054 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003056 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3057 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003058 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003059 __ addsd(first.AsFpuRegister<XmmRegister>(),
3060 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003061 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3062 const_area->GetBaseMethodAddress(),
3063 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003064 } else {
3065 DCHECK(second.IsDoubleStackSlot());
3066 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003067 }
3068 break;
3069 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003070
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003071 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003072 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003073 }
3074}
3075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003076void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003077 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003078 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003079 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kInt32:
3081 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003082 locations->SetInAt(0, Location::RequiresRegister());
3083 locations->SetInAt(1, Location::Any());
3084 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003085 break;
3086 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003087 case DataType::Type::kFloat32:
3088 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003089 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003090 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3091 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003092 } else if (sub->InputAt(1)->IsConstant()) {
3093 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003094 } else {
3095 locations->SetInAt(1, Location::Any());
3096 }
Calin Juravle11351682014-10-23 15:38:15 +01003097 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003098 break;
Calin Juravle11351682014-10-23 15:38:15 +01003099 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003100
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003101 default:
Calin Juravle11351682014-10-23 15:38:15 +01003102 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003103 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003104}
3105
3106void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3107 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003108 Location first = locations->InAt(0);
3109 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003110 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003113 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003115 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003116 __ subl(first.AsRegister<Register>(),
3117 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003120 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003121 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003122 }
3123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003124 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003125 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003126 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3127 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003128 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003129 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003130 __ sbbl(first.AsRegisterPairHigh<Register>(),
3131 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003132 } else {
3133 DCHECK(second.IsConstant()) << second;
3134 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3135 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3136 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003137 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003138 break;
3139 }
3140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003142 if (second.IsFpuRegister()) {
3143 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3144 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3145 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003146 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 __ subss(first.AsFpuRegister<XmmRegister>(),
3148 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003149 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3150 const_area->GetBaseMethodAddress(),
3151 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003152 } else {
3153 DCHECK(second.IsStackSlot());
3154 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3155 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003156 break;
Calin Juravle11351682014-10-23 15:38:15 +01003157 }
3158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003159 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003160 if (second.IsFpuRegister()) {
3161 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3162 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3163 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003164 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 __ subsd(first.AsFpuRegister<XmmRegister>(),
3166 codegen_->LiteralDoubleAddress(
3167 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003168 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003169 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3170 } else {
3171 DCHECK(second.IsDoubleStackSlot());
3172 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3173 }
Calin Juravle11351682014-10-23 15:38:15 +01003174 break;
3175 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003176
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003177 default:
Calin Juravle11351682014-10-23 15:38:15 +01003178 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003179 }
3180}
3181
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182void LocationsBuilderX86::VisitMul(HMul* mul) {
3183 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003184 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003185 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003186 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 locations->SetInAt(0, Location::RequiresRegister());
3188 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003189 if (mul->InputAt(1)->IsIntConstant()) {
3190 // Can use 3 operand multiply.
3191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3192 } else {
3193 locations->SetOut(Location::SameAsFirstInput());
3194 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198 locations->SetInAt(1, Location::Any());
3199 locations->SetOut(Location::SameAsFirstInput());
3200 // Needed for imul on 32bits with 64bits output.
3201 locations->AddTemp(Location::RegisterLocation(EAX));
3202 locations->AddTemp(Location::RegisterLocation(EDX));
3203 break;
3204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kFloat32:
3206 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003207 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003208 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3209 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003210 } else if (mul->InputAt(1)->IsConstant()) {
3211 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003212 } else {
3213 locations->SetInAt(1, Location::Any());
3214 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003215 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003217 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218
3219 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003220 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221 }
3222}
3223
3224void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3225 LocationSummary* locations = mul->GetLocations();
3226 Location first = locations->InAt(0);
3227 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003228 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003229
3230 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003232 // The constant may have ended up in a register, so test explicitly to avoid
3233 // problems where the output may not be the same as the first operand.
3234 if (mul->InputAt(1)->IsIntConstant()) {
3235 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3236 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3237 } else if (second.IsRegister()) {
3238 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003240 } else {
3241 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003242 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003244 }
3245 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003248 Register in1_hi = first.AsRegisterPairHigh<Register>();
3249 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 Register eax = locations->GetTemp(0).AsRegister<Register>();
3251 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252
3253 DCHECK_EQ(EAX, eax);
3254 DCHECK_EQ(EDX, edx);
3255
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003256 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257 // output: in1
3258 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3259 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3260 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003261 if (second.IsConstant()) {
3262 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003263
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003264 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3265 int32_t low_value = Low32Bits(value);
3266 int32_t high_value = High32Bits(value);
3267 Immediate low(low_value);
3268 Immediate high(high_value);
3269
3270 __ movl(eax, high);
3271 // eax <- in1.lo * in2.hi
3272 __ imull(eax, in1_lo);
3273 // in1.hi <- in1.hi * in2.lo
3274 __ imull(in1_hi, low);
3275 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3276 __ addl(in1_hi, eax);
3277 // move in2_lo to eax to prepare for double precision
3278 __ movl(eax, low);
3279 // edx:eax <- in1.lo * in2.lo
3280 __ mull(in1_lo);
3281 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3282 __ addl(in1_hi, edx);
3283 // in1.lo <- (in1.lo * in2.lo)[31:0];
3284 __ movl(in1_lo, eax);
3285 } else if (second.IsRegisterPair()) {
3286 Register in2_hi = second.AsRegisterPairHigh<Register>();
3287 Register in2_lo = second.AsRegisterPairLow<Register>();
3288
3289 __ movl(eax, in2_hi);
3290 // eax <- in1.lo * in2.hi
3291 __ imull(eax, in1_lo);
3292 // in1.hi <- in1.hi * in2.lo
3293 __ imull(in1_hi, in2_lo);
3294 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3295 __ addl(in1_hi, eax);
3296 // move in1_lo to eax to prepare for double precision
3297 __ movl(eax, in1_lo);
3298 // edx:eax <- in1.lo * in2.lo
3299 __ mull(in2_lo);
3300 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3301 __ addl(in1_hi, edx);
3302 // in1.lo <- (in1.lo * in2.lo)[31:0];
3303 __ movl(in1_lo, eax);
3304 } else {
3305 DCHECK(second.IsDoubleStackSlot()) << second;
3306 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3307 Address in2_lo(ESP, second.GetStackIndex());
3308
3309 __ movl(eax, in2_hi);
3310 // eax <- in1.lo * in2.hi
3311 __ imull(eax, in1_lo);
3312 // in1.hi <- in1.hi * in2.lo
3313 __ imull(in1_hi, in2_lo);
3314 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3315 __ addl(in1_hi, eax);
3316 // move in1_lo to eax to prepare for double precision
3317 __ movl(eax, in1_lo);
3318 // edx:eax <- in1.lo * in2.lo
3319 __ mull(in2_lo);
3320 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3321 __ addl(in1_hi, edx);
3322 // in1.lo <- (in1.lo * in2.lo)[31:0];
3323 __ movl(in1_lo, eax);
3324 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003325
3326 break;
3327 }
3328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003330 DCHECK(first.Equals(locations->Out()));
3331 if (second.IsFpuRegister()) {
3332 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3333 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3334 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003335 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003336 __ mulss(first.AsFpuRegister<XmmRegister>(),
3337 codegen_->LiteralFloatAddress(
3338 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003339 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003340 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3341 } else {
3342 DCHECK(second.IsStackSlot());
3343 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3344 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003345 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003346 }
3347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003348 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 DCHECK(first.Equals(locations->Out()));
3350 if (second.IsFpuRegister()) {
3351 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3352 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3353 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003354 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003355 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3356 codegen_->LiteralDoubleAddress(
3357 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003358 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003359 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3360 } else {
3361 DCHECK(second.IsDoubleStackSlot());
3362 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3363 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003364 break;
3365 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003366
3367 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003368 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003369 }
3370}
3371
Roland Levillain232ade02015-04-20 15:14:36 +01003372void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3373 uint32_t temp_offset,
3374 uint32_t stack_adjustment,
3375 bool is_fp,
3376 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003377 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003378 DCHECK(!is_wide);
3379 if (is_fp) {
3380 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3381 } else {
3382 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3383 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003385 DCHECK(is_wide);
3386 if (is_fp) {
3387 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3388 } else {
3389 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 } else {
3392 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003393 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003394 Location stack_temp = Location::StackSlot(temp_offset);
3395 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003396 if (is_fp) {
3397 __ flds(Address(ESP, temp_offset));
3398 } else {
3399 __ filds(Address(ESP, temp_offset));
3400 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003401 } else {
3402 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3403 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003404 if (is_fp) {
3405 __ fldl(Address(ESP, temp_offset));
3406 } else {
3407 __ fildl(Address(ESP, temp_offset));
3408 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003409 }
3410 }
3411}
3412
3413void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 DataType::Type type = rem->GetResultType();
3415 bool is_float = type == DataType::Type::kFloat32;
3416 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003417 LocationSummary* locations = rem->GetLocations();
3418 Location first = locations->InAt(0);
3419 Location second = locations->InAt(1);
3420 Location out = locations->Out();
3421
3422 // Create stack space for 2 elements.
3423 // TODO: enhance register allocator to ask for stack temporaries.
3424 __ subl(ESP, Immediate(2 * elem_size));
3425
3426 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003427 const bool is_wide = !is_float;
3428 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3429 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003430
3431 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003432 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003433 __ Bind(&retry);
3434 __ fprem();
3435
3436 // Move FP status to AX.
3437 __ fstsw();
3438
3439 // And see if the argument reduction is complete. This is signaled by the
3440 // C2 FPU flag bit set to 0.
3441 __ andl(EAX, Immediate(kC2ConditionMask));
3442 __ j(kNotEqual, &retry);
3443
3444 // We have settled on the final value. Retrieve it into an XMM register.
3445 // Store FP top of stack to real stack.
3446 if (is_float) {
3447 __ fsts(Address(ESP, 0));
3448 } else {
3449 __ fstl(Address(ESP, 0));
3450 }
3451
3452 // Pop the 2 items from the FP stack.
3453 __ fucompp();
3454
3455 // Load the value from the stack into an XMM register.
3456 DCHECK(out.IsFpuRegister()) << out;
3457 if (is_float) {
3458 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3459 } else {
3460 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3461 }
3462
3463 // And remove the temporary stack space we allocated.
3464 __ addl(ESP, Immediate(2 * elem_size));
3465}
3466
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003467
3468void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3469 DCHECK(instruction->IsDiv() || instruction->IsRem());
3470
3471 LocationSummary* locations = instruction->GetLocations();
3472 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003473 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475 Register out_register = locations->Out().AsRegister<Register>();
3476 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003477 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003478
3479 DCHECK(imm == 1 || imm == -1);
3480
3481 if (instruction->IsRem()) {
3482 __ xorl(out_register, out_register);
3483 } else {
3484 __ movl(out_register, input_register);
3485 if (imm == -1) {
3486 __ negl(out_register);
3487 }
3488 }
3489}
3490
3491
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003492void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 LocationSummary* locations = instruction->GetLocations();
3494
3495 Register out_register = locations->Out().AsRegister<Register>();
3496 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003497 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003498 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3499 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 Register num = locations->GetTemp(0).AsRegister<Register>();
3502
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003503 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 __ testl(input_register, input_register);
3505 __ cmovl(kGreaterEqual, num, input_register);
3506 int shift = CTZ(imm);
3507 __ sarl(num, Immediate(shift));
3508
3509 if (imm < 0) {
3510 __ negl(num);
3511 }
3512
3513 __ movl(out_register, num);
3514}
3515
3516void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3517 DCHECK(instruction->IsDiv() || instruction->IsRem());
3518
3519 LocationSummary* locations = instruction->GetLocations();
3520 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3521
3522 Register eax = locations->InAt(0).AsRegister<Register>();
3523 Register out = locations->Out().AsRegister<Register>();
3524 Register num;
3525 Register edx;
3526
3527 if (instruction->IsDiv()) {
3528 edx = locations->GetTemp(0).AsRegister<Register>();
3529 num = locations->GetTemp(1).AsRegister<Register>();
3530 } else {
3531 edx = locations->Out().AsRegister<Register>();
3532 num = locations->GetTemp(0).AsRegister<Register>();
3533 }
3534
3535 DCHECK_EQ(EAX, eax);
3536 DCHECK_EQ(EDX, edx);
3537 if (instruction->IsDiv()) {
3538 DCHECK_EQ(EAX, out);
3539 } else {
3540 DCHECK_EQ(EDX, out);
3541 }
3542
3543 int64_t magic;
3544 int shift;
3545 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3546
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003547 // Save the numerator.
3548 __ movl(num, eax);
3549
3550 // EAX = magic
3551 __ movl(eax, Immediate(magic));
3552
3553 // EDX:EAX = magic * numerator
3554 __ imull(num);
3555
3556 if (imm > 0 && magic < 0) {
3557 // EDX += num
3558 __ addl(edx, num);
3559 } else if (imm < 0 && magic > 0) {
3560 __ subl(edx, num);
3561 }
3562
3563 // Shift if needed.
3564 if (shift != 0) {
3565 __ sarl(edx, Immediate(shift));
3566 }
3567
3568 // EDX += 1 if EDX < 0
3569 __ movl(eax, edx);
3570 __ shrl(edx, Immediate(31));
3571 __ addl(edx, eax);
3572
3573 if (instruction->IsRem()) {
3574 __ movl(eax, num);
3575 __ imull(edx, Immediate(imm));
3576 __ subl(eax, edx);
3577 __ movl(edx, eax);
3578 } else {
3579 __ movl(eax, edx);
3580 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581}
3582
Calin Juravlebacfec32014-11-14 15:54:36 +00003583void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3584 DCHECK(instruction->IsDiv() || instruction->IsRem());
3585
3586 LocationSummary* locations = instruction->GetLocations();
3587 Location out = locations->Out();
3588 Location first = locations->InAt(0);
3589 Location second = locations->InAt(1);
3590 bool is_div = instruction->IsDiv();
3591
3592 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003593 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003594 DCHECK_EQ(EAX, first.AsRegister<Register>());
3595 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003596
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003597 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003598 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599
3600 if (imm == 0) {
3601 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3602 } else if (imm == 1 || imm == -1) {
3603 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003604 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003605 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 } else {
3607 DCHECK(imm <= -2 || imm >= 2);
3608 GenerateDivRemWithAnyConstant(instruction);
3609 }
3610 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003611 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003612 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003614
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 Register second_reg = second.AsRegister<Register>();
3616 // 0x80000000/-1 triggers an arithmetic exception!
3617 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3618 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003619
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 __ cmpl(second_reg, Immediate(-1));
3621 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003622
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 // edx:eax <- sign-extended of eax
3624 __ cdq();
3625 // eax = quotient, edx = remainder
3626 __ idivl(second_reg);
3627 __ Bind(slow_path->GetExitLabel());
3628 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003629 break;
3630 }
3631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003632 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 InvokeRuntimeCallingConvention calling_convention;
3634 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3635 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3636 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3637 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3638 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3639 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3640
3641 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003642 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003643 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003644 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003645 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003646 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003647 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 break;
3649 }
3650
3651 default:
3652 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3653 }
3654}
3655
Calin Juravle7c4954d2014-10-28 16:57:40 +00003656void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003657 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003658 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003659 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003660 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661
Calin Juravle7c4954d2014-10-28 16:57:40 +00003662 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003664 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003665 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003666 locations->SetOut(Location::SameAsFirstInput());
3667 // Intel uses edx:eax as the dividend.
3668 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003669 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3670 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3671 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003672 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003673 locations->AddTemp(Location::RequiresRegister());
3674 }
Calin Juravled0d48522014-11-04 16:40:20 +00003675 break;
3676 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003677 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003678 InvokeRuntimeCallingConvention calling_convention;
3679 locations->SetInAt(0, Location::RegisterPairLocation(
3680 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3681 locations->SetInAt(1, Location::RegisterPairLocation(
3682 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3683 // Runtime helper puts the result in EAX, EDX.
3684 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003685 break;
3686 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 case DataType::Type::kFloat32:
3688 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003689 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003690 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3691 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003692 } else if (div->InputAt(1)->IsConstant()) {
3693 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003694 } else {
3695 locations->SetInAt(1, Location::Any());
3696 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003697 locations->SetOut(Location::SameAsFirstInput());
3698 break;
3699 }
3700
3701 default:
3702 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3703 }
3704}
3705
3706void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3707 LocationSummary* locations = div->GetLocations();
3708 Location first = locations->InAt(0);
3709 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003710
3711 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003712 case DataType::Type::kInt32:
3713 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003714 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003715 break;
3716 }
3717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003719 if (second.IsFpuRegister()) {
3720 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3721 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3722 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003723 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003724 __ divss(first.AsFpuRegister<XmmRegister>(),
3725 codegen_->LiteralFloatAddress(
3726 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003727 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3729 } else {
3730 DCHECK(second.IsStackSlot());
3731 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3732 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003733 break;
3734 }
3735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 if (second.IsFpuRegister()) {
3738 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3739 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3740 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003741 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003742 __ divsd(first.AsFpuRegister<XmmRegister>(),
3743 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003744 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3745 const_area->GetBaseMethodAddress(),
3746 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003747 } else {
3748 DCHECK(second.IsDoubleStackSlot());
3749 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3750 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003751 break;
3752 }
3753
3754 default:
3755 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3756 }
3757}
3758
Calin Juravlebacfec32014-11-14 15:54:36 +00003759void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003762 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003763 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003764 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003765 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003766
Calin Juravled2ec87d2014-12-08 14:24:46 +00003767 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003768 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003769 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003771 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003772 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3773 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3774 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003775 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003776 locations->AddTemp(Location::RequiresRegister());
3777 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 break;
3779 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 InvokeRuntimeCallingConvention calling_convention;
3782 locations->SetInAt(0, Location::RegisterPairLocation(
3783 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3784 locations->SetInAt(1, Location::RegisterPairLocation(
3785 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3786 // Runtime helper puts the result in EAX, EDX.
3787 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3788 break;
3789 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 case DataType::Type::kFloat64:
3791 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003792 locations->SetInAt(0, Location::Any());
3793 locations->SetInAt(1, Location::Any());
3794 locations->SetOut(Location::RequiresFpuRegister());
3795 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003796 break;
3797 }
3798
3799 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003800 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 }
3802}
3803
3804void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003805 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003806 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 case DataType::Type::kInt32:
3808 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 GenerateDivRemIntegral(rem);
3810 break;
3811 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003812 case DataType::Type::kFloat32:
3813 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003814 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003815 break;
3816 }
3817 default:
3818 LOG(FATAL) << "Unexpected rem type " << type;
3819 }
3820}
3821
Aart Bik1f8d51b2018-02-15 10:42:37 -08003822static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3823 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3824 switch (minmax->GetResultType()) {
3825 case DataType::Type::kInt32:
3826 locations->SetInAt(0, Location::RequiresRegister());
3827 locations->SetInAt(1, Location::RequiresRegister());
3828 locations->SetOut(Location::SameAsFirstInput());
3829 break;
3830 case DataType::Type::kInt64:
3831 locations->SetInAt(0, Location::RequiresRegister());
3832 locations->SetInAt(1, Location::RequiresRegister());
3833 locations->SetOut(Location::SameAsFirstInput());
3834 // Register to use to perform a long subtract to set cc.
3835 locations->AddTemp(Location::RequiresRegister());
3836 break;
3837 case DataType::Type::kFloat32:
3838 locations->SetInAt(0, Location::RequiresFpuRegister());
3839 locations->SetInAt(1, Location::RequiresFpuRegister());
3840 locations->SetOut(Location::SameAsFirstInput());
3841 locations->AddTemp(Location::RequiresRegister());
3842 break;
3843 case DataType::Type::kFloat64:
3844 locations->SetInAt(0, Location::RequiresFpuRegister());
3845 locations->SetInAt(1, Location::RequiresFpuRegister());
3846 locations->SetOut(Location::SameAsFirstInput());
3847 break;
3848 default:
3849 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3850 }
3851}
3852
Aart Bik351df3e2018-03-07 11:54:57 -08003853void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3854 bool is_min,
3855 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003856 Location op1_loc = locations->InAt(0);
3857 Location op2_loc = locations->InAt(1);
3858
3859 // Shortcut for same input locations.
3860 if (op1_loc.Equals(op2_loc)) {
3861 // Can return immediately, as op1_loc == out_loc.
3862 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3863 // a copy here.
3864 DCHECK(locations->Out().Equals(op1_loc));
3865 return;
3866 }
3867
3868 if (type == DataType::Type::kInt64) {
3869 // Need to perform a subtract to get the sign right.
3870 // op1 is already in the same location as the output.
3871 Location output = locations->Out();
3872 Register output_lo = output.AsRegisterPairLow<Register>();
3873 Register output_hi = output.AsRegisterPairHigh<Register>();
3874
3875 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3876 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3877
3878 // The comparison is performed by subtracting the second operand from
3879 // the first operand and then setting the status flags in the same
3880 // manner as the SUB instruction."
3881 __ cmpl(output_lo, op2_lo);
3882
3883 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3884 Register temp = locations->GetTemp(0).AsRegister<Register>();
3885 __ movl(temp, output_hi);
3886 __ sbbl(temp, op2_hi);
3887
3888 // Now the condition code is correct.
3889 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3890 __ cmovl(cond, output_lo, op2_lo);
3891 __ cmovl(cond, output_hi, op2_hi);
3892 } else {
3893 DCHECK_EQ(type, DataType::Type::kInt32);
3894 Register out = locations->Out().AsRegister<Register>();
3895 Register op2 = op2_loc.AsRegister<Register>();
3896
3897 // (out := op1)
3898 // out <=? op2
3899 // if out is min jmp done
3900 // out := op2
3901 // done:
3902
3903 __ cmpl(out, op2);
3904 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3905 __ cmovl(cond, out, op2);
3906 }
3907}
3908
3909void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3910 bool is_min,
3911 DataType::Type type) {
3912 Location op1_loc = locations->InAt(0);
3913 Location op2_loc = locations->InAt(1);
3914 Location out_loc = locations->Out();
3915 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3916
3917 // Shortcut for same input locations.
3918 if (op1_loc.Equals(op2_loc)) {
3919 DCHECK(out_loc.Equals(op1_loc));
3920 return;
3921 }
3922
3923 // (out := op1)
3924 // out <=? op2
3925 // if Nan jmp Nan_label
3926 // if out is min jmp done
3927 // if op2 is min jmp op2_label
3928 // handle -0/+0
3929 // jmp done
3930 // Nan_label:
3931 // out := NaN
3932 // op2_label:
3933 // out := op2
3934 // done:
3935 //
3936 // This removes one jmp, but needs to copy one input (op1) to out.
3937 //
3938 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3939
3940 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3941
3942 NearLabel nan, done, op2_label;
3943 if (type == DataType::Type::kFloat64) {
3944 __ ucomisd(out, op2);
3945 } else {
3946 DCHECK_EQ(type, DataType::Type::kFloat32);
3947 __ ucomiss(out, op2);
3948 }
3949
3950 __ j(Condition::kParityEven, &nan);
3951
3952 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3953 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3954
3955 // Handle 0.0/-0.0.
3956 if (is_min) {
3957 if (type == DataType::Type::kFloat64) {
3958 __ orpd(out, op2);
3959 } else {
3960 __ orps(out, op2);
3961 }
3962 } else {
3963 if (type == DataType::Type::kFloat64) {
3964 __ andpd(out, op2);
3965 } else {
3966 __ andps(out, op2);
3967 }
3968 }
3969 __ jmp(&done);
3970
3971 // NaN handling.
3972 __ Bind(&nan);
3973 if (type == DataType::Type::kFloat64) {
3974 // TODO: Use a constant from the constant table (requires extra input).
3975 __ LoadLongConstant(out, kDoubleNaN);
3976 } else {
3977 Register constant = locations->GetTemp(0).AsRegister<Register>();
3978 __ movl(constant, Immediate(kFloatNaN));
3979 __ movd(out, constant);
3980 }
3981 __ jmp(&done);
3982
3983 // out := op2;
3984 __ Bind(&op2_label);
3985 if (type == DataType::Type::kFloat64) {
3986 __ movsd(out, op2);
3987 } else {
3988 __ movss(out, op2);
3989 }
3990
3991 // Done.
3992 __ Bind(&done);
3993}
3994
Aart Bik351df3e2018-03-07 11:54:57 -08003995void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3996 DataType::Type type = minmax->GetResultType();
3997 switch (type) {
3998 case DataType::Type::kInt32:
3999 case DataType::Type::kInt64:
4000 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4001 break;
4002 case DataType::Type::kFloat32:
4003 case DataType::Type::kFloat64:
4004 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4005 break;
4006 default:
4007 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4008 }
4009}
4010
Aart Bik1f8d51b2018-02-15 10:42:37 -08004011void LocationsBuilderX86::VisitMin(HMin* min) {
4012 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4013}
4014
4015void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004016 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004017}
4018
4019void LocationsBuilderX86::VisitMax(HMax* max) {
4020 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4021}
4022
4023void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004024 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004025}
4026
Aart Bik3dad3412018-02-28 12:01:46 -08004027void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4028 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4029 switch (abs->GetResultType()) {
4030 case DataType::Type::kInt32:
4031 locations->SetInAt(0, Location::RegisterLocation(EAX));
4032 locations->SetOut(Location::SameAsFirstInput());
4033 locations->AddTemp(Location::RegisterLocation(EDX));
4034 break;
4035 case DataType::Type::kInt64:
4036 locations->SetInAt(0, Location::RequiresRegister());
4037 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4038 locations->AddTemp(Location::RequiresRegister());
4039 break;
4040 case DataType::Type::kFloat32:
4041 locations->SetInAt(0, Location::RequiresFpuRegister());
4042 locations->SetOut(Location::SameAsFirstInput());
4043 locations->AddTemp(Location::RequiresFpuRegister());
4044 locations->AddTemp(Location::RequiresRegister());
4045 break;
4046 case DataType::Type::kFloat64:
4047 locations->SetInAt(0, Location::RequiresFpuRegister());
4048 locations->SetOut(Location::SameAsFirstInput());
4049 locations->AddTemp(Location::RequiresFpuRegister());
4050 break;
4051 default:
4052 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4053 }
4054}
4055
4056void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4057 LocationSummary* locations = abs->GetLocations();
4058 switch (abs->GetResultType()) {
4059 case DataType::Type::kInt32: {
4060 Register out = locations->Out().AsRegister<Register>();
4061 DCHECK_EQ(out, EAX);
4062 Register temp = locations->GetTemp(0).AsRegister<Register>();
4063 DCHECK_EQ(temp, EDX);
4064 // Sign extend EAX into EDX.
4065 __ cdq();
4066 // XOR EAX with sign.
4067 __ xorl(EAX, EDX);
4068 // Subtract out sign to correct.
4069 __ subl(EAX, EDX);
4070 // The result is in EAX.
4071 break;
4072 }
4073 case DataType::Type::kInt64: {
4074 Location input = locations->InAt(0);
4075 Register input_lo = input.AsRegisterPairLow<Register>();
4076 Register input_hi = input.AsRegisterPairHigh<Register>();
4077 Location output = locations->Out();
4078 Register output_lo = output.AsRegisterPairLow<Register>();
4079 Register output_hi = output.AsRegisterPairHigh<Register>();
4080 Register temp = locations->GetTemp(0).AsRegister<Register>();
4081 // Compute the sign into the temporary.
4082 __ movl(temp, input_hi);
4083 __ sarl(temp, Immediate(31));
4084 // Store the sign into the output.
4085 __ movl(output_lo, temp);
4086 __ movl(output_hi, temp);
4087 // XOR the input to the output.
4088 __ xorl(output_lo, input_lo);
4089 __ xorl(output_hi, input_hi);
4090 // Subtract the sign.
4091 __ subl(output_lo, temp);
4092 __ sbbl(output_hi, temp);
4093 break;
4094 }
4095 case DataType::Type::kFloat32: {
4096 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4097 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4098 Register constant = locations->GetTemp(1).AsRegister<Register>();
4099 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4100 __ movd(temp, constant);
4101 __ andps(out, temp);
4102 break;
4103 }
4104 case DataType::Type::kFloat64: {
4105 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4106 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4107 // TODO: Use a constant from the constant table (requires extra input).
4108 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4109 __ andpd(out, temp);
4110 break;
4111 }
4112 default:
4113 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4114 }
4115}
4116
Calin Juravled0d48522014-11-04 16:40:20 +00004117void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004118 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004119 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004121 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 case DataType::Type::kInt8:
4123 case DataType::Type::kUint16:
4124 case DataType::Type::kInt16:
4125 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004126 locations->SetInAt(0, Location::Any());
4127 break;
4128 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004129 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004130 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4131 if (!instruction->IsConstant()) {
4132 locations->AddTemp(Location::RequiresRegister());
4133 }
4134 break;
4135 }
4136 default:
4137 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4138 }
Calin Juravled0d48522014-11-04 16:40:20 +00004139}
4140
4141void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004142 SlowPathCode* slow_path =
4143 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004144 codegen_->AddSlowPath(slow_path);
4145
4146 LocationSummary* locations = instruction->GetLocations();
4147 Location value = locations->InAt(0);
4148
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004149 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004151 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 case DataType::Type::kInt8:
4153 case DataType::Type::kUint16:
4154 case DataType::Type::kInt16:
4155 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004156 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004158 __ j(kEqual, slow_path->GetEntryLabel());
4159 } else if (value.IsStackSlot()) {
4160 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4161 __ j(kEqual, slow_path->GetEntryLabel());
4162 } else {
4163 DCHECK(value.IsConstant()) << value;
4164 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004165 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004166 }
4167 }
4168 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004169 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004171 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004172 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004173 __ movl(temp, value.AsRegisterPairLow<Register>());
4174 __ orl(temp, value.AsRegisterPairHigh<Register>());
4175 __ j(kEqual, slow_path->GetEntryLabel());
4176 } else {
4177 DCHECK(value.IsConstant()) << value;
4178 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4179 __ jmp(slow_path->GetEntryLabel());
4180 }
4181 }
4182 break;
4183 }
4184 default:
4185 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004186 }
Calin Juravled0d48522014-11-04 16:40:20 +00004187}
4188
Calin Juravle9aec02f2014-11-18 23:06:35 +00004189void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4190 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4191
4192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004193 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004194
4195 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004196 case DataType::Type::kInt32:
4197 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004198 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004199 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004200 // The shift count needs to be in CL or a constant.
4201 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004202 locations->SetOut(Location::SameAsFirstInput());
4203 break;
4204 }
4205 default:
4206 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4207 }
4208}
4209
4210void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4211 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4212
4213 LocationSummary* locations = op->GetLocations();
4214 Location first = locations->InAt(0);
4215 Location second = locations->InAt(1);
4216 DCHECK(first.Equals(locations->Out()));
4217
4218 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004220 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004221 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004222 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004224 DCHECK_EQ(ECX, second_reg);
4225 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004226 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004227 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004228 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004229 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004230 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004231 }
4232 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004233 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004234 if (shift == 0) {
4235 return;
4236 }
4237 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004238 if (op->IsShl()) {
4239 __ shll(first_reg, imm);
4240 } else if (op->IsShr()) {
4241 __ sarl(first_reg, imm);
4242 } else {
4243 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004244 }
4245 }
4246 break;
4247 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004248 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004249 if (second.IsRegister()) {
4250 Register second_reg = second.AsRegister<Register>();
4251 DCHECK_EQ(ECX, second_reg);
4252 if (op->IsShl()) {
4253 GenerateShlLong(first, second_reg);
4254 } else if (op->IsShr()) {
4255 GenerateShrLong(first, second_reg);
4256 } else {
4257 GenerateUShrLong(first, second_reg);
4258 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004259 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004260 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004261 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004262 // Nothing to do if the shift is 0, as the input is already the output.
4263 if (shift != 0) {
4264 if (op->IsShl()) {
4265 GenerateShlLong(first, shift);
4266 } else if (op->IsShr()) {
4267 GenerateShrLong(first, shift);
4268 } else {
4269 GenerateUShrLong(first, shift);
4270 }
4271 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004272 }
4273 break;
4274 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004275 default:
4276 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4277 }
4278}
4279
Mark P Mendell73945692015-04-29 14:56:17 +00004280void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4281 Register low = loc.AsRegisterPairLow<Register>();
4282 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004283 if (shift == 1) {
4284 // This is just an addition.
4285 __ addl(low, low);
4286 __ adcl(high, high);
4287 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004288 // Shift by 32 is easy. High gets low, and low gets 0.
4289 codegen_->EmitParallelMoves(
4290 loc.ToLow(),
4291 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004292 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004293 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4294 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004296 } else if (shift > 32) {
4297 // Low part becomes 0. High part is low part << (shift-32).
4298 __ movl(high, low);
4299 __ shll(high, Immediate(shift - 32));
4300 __ xorl(low, low);
4301 } else {
4302 // Between 1 and 31.
4303 __ shld(high, low, Immediate(shift));
4304 __ shll(low, Immediate(shift));
4305 }
4306}
4307
Calin Juravle9aec02f2014-11-18 23:06:35 +00004308void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004309 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004310 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4311 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4312 __ testl(shifter, Immediate(32));
4313 __ j(kEqual, &done);
4314 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4315 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4316 __ Bind(&done);
4317}
4318
Mark P Mendell73945692015-04-29 14:56:17 +00004319void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4320 Register low = loc.AsRegisterPairLow<Register>();
4321 Register high = loc.AsRegisterPairHigh<Register>();
4322 if (shift == 32) {
4323 // Need to copy the sign.
4324 DCHECK_NE(low, high);
4325 __ movl(low, high);
4326 __ sarl(high, Immediate(31));
4327 } else if (shift > 32) {
4328 DCHECK_NE(low, high);
4329 // High part becomes sign. Low part is shifted by shift - 32.
4330 __ movl(low, high);
4331 __ sarl(high, Immediate(31));
4332 __ sarl(low, Immediate(shift - 32));
4333 } else {
4334 // Between 1 and 31.
4335 __ shrd(low, high, Immediate(shift));
4336 __ sarl(high, Immediate(shift));
4337 }
4338}
4339
Calin Juravle9aec02f2014-11-18 23:06:35 +00004340void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004341 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004342 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4343 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4344 __ testl(shifter, Immediate(32));
4345 __ j(kEqual, &done);
4346 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4347 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4348 __ Bind(&done);
4349}
4350
Mark P Mendell73945692015-04-29 14:56:17 +00004351void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4352 Register low = loc.AsRegisterPairLow<Register>();
4353 Register high = loc.AsRegisterPairHigh<Register>();
4354 if (shift == 32) {
4355 // Shift by 32 is easy. Low gets high, and high gets 0.
4356 codegen_->EmitParallelMoves(
4357 loc.ToHigh(),
4358 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004360 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4361 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004363 } else if (shift > 32) {
4364 // Low part is high >> (shift - 32). High part becomes 0.
4365 __ movl(low, high);
4366 __ shrl(low, Immediate(shift - 32));
4367 __ xorl(high, high);
4368 } else {
4369 // Between 1 and 31.
4370 __ shrd(low, high, Immediate(shift));
4371 __ shrl(high, Immediate(shift));
4372 }
4373}
4374
Calin Juravle9aec02f2014-11-18 23:06:35 +00004375void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004376 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004377 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4378 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4379 __ testl(shifter, Immediate(32));
4380 __ j(kEqual, &done);
4381 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4382 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4383 __ Bind(&done);
4384}
4385
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004386void LocationsBuilderX86::VisitRor(HRor* ror) {
4387 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004388 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004389
4390 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004391 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004392 // Add the temporary needed.
4393 locations->AddTemp(Location::RequiresRegister());
4394 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004395 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004396 locations->SetInAt(0, Location::RequiresRegister());
4397 // The shift count needs to be in CL (unless it is a constant).
4398 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4399 locations->SetOut(Location::SameAsFirstInput());
4400 break;
4401 default:
4402 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4403 UNREACHABLE();
4404 }
4405}
4406
4407void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4408 LocationSummary* locations = ror->GetLocations();
4409 Location first = locations->InAt(0);
4410 Location second = locations->InAt(1);
4411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004412 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004413 Register first_reg = first.AsRegister<Register>();
4414 if (second.IsRegister()) {
4415 Register second_reg = second.AsRegister<Register>();
4416 __ rorl(first_reg, second_reg);
4417 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004418 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004419 __ rorl(first_reg, imm);
4420 }
4421 return;
4422 }
4423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004425 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4426 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4427 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4428 if (second.IsRegister()) {
4429 Register second_reg = second.AsRegister<Register>();
4430 DCHECK_EQ(second_reg, ECX);
4431 __ movl(temp_reg, first_reg_hi);
4432 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4433 __ shrd(first_reg_lo, temp_reg, second_reg);
4434 __ movl(temp_reg, first_reg_hi);
4435 __ testl(second_reg, Immediate(32));
4436 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4437 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4438 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004439 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004440 if (shift_amt == 0) {
4441 // Already fine.
4442 return;
4443 }
4444 if (shift_amt == 32) {
4445 // Just swap.
4446 __ movl(temp_reg, first_reg_lo);
4447 __ movl(first_reg_lo, first_reg_hi);
4448 __ movl(first_reg_hi, temp_reg);
4449 return;
4450 }
4451
4452 Immediate imm(shift_amt);
4453 // Save the constents of the low value.
4454 __ movl(temp_reg, first_reg_lo);
4455
4456 // Shift right into low, feeding bits from high.
4457 __ shrd(first_reg_lo, first_reg_hi, imm);
4458
4459 // Shift right into high, feeding bits from the original low.
4460 __ shrd(first_reg_hi, temp_reg, imm);
4461
4462 // Swap if needed.
4463 if (shift_amt > 32) {
4464 __ movl(temp_reg, first_reg_lo);
4465 __ movl(first_reg_lo, first_reg_hi);
4466 __ movl(first_reg_hi, temp_reg);
4467 }
4468 }
4469}
4470
Calin Juravle9aec02f2014-11-18 23:06:35 +00004471void LocationsBuilderX86::VisitShl(HShl* shl) {
4472 HandleShift(shl);
4473}
4474
4475void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4476 HandleShift(shl);
4477}
4478
4479void LocationsBuilderX86::VisitShr(HShr* shr) {
4480 HandleShift(shr);
4481}
4482
4483void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4484 HandleShift(shr);
4485}
4486
4487void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4488 HandleShift(ushr);
4489}
4490
4491void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4492 HandleShift(ushr);
4493}
4494
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004495void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004496 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4497 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004498 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004499 InvokeRuntimeCallingConvention calling_convention;
4500 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004501}
4502
4503void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004504 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4505 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4506 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004507}
4508
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004509void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004510 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4511 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004512 locations->SetOut(Location::RegisterLocation(EAX));
4513 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004514 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4515 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004516}
4517
4518void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004519 // Note: if heap poisoning is enabled, the entry point takes cares
4520 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004521 QuickEntrypointEnum entrypoint =
4522 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4523 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004524 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525 DCHECK(!codegen_->IsLeafMethod());
4526}
4527
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004528void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004529 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004530 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004531 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4532 if (location.IsStackSlot()) {
4533 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4534 } else if (location.IsDoubleStackSlot()) {
4535 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004536 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004537 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004538}
4539
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004540void InstructionCodeGeneratorX86::VisitParameterValue(
4541 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4542}
4543
4544void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4545 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004546 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004547 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4548}
4549
4550void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004551}
4552
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004553void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4554 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004555 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004556 locations->SetInAt(0, Location::RequiresRegister());
4557 locations->SetOut(Location::RequiresRegister());
4558}
4559
4560void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4561 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004562 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004563 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004564 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004565 __ movl(locations->Out().AsRegister<Register>(),
4566 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004567 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004568 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004569 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004570 __ movl(locations->Out().AsRegister<Register>(),
4571 Address(locations->InAt(0).AsRegister<Register>(),
4572 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4573 // temp = temp->GetImtEntryAt(method_offset);
4574 __ movl(locations->Out().AsRegister<Register>(),
4575 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004576 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004577}
4578
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004579void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004581 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004582 locations->SetInAt(0, Location::RequiresRegister());
4583 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004584}
4585
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004586void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4587 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004588 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004589 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004590 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004591 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004592 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004593 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004594 break;
4595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004596 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004597 __ notl(out.AsRegisterPairLow<Register>());
4598 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004599 break;
4600
4601 default:
4602 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4603 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004604}
4605
David Brazdil66d126e2015-04-03 16:02:44 +01004606void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4607 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004608 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004609 locations->SetInAt(0, Location::RequiresRegister());
4610 locations->SetOut(Location::SameAsFirstInput());
4611}
4612
4613void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004614 LocationSummary* locations = bool_not->GetLocations();
4615 Location in = locations->InAt(0);
4616 Location out = locations->Out();
4617 DCHECK(in.Equals(out));
4618 __ xorl(out.AsRegister<Register>(), Immediate(1));
4619}
4620
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004621void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004622 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004623 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004624 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004625 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004626 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004627 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004628 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004629 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004630 case DataType::Type::kInt32:
4631 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004632 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004633 locations->SetInAt(1, Location::Any());
4634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4635 break;
4636 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kFloat32:
4638 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004639 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004640 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4641 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4642 } else if (compare->InputAt(1)->IsConstant()) {
4643 locations->SetInAt(1, Location::RequiresFpuRegister());
4644 } else {
4645 locations->SetInAt(1, Location::Any());
4646 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004647 locations->SetOut(Location::RequiresRegister());
4648 break;
4649 }
4650 default:
4651 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4652 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004653}
4654
4655void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004656 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004658 Location left = locations->InAt(0);
4659 Location right = locations->InAt(1);
4660
Mark Mendell0c9497d2015-08-21 09:30:05 -04004661 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004662 Condition less_cond = kLess;
4663
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004664 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004665 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004666 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004667 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004668 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004669 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004671 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004672 break;
4673 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004675 Register left_low = left.AsRegisterPairLow<Register>();
4676 Register left_high = left.AsRegisterPairHigh<Register>();
4677 int32_t val_low = 0;
4678 int32_t val_high = 0;
4679 bool right_is_const = false;
4680
4681 if (right.IsConstant()) {
4682 DCHECK(right.GetConstant()->IsLongConstant());
4683 right_is_const = true;
4684 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4685 val_low = Low32Bits(val);
4686 val_high = High32Bits(val);
4687 }
4688
Calin Juravleddb7df22014-11-25 20:56:51 +00004689 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004690 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004691 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004692 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004693 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004694 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004695 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004696 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004697 __ j(kLess, &less); // Signed compare.
4698 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004699 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004700 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004701 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004702 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004703 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004704 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004705 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004706 }
Aart Bika19616e2016-02-01 18:57:58 -08004707 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004708 break;
4709 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004710 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004711 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004712 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004713 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004714 break;
4715 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004716 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004717 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004718 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004719 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004720 break;
4721 }
4722 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004723 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004724 }
Aart Bika19616e2016-02-01 18:57:58 -08004725
Calin Juravleddb7df22014-11-25 20:56:51 +00004726 __ movl(out, Immediate(0));
4727 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004728 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004729
4730 __ Bind(&greater);
4731 __ movl(out, Immediate(1));
4732 __ jmp(&done);
4733
4734 __ Bind(&less);
4735 __ movl(out, Immediate(-1));
4736
4737 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004738}
4739
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004740void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004742 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004743 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004744 locations->SetInAt(i, Location::Any());
4745 }
4746 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004747}
4748
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004749void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004750 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004751}
4752
Roland Levillain7c1559a2015-12-15 10:55:36 +00004753void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004754 /*
4755 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4756 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4757 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4758 */
4759 switch (kind) {
4760 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004761 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004762 break;
4763 }
4764 case MemBarrierKind::kAnyStore:
4765 case MemBarrierKind::kLoadAny:
4766 case MemBarrierKind::kStoreStore: {
4767 // nop
4768 break;
4769 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004770 case MemBarrierKind::kNTStoreStore:
4771 // Non-Temporal Store/Store needs an explicit fence.
4772 MemoryFence(/* non-temporal */ true);
4773 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004774 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004775}
4776
Vladimir Markodc151b22015-10-15 18:02:30 +01004777HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4778 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004779 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004780 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004781}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004783Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4784 Register temp) {
4785 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004786 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004787 if (!invoke->GetLocations()->Intrinsified()) {
4788 return location.AsRegister<Register>();
4789 }
4790 // For intrinsics we allow any location, so it may be on the stack.
4791 if (!location.IsRegister()) {
4792 __ movl(temp, Address(ESP, location.GetStackIndex()));
4793 return temp;
4794 }
4795 // For register locations, check if the register was saved. If so, get it from the stack.
4796 // Note: There is a chance that the register was saved but not overwritten, so we could
4797 // save one load. However, since this is just an intrinsic slow path we prefer this
4798 // simple and more robust approach rather that trying to determine if that's the case.
4799 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004800 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4801 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4802 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4803 __ movl(temp, Address(ESP, stack_offset));
4804 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004805 }
4806 return location.AsRegister<Register>();
4807}
4808
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004809void CodeGeneratorX86::GenerateStaticOrDirectCall(
4810 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004811 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4812 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004813 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004814 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004815 uint32_t offset =
4816 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4817 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004818 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004819 }
Vladimir Marko58155012015-08-19 12:49:41 +00004820 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004821 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004822 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004823 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4824 DCHECK(GetCompilerOptions().IsBootImage());
4825 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4826 temp.AsRegister<Register>());
4827 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004828 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004829 break;
4830 }
Vladimir Markob066d432018-01-03 13:14:37 +00004831 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4832 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4833 temp.AsRegister<Register>());
4834 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4835 RecordBootImageRelRoPatch(
4836 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004837 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004838 break;
4839 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004840 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004841 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4842 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004843 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004844 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004845 break;
4846 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004847 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4848 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4849 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004850 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4851 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4852 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004853 }
Vladimir Marko58155012015-08-19 12:49:41 +00004854 }
4855
4856 switch (invoke->GetCodePtrLocation()) {
4857 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4858 __ call(GetFrameEntryLabel());
4859 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004860 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4861 // (callee_method + offset_of_quick_compiled_code)()
4862 __ call(Address(callee_method.AsRegister<Register>(),
4863 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004864 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004865 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004866 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004867 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004868
4869 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004870}
4871
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004872void CodeGeneratorX86::GenerateVirtualCall(
4873 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004874 Register temp = temp_in.AsRegister<Register>();
4875 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4876 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004877
4878 // Use the calling convention instead of the location of the receiver, as
4879 // intrinsics may have put the receiver in a different register. In the intrinsics
4880 // slow path, the arguments have been moved to the right place, so here we are
4881 // guaranteed that the receiver is the first register of the calling convention.
4882 InvokeDexCallingConvention calling_convention;
4883 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004884 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004885 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004886 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004887 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004888 // Instead of simply (possibly) unpoisoning `temp` here, we should
4889 // emit a read barrier for the previous class reference load.
4890 // However this is not required in practice, as this is an
4891 // intermediate/temporary reference and because the current
4892 // concurrent copying collector keeps the from-space memory
4893 // intact/accessible until the end of the marking phase (the
4894 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004895 __ MaybeUnpoisonHeapReference(temp);
4896 // temp = temp->GetMethodAt(method_offset);
4897 __ movl(temp, Address(temp, method_offset));
4898 // call temp->GetEntryPoint();
4899 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004900 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004901 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004902}
4903
Vladimir Marko6fd16062018-06-26 11:02:04 +01004904void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
4905 uint32_t intrinsic_data) {
4906 boot_image_intrinsic_patches_.emplace_back(
4907 method_address, /* target_dex_file */ nullptr, intrinsic_data);
4908 __ Bind(&boot_image_intrinsic_patches_.back().label);
4909}
4910
Vladimir Markob066d432018-01-03 13:14:37 +00004911void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4912 uint32_t boot_image_offset) {
4913 boot_image_method_patches_.emplace_back(
4914 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4915 __ Bind(&boot_image_method_patches_.back().label);
4916}
4917
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004918void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004919 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004920 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004921 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004922 boot_image_method_patches_.emplace_back(
4923 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004924 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004925}
4926
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004927void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4928 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4929 HX86ComputeBaseMethodAddress* method_address =
4930 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004931 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004932 method_bss_entry_patches_.emplace_back(
4933 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4934 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004935}
4936
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004937void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4938 HX86ComputeBaseMethodAddress* method_address =
4939 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4940 boot_image_type_patches_.emplace_back(
4941 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004942 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004943}
4944
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004945Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004946 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004947 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4948 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004949 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004950 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004951}
4952
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004953void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4954 HX86ComputeBaseMethodAddress* method_address =
4955 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4956 boot_image_string_patches_.emplace_back(
4957 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4958 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004959}
4960
Vladimir Markoaad75c62016-10-03 08:46:48 +00004961Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4962 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004963 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004964 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004965 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004966 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004967 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004968}
4969
Vladimir Markoeebb8212018-06-05 14:57:24 +01004970void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004971 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01004972 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004973 if (GetCompilerOptions().IsBootImage()) {
4974 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4975 HX86ComputeBaseMethodAddress* method_address =
4976 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4977 DCHECK(method_address != nullptr);
4978 Register method_address_reg =
4979 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4980 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
4981 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004982 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004983 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));
Vladimir Marko6fd16062018-06-26 11:02:04 +01004990 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004991 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004992 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004993 gc::Heap* heap = Runtime::Current()->GetHeap();
4994 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004995 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004996 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
4997 }
4998}
4999
Vladimir Marko6fd16062018-06-26 11:02:04 +01005000void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5001 uint32_t boot_image_offset) {
5002 DCHECK(invoke->IsStatic());
5003 InvokeRuntimeCallingConvention calling_convention;
5004 Register argument = calling_convention.GetRegisterAt(0);
5005 if (GetCompilerOptions().IsBootImage()) {
5006 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5007 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5008 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5009 HX86ComputeBaseMethodAddress* method_address =
5010 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5011 DCHECK(method_address != nullptr);
5012 Register method_address_reg =
5013 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5014 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5015 MethodReference target_method = invoke->GetTargetMethod();
5016 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5017 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5018 __ Bind(&boot_image_type_patches_.back().label);
5019 } else {
5020 LoadBootImageAddress(argument, boot_image_offset, invoke);
5021 }
5022 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5023 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5024}
5025
Vladimir Markoaad75c62016-10-03 08:46:48 +00005026// The label points to the end of the "movl" or another instruction but the literal offset
5027// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5028constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5029
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005030template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005031inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005032 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005033 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005034 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005035 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005036 linker_patches->push_back(Factory(literal_offset,
5037 info.target_dex_file,
5038 GetMethodAddressOffset(info.method_address),
5039 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005040 }
5041}
5042
Vladimir Marko6fd16062018-06-26 11:02:04 +01005043template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5044linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5045 const DexFile* target_dex_file,
5046 uint32_t pc_insn_offset,
5047 uint32_t boot_image_offset) {
5048 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5049 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005050}
5051
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005052void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005053 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005054 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005055 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005056 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005057 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005058 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005059 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005060 string_bss_entry_patches_.size() +
5061 boot_image_intrinsic_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005062 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005063 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005064 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5065 boot_image_method_patches_, linker_patches);
5066 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5067 boot_image_type_patches_, linker_patches);
5068 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005069 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005070 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5071 boot_image_intrinsic_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005072 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005073 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00005074 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005075 DCHECK(boot_image_type_patches_.empty());
5076 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005077 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005078 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005079 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5080 method_bss_entry_patches_, linker_patches);
5081 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5082 type_bss_entry_patches_, linker_patches);
5083 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5084 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005085 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005086}
5087
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005088void CodeGeneratorX86::MarkGCCard(Register temp,
5089 Register card,
5090 Register object,
5091 Register value,
5092 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005093 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005094 if (value_can_be_null) {
5095 __ testl(value, value);
5096 __ j(kEqual, &is_null);
5097 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005098 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099 __ movl(temp, object);
5100 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005101 __ movb(Address(temp, card, TIMES_1, 0),
5102 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005103 if (value_can_be_null) {
5104 __ Bind(&is_null);
5105 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106}
5107
Calin Juravle52c48962014-12-16 17:02:57 +00005108void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5109 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005110
5111 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005112 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005113 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005114 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5115 kEmitCompilerReadBarrier
5116 ? LocationSummary::kCallOnSlowPath
5117 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005118 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005119 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005120 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005121 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005123 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005124 locations->SetOut(Location::RequiresFpuRegister());
5125 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005126 // The output overlaps in case of long: we don't want the low move
5127 // to overwrite the object's location. Likewise, in the case of
5128 // an object field get with read barriers enabled, we do not want
5129 // the move to overwrite the object's location, as we need it to emit
5130 // the read barrier.
5131 locations->SetOut(
5132 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005134 Location::kOutputOverlap :
5135 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005136 }
Calin Juravle52c48962014-12-16 17:02:57 +00005137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005139 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005140 // So we use an XMM register as a temp to achieve atomicity (first
5141 // load the temp into the XMM and then copy the XMM into the
5142 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005143 locations->AddTemp(Location::RequiresFpuRegister());
5144 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145}
5146
Calin Juravle52c48962014-12-16 17:02:57 +00005147void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5148 const FieldInfo& field_info) {
5149 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150
Calin Juravle52c48962014-12-16 17:02:57 +00005151 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005152 Location base_loc = locations->InAt(0);
5153 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005154 Location out = locations->Out();
5155 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005156 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5157 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005158 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5159
Vladimir Marko61b92282017-10-11 13:23:17 +01005160 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005161 case DataType::Type::kBool:
5162 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005163 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164 break;
5165 }
5166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005167 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005168 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169 break;
5170 }
5171
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005172 case DataType::Type::kUint16: {
5173 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005174 break;
5175 }
5176
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005177 case DataType::Type::kInt16: {
5178 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179 break;
5180 }
5181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005183 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005187 // /* HeapReference<Object> */ out = *(base + offset)
5188 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005189 // Note that a potential implicit null check is handled in this
5190 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5191 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005192 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005193 if (is_volatile) {
5194 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5195 }
5196 } else {
5197 __ movl(out.AsRegister<Register>(), Address(base, offset));
5198 codegen_->MaybeRecordImplicitNullCheck(instruction);
5199 if (is_volatile) {
5200 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5201 }
5202 // If read barriers are enabled, emit read barriers other than
5203 // Baker's using a slow path (and also unpoison the loaded
5204 // reference, if heap poisoning is enabled).
5205 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5206 }
5207 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208 }
5209
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005210 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005211 if (is_volatile) {
5212 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5213 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005215 __ movd(out.AsRegisterPairLow<Register>(), temp);
5216 __ psrlq(temp, Immediate(32));
5217 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5218 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005219 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005220 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005222 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5223 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005224 break;
5225 }
5226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005227 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005228 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005229 break;
5230 }
5231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005233 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005234 break;
5235 }
5236
Aart Bik66c158e2018-01-31 12:55:04 -08005237 case DataType::Type::kUint32:
5238 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005239 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005240 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005241 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005242 }
Calin Juravle52c48962014-12-16 17:02:57 +00005243
Vladimir Marko61b92282017-10-11 13:23:17 +01005244 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005245 // Potential implicit null checks, in the case of reference or
5246 // long fields, are handled in the previous switch statement.
5247 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005248 codegen_->MaybeRecordImplicitNullCheck(instruction);
5249 }
5250
Calin Juravle52c48962014-12-16 17:02:57 +00005251 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005252 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005253 // Memory barriers, in the case of references, are also handled
5254 // in the previous switch statement.
5255 } else {
5256 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5257 }
Roland Levillain4d027112015-07-01 15:41:14 +01005258 }
Calin Juravle52c48962014-12-16 17:02:57 +00005259}
5260
5261void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5262 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5263
5264 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005265 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005266 locations->SetInAt(0, Location::RequiresRegister());
5267 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005268 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005269 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005270
5271 // The register allocator does not support multiple
5272 // inputs that die at entry with one in a specific register.
5273 if (is_byte_type) {
5274 // Ensure the value is in a byte register.
5275 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 } else if (DataType::IsFloatingPointType(field_type)) {
5277 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005278 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5279 locations->SetInAt(1, Location::RequiresFpuRegister());
5280 } else {
5281 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5282 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005283 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005284 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005285 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005286
Calin Juravle52c48962014-12-16 17:02:57 +00005287 // 64bits value can be atomically written to an address with movsd and an XMM register.
5288 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5289 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5290 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5291 // isolated cases when we need this it isn't worth adding the extra complexity.
5292 locations->AddTemp(Location::RequiresFpuRegister());
5293 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005294 } else {
5295 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5296
5297 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5298 // Temporary registers for the write barrier.
5299 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5300 // Ensure the card is in a byte register.
5301 locations->AddTemp(Location::RegisterLocation(ECX));
5302 }
Calin Juravle52c48962014-12-16 17:02:57 +00005303 }
5304}
5305
5306void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005307 const FieldInfo& field_info,
5308 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005309 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5310
5311 LocationSummary* locations = instruction->GetLocations();
5312 Register base = locations->InAt(0).AsRegister<Register>();
5313 Location value = locations->InAt(1);
5314 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005316 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005317 bool needs_write_barrier =
5318 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005319
5320 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005321 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005322 }
5323
Mark Mendell81489372015-11-04 11:30:41 -05005324 bool maybe_record_implicit_null_check_done = false;
5325
Calin Juravle52c48962014-12-16 17:02:57 +00005326 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005327 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005328 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005329 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005330 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5331 break;
5332 }
5333
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005334 case DataType::Type::kUint16:
5335 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005336 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005337 __ movw(Address(base, offset),
5338 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005339 } else {
5340 __ movw(Address(base, offset), value.AsRegister<Register>());
5341 }
Calin Juravle52c48962014-12-16 17:02:57 +00005342 break;
5343 }
5344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005345 case DataType::Type::kInt32:
5346 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005347 if (kPoisonHeapReferences && needs_write_barrier) {
5348 // Note that in the case where `value` is a null reference,
5349 // we do not enter this block, as the reference does not
5350 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005352 Register temp = locations->GetTemp(0).AsRegister<Register>();
5353 __ movl(temp, value.AsRegister<Register>());
5354 __ PoisonHeapReference(temp);
5355 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005356 } else if (value.IsConstant()) {
5357 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5358 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005359 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005360 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005361 __ movl(Address(base, offset), value.AsRegister<Register>());
5362 }
Calin Juravle52c48962014-12-16 17:02:57 +00005363 break;
5364 }
5365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005367 if (is_volatile) {
5368 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5369 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5370 __ movd(temp1, value.AsRegisterPairLow<Register>());
5371 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5372 __ punpckldq(temp1, temp2);
5373 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005374 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005375 } else if (value.IsConstant()) {
5376 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5377 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5378 codegen_->MaybeRecordImplicitNullCheck(instruction);
5379 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005380 } else {
5381 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005382 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005383 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5384 }
Mark Mendell81489372015-11-04 11:30:41 -05005385 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005386 break;
5387 }
5388
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005389 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005390 if (value.IsConstant()) {
5391 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5392 __ movl(Address(base, offset), Immediate(v));
5393 } else {
5394 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5395 }
Calin Juravle52c48962014-12-16 17:02:57 +00005396 break;
5397 }
5398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005400 if (value.IsConstant()) {
5401 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5402 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5403 codegen_->MaybeRecordImplicitNullCheck(instruction);
5404 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5405 maybe_record_implicit_null_check_done = true;
5406 } else {
5407 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5408 }
Calin Juravle52c48962014-12-16 17:02:57 +00005409 break;
5410 }
5411
Aart Bik66c158e2018-01-31 12:55:04 -08005412 case DataType::Type::kUint32:
5413 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005414 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005415 LOG(FATAL) << "Unreachable type " << field_type;
5416 UNREACHABLE();
5417 }
5418
Mark Mendell81489372015-11-04 11:30:41 -05005419 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005420 codegen_->MaybeRecordImplicitNullCheck(instruction);
5421 }
5422
Roland Levillain4d027112015-07-01 15:41:14 +01005423 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005424 Register temp = locations->GetTemp(0).AsRegister<Register>();
5425 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005426 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005427 }
5428
Calin Juravle52c48962014-12-16 17:02:57 +00005429 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005430 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005431 }
5432}
5433
5434void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5435 HandleFieldGet(instruction, instruction->GetFieldInfo());
5436}
5437
5438void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5439 HandleFieldGet(instruction, instruction->GetFieldInfo());
5440}
5441
5442void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5443 HandleFieldSet(instruction, instruction->GetFieldInfo());
5444}
5445
5446void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005447 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005448}
5449
5450void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5451 HandleFieldSet(instruction, instruction->GetFieldInfo());
5452}
5453
5454void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005455 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005456}
5457
5458void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5459 HandleFieldGet(instruction, instruction->GetFieldInfo());
5460}
5461
5462void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5463 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005464}
5465
Calin Juravlee460d1d2015-09-29 04:52:17 +01005466void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5467 HUnresolvedInstanceFieldGet* instruction) {
5468 FieldAccessCallingConventionX86 calling_convention;
5469 codegen_->CreateUnresolvedFieldLocationSummary(
5470 instruction, instruction->GetFieldType(), calling_convention);
5471}
5472
5473void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5474 HUnresolvedInstanceFieldGet* instruction) {
5475 FieldAccessCallingConventionX86 calling_convention;
5476 codegen_->GenerateUnresolvedFieldAccess(instruction,
5477 instruction->GetFieldType(),
5478 instruction->GetFieldIndex(),
5479 instruction->GetDexPc(),
5480 calling_convention);
5481}
5482
5483void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5484 HUnresolvedInstanceFieldSet* instruction) {
5485 FieldAccessCallingConventionX86 calling_convention;
5486 codegen_->CreateUnresolvedFieldLocationSummary(
5487 instruction, instruction->GetFieldType(), calling_convention);
5488}
5489
5490void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5491 HUnresolvedInstanceFieldSet* instruction) {
5492 FieldAccessCallingConventionX86 calling_convention;
5493 codegen_->GenerateUnresolvedFieldAccess(instruction,
5494 instruction->GetFieldType(),
5495 instruction->GetFieldIndex(),
5496 instruction->GetDexPc(),
5497 calling_convention);
5498}
5499
5500void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5501 HUnresolvedStaticFieldGet* instruction) {
5502 FieldAccessCallingConventionX86 calling_convention;
5503 codegen_->CreateUnresolvedFieldLocationSummary(
5504 instruction, instruction->GetFieldType(), calling_convention);
5505}
5506
5507void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5508 HUnresolvedStaticFieldGet* instruction) {
5509 FieldAccessCallingConventionX86 calling_convention;
5510 codegen_->GenerateUnresolvedFieldAccess(instruction,
5511 instruction->GetFieldType(),
5512 instruction->GetFieldIndex(),
5513 instruction->GetDexPc(),
5514 calling_convention);
5515}
5516
5517void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5518 HUnresolvedStaticFieldSet* instruction) {
5519 FieldAccessCallingConventionX86 calling_convention;
5520 codegen_->CreateUnresolvedFieldLocationSummary(
5521 instruction, instruction->GetFieldType(), calling_convention);
5522}
5523
5524void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5525 HUnresolvedStaticFieldSet* instruction) {
5526 FieldAccessCallingConventionX86 calling_convention;
5527 codegen_->GenerateUnresolvedFieldAccess(instruction,
5528 instruction->GetFieldType(),
5529 instruction->GetFieldIndex(),
5530 instruction->GetDexPc(),
5531 calling_convention);
5532}
5533
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005534void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005535 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5536 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5537 ? Location::RequiresRegister()
5538 : Location::Any();
5539 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005540}
5541
Calin Juravle2ae48182016-03-16 14:05:09 +00005542void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5543 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005544 return;
5545 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005546 LocationSummary* locations = instruction->GetLocations();
5547 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005548
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005549 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005550 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005551}
5552
Calin Juravle2ae48182016-03-16 14:05:09 +00005553void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005554 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005555 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005556
5557 LocationSummary* locations = instruction->GetLocations();
5558 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005559
5560 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005561 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005562 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005563 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005564 } else {
5565 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005566 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005567 __ jmp(slow_path->GetEntryLabel());
5568 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005569 }
5570 __ j(kEqual, slow_path->GetEntryLabel());
5571}
5572
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005573void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005574 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005575}
5576
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005577void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005578 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005581 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5582 object_array_get_with_read_barrier
5583 ? LocationSummary::kCallOnSlowPath
5584 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005585 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005586 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005587 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005588 locations->SetInAt(0, Location::RequiresRegister());
5589 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5592 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005593 // The output overlaps in case of long: we don't want the low move
5594 // to overwrite the array's location. Likewise, in the case of an
5595 // object array get with read barriers enabled, we do not want the
5596 // move to overwrite the array's location, as we need it to emit
5597 // the read barrier.
5598 locations->SetOut(
5599 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5601 ? Location::kOutputOverlap
5602 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005604}
5605
5606void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5607 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005608 Location obj_loc = locations->InAt(0);
5609 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005610 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005611 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005612 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005614 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005615 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005616 case DataType::Type::kBool:
5617 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005618 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005619 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620 break;
5621 }
5622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005623 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005624 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005625 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005626 break;
5627 }
5628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005630 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005631 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5632 // Branch cases into compressed and uncompressed for each index's type.
5633 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5634 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005635 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005637 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5638 "Expecting 0=compressed, 1=uncompressed");
5639 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005640 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5641 __ jmp(&done);
5642 __ Bind(&not_compressed);
5643 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5644 __ Bind(&done);
5645 } else {
5646 // Common case for charAt of array of char or when string compression's
5647 // feature is turned off.
5648 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5649 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005650 break;
5651 }
5652
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005653 case DataType::Type::kInt16: {
5654 Register out = out_loc.AsRegister<Register>();
5655 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5656 break;
5657 }
5658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005659 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005660 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005661 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005662 break;
5663 }
5664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005665 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005666 static_assert(
5667 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5668 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005669 // /* HeapReference<Object> */ out =
5670 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5671 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005672 // Note that a potential implicit null check is handled in this
5673 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5674 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005675 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005676 } else {
5677 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005678 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5679 codegen_->MaybeRecordImplicitNullCheck(instruction);
5680 // If read barriers are enabled, emit read barriers other than
5681 // Baker's using a slow path (and also unpoison the loaded
5682 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005683 if (index.IsConstant()) {
5684 uint32_t offset =
5685 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005686 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5687 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005688 codegen_->MaybeGenerateReadBarrierSlow(
5689 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5690 }
5691 }
5692 break;
5693 }
5694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005696 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005697 __ movl(out_loc.AsRegisterPairLow<Register>(),
5698 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5699 codegen_->MaybeRecordImplicitNullCheck(instruction);
5700 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5701 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005702 break;
5703 }
5704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005706 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005707 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005708 break;
5709 }
5710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005711 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005712 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005713 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005714 break;
5715 }
5716
Aart Bik66c158e2018-01-31 12:55:04 -08005717 case DataType::Type::kUint32:
5718 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005720 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005721 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005725 // Potential implicit null checks, in the case of reference or
5726 // long arrays, are handled in the previous switch statement.
5727 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005728 codegen_->MaybeRecordImplicitNullCheck(instruction);
5729 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005730}
5731
5732void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005733 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005735 bool needs_write_barrier =
5736 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005738
Vladimir Markoca6fff82017-10-03 14:49:14 +01005739 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005740 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005741 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005742 LocationSummary::kCallOnSlowPath :
5743 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005744
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005745 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005746 // We need the inputs to be different than the output in case of long operation.
5747 // In case of a byte operation, the register allocator does not support multiple
5748 // inputs that die at entry with one in a specific register.
5749 locations->SetInAt(0, Location::RequiresRegister());
5750 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5751 if (is_byte_type) {
5752 // Ensure the value is in a byte register.
5753 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005754 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005755 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005756 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005757 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5758 }
5759 if (needs_write_barrier) {
5760 // Temporary registers for the write barrier.
5761 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5762 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005763 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005764 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005765}
5766
5767void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5768 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005769 Location array_loc = locations->InAt(0);
5770 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005771 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005772 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005774 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5775 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5776 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005778 bool needs_write_barrier =
5779 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005780
5781 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005782 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005783 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005785 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005786 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005787 if (value.IsRegister()) {
5788 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005789 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005790 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005791 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005792 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005793 break;
5794 }
5795
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005796 case DataType::Type::kUint16:
5797 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005798 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005799 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005800 if (value.IsRegister()) {
5801 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005802 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005803 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005804 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005805 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005806 break;
5807 }
5808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005810 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005811 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005812
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005813 if (!value.IsRegister()) {
5814 // Just setting null.
5815 DCHECK(instruction->InputAt(2)->IsNullConstant());
5816 DCHECK(value.IsConstant()) << value;
5817 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005818 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005819 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005820 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005821 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005822 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005823
5824 DCHECK(needs_write_barrier);
5825 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005826 // We cannot use a NearLabel for `done`, as its range may be too
5827 // short when Baker read barriers are enabled.
5828 Label done;
5829 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005830 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005831 Location temp_loc = locations->GetTemp(0);
5832 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005833 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005834 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005835 codegen_->AddSlowPath(slow_path);
5836 if (instruction->GetValueCanBeNull()) {
5837 __ testl(register_value, register_value);
5838 __ j(kNotEqual, &not_null);
5839 __ movl(address, Immediate(0));
5840 codegen_->MaybeRecordImplicitNullCheck(instruction);
5841 __ jmp(&done);
5842 __ Bind(&not_null);
5843 }
5844
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005845 // Note that when Baker read barriers are enabled, the type
5846 // checks are performed without read barriers. This is fine,
5847 // even in the case where a class object is in the from-space
5848 // after the flip, as a comparison involving such a type would
5849 // not produce a false positive; it may of course produce a
5850 // false negative, in which case we would take the ArraySet
5851 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005852
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005853 // /* HeapReference<Class> */ temp = array->klass_
5854 __ movl(temp, Address(array, class_offset));
5855 codegen_->MaybeRecordImplicitNullCheck(instruction);
5856 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005857
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005858 // /* HeapReference<Class> */ temp = temp->component_type_
5859 __ movl(temp, Address(temp, component_offset));
5860 // If heap poisoning is enabled, no need to unpoison `temp`
5861 // nor the object reference in `register_value->klass`, as
5862 // we are comparing two poisoned references.
5863 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005864
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005865 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5866 __ j(kEqual, &do_put);
5867 // If heap poisoning is enabled, the `temp` reference has
5868 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869 __ MaybeUnpoisonHeapReference(temp);
5870
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005871 // If heap poisoning is enabled, no need to unpoison the
5872 // heap reference loaded below, as it is only used for a
5873 // comparison with null.
5874 __ cmpl(Address(temp, super_offset), Immediate(0));
5875 __ j(kNotEqual, slow_path->GetEntryLabel());
5876 __ Bind(&do_put);
5877 } else {
5878 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005879 }
5880 }
5881
5882 if (kPoisonHeapReferences) {
5883 __ movl(temp, register_value);
5884 __ PoisonHeapReference(temp);
5885 __ movl(address, temp);
5886 } else {
5887 __ movl(address, register_value);
5888 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005889 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005890 codegen_->MaybeRecordImplicitNullCheck(instruction);
5891 }
5892
5893 Register card = locations->GetTemp(1).AsRegister<Register>();
5894 codegen_->MarkGCCard(
5895 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5896 __ Bind(&done);
5897
5898 if (slow_path != nullptr) {
5899 __ Bind(slow_path->GetExitLabel());
5900 }
5901
5902 break;
5903 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005905 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005906 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005907 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005908 if (value.IsRegister()) {
5909 __ movl(address, value.AsRegister<Register>());
5910 } else {
5911 DCHECK(value.IsConstant()) << value;
5912 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5913 __ movl(address, Immediate(v));
5914 }
5915 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005916 break;
5917 }
5918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005920 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005921 if (value.IsRegisterPair()) {
5922 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5923 value.AsRegisterPairLow<Register>());
5924 codegen_->MaybeRecordImplicitNullCheck(instruction);
5925 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5926 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005927 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005928 DCHECK(value.IsConstant());
5929 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5930 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5931 Immediate(Low32Bits(val)));
5932 codegen_->MaybeRecordImplicitNullCheck(instruction);
5933 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5934 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005935 }
5936 break;
5937 }
5938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005939 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005940 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005941 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005942 if (value.IsFpuRegister()) {
5943 __ movss(address, value.AsFpuRegister<XmmRegister>());
5944 } else {
5945 DCHECK(value.IsConstant());
5946 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5947 __ movl(address, Immediate(v));
5948 }
5949 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005950 break;
5951 }
5952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005954 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005955 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005956 if (value.IsFpuRegister()) {
5957 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5958 } else {
5959 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005960 Address address_hi =
5961 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005962 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5963 __ movl(address, Immediate(Low32Bits(v)));
5964 codegen_->MaybeRecordImplicitNullCheck(instruction);
5965 __ movl(address_hi, Immediate(High32Bits(v)));
5966 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005967 break;
5968 }
5969
Aart Bik66c158e2018-01-31 12:55:04 -08005970 case DataType::Type::kUint32:
5971 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005974 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005975 }
5976}
5977
5978void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005979 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005980 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005981 if (!instruction->IsEmittedAtUseSite()) {
5982 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5983 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005984}
5985
5986void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005987 if (instruction->IsEmittedAtUseSite()) {
5988 return;
5989 }
5990
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005991 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005992 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005993 Register obj = locations->InAt(0).AsRegister<Register>();
5994 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005995 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005996 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005997 // Mask out most significant bit in case the array is String's array of char.
5998 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005999 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006000 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001}
6002
6003void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006004 RegisterSet caller_saves = RegisterSet::Empty();
6005 InvokeRuntimeCallingConvention calling_convention;
6006 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6007 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6008 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006009 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006010 HInstruction* length = instruction->InputAt(1);
6011 if (!length->IsEmittedAtUseSite()) {
6012 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6013 }
jessicahandojo4877b792016-09-08 19:49:13 -07006014 // Need register to see array's length.
6015 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6016 locations->AddTemp(Location::RequiresRegister());
6017 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006018}
6019
6020void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006021 const bool is_string_compressed_char_at =
6022 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006023 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006024 Location index_loc = locations->InAt(0);
6025 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006026 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006027 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006028
Mark Mendell99dbd682015-04-22 16:18:52 -04006029 if (length_loc.IsConstant()) {
6030 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6031 if (index_loc.IsConstant()) {
6032 // BCE will remove the bounds check if we are guarenteed to pass.
6033 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6034 if (index < 0 || index >= length) {
6035 codegen_->AddSlowPath(slow_path);
6036 __ jmp(slow_path->GetEntryLabel());
6037 } else {
6038 // Some optimization after BCE may have generated this, and we should not
6039 // generate a bounds check if it is a valid range.
6040 }
6041 return;
6042 }
6043
6044 // We have to reverse the jump condition because the length is the constant.
6045 Register index_reg = index_loc.AsRegister<Register>();
6046 __ cmpl(index_reg, Immediate(length));
6047 codegen_->AddSlowPath(slow_path);
6048 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006049 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006050 HInstruction* array_length = instruction->InputAt(1);
6051 if (array_length->IsEmittedAtUseSite()) {
6052 // Address the length field in the array.
6053 DCHECK(array_length->IsArrayLength());
6054 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6055 Location array_loc = array_length->GetLocations()->InAt(0);
6056 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006057 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006058 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6059 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006060 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6061 __ movl(length_reg, array_len);
6062 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006063 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006064 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006065 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006066 // Checking bounds for general case:
6067 // Array of char or string's array with feature compression off.
6068 if (index_loc.IsConstant()) {
6069 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6070 __ cmpl(array_len, Immediate(value));
6071 } else {
6072 __ cmpl(array_len, index_loc.AsRegister<Register>());
6073 }
6074 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006075 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006076 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006077 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006078 }
6079 codegen_->AddSlowPath(slow_path);
6080 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006081 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006082}
6083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006084void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006085 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006086}
6087
6088void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006089 if (instruction->GetNext()->IsSuspendCheck() &&
6090 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6091 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6092 // The back edge will generate the suspend check.
6093 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6094 }
6095
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006096 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6097}
6098
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006099void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006100 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6101 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006102 // In suspend check slow path, usually there are no caller-save registers at all.
6103 // If SIMD instructions are present, however, we force spilling all live SIMD
6104 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006105 locations->SetCustomSlowPathCallerSaves(
6106 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006107}
6108
6109void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006110 HBasicBlock* block = instruction->GetBlock();
6111 if (block->GetLoopInformation() != nullptr) {
6112 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6113 // The back edge will generate the suspend check.
6114 return;
6115 }
6116 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6117 // The goto will generate the suspend check.
6118 return;
6119 }
6120 GenerateSuspendCheck(instruction, nullptr);
6121}
6122
6123void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6124 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006125 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006126 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6127 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006128 slow_path =
6129 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006130 instruction->SetSlowPath(slow_path);
6131 codegen_->AddSlowPath(slow_path);
6132 if (successor != nullptr) {
6133 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006134 }
6135 } else {
6136 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6137 }
6138
Andreas Gampe542451c2016-07-26 09:02:02 -07006139 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006140 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006141 if (successor == nullptr) {
6142 __ j(kNotEqual, slow_path->GetEntryLabel());
6143 __ Bind(slow_path->GetReturnLabel());
6144 } else {
6145 __ j(kEqual, codegen_->GetLabelOf(successor));
6146 __ jmp(slow_path->GetEntryLabel());
6147 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006148}
6149
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006150X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6151 return codegen_->GetAssembler();
6152}
6153
Aart Bikcfe50bb2017-12-12 14:54:12 -08006154void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006155 ScratchRegisterScope ensure_scratch(
6156 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6157 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6158 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006159
Aart Bikcfe50bb2017-12-12 14:54:12 -08006160 // Now that temp register is available (possibly spilled), move blocks of memory.
6161 for (int i = 0; i < number_of_words; i++) {
6162 __ movl(temp_reg, Address(ESP, src + stack_offset));
6163 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6164 stack_offset += kX86WordSize;
6165 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006166}
6167
6168void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006169 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006170 Location source = move->GetSource();
6171 Location destination = move->GetDestination();
6172
6173 if (source.IsRegister()) {
6174 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006175 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006176 } else if (destination.IsFpuRegister()) {
6177 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006178 } else {
6179 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006180 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006181 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006182 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006183 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006184 // Create stack space for 2 elements.
6185 __ subl(ESP, Immediate(2 * elem_size));
6186 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6187 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6188 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6189 // And remove the temporary stack space we allocated.
6190 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006191 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006192 if (destination.IsRegister()) {
6193 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6194 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006195 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006196 } else if (destination.IsRegisterPair()) {
6197 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6198 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6199 __ psrlq(src_reg, Immediate(32));
6200 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006201 } else if (destination.IsStackSlot()) {
6202 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006203 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006204 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006205 } else {
6206 DCHECK(destination.IsSIMDStackSlot());
6207 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006208 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006209 } else if (source.IsStackSlot()) {
6210 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006211 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006212 } else if (destination.IsFpuRegister()) {
6213 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006214 } else {
6215 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006216 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006217 }
6218 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006219 if (destination.IsRegisterPair()) {
6220 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6221 __ movl(destination.AsRegisterPairHigh<Register>(),
6222 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6223 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006224 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6225 } else {
6226 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006227 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006228 }
Aart Bik5576f372017-03-23 16:17:37 -07006229 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006230 if (destination.IsFpuRegister()) {
6231 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6232 } else {
6233 DCHECK(destination.IsSIMDStackSlot());
6234 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6235 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006236 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006237 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006238 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006239 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006240 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006241 if (value == 0) {
6242 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6243 } else {
6244 __ movl(destination.AsRegister<Register>(), Immediate(value));
6245 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006246 } else {
6247 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006248 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006249 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006250 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006251 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006252 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006253 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006254 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006255 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6256 if (value == 0) {
6257 // Easy handling of 0.0.
6258 __ xorps(dest, dest);
6259 } else {
6260 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006261 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6262 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6263 __ movl(temp, Immediate(value));
6264 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006265 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006266 } else {
6267 DCHECK(destination.IsStackSlot()) << destination;
6268 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6269 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006270 } else if (constant->IsLongConstant()) {
6271 int64_t value = constant->AsLongConstant()->GetValue();
6272 int32_t low_value = Low32Bits(value);
6273 int32_t high_value = High32Bits(value);
6274 Immediate low(low_value);
6275 Immediate high(high_value);
6276 if (destination.IsDoubleStackSlot()) {
6277 __ movl(Address(ESP, destination.GetStackIndex()), low);
6278 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6279 } else {
6280 __ movl(destination.AsRegisterPairLow<Register>(), low);
6281 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6282 }
6283 } else {
6284 DCHECK(constant->IsDoubleConstant());
6285 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006286 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006287 int32_t low_value = Low32Bits(value);
6288 int32_t high_value = High32Bits(value);
6289 Immediate low(low_value);
6290 Immediate high(high_value);
6291 if (destination.IsFpuRegister()) {
6292 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6293 if (value == 0) {
6294 // Easy handling of 0.0.
6295 __ xorpd(dest, dest);
6296 } else {
6297 __ pushl(high);
6298 __ pushl(low);
6299 __ movsd(dest, Address(ESP, 0));
6300 __ addl(ESP, Immediate(8));
6301 }
6302 } else {
6303 DCHECK(destination.IsDoubleStackSlot()) << destination;
6304 __ movl(Address(ESP, destination.GetStackIndex()), low);
6305 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6306 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006307 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006308 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006309 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006310 }
6311}
6312
Mark Mendella5c19ce2015-04-01 12:51:05 -04006313void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006314 Register suggested_scratch = reg == EAX ? EBX : EAX;
6315 ScratchRegisterScope ensure_scratch(
6316 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6317
6318 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6319 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6320 __ movl(Address(ESP, mem + stack_offset), reg);
6321 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006322}
6323
Mark Mendell7c8d0092015-01-26 11:21:33 -05006324void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006325 ScratchRegisterScope ensure_scratch(
6326 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6327
6328 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6329 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6330 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6331 __ movss(Address(ESP, mem + stack_offset), reg);
6332 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006333}
6334
Aart Bikcfe50bb2017-12-12 14:54:12 -08006335void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6336 size_t extra_slot = 4 * kX86WordSize;
6337 __ subl(ESP, Immediate(extra_slot));
6338 __ movups(Address(ESP, 0), XmmRegister(reg));
6339 ExchangeMemory(0, mem + extra_slot, 4);
6340 __ movups(XmmRegister(reg), Address(ESP, 0));
6341 __ addl(ESP, Immediate(extra_slot));
6342}
6343
6344void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006345 ScratchRegisterScope ensure_scratch1(
6346 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006347
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006348 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6349 ScratchRegisterScope ensure_scratch2(
6350 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006351
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006352 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6353 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006354
6355 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6356 for (int i = 0; i < number_of_words; i++) {
6357 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6358 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6359 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6360 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6361 stack_offset += kX86WordSize;
6362 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006363}
6364
6365void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006366 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006367 Location source = move->GetSource();
6368 Location destination = move->GetDestination();
6369
6370 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006371 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6372 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6373 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6374 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6375 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006376 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006377 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006378 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006379 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006380 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006381 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006382 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6383 // Use XOR Swap algorithm to avoid a temporary.
6384 DCHECK_NE(source.reg(), destination.reg());
6385 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6386 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6387 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6388 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6389 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6390 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6391 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006392 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6393 // Take advantage of the 16 bytes in the XMM register.
6394 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6395 Address stack(ESP, destination.GetStackIndex());
6396 // Load the double into the high doubleword.
6397 __ movhpd(reg, stack);
6398
6399 // Store the low double into the destination.
6400 __ movsd(stack, reg);
6401
6402 // Move the high double to the low double.
6403 __ psrldq(reg, Immediate(8));
6404 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6405 // Take advantage of the 16 bytes in the XMM register.
6406 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6407 Address stack(ESP, source.GetStackIndex());
6408 // Load the double into the high doubleword.
6409 __ movhpd(reg, stack);
6410
6411 // Store the low double into the destination.
6412 __ movsd(stack, reg);
6413
6414 // Move the high double to the low double.
6415 __ psrldq(reg, Immediate(8));
6416 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006417 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6418 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6419 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6420 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6421 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6422 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6423 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006424 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006425 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006426 }
6427}
6428
6429void ParallelMoveResolverX86::SpillScratch(int reg) {
6430 __ pushl(static_cast<Register>(reg));
6431}
6432
6433void ParallelMoveResolverX86::RestoreScratch(int reg) {
6434 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006435}
6436
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006437HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6438 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006439 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006440 case HLoadClass::LoadKind::kInvalid:
6441 LOG(FATAL) << "UNREACHABLE";
6442 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 case HLoadClass::LoadKind::kReferrersClass:
6444 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006445 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006446 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006447 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006448 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006449 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006450 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006451 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 DCHECK(Runtime::Current()->UseJitCompilation());
6453 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006454 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006455 break;
6456 }
6457 return desired_class_load_kind;
6458}
6459
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006460void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006461 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006462 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006463 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006464 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006465 cls,
6466 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006467 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006468 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006469 return;
6470 }
Vladimir Marko41559982017-01-06 14:04:23 +00006471 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006472
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006473 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6474 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006475 ? LocationSummary::kCallOnSlowPath
6476 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006477 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006478 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006479 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006480 }
6481
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006482 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006483 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006484 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006485 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006486 locations->SetInAt(0, Location::RequiresRegister());
6487 }
6488 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006489 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6490 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6491 // Rely on the type resolution and/or initialization to save everything.
6492 RegisterSet caller_saves = RegisterSet::Empty();
6493 InvokeRuntimeCallingConvention calling_convention;
6494 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6495 locations->SetCustomSlowPathCallerSaves(caller_saves);
6496 } else {
6497 // For non-Baker read barrier we have a temp-clobbering call.
6498 }
6499 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006500}
6501
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006502Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006503 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006504 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006505 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006506 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006507 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006508 PatchInfo<Label>* info = &jit_class_patches_.back();
6509 return &info->label;
6510}
6511
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006512// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6513// move.
6514void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006515 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006516 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006517 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006518 return;
6519 }
Vladimir Marko41559982017-01-06 14:04:23 +00006520 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006521
Vladimir Marko41559982017-01-06 14:04:23 +00006522 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523 Location out_loc = locations->Out();
6524 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006526 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006527 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6528 ? kWithoutReadBarrier
6529 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006530 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006531 case HLoadClass::LoadKind::kReferrersClass: {
6532 DCHECK(!cls->CanCallRuntime());
6533 DCHECK(!cls->MustGenerateClinitCheck());
6534 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6535 Register current_method = locations->InAt(0).AsRegister<Register>();
6536 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006537 cls,
6538 out_loc,
6539 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006540 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006541 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006542 break;
6543 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006544 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006545 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006546 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006547 Register method_address = locations->InAt(0).AsRegister<Register>();
6548 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006549 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006550 break;
6551 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006552 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006553 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6554 Register method_address = locations->InAt(0).AsRegister<Register>();
6555 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006556 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6557 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006558 break;
6559 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006560 case HLoadClass::LoadKind::kBssEntry: {
6561 Register method_address = locations->InAt(0).AsRegister<Register>();
6562 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6563 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6564 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6565 generate_null_check = true;
6566 break;
6567 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006568 case HLoadClass::LoadKind::kJitBootImageAddress: {
6569 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6570 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6571 DCHECK_NE(address, 0u);
6572 __ movl(out, Immediate(address));
6573 break;
6574 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006575 case HLoadClass::LoadKind::kJitTableAddress: {
6576 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6577 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006578 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006579 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006580 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006581 break;
6582 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006583 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006584 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006585 LOG(FATAL) << "UNREACHABLE";
6586 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006587 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006588
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006589 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6590 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006591 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006592 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6593 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006594
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006595 if (generate_null_check) {
6596 __ testl(out, out);
6597 __ j(kEqual, slow_path->GetEntryLabel());
6598 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006599
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006600 if (cls->MustGenerateClinitCheck()) {
6601 GenerateClassInitializationCheck(slow_path, out);
6602 } else {
6603 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006604 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006605 }
6606}
6607
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006608void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6609 InvokeRuntimeCallingConvention calling_convention;
6610 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6611 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6612}
6613
6614void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6615 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6616}
6617
Orion Hodson18259d72018-04-12 11:18:23 +01006618void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6619 InvokeRuntimeCallingConvention calling_convention;
6620 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6621 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6622}
6623
6624void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6625 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6626}
6627
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006628void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6629 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006630 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006631 locations->SetInAt(0, Location::RequiresRegister());
6632 if (check->HasUses()) {
6633 locations->SetOut(Location::SameAsFirstInput());
6634 }
6635}
6636
6637void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006638 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006639 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006640 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006641 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006642 GenerateClassInitializationCheck(slow_path,
6643 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006644}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006645
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006646void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006647 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006648 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6649 const size_t status_byte_offset =
6650 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6651 constexpr uint32_t shifted_initialized_value =
6652 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6653
6654 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006655 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006656 __ Bind(slow_path->GetExitLabel());
6657 // No need for memory fence, thanks to the X86 memory model.
6658}
6659
Vladimir Marko175e7862018-03-27 09:03:13 +00006660void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6661 Register temp) {
6662 uint32_t path_to_root = check->GetBitstringPathToRoot();
6663 uint32_t mask = check->GetBitstringMask();
6664 DCHECK(IsPowerOfTwo(mask + 1));
6665 size_t mask_bits = WhichPowerOf2(mask + 1);
6666
6667 if (mask_bits == 16u) {
6668 // Compare the bitstring in memory.
6669 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6670 } else {
6671 // /* uint32_t */ temp = temp->status_
6672 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6673 // Compare the bitstring bits using SUB.
6674 __ subl(temp, Immediate(path_to_root));
6675 // Shift out bits that do not contribute to the comparison.
6676 __ shll(temp, Immediate(32u - mask_bits));
6677 }
6678}
6679
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006680HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6681 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006682 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006683 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006684 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006685 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006686 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006687 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006688 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006689 case HLoadString::LoadKind::kJitTableAddress:
6690 DCHECK(Runtime::Current()->UseJitCompilation());
6691 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006692 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006693 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006694 }
6695 return desired_string_load_kind;
6696}
6697
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006698void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006699 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006701 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006702 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006703 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006704 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006705 locations->SetInAt(0, Location::RequiresRegister());
6706 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006707 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006708 locations->SetOut(Location::RegisterLocation(EAX));
6709 } else {
6710 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006711 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6712 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006713 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006714 RegisterSet caller_saves = RegisterSet::Empty();
6715 InvokeRuntimeCallingConvention calling_convention;
6716 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6717 locations->SetCustomSlowPathCallerSaves(caller_saves);
6718 } else {
6719 // For non-Baker read barrier we have a temp-clobbering call.
6720 }
6721 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006722 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006723}
6724
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006725Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006726 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006727 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006728 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006729 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006730 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006731 PatchInfo<Label>* info = &jit_string_patches_.back();
6732 return &info->label;
6733}
6734
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006735// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6736// move.
6737void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006738 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 Location out_loc = locations->Out();
6740 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006741
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006742 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006743 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006744 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006745 Register method_address = locations->InAt(0).AsRegister<Register>();
6746 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006747 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006748 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006749 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006750 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006751 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6752 Register method_address = locations->InAt(0).AsRegister<Register>();
6753 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006754 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6755 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006756 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006757 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006758 case HLoadString::LoadKind::kBssEntry: {
6759 Register method_address = locations->InAt(0).AsRegister<Register>();
6760 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6761 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006762 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006763 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006764 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006765 codegen_->AddSlowPath(slow_path);
6766 __ testl(out, out);
6767 __ j(kEqual, slow_path->GetEntryLabel());
6768 __ Bind(slow_path->GetExitLabel());
6769 return;
6770 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006771 case HLoadString::LoadKind::kJitBootImageAddress: {
6772 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6773 DCHECK_NE(address, 0u);
6774 __ movl(out, Immediate(address));
6775 return;
6776 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006777 case HLoadString::LoadKind::kJitTableAddress: {
6778 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6779 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006780 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006781 // /* GcRoot<mirror::String> */ out = *address
6782 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6783 return;
6784 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006785 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006786 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006787 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006788
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006789 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006790 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006791 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006792 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006793 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6794 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006795}
6796
David Brazdilcb1c0552015-08-04 16:22:25 +01006797static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006798 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006799}
6800
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006801void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6802 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006803 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006804 locations->SetOut(Location::RequiresRegister());
6805}
6806
6807void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006808 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6809}
6810
6811void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006812 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006813}
6814
6815void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6816 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006817}
6818
6819void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006820 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6821 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006822 InvokeRuntimeCallingConvention calling_convention;
6823 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6824}
6825
6826void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006827 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006828 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006829}
6830
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006831// Temp is used for read barrier.
6832static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6833 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006834 !kUseBakerReadBarrier &&
6835 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006836 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006837 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6838 return 1;
6839 }
6840 return 0;
6841}
6842
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006843// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6844// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006845// The other checks have one temp for loading the object's class.
6846static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006847 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006848 return 2;
6849 }
6850 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006851}
6852
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006853void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006854 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006855 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006856 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006857 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006858 case TypeCheckKind::kExactCheck:
6859 case TypeCheckKind::kAbstractClassCheck:
6860 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006861 case TypeCheckKind::kArrayObjectCheck: {
6862 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6863 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6864 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006865 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006866 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006867 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006868 case TypeCheckKind::kUnresolvedCheck:
6869 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006870 call_kind = LocationSummary::kCallOnSlowPath;
6871 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006872 case TypeCheckKind::kBitstringCheck:
6873 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006874 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006875
Vladimir Markoca6fff82017-10-03 14:49:14 +01006876 LocationSummary* locations =
6877 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006878 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006879 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006880 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006881 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006882 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6883 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6884 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6885 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6886 } else {
6887 locations->SetInAt(1, Location::Any());
6888 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006889 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6890 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006891 // When read barriers are enabled, we need a temporary register for some cases.
6892 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006893}
6894
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006895void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006896 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006897 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006898 Location obj_loc = locations->InAt(0);
6899 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006900 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006901 Location out_loc = locations->Out();
6902 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006903 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6904 DCHECK_LE(num_temps, 1u);
6905 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006906 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006907 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6908 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6909 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006910 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006911 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006912
6913 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006914 // Avoid null check if we know obj is not null.
6915 if (instruction->MustDoNullCheck()) {
6916 __ testl(obj, obj);
6917 __ j(kEqual, &zero);
6918 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006919
Roland Levillain7c1559a2015-12-15 10:55:36 +00006920 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006921 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006922 ReadBarrierOption read_barrier_option =
6923 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006924 // /* HeapReference<Class> */ out = obj->klass_
6925 GenerateReferenceLoadTwoRegisters(instruction,
6926 out_loc,
6927 obj_loc,
6928 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006929 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006930 if (cls.IsRegister()) {
6931 __ cmpl(out, cls.AsRegister<Register>());
6932 } else {
6933 DCHECK(cls.IsStackSlot()) << cls;
6934 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6935 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006936
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006937 // Classes must be equal for the instanceof to succeed.
6938 __ j(kNotEqual, &zero);
6939 __ movl(out, Immediate(1));
6940 __ jmp(&done);
6941 break;
6942 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006943
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006944 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006945 ReadBarrierOption read_barrier_option =
6946 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006947 // /* HeapReference<Class> */ out = obj->klass_
6948 GenerateReferenceLoadTwoRegisters(instruction,
6949 out_loc,
6950 obj_loc,
6951 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006952 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006953 // If the class is abstract, we eagerly fetch the super class of the
6954 // object to avoid doing a comparison we know will fail.
6955 NearLabel loop;
6956 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006957 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006958 GenerateReferenceLoadOneRegister(instruction,
6959 out_loc,
6960 super_offset,
6961 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006962 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006963 __ testl(out, out);
6964 // If `out` is null, we use it for the result, and jump to `done`.
6965 __ j(kEqual, &done);
6966 if (cls.IsRegister()) {
6967 __ cmpl(out, cls.AsRegister<Register>());
6968 } else {
6969 DCHECK(cls.IsStackSlot()) << cls;
6970 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6971 }
6972 __ j(kNotEqual, &loop);
6973 __ movl(out, Immediate(1));
6974 if (zero.IsLinked()) {
6975 __ jmp(&done);
6976 }
6977 break;
6978 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006979
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006980 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006981 ReadBarrierOption read_barrier_option =
6982 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006983 // /* HeapReference<Class> */ out = obj->klass_
6984 GenerateReferenceLoadTwoRegisters(instruction,
6985 out_loc,
6986 obj_loc,
6987 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006988 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006989 // Walk over the class hierarchy to find a match.
6990 NearLabel loop, success;
6991 __ Bind(&loop);
6992 if (cls.IsRegister()) {
6993 __ cmpl(out, cls.AsRegister<Register>());
6994 } else {
6995 DCHECK(cls.IsStackSlot()) << cls;
6996 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6997 }
6998 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006999 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007000 GenerateReferenceLoadOneRegister(instruction,
7001 out_loc,
7002 super_offset,
7003 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007004 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007005 __ testl(out, out);
7006 __ j(kNotEqual, &loop);
7007 // If `out` is null, we use it for the result, and jump to `done`.
7008 __ jmp(&done);
7009 __ Bind(&success);
7010 __ movl(out, Immediate(1));
7011 if (zero.IsLinked()) {
7012 __ jmp(&done);
7013 }
7014 break;
7015 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007016
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007017 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007018 ReadBarrierOption read_barrier_option =
7019 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007020 // /* HeapReference<Class> */ out = obj->klass_
7021 GenerateReferenceLoadTwoRegisters(instruction,
7022 out_loc,
7023 obj_loc,
7024 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007025 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007026 // Do an exact check.
7027 NearLabel exact_check;
7028 if (cls.IsRegister()) {
7029 __ cmpl(out, cls.AsRegister<Register>());
7030 } else {
7031 DCHECK(cls.IsStackSlot()) << cls;
7032 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7033 }
7034 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007035 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007036 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007037 GenerateReferenceLoadOneRegister(instruction,
7038 out_loc,
7039 component_offset,
7040 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007041 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007042 __ testl(out, out);
7043 // If `out` is null, we use it for the result, and jump to `done`.
7044 __ j(kEqual, &done);
7045 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7046 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007047 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007048 __ movl(out, Immediate(1));
7049 __ jmp(&done);
7050 break;
7051 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007052
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007053 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007054 // No read barrier since the slow path will retry upon failure.
7055 // /* HeapReference<Class> */ out = obj->klass_
7056 GenerateReferenceLoadTwoRegisters(instruction,
7057 out_loc,
7058 obj_loc,
7059 class_offset,
7060 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007061 if (cls.IsRegister()) {
7062 __ cmpl(out, cls.AsRegister<Register>());
7063 } else {
7064 DCHECK(cls.IsStackSlot()) << cls;
7065 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7066 }
7067 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007068 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7069 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007070 codegen_->AddSlowPath(slow_path);
7071 __ j(kNotEqual, slow_path->GetEntryLabel());
7072 __ movl(out, Immediate(1));
7073 if (zero.IsLinked()) {
7074 __ jmp(&done);
7075 }
7076 break;
7077 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007078
Calin Juravle98893e12015-10-02 21:05:03 +01007079 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007080 case TypeCheckKind::kInterfaceCheck: {
7081 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007082 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007083 // cases.
7084 //
7085 // We cannot directly call the InstanceofNonTrivial runtime
7086 // entry point without resorting to a type checking slow path
7087 // here (i.e. by calling InvokeRuntime directly), as it would
7088 // require to assign fixed registers for the inputs of this
7089 // HInstanceOf instruction (following the runtime calling
7090 // convention), which might be cluttered by the potential first
7091 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 //
7093 // TODO: Introduce a new runtime entry point taking the object
7094 // to test (instead of its class) as argument, and let it deal
7095 // with the read barrier issues. This will let us refactor this
7096 // case of the `switch` code as it was previously (with a direct
7097 // call to the runtime not using a type checking slow path).
7098 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007099 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007100 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7101 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007102 codegen_->AddSlowPath(slow_path);
7103 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007104 if (zero.IsLinked()) {
7105 __ jmp(&done);
7106 }
7107 break;
7108 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007109
7110 case TypeCheckKind::kBitstringCheck: {
7111 // /* HeapReference<Class> */ temp = obj->klass_
7112 GenerateReferenceLoadTwoRegisters(instruction,
7113 out_loc,
7114 obj_loc,
7115 class_offset,
7116 kWithoutReadBarrier);
7117
7118 GenerateBitstringTypeCheckCompare(instruction, out);
7119 __ j(kNotEqual, &zero);
7120 __ movl(out, Immediate(1));
7121 __ jmp(&done);
7122 break;
7123 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007124 }
7125
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007126 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007127 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007128 __ xorl(out, out);
7129 }
7130
7131 if (done.IsLinked()) {
7132 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007133 }
7134
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007135 if (slow_path != nullptr) {
7136 __ Bind(slow_path->GetExitLabel());
7137 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007138}
7139
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007140void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007141 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007142 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007143 LocationSummary* locations =
7144 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007145 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007146 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7147 // Require a register for the interface check since there is a loop that compares the class to
7148 // a memory address.
7149 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007150 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7151 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7152 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7153 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007154 } else {
7155 locations->SetInAt(1, Location::Any());
7156 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007157 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007158 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7159}
7160
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007161void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007162 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007163 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007164 Location obj_loc = locations->InAt(0);
7165 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007166 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007167 Location temp_loc = locations->GetTemp(0);
7168 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007169 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7170 DCHECK_GE(num_temps, 1u);
7171 DCHECK_LE(num_temps, 2u);
7172 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7173 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7174 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7175 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7176 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7177 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7178 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7179 const uint32_t object_array_data_offset =
7180 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007181
Vladimir Marko87584542017-12-12 17:47:52 +00007182 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007183 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007184 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7185 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007187
Roland Levillain0d5a2812015-11-13 10:07:31 +00007188 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007189 // Avoid null check if we know obj is not null.
7190 if (instruction->MustDoNullCheck()) {
7191 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007192 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007193 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007194
Roland Levillain0d5a2812015-11-13 10:07:31 +00007195 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007196 case TypeCheckKind::kExactCheck:
7197 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007198 // /* HeapReference<Class> */ temp = obj->klass_
7199 GenerateReferenceLoadTwoRegisters(instruction,
7200 temp_loc,
7201 obj_loc,
7202 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007203 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205 if (cls.IsRegister()) {
7206 __ cmpl(temp, cls.AsRegister<Register>());
7207 } else {
7208 DCHECK(cls.IsStackSlot()) << cls;
7209 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7210 }
7211 // Jump to slow path for throwing the exception or doing a
7212 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007213 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007214 break;
7215 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007217 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007218 // /* HeapReference<Class> */ temp = obj->klass_
7219 GenerateReferenceLoadTwoRegisters(instruction,
7220 temp_loc,
7221 obj_loc,
7222 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007223 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007225 // If the class is abstract, we eagerly fetch the super class of the
7226 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007227 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007228 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007229 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007230 GenerateReferenceLoadOneRegister(instruction,
7231 temp_loc,
7232 super_offset,
7233 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007234 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007235
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007236 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7237 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007238 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007239 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007240
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007241 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007242 if (cls.IsRegister()) {
7243 __ cmpl(temp, cls.AsRegister<Register>());
7244 } else {
7245 DCHECK(cls.IsStackSlot()) << cls;
7246 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7247 }
7248 __ j(kNotEqual, &loop);
7249 break;
7250 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007251
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007252 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007253 // /* HeapReference<Class> */ temp = obj->klass_
7254 GenerateReferenceLoadTwoRegisters(instruction,
7255 temp_loc,
7256 obj_loc,
7257 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007258 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007259
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007260 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007261 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007262 __ Bind(&loop);
7263 if (cls.IsRegister()) {
7264 __ cmpl(temp, cls.AsRegister<Register>());
7265 } else {
7266 DCHECK(cls.IsStackSlot()) << cls;
7267 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7268 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007269 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007270
Roland Levillain0d5a2812015-11-13 10:07:31 +00007271 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007272 GenerateReferenceLoadOneRegister(instruction,
7273 temp_loc,
7274 super_offset,
7275 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007276 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007277
7278 // If the class reference currently in `temp` is not null, jump
7279 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007280 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007281 __ j(kNotZero, &loop);
7282 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007283 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007284 break;
7285 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007286
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007287 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007288 // /* HeapReference<Class> */ temp = obj->klass_
7289 GenerateReferenceLoadTwoRegisters(instruction,
7290 temp_loc,
7291 obj_loc,
7292 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007293 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007294
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007295 // Do an exact check.
7296 if (cls.IsRegister()) {
7297 __ cmpl(temp, cls.AsRegister<Register>());
7298 } else {
7299 DCHECK(cls.IsStackSlot()) << cls;
7300 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7301 }
7302 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007303
7304 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007305 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007306 GenerateReferenceLoadOneRegister(instruction,
7307 temp_loc,
7308 component_offset,
7309 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007310 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007311
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007312 // If the component type is null (i.e. the object not an array), jump to the slow path to
7313 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007314 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007315 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007316
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007317 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007318 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007319 break;
7320 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007321
Calin Juravle98893e12015-10-02 21:05:03 +01007322 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007323 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007324 // We cannot directly call the CheckCast runtime entry point
7325 // without resorting to a type checking slow path here (i.e. by
7326 // calling InvokeRuntime directly), as it would require to
7327 // assign fixed registers for the inputs of this HInstanceOf
7328 // instruction (following the runtime calling convention), which
7329 // might be cluttered by the potential first read barrier
7330 // emission at the beginning of this method.
7331 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007332 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007333
7334 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007335 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7336 // We can not get false positives by doing this.
7337 // /* HeapReference<Class> */ temp = obj->klass_
7338 GenerateReferenceLoadTwoRegisters(instruction,
7339 temp_loc,
7340 obj_loc,
7341 class_offset,
7342 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007343
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007344 // /* HeapReference<Class> */ temp = temp->iftable_
7345 GenerateReferenceLoadTwoRegisters(instruction,
7346 temp_loc,
7347 temp_loc,
7348 iftable_offset,
7349 kWithoutReadBarrier);
7350 // Iftable is never null.
7351 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7352 // Maybe poison the `cls` for direct comparison with memory.
7353 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7354 // Loop through the iftable and check if any class matches.
7355 NearLabel start_loop;
7356 __ Bind(&start_loop);
7357 // Need to subtract first to handle the empty array case.
7358 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7359 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7360 // Go to next interface if the classes do not match.
7361 __ cmpl(cls.AsRegister<Register>(),
7362 CodeGeneratorX86::ArrayAddress(temp,
7363 maybe_temp2_loc,
7364 TIMES_4,
7365 object_array_data_offset));
7366 __ j(kNotEqual, &start_loop);
7367 // If `cls` was poisoned above, unpoison it.
7368 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007369 break;
7370 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007371
7372 case TypeCheckKind::kBitstringCheck: {
7373 // /* HeapReference<Class> */ temp = obj->klass_
7374 GenerateReferenceLoadTwoRegisters(instruction,
7375 temp_loc,
7376 obj_loc,
7377 class_offset,
7378 kWithoutReadBarrier);
7379
7380 GenerateBitstringTypeCheckCompare(instruction, temp);
7381 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7382 break;
7383 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007384 }
7385 __ Bind(&done);
7386
Roland Levillain0d5a2812015-11-13 10:07:31 +00007387 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007388}
7389
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007390void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007391 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7392 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007393 InvokeRuntimeCallingConvention calling_convention;
7394 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7395}
7396
7397void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007398 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7399 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007400 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007401 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007402 if (instruction->IsEnter()) {
7403 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7404 } else {
7405 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7406 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007407}
7408
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007409void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7410void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7411void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7412
7413void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7414 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007415 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007416 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7417 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007418 locations->SetInAt(0, Location::RequiresRegister());
7419 locations->SetInAt(1, Location::Any());
7420 locations->SetOut(Location::SameAsFirstInput());
7421}
7422
7423void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7424 HandleBitwiseOperation(instruction);
7425}
7426
7427void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7428 HandleBitwiseOperation(instruction);
7429}
7430
7431void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7432 HandleBitwiseOperation(instruction);
7433}
7434
7435void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7436 LocationSummary* locations = instruction->GetLocations();
7437 Location first = locations->InAt(0);
7438 Location second = locations->InAt(1);
7439 DCHECK(first.Equals(locations->Out()));
7440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007441 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007442 if (second.IsRegister()) {
7443 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007444 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007445 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007446 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007447 } else {
7448 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007449 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007450 }
7451 } else if (second.IsConstant()) {
7452 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007453 __ andl(first.AsRegister<Register>(),
7454 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007455 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007456 __ orl(first.AsRegister<Register>(),
7457 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007458 } else {
7459 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007460 __ xorl(first.AsRegister<Register>(),
7461 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007462 }
7463 } else {
7464 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007465 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007466 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007467 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007468 } else {
7469 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007470 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007471 }
7472 }
7473 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007474 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007475 if (second.IsRegisterPair()) {
7476 if (instruction->IsAnd()) {
7477 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7478 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7479 } else if (instruction->IsOr()) {
7480 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7481 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7482 } else {
7483 DCHECK(instruction->IsXor());
7484 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7485 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7486 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007487 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007488 if (instruction->IsAnd()) {
7489 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7490 __ andl(first.AsRegisterPairHigh<Register>(),
7491 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7492 } else if (instruction->IsOr()) {
7493 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7494 __ orl(first.AsRegisterPairHigh<Register>(),
7495 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7496 } else {
7497 DCHECK(instruction->IsXor());
7498 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7499 __ xorl(first.AsRegisterPairHigh<Register>(),
7500 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7501 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007502 } else {
7503 DCHECK(second.IsConstant()) << second;
7504 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007505 int32_t low_value = Low32Bits(value);
7506 int32_t high_value = High32Bits(value);
7507 Immediate low(low_value);
7508 Immediate high(high_value);
7509 Register first_low = first.AsRegisterPairLow<Register>();
7510 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007511 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007512 if (low_value == 0) {
7513 __ xorl(first_low, first_low);
7514 } else if (low_value != -1) {
7515 __ andl(first_low, low);
7516 }
7517 if (high_value == 0) {
7518 __ xorl(first_high, first_high);
7519 } else if (high_value != -1) {
7520 __ andl(first_high, high);
7521 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007522 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007523 if (low_value != 0) {
7524 __ orl(first_low, low);
7525 }
7526 if (high_value != 0) {
7527 __ orl(first_high, high);
7528 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007529 } else {
7530 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007531 if (low_value != 0) {
7532 __ xorl(first_low, low);
7533 }
7534 if (high_value != 0) {
7535 __ xorl(first_high, high);
7536 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007537 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007538 }
7539 }
7540}
7541
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007542void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7543 HInstruction* instruction,
7544 Location out,
7545 uint32_t offset,
7546 Location maybe_temp,
7547 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007548 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007549 if (read_barrier_option == kWithReadBarrier) {
7550 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007551 if (kUseBakerReadBarrier) {
7552 // Load with fast path based Baker's read barrier.
7553 // /* HeapReference<Object> */ out = *(out + offset)
7554 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007555 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007556 } else {
7557 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007558 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007559 // in the following move operation, as we will need it for the
7560 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007561 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007562 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007563 // /* HeapReference<Object> */ out = *(out + offset)
7564 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007565 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007566 }
7567 } else {
7568 // Plain load with no read barrier.
7569 // /* HeapReference<Object> */ out = *(out + offset)
7570 __ movl(out_reg, Address(out_reg, offset));
7571 __ MaybeUnpoisonHeapReference(out_reg);
7572 }
7573}
7574
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007575void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7576 HInstruction* instruction,
7577 Location out,
7578 Location obj,
7579 uint32_t offset,
7580 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007581 Register out_reg = out.AsRegister<Register>();
7582 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007583 if (read_barrier_option == kWithReadBarrier) {
7584 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007585 if (kUseBakerReadBarrier) {
7586 // Load with fast path based Baker's read barrier.
7587 // /* HeapReference<Object> */ out = *(obj + offset)
7588 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007589 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007590 } else {
7591 // Load with slow path based read barrier.
7592 // /* HeapReference<Object> */ out = *(obj + offset)
7593 __ movl(out_reg, Address(obj_reg, offset));
7594 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7595 }
7596 } else {
7597 // Plain load with no read barrier.
7598 // /* HeapReference<Object> */ out = *(obj + offset)
7599 __ movl(out_reg, Address(obj_reg, offset));
7600 __ MaybeUnpoisonHeapReference(out_reg);
7601 }
7602}
7603
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007604void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7605 HInstruction* instruction,
7606 Location root,
7607 const Address& address,
7608 Label* fixup_label,
7609 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007610 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007611 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007612 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007613 if (kUseBakerReadBarrier) {
7614 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7615 // Baker's read barrier are used:
7616 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007617 // root = obj.field;
7618 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7619 // if (temp != null) {
7620 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007621 // }
7622
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007623 // /* GcRoot<mirror::Object> */ root = *address
7624 __ movl(root_reg, address);
7625 if (fixup_label != nullptr) {
7626 __ Bind(fixup_label);
7627 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007628 static_assert(
7629 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7630 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7631 "have different sizes.");
7632 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7633 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7634 "have different sizes.");
7635
Vladimir Marko953437b2016-08-24 08:30:46 +00007636 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007637 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007638 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007639 codegen_->AddSlowPath(slow_path);
7640
Roland Levillaind966ce72017-02-09 16:20:14 +00007641 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7642 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007643 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007644 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7645 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007646 __ j(kNotEqual, slow_path->GetEntryLabel());
7647 __ Bind(slow_path->GetExitLabel());
7648 } else {
7649 // GC root loaded through a slow path for read barriers other
7650 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007651 // /* GcRoot<mirror::Object>* */ root = address
7652 __ leal(root_reg, address);
7653 if (fixup_label != nullptr) {
7654 __ Bind(fixup_label);
7655 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007656 // /* mirror::Object* */ root = root->Read()
7657 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7658 }
7659 } else {
7660 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007661 // /* GcRoot<mirror::Object> */ root = *address
7662 __ movl(root_reg, address);
7663 if (fixup_label != nullptr) {
7664 __ Bind(fixup_label);
7665 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007666 // Note that GC roots are not affected by heap poisoning, thus we
7667 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007668 }
7669}
7670
7671void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7672 Location ref,
7673 Register obj,
7674 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007675 bool needs_null_check) {
7676 DCHECK(kEmitCompilerReadBarrier);
7677 DCHECK(kUseBakerReadBarrier);
7678
7679 // /* HeapReference<Object> */ ref = *(obj + offset)
7680 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007681 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007682}
7683
7684void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7685 Location ref,
7686 Register obj,
7687 uint32_t data_offset,
7688 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007689 bool needs_null_check) {
7690 DCHECK(kEmitCompilerReadBarrier);
7691 DCHECK(kUseBakerReadBarrier);
7692
Roland Levillain3d312422016-06-23 13:53:42 +01007693 static_assert(
7694 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7695 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007696 // /* HeapReference<Object> */ ref =
7697 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007698 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007699 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007700}
7701
7702void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7703 Location ref,
7704 Register obj,
7705 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007706 bool needs_null_check,
7707 bool always_update_field,
7708 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007709 DCHECK(kEmitCompilerReadBarrier);
7710 DCHECK(kUseBakerReadBarrier);
7711
7712 // In slow path based read barriers, the read barrier call is
7713 // inserted after the original load. However, in fast path based
7714 // Baker's read barriers, we need to perform the load of
7715 // mirror::Object::monitor_ *before* the original reference load.
7716 // This load-load ordering is required by the read barrier.
7717 // The fast path/slow path (for Baker's algorithm) should look like:
7718 //
7719 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7720 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7721 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007722 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007723 // if (is_gray) {
7724 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7725 // }
7726 //
7727 // Note: the original implementation in ReadBarrier::Barrier is
7728 // slightly more complex as:
7729 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007730 // the high-bits of rb_state, which are expected to be all zeroes
7731 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7732 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007733 // - it performs additional checks that we do not do here for
7734 // performance reasons.
7735
7736 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007737 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7738
Vladimir Marko953437b2016-08-24 08:30:46 +00007739 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007740 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7741 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007742 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7743 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7744 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7745
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007746 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007747 // ref = ReadBarrier::Mark(ref);
7748 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7749 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007750 if (needs_null_check) {
7751 MaybeRecordImplicitNullCheck(instruction);
7752 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007753
7754 // Load fence to prevent load-load reordering.
7755 // Note that this is a no-op, thanks to the x86 memory model.
7756 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7757
7758 // The actual reference load.
7759 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007760 __ movl(ref_reg, src); // Flags are unaffected.
7761
7762 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7763 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007764 SlowPathCode* slow_path;
7765 if (always_update_field) {
7766 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007767 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007768 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7769 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007770 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007771 instruction, ref, /* unpoison_ref_before_marking */ true);
7772 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007773 AddSlowPath(slow_path);
7774
7775 // We have done the "if" of the gray bit check above, now branch based on the flags.
7776 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007777
7778 // Object* ref = ref_addr->AsMirrorPtr()
7779 __ MaybeUnpoisonHeapReference(ref_reg);
7780
Roland Levillain7c1559a2015-12-15 10:55:36 +00007781 __ Bind(slow_path->GetExitLabel());
7782}
7783
7784void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7785 Location out,
7786 Location ref,
7787 Location obj,
7788 uint32_t offset,
7789 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007790 DCHECK(kEmitCompilerReadBarrier);
7791
Roland Levillain7c1559a2015-12-15 10:55:36 +00007792 // Insert a slow path based read barrier *after* the reference load.
7793 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007794 // If heap poisoning is enabled, the unpoisoning of the loaded
7795 // reference will be carried out by the runtime within the slow
7796 // path.
7797 //
7798 // Note that `ref` currently does not get unpoisoned (when heap
7799 // poisoning is enabled), which is alright as the `ref` argument is
7800 // not used by the artReadBarrierSlow entry point.
7801 //
7802 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007803 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007804 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7805 AddSlowPath(slow_path);
7806
Roland Levillain0d5a2812015-11-13 10:07:31 +00007807 __ jmp(slow_path->GetEntryLabel());
7808 __ Bind(slow_path->GetExitLabel());
7809}
7810
Roland Levillain7c1559a2015-12-15 10:55:36 +00007811void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7812 Location out,
7813 Location ref,
7814 Location obj,
7815 uint32_t offset,
7816 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007817 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007818 // Baker's read barriers shall be handled by the fast path
7819 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7820 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007821 // If heap poisoning is enabled, unpoisoning will be taken care of
7822 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007823 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007824 } else if (kPoisonHeapReferences) {
7825 __ UnpoisonHeapReference(out.AsRegister<Register>());
7826 }
7827}
7828
Roland Levillain7c1559a2015-12-15 10:55:36 +00007829void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7830 Location out,
7831 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007832 DCHECK(kEmitCompilerReadBarrier);
7833
Roland Levillain7c1559a2015-12-15 10:55:36 +00007834 // Insert a slow path based read barrier *after* the GC root load.
7835 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007836 // Note that GC roots are not affected by heap poisoning, so we do
7837 // not need to do anything special for this here.
7838 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007839 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007840 AddSlowPath(slow_path);
7841
Roland Levillain0d5a2812015-11-13 10:07:31 +00007842 __ jmp(slow_path->GetEntryLabel());
7843 __ Bind(slow_path->GetExitLabel());
7844}
7845
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007846void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007847 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007848 LOG(FATAL) << "Unreachable";
7849}
7850
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007851void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007852 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007853 LOG(FATAL) << "Unreachable";
7854}
7855
Mark Mendellfe57faa2015-09-18 09:26:15 -04007856// Simple implementation of packed switch - generate cascaded compare/jumps.
7857void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7858 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007859 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007860 locations->SetInAt(0, Location::RequiresRegister());
7861}
7862
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007863void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7864 int32_t lower_bound,
7865 uint32_t num_entries,
7866 HBasicBlock* switch_block,
7867 HBasicBlock* default_block) {
7868 // Figure out the correct compare values and jump conditions.
7869 // Handle the first compare/branch as a special case because it might
7870 // jump to the default case.
7871 DCHECK_GT(num_entries, 2u);
7872 Condition first_condition;
7873 uint32_t index;
7874 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7875 if (lower_bound != 0) {
7876 first_condition = kLess;
7877 __ cmpl(value_reg, Immediate(lower_bound));
7878 __ j(first_condition, codegen_->GetLabelOf(default_block));
7879 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007880
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007881 index = 1;
7882 } else {
7883 // Handle all the compare/jumps below.
7884 first_condition = kBelow;
7885 index = 0;
7886 }
7887
7888 // Handle the rest of the compare/jumps.
7889 for (; index + 1 < num_entries; index += 2) {
7890 int32_t compare_to_value = lower_bound + index + 1;
7891 __ cmpl(value_reg, Immediate(compare_to_value));
7892 // Jump to successors[index] if value < case_value[index].
7893 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7894 // Jump to successors[index + 1] if value == case_value[index + 1].
7895 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7896 }
7897
7898 if (index != num_entries) {
7899 // There are an odd number of entries. Handle the last one.
7900 DCHECK_EQ(index + 1, num_entries);
7901 __ cmpl(value_reg, Immediate(lower_bound + index));
7902 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007903 }
7904
7905 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007906 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7907 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007908 }
7909}
7910
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007911void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7912 int32_t lower_bound = switch_instr->GetStartValue();
7913 uint32_t num_entries = switch_instr->GetNumEntries();
7914 LocationSummary* locations = switch_instr->GetLocations();
7915 Register value_reg = locations->InAt(0).AsRegister<Register>();
7916
7917 GenPackedSwitchWithCompares(value_reg,
7918 lower_bound,
7919 num_entries,
7920 switch_instr->GetBlock(),
7921 switch_instr->GetDefaultBlock());
7922}
7923
Mark Mendell805b3b52015-09-18 14:10:29 -04007924void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7925 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007926 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007927 locations->SetInAt(0, Location::RequiresRegister());
7928
7929 // Constant area pointer.
7930 locations->SetInAt(1, Location::RequiresRegister());
7931
7932 // And the temporary we need.
7933 locations->AddTemp(Location::RequiresRegister());
7934}
7935
7936void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7937 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007938 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007939 LocationSummary* locations = switch_instr->GetLocations();
7940 Register value_reg = locations->InAt(0).AsRegister<Register>();
7941 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7942
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007943 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7944 GenPackedSwitchWithCompares(value_reg,
7945 lower_bound,
7946 num_entries,
7947 switch_instr->GetBlock(),
7948 default_block);
7949 return;
7950 }
7951
Mark Mendell805b3b52015-09-18 14:10:29 -04007952 // Optimizing has a jump area.
7953 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7954 Register constant_area = locations->InAt(1).AsRegister<Register>();
7955
7956 // Remove the bias, if needed.
7957 if (lower_bound != 0) {
7958 __ leal(temp_reg, Address(value_reg, -lower_bound));
7959 value_reg = temp_reg;
7960 }
7961
7962 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007963 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007964 __ cmpl(value_reg, Immediate(num_entries - 1));
7965 __ j(kAbove, codegen_->GetLabelOf(default_block));
7966
7967 // We are in the range of the table.
7968 // Load (target-constant_area) from the jump table, indexing by the value.
7969 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7970
7971 // Compute the actual target address by adding in constant_area.
7972 __ addl(temp_reg, constant_area);
7973
7974 // And jump.
7975 __ jmp(temp_reg);
7976}
7977
Mark Mendell0616ae02015-04-17 12:49:27 -04007978void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7979 HX86ComputeBaseMethodAddress* insn) {
7980 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007981 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007982 locations->SetOut(Location::RequiresRegister());
7983}
7984
7985void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7986 HX86ComputeBaseMethodAddress* insn) {
7987 LocationSummary* locations = insn->GetLocations();
7988 Register reg = locations->Out().AsRegister<Register>();
7989
7990 // Generate call to next instruction.
7991 Label next_instruction;
7992 __ call(&next_instruction);
7993 __ Bind(&next_instruction);
7994
7995 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007996 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007997
7998 // Grab the return address off the stack.
7999 __ popl(reg);
8000}
8001
8002void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8003 HX86LoadFromConstantTable* insn) {
8004 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008005 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008006
8007 locations->SetInAt(0, Location::RequiresRegister());
8008 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8009
8010 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008011 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008012 return;
8013 }
8014
8015 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008016 case DataType::Type::kFloat32:
8017 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008018 locations->SetOut(Location::RequiresFpuRegister());
8019 break;
8020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008021 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008022 locations->SetOut(Location::RequiresRegister());
8023 break;
8024
8025 default:
8026 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8027 }
8028}
8029
8030void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008031 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008032 return;
8033 }
8034
8035 LocationSummary* locations = insn->GetLocations();
8036 Location out = locations->Out();
8037 Register const_area = locations->InAt(0).AsRegister<Register>();
8038 HConstant *value = insn->GetConstant();
8039
8040 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008041 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008042 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008043 codegen_->LiteralFloatAddress(
8044 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008045 break;
8046
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008047 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008048 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008049 codegen_->LiteralDoubleAddress(
8050 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008051 break;
8052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008053 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008054 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008055 codegen_->LiteralInt32Address(
8056 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008057 break;
8058
8059 default:
8060 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8061 }
8062}
8063
Mark Mendell0616ae02015-04-17 12:49:27 -04008064/**
8065 * Class to handle late fixup of offsets into constant area.
8066 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008067class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008068 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008069 RIPFixup(CodeGeneratorX86& codegen,
8070 HX86ComputeBaseMethodAddress* base_method_address,
8071 size_t offset)
8072 : codegen_(&codegen),
8073 base_method_address_(base_method_address),
8074 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008075
8076 protected:
8077 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8078
8079 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008080 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008081
8082 private:
8083 void Process(const MemoryRegion& region, int pos) OVERRIDE {
8084 // Patch the correct offset for the instruction. The place to patch is the
8085 // last 4 bytes of the instruction.
8086 // The value to patch is the distance from the offset in the constant area
8087 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008088 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008089 int32_t relative_position =
8090 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008091
8092 // Patch in the right value.
8093 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8094 }
8095
Mark Mendell0616ae02015-04-17 12:49:27 -04008096 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008097 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008098};
8099
Mark Mendell805b3b52015-09-18 14:10:29 -04008100/**
8101 * Class to handle late fixup of offsets to a jump table that will be created in the
8102 * constant area.
8103 */
8104class JumpTableRIPFixup : public RIPFixup {
8105 public:
8106 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008107 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8108 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008109
8110 void CreateJumpTable() {
8111 X86Assembler* assembler = codegen_->GetAssembler();
8112
8113 // Ensure that the reference to the jump table has the correct offset.
8114 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8115 SetOffset(offset_in_constant_table);
8116
8117 // The label values in the jump table are computed relative to the
8118 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008119 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008120
8121 // Populate the jump table with the correct values for the jump table.
8122 int32_t num_entries = switch_instr_->GetNumEntries();
8123 HBasicBlock* block = switch_instr_->GetBlock();
8124 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8125 // The value that we want is the target offset - the position of the table.
8126 for (int32_t i = 0; i < num_entries; i++) {
8127 HBasicBlock* b = successors[i];
8128 Label* l = codegen_->GetLabelOf(b);
8129 DCHECK(l->IsBound());
8130 int32_t offset_to_block = l->Position() - relative_offset;
8131 assembler->AppendInt32(offset_to_block);
8132 }
8133 }
8134
8135 private:
8136 const HX86PackedSwitch* switch_instr_;
8137};
8138
8139void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8140 // Generate the constant area if needed.
8141 X86Assembler* assembler = GetAssembler();
8142 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8143 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8144 // byte values.
8145 assembler->Align(4, 0);
8146 constant_area_start_ = assembler->CodeSize();
8147
8148 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008149 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008150 jump_table->CreateJumpTable();
8151 }
8152
8153 // And now add the constant area to the generated code.
8154 assembler->AddConstantArea();
8155 }
8156
8157 // And finish up.
8158 CodeGenerator::Finalize(allocator);
8159}
8160
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008161Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8162 HX86ComputeBaseMethodAddress* method_base,
8163 Register reg) {
8164 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008165 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008166 return Address(reg, kDummy32BitOffset, fixup);
8167}
8168
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008169Address CodeGeneratorX86::LiteralFloatAddress(float v,
8170 HX86ComputeBaseMethodAddress* method_base,
8171 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008172 AssemblerFixup* fixup =
8173 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008174 return Address(reg, kDummy32BitOffset, fixup);
8175}
8176
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008177Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8178 HX86ComputeBaseMethodAddress* method_base,
8179 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008180 AssemblerFixup* fixup =
8181 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008182 return Address(reg, kDummy32BitOffset, fixup);
8183}
8184
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008185Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8186 HX86ComputeBaseMethodAddress* method_base,
8187 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008188 AssemblerFixup* fixup =
8189 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008190 return Address(reg, kDummy32BitOffset, fixup);
8191}
8192
Aart Bika19616e2016-02-01 18:57:58 -08008193void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8194 if (value == 0) {
8195 __ xorl(dest, dest);
8196 } else {
8197 __ movl(dest, Immediate(value));
8198 }
8199}
8200
8201void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8202 if (value == 0) {
8203 __ testl(dest, dest);
8204 } else {
8205 __ cmpl(dest, Immediate(value));
8206 }
8207}
8208
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008209void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8210 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008211 GenerateIntCompare(lhs_reg, rhs);
8212}
8213
8214void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008215 if (rhs.IsConstant()) {
8216 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008217 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008218 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008219 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008220 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008221 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008222 }
8223}
8224
8225Address CodeGeneratorX86::ArrayAddress(Register obj,
8226 Location index,
8227 ScaleFactor scale,
8228 uint32_t data_offset) {
8229 return index.IsConstant() ?
8230 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8231 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8232}
8233
Mark Mendell805b3b52015-09-18 14:10:29 -04008234Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8235 Register reg,
8236 Register value) {
8237 // Create a fixup to be used to create and address the jump table.
8238 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008239 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008240
8241 // We have to populate the jump tables.
8242 fixups_to_jump_tables_.push_back(table_fixup);
8243
8244 // We want a scaled address, as we are extracting the correct offset from the table.
8245 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8246}
8247
Andreas Gampe85b62f22015-09-09 13:15:38 -07008248// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008249void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008250 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008251 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008252 return;
8253 }
8254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008255 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008256
8257 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8258 if (target.Equals(return_loc)) {
8259 return;
8260 }
8261
8262 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8263 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008264 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008265 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008266 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8267 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008268 GetMoveResolver()->EmitNativeCode(&parallel_move);
8269 } else {
8270 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008271 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008272 parallel_move.AddMove(return_loc, target, type, nullptr);
8273 GetMoveResolver()->EmitNativeCode(&parallel_move);
8274 }
8275}
8276
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008277void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8278 const uint8_t* roots_data,
8279 const PatchInfo<Label>& info,
8280 uint64_t index_in_table) const {
8281 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8282 uintptr_t address =
8283 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8284 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8285 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8286 dchecked_integral_cast<uint32_t>(address);
8287}
8288
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008289void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8290 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008291 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008292 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008293 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008294 }
8295
8296 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008297 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008298 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008299 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008300 }
8301}
8302
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008303void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8304 ATTRIBUTE_UNUSED) {
8305 LOG(FATAL) << "Unreachable";
8306}
8307
8308void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8309 ATTRIBUTE_UNUSED) {
8310 LOG(FATAL) << "Unreachable";
8311}
8312
Roland Levillain4d027112015-07-01 15:41:14 +01008313#undef __
8314
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008315} // namespace x86
8316} // namespace art