blob: 12872edd0dd9989a01432a5d1fd1c53912dd1729 [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
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100961size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
962 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
963 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100964}
965
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100966size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
967 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
968 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100969}
970
Mark Mendell7c8d0092015-01-26 11:21:33 -0500971size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700972 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700973 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700974 } else {
975 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
976 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500977 return GetFloatingPointSpillSlotSize();
978}
979
980size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700981 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700982 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700983 } else {
984 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
985 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500986 return GetFloatingPointSpillSlotSize();
987}
988
Calin Juravle175dc732015-08-25 15:42:32 +0100989void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
990 HInstruction* instruction,
991 uint32_t dex_pc,
992 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100993 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100994 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
995 if (EntrypointRequiresStackMap(entrypoint)) {
996 RecordPcInfo(instruction, dex_pc, slow_path);
997 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100998}
999
Roland Levillaindec8f632016-07-22 17:10:06 +01001000void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1001 HInstruction* instruction,
1002 SlowPathCode* slow_path) {
1003 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001004 GenerateInvokeRuntime(entry_point_offset);
1005}
1006
1007void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001008 __ fs()->call(Address::Absolute(entry_point_offset));
1009}
1010
Mark Mendellfb8d2792015-03-31 22:16:59 -04001011CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001012 const X86InstructionSetFeatures& isa_features,
1013 const CompilerOptions& compiler_options,
1014 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001015 : CodeGenerator(graph,
1016 kNumberOfCpuRegisters,
1017 kNumberOfXmmRegisters,
1018 kNumberOfRegisterPairs,
1019 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1020 arraysize(kCoreCalleeSaves))
1021 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001022 0,
1023 compiler_options,
1024 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001025 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001026 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001027 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001028 move_resolver_(graph->GetAllocator(), this),
1029 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001030 isa_features_(isa_features),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001031 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1032 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1033 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1034 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001035 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001036 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1037 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1038 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001039 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001041 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001043 // Use a fake return address register to mimic Quick.
1044 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001045}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001046
David Brazdil58282f42016-01-14 12:45:10 +00001047void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001049 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001050}
1051
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001052InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001053 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001054 assembler_(codegen->GetAssembler()),
1055 codegen_(codegen) {}
1056
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001057static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001058 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001059}
1060
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001061void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001063 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001064 bool skip_overflow_check =
1065 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001066 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001067
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001068 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1069 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1070 Immediate(1));
1071 }
1072
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001073 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001074 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1075 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001076 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001077 }
1078
Mark Mendell5f874182015-03-04 15:42:45 -05001079 if (HasEmptyFrame()) {
1080 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001081 }
Mark Mendell5f874182015-03-04 15:42:45 -05001082
1083 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1084 Register reg = kCoreCalleeSaves[i];
1085 if (allocated_registers_.ContainsCoreRegister(reg)) {
1086 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001087 __ cfi().AdjustCFAOffset(kX86WordSize);
1088 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001089 }
1090 }
1091
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092 int adjust = GetFrameSize() - FrameEntrySpillSize();
1093 __ subl(ESP, Immediate(adjust));
1094 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001095 // Save the current method if we need it. Note that we do not
1096 // do this in HCurrentMethod, as the instruction might have been removed
1097 // in the SSA graph.
1098 if (RequiresCurrentMethod()) {
1099 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1100 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001101
1102 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1103 // Initialize should_deoptimize flag to 0.
1104 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1105 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001106}
1107
1108void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001109 __ cfi().RememberState();
1110 if (!HasEmptyFrame()) {
1111 int adjust = GetFrameSize() - FrameEntrySpillSize();
1112 __ addl(ESP, Immediate(adjust));
1113 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001114
David Srbeckyc34dc932015-04-12 09:27:43 +01001115 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1116 Register reg = kCoreCalleeSaves[i];
1117 if (allocated_registers_.ContainsCoreRegister(reg)) {
1118 __ popl(reg);
1119 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1120 __ cfi().Restore(DWARFReg(reg));
1121 }
Mark Mendell5f874182015-03-04 15:42:45 -05001122 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001123 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001124 __ ret();
1125 __ cfi().RestoreState();
1126 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001129void CodeGeneratorX86::Bind(HBasicBlock* block) {
1130 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001131}
1132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001133Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001134 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001135 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001136 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001137 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 case DataType::Type::kInt8:
1139 case DataType::Type::kUint16:
1140 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001141 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001142 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143 return Location::RegisterLocation(EAX);
1144
Aart Bik66c158e2018-01-31 12:55:04 -08001145 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001147 return Location::RegisterPairLocation(EAX, EDX);
1148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001149 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150 return Location::NoLocation();
1151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001152 case DataType::Type::kFloat64:
1153 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001154 return Location::FpuRegisterLocation(XMM0);
1155 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001156
1157 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001158}
1159
1160Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1161 return Location::RegisterLocation(kMethodRegisterArgument);
1162}
1163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001164Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001165 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001166 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001168 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001169 case DataType::Type::kInt8:
1170 case DataType::Type::kUint16:
1171 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001172 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001174 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001178 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001179 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001180 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001182 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 uint32_t index = gp_index_;
1184 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001185 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001187 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1188 calling_convention.GetRegisterPairAt(index));
1189 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001190 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001191 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1192 }
1193 }
1194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001195 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001196 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001197 stack_index_++;
1198 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1199 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1200 } else {
1201 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1202 }
1203 }
1204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001206 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001207 stack_index_ += 2;
1208 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1209 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1210 } else {
1211 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001212 }
1213 }
1214
Aart Bik66c158e2018-01-31 12:55:04 -08001215 case DataType::Type::kUint32:
1216 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001218 LOG(FATAL) << "Unexpected parameter type " << type;
1219 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001220 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001221 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001222}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224void CodeGeneratorX86::Move32(Location destination, Location source) {
1225 if (source.Equals(destination)) {
1226 return;
1227 }
1228 if (destination.IsRegister()) {
1229 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 } else {
1234 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 } else if (destination.IsFpuRegister()) {
1238 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 } else {
1243 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001247 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001249 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001250 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001252 } else if (source.IsConstant()) {
1253 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001254 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001255 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 } else {
1257 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001258 __ pushl(Address(ESP, source.GetStackIndex()));
1259 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 }
1261 }
1262}
1263
1264void CodeGeneratorX86::Move64(Location destination, Location source) {
1265 if (source.Equals(destination)) {
1266 return;
1267 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001268 if (destination.IsRegisterPair()) {
1269 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001270 EmitParallelMoves(
1271 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1272 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001273 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001274 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001275 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001276 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1279 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1280 __ psrlq(src_reg, Immediate(32));
1281 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001282 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001283 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001284 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001285 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1286 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001287 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1288 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001289 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001290 if (source.IsFpuRegister()) {
1291 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1292 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001293 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001294 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001295 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001296 // Create stack space for 2 elements.
1297 __ subl(ESP, Immediate(2 * elem_size));
1298 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1299 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1300 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1301 // And remove the temporary stack space we allocated.
1302 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 } else {
1304 LOG(FATAL) << "Unimplemented";
1305 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001306 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001307 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001309 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001312 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001313 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001315 } else if (source.IsConstant()) {
1316 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001317 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1318 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001319 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001320 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1321 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001322 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001323 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001324 EmitParallelMoves(
1325 Location::StackSlot(source.GetStackIndex()),
1326 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001327 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001328 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001329 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001330 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001331 }
1332 }
1333}
1334
Calin Juravle175dc732015-08-25 15:42:32 +01001335void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1336 DCHECK(location.IsRegister());
1337 __ movl(location.AsRegister<Register>(), Immediate(value));
1338}
1339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001340void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001341 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1343 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1344 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001345 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001346 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001347 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001348 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001349}
1350
1351void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1352 if (location.IsRegister()) {
1353 locations->AddTemp(location);
1354 } else if (location.IsRegisterPair()) {
1355 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1356 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1357 } else {
1358 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1359 }
1360}
1361
David Brazdilfc6a86a2015-06-26 10:33:45 +00001362void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001363 if (successor->IsExitBlock()) {
1364 DCHECK(got->GetPrevious()->AlwaysThrows());
1365 return; // no code needed
1366 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001367
1368 HBasicBlock* block = got->GetBlock();
1369 HInstruction* previous = got->GetPrevious();
1370
1371 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001372 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001373 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1374 __ pushl(EAX);
1375 __ movl(EAX, Address(ESP, kX86WordSize));
1376 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1377 __ popl(EAX);
1378 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001379 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1380 return;
1381 }
1382
1383 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1384 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1385 }
1386 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001387 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001388 }
1389}
1390
David Brazdilfc6a86a2015-06-26 10:33:45 +00001391void LocationsBuilderX86::VisitGoto(HGoto* got) {
1392 got->SetLocations(nullptr);
1393}
1394
1395void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1396 HandleGoto(got, got->GetSuccessor());
1397}
1398
1399void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1400 try_boundary->SetLocations(nullptr);
1401}
1402
1403void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1404 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1405 if (!successor->IsExitBlock()) {
1406 HandleGoto(try_boundary, successor);
1407 }
1408}
1409
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001411 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412}
1413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001414void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001415}
1416
Mark Mendell152408f2015-12-31 12:28:50 -05001417template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001418void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001419 LabelType* true_label,
1420 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 if (cond->IsFPConditionTrueIfNaN()) {
1422 __ j(kUnordered, true_label);
1423 } else if (cond->IsFPConditionFalseIfNaN()) {
1424 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001425 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001426 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001427}
1428
Mark Mendell152408f2015-12-31 12:28:50 -05001429template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001430void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001431 LabelType* true_label,
1432 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001433 LocationSummary* locations = cond->GetLocations();
1434 Location left = locations->InAt(0);
1435 Location right = locations->InAt(1);
1436 IfCondition if_cond = cond->GetCondition();
1437
Mark Mendellc4701932015-04-10 13:18:51 -04001438 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001439 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001440 IfCondition true_high_cond = if_cond;
1441 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001442 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001443
1444 // Set the conditions for the test, remembering that == needs to be
1445 // decided using the low words.
1446 switch (if_cond) {
1447 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001448 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001449 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001450 break;
1451 case kCondLT:
1452 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001453 break;
1454 case kCondLE:
1455 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001456 break;
1457 case kCondGT:
1458 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001459 break;
1460 case kCondGE:
1461 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001462 break;
Aart Bike9f37602015-10-09 11:15:55 -07001463 case kCondB:
1464 false_high_cond = kCondA;
1465 break;
1466 case kCondBE:
1467 true_high_cond = kCondB;
1468 break;
1469 case kCondA:
1470 false_high_cond = kCondB;
1471 break;
1472 case kCondAE:
1473 true_high_cond = kCondA;
1474 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001475 }
1476
1477 if (right.IsConstant()) {
1478 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001479 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001480 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001481
Aart Bika19616e2016-02-01 18:57:58 -08001482 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001483 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001484 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001485 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001486 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001487 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001488 __ j(X86Condition(true_high_cond), true_label);
1489 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001490 }
1491 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001492 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001493 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001494 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001495 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001496
1497 __ cmpl(left_high, right_high);
1498 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001499 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001500 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001501 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001502 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001503 __ j(X86Condition(true_high_cond), true_label);
1504 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001505 }
1506 // Must be equal high, so compare the lows.
1507 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001508 } else {
1509 DCHECK(right.IsDoubleStackSlot());
1510 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1511 if (if_cond == kCondNE) {
1512 __ j(X86Condition(true_high_cond), true_label);
1513 } else if (if_cond == kCondEQ) {
1514 __ j(X86Condition(false_high_cond), false_label);
1515 } else {
1516 __ j(X86Condition(true_high_cond), true_label);
1517 __ j(X86Condition(false_high_cond), false_label);
1518 }
1519 // Must be equal high, so compare the lows.
1520 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001521 }
1522 // The last comparison might be unsigned.
1523 __ j(final_condition, true_label);
1524}
1525
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001526void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1527 Location rhs,
1528 HInstruction* insn,
1529 bool is_double) {
1530 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1531 if (is_double) {
1532 if (rhs.IsFpuRegister()) {
1533 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1534 } else if (const_area != nullptr) {
1535 DCHECK(const_area->IsEmittedAtUseSite());
1536 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1537 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001538 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1539 const_area->GetBaseMethodAddress(),
1540 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001541 } else {
1542 DCHECK(rhs.IsDoubleStackSlot());
1543 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1544 }
1545 } else {
1546 if (rhs.IsFpuRegister()) {
1547 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1548 } else if (const_area != nullptr) {
1549 DCHECK(const_area->IsEmittedAtUseSite());
1550 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1551 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001552 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1553 const_area->GetBaseMethodAddress(),
1554 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001555 } else {
1556 DCHECK(rhs.IsStackSlot());
1557 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1558 }
1559 }
1560}
1561
Mark Mendell152408f2015-12-31 12:28:50 -05001562template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001563void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001564 LabelType* true_target_in,
1565 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001566 // Generated branching requires both targets to be explicit. If either of the
1567 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001568 LabelType fallthrough_target;
1569 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1570 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001571
Mark Mendellc4701932015-04-10 13:18:51 -04001572 LocationSummary* locations = condition->GetLocations();
1573 Location left = locations->InAt(0);
1574 Location right = locations->InAt(1);
1575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001576 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001577 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001578 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001579 GenerateLongComparesAndJumps(condition, true_target, false_target);
1580 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001581 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001582 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001583 GenerateFPJumps(condition, true_target, false_target);
1584 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001585 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001586 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001587 GenerateFPJumps(condition, true_target, false_target);
1588 break;
1589 default:
1590 LOG(FATAL) << "Unexpected compare type " << type;
1591 }
1592
David Brazdil0debae72015-11-12 18:37:00 +00001593 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001594 __ jmp(false_target);
1595 }
David Brazdil0debae72015-11-12 18:37:00 +00001596
1597 if (fallthrough_target.IsLinked()) {
1598 __ Bind(&fallthrough_target);
1599 }
Mark Mendellc4701932015-04-10 13:18:51 -04001600}
1601
David Brazdil0debae72015-11-12 18:37:00 +00001602static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1603 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1604 // are set only strictly before `branch`. We can't use the eflags on long/FP
1605 // conditions if they are materialized due to the complex branching.
1606 return cond->IsCondition() &&
1607 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001608 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1609 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001610}
1611
Mark Mendell152408f2015-12-31 12:28:50 -05001612template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001613void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001614 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001615 LabelType* true_target,
1616 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001617 HInstruction* cond = instruction->InputAt(condition_input_index);
1618
1619 if (true_target == nullptr && false_target == nullptr) {
1620 // Nothing to do. The code always falls through.
1621 return;
1622 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001623 // Constant condition, statically compared against "true" (integer value 1).
1624 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001625 if (true_target != nullptr) {
1626 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001627 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001628 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001629 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001630 if (false_target != nullptr) {
1631 __ jmp(false_target);
1632 }
1633 }
1634 return;
1635 }
1636
1637 // The following code generates these patterns:
1638 // (1) true_target == nullptr && false_target != nullptr
1639 // - opposite condition true => branch to false_target
1640 // (2) true_target != nullptr && false_target == nullptr
1641 // - condition true => branch to true_target
1642 // (3) true_target != nullptr && false_target != nullptr
1643 // - condition true => branch to true_target
1644 // - branch to false_target
1645 if (IsBooleanValueOrMaterializedCondition(cond)) {
1646 if (AreEflagsSetFrom(cond, instruction)) {
1647 if (true_target == nullptr) {
1648 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1649 } else {
1650 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1651 }
1652 } else {
1653 // Materialized condition, compare against 0.
1654 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1655 if (lhs.IsRegister()) {
1656 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1657 } else {
1658 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1659 }
1660 if (true_target == nullptr) {
1661 __ j(kEqual, false_target);
1662 } else {
1663 __ j(kNotEqual, true_target);
1664 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001665 }
1666 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001667 // Condition has not been materialized, use its inputs as the comparison and
1668 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001669 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001670
1671 // If this is a long or FP comparison that has been folded into
1672 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 DataType::Type type = condition->InputAt(0)->GetType();
1674 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001675 GenerateCompareTestAndBranch(condition, true_target, false_target);
1676 return;
1677 }
1678
1679 Location lhs = condition->GetLocations()->InAt(0);
1680 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001682 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001683 if (true_target == nullptr) {
1684 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1685 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001686 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001687 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001688 }
David Brazdil0debae72015-11-12 18:37:00 +00001689
1690 // If neither branch falls through (case 3), the conditional branch to `true_target`
1691 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1692 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001694 }
1695}
1696
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001697void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001698 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001699 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700 locations->SetInAt(0, Location::Any());
1701 }
1702}
1703
1704void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001705 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1706 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1707 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1708 nullptr : codegen_->GetLabelOf(true_successor);
1709 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1710 nullptr : codegen_->GetLabelOf(false_successor);
1711 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001712}
1713
1714void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001715 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001716 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001717 InvokeRuntimeCallingConvention calling_convention;
1718 RegisterSet caller_saves = RegisterSet::Empty();
1719 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1720 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001721 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001722 locations->SetInAt(0, Location::Any());
1723 }
1724}
1725
1726void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001727 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001728 GenerateTestAndBranch<Label>(deoptimize,
1729 /* condition_input_index */ 0,
1730 slow_path->GetEntryLabel(),
1731 /* false_target */ nullptr);
1732}
1733
Mingyao Yang063fc772016-08-02 11:02:54 -07001734void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001735 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001736 LocationSummary(flag, LocationSummary::kNoCall);
1737 locations->SetOut(Location::RequiresRegister());
1738}
1739
1740void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1741 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1742 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1743}
1744
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001745static bool SelectCanUseCMOV(HSelect* select) {
1746 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001748 return false;
1749 }
1750
1751 // A FP condition doesn't generate the single CC that we need.
1752 // In 32 bit mode, a long condition doesn't generate a single CC either.
1753 HInstruction* condition = select->GetCondition();
1754 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 DataType::Type compare_type = condition->InputAt(0)->GetType();
1756 if (compare_type == DataType::Type::kInt64 ||
1757 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001758 return false;
1759 }
1760 }
1761
1762 // We can generate a CMOV for this Select.
1763 return true;
1764}
1765
David Brazdil74eb1b22015-12-14 11:44:01 +00001766void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001767 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001768 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001769 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001770 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001771 } else {
1772 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001773 if (SelectCanUseCMOV(select)) {
1774 if (select->InputAt(1)->IsConstant()) {
1775 // Cmov can't handle a constant value.
1776 locations->SetInAt(1, Location::RequiresRegister());
1777 } else {
1778 locations->SetInAt(1, Location::Any());
1779 }
1780 } else {
1781 locations->SetInAt(1, Location::Any());
1782 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001783 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001784 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1785 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001786 }
1787 locations->SetOut(Location::SameAsFirstInput());
1788}
1789
1790void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1791 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001792 DCHECK(locations->InAt(0).Equals(locations->Out()));
1793 if (SelectCanUseCMOV(select)) {
1794 // If both the condition and the source types are integer, we can generate
1795 // a CMOV to implement Select.
1796
1797 HInstruction* select_condition = select->GetCondition();
1798 Condition cond = kNotEqual;
1799
1800 // Figure out how to test the 'condition'.
1801 if (select_condition->IsCondition()) {
1802 HCondition* condition = select_condition->AsCondition();
1803 if (!condition->IsEmittedAtUseSite()) {
1804 // This was a previously materialized condition.
1805 // Can we use the existing condition code?
1806 if (AreEflagsSetFrom(condition, select)) {
1807 // Materialization was the previous instruction. Condition codes are right.
1808 cond = X86Condition(condition->GetCondition());
1809 } else {
1810 // No, we have to recreate the condition code.
1811 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1812 __ testl(cond_reg, cond_reg);
1813 }
1814 } else {
1815 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1817 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001818 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001819 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001820 cond = X86Condition(condition->GetCondition());
1821 }
1822 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001823 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001824 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1825 __ testl(cond_reg, cond_reg);
1826 }
1827
1828 // If the condition is true, overwrite the output, which already contains false.
1829 Location false_loc = locations->InAt(0);
1830 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001832 // 64 bit conditional move.
1833 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1834 Register false_low = false_loc.AsRegisterPairLow<Register>();
1835 if (true_loc.IsRegisterPair()) {
1836 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1837 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1838 } else {
1839 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1840 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1841 }
1842 } else {
1843 // 32 bit conditional move.
1844 Register false_reg = false_loc.AsRegister<Register>();
1845 if (true_loc.IsRegister()) {
1846 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1847 } else {
1848 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1849 }
1850 }
1851 } else {
1852 NearLabel false_target;
1853 GenerateTestAndBranch<NearLabel>(
1854 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1855 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1856 __ Bind(&false_target);
1857 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001858}
1859
David Srbecky0cf44932015-12-09 14:09:59 +00001860void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001862}
1863
David Srbeckyd28f4a02016-03-14 17:14:24 +00001864void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1865 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001866}
1867
1868void CodeGeneratorX86::GenerateNop() {
1869 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001870}
1871
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001873 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001874 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001875 // Handle the long/FP comparisons made in instruction simplification.
1876 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001877 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001878 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001879 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001880 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001881 locations->SetOut(Location::RequiresRegister());
1882 }
1883 break;
1884 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001885 case DataType::Type::kFloat32:
1886 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001887 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001888 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1889 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1890 } else if (cond->InputAt(1)->IsConstant()) {
1891 locations->SetInAt(1, Location::RequiresFpuRegister());
1892 } else {
1893 locations->SetInAt(1, Location::Any());
1894 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001895 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001896 locations->SetOut(Location::RequiresRegister());
1897 }
1898 break;
1899 }
1900 default:
1901 locations->SetInAt(0, Location::RequiresRegister());
1902 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001903 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001904 // We need a byte register.
1905 locations->SetOut(Location::RegisterLocation(ECX));
1906 }
1907 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001908 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001909}
1910
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001911void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001912 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001913 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001914 }
Mark Mendellc4701932015-04-10 13:18:51 -04001915
1916 LocationSummary* locations = cond->GetLocations();
1917 Location lhs = locations->InAt(0);
1918 Location rhs = locations->InAt(1);
1919 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001920 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001921
1922 switch (cond->InputAt(0)->GetType()) {
1923 default: {
1924 // Integer case.
1925
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001926 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001927 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001928 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001929 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001930 return;
1931 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001933 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001936 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001937 GenerateFPJumps(cond, &true_label, &false_label);
1938 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001939 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001940 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001941 GenerateFPJumps(cond, &true_label, &false_label);
1942 break;
1943 }
1944
1945 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001946 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001947
Roland Levillain4fa13f62015-07-06 18:11:54 +01001948 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001949 __ Bind(&false_label);
1950 __ xorl(reg, reg);
1951 __ jmp(&done_label);
1952
Roland Levillain4fa13f62015-07-06 18:11:54 +01001953 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001954 __ Bind(&true_label);
1955 __ movl(reg, Immediate(1));
1956 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001997}
1998
1999void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002001}
2002
2003void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002005}
2006
Aart Bike9f37602015-10-09 11:15:55 -07002007void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
2031void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002033}
2034
2035void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002036 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002037}
2038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002041 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002042 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043}
2044
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002045void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002046 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047}
2048
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002049void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2050 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002051 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002052 locations->SetOut(Location::ConstantLocation(constant));
2053}
2054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002055void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002056 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002057}
2058
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002059void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002060 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002061 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002062 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063}
2064
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002065void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066 // Will be generated at use site.
2067}
2068
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002069void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2070 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002071 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072 locations->SetOut(Location::ConstantLocation(constant));
2073}
2074
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002075void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002076 // Will be generated at use site.
2077}
2078
2079void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2080 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002081 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002082 locations->SetOut(Location::ConstantLocation(constant));
2083}
2084
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002085void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002086 // Will be generated at use site.
2087}
2088
Igor Murashkind01745e2017-04-05 16:40:31 -07002089void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2090 constructor_fence->SetLocations(nullptr);
2091}
2092
2093void InstructionCodeGeneratorX86::VisitConstructorFence(
2094 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2095 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2096}
2097
Calin Juravle27df7582015-04-17 19:12:31 +01002098void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2099 memory_barrier->SetLocations(nullptr);
2100}
2101
2102void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002103 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002104}
2105
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002106void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002107 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002108}
2109
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002110void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002111 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002112}
2113
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002114void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002115 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002116 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002118 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002120 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt8:
2122 case DataType::Type::kUint16:
2123 case DataType::Type::kInt16:
2124 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002125 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126 break;
2127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 break;
2132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 case DataType::Type::kFloat32:
2134 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002135 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002136 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002137 break;
2138
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002139 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002142}
2143
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002144void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002145 if (kIsDebugBuild) {
2146 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002147 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002149 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 case DataType::Type::kInt8:
2151 case DataType::Type::kUint16:
2152 case DataType::Type::kInt16:
2153 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002155 break;
2156
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002158 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2159 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002160 break;
2161
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 case DataType::Type::kFloat32:
2163 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 break;
2166
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002167 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002168 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002169 }
2170 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002171 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002172}
2173
Calin Juravle175dc732015-08-25 15:42:32 +01002174void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2175 // The trampoline uses the same calling convention as dex calling conventions,
2176 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2177 // the method_idx.
2178 HandleInvoke(invoke);
2179}
2180
2181void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2182 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2183}
2184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002185void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002186 // Explicit clinit checks triggered by static invokes must have been pruned by
2187 // art::PrepareForRegisterAllocation.
2188 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002189
Mark Mendellfb8d2792015-03-31 22:16:59 -04002190 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002191 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002192 if (invoke->GetLocations()->CanCall() &&
2193 invoke->HasPcRelativeMethodLoadKind() &&
2194 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002195 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002196 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002197 return;
2198 }
2199
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002200 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002201
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002202 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002203 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002204 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002205 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002206}
2207
Mark Mendell09ed1a32015-03-25 08:30:06 -04002208static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2209 if (invoke->GetLocations()->Intrinsified()) {
2210 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2211 intrinsic.Dispatch(invoke);
2212 return true;
2213 }
2214 return false;
2215}
2216
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002217void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002218 // Explicit clinit checks triggered by static invokes must have been pruned by
2219 // art::PrepareForRegisterAllocation.
2220 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002221
Mark Mendell09ed1a32015-03-25 08:30:06 -04002222 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2223 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002224 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002225
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002226 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002227 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002228 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002229}
2230
2231void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002232 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2233 if (intrinsic.TryDispatch(invoke)) {
2234 return;
2235 }
2236
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002237 HandleInvoke(invoke);
2238}
2239
2240void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002241 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002242 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002243}
2244
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002245void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002246 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2247 return;
2248 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002250 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002251 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002252}
2253
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 // This call to HandleInvoke allocates a temporary (core) register
2256 // which is also used to transfer the hidden argument from FP to
2257 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 HandleInvoke(invoke);
2259 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002260 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261}
2262
2263void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2264 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 LocationSummary* locations = invoke->GetLocations();
2266 Register temp = locations->GetTemp(0).AsRegister<Register>();
2267 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002268 Location receiver = locations->InAt(0);
2269 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2270
Roland Levillain0d5a2812015-11-13 10:07:31 +00002271 // Set the hidden argument. This is safe to do this here, as XMM7
2272 // won't be modified thereafter, before the `call` instruction.
2273 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002275 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 if (receiver.IsStackSlot()) {
2278 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002279 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 __ movl(temp, Address(temp, class_offset));
2281 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002282 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002283 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284 }
Roland Levillain4d027112015-07-01 15:41:14 +01002285 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002286 // Instead of simply (possibly) unpoisoning `temp` here, we should
2287 // emit a read barrier for the previous class reference load.
2288 // However this is not required in practice, as this is an
2289 // intermediate/temporary reference and because the current
2290 // concurrent copying collector keeps the from-space memory
2291 // intact/accessible until the end of the marking phase (the
2292 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002293 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002294 // temp = temp->GetAddressOfIMT()
2295 __ movl(temp,
2296 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002298 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002299 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002300 __ movl(temp, Address(temp, method_offset));
2301 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002302 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002303 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002304
2305 DCHECK(!codegen_->IsLeafMethod());
2306 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2307}
2308
Orion Hodsonac141392017-01-13 11:53:47 +00002309void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2310 HandleInvoke(invoke);
2311}
2312
2313void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2314 codegen_->GenerateInvokePolymorphicCall(invoke);
2315}
2316
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002317void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2318 HandleInvoke(invoke);
2319}
2320
2321void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2322 codegen_->GenerateInvokeCustomCall(invoke);
2323}
2324
Roland Levillain88cb1752014-10-20 16:36:47 +01002325void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002327 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002328 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002329 case DataType::Type::kInt32:
2330 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002331 locations->SetInAt(0, Location::RequiresRegister());
2332 locations->SetOut(Location::SameAsFirstInput());
2333 break;
2334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002336 locations->SetInAt(0, Location::RequiresFpuRegister());
2337 locations->SetOut(Location::SameAsFirstInput());
2338 locations->AddTemp(Location::RequiresRegister());
2339 locations->AddTemp(Location::RequiresFpuRegister());
2340 break;
2341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002343 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002344 locations->SetOut(Location::SameAsFirstInput());
2345 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002346 break;
2347
2348 default:
2349 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2350 }
2351}
2352
2353void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2354 LocationSummary* locations = neg->GetLocations();
2355 Location out = locations->Out();
2356 Location in = locations->InAt(0);
2357 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002359 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002362 break;
2363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002365 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002366 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002367 __ negl(out.AsRegisterPairLow<Register>());
2368 // Negation is similar to subtraction from zero. The least
2369 // significant byte triggers a borrow when it is different from
2370 // zero; to take it into account, add 1 to the most significant
2371 // byte if the carry flag (CF) is set to 1 after the first NEGL
2372 // operation.
2373 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2374 __ negl(out.AsRegisterPairHigh<Register>());
2375 break;
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002378 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002379 Register constant = locations->GetTemp(0).AsRegister<Register>();
2380 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002381 // Implement float negation with an exclusive or with value
2382 // 0x80000000 (mask for bit 31, representing the sign of a
2383 // single-precision floating-point number).
2384 __ movl(constant, Immediate(INT32_C(0x80000000)));
2385 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002386 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002387 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002388 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002389
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002390 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002391 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002392 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002393 // Implement double negation with an exclusive or with value
2394 // 0x8000000000000000 (mask for bit 63, representing the sign of
2395 // a double-precision floating-point number).
2396 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002398 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002399 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002400
2401 default:
2402 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2403 }
2404}
2405
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002406void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2407 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002408 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002410 locations->SetInAt(0, Location::RequiresFpuRegister());
2411 locations->SetInAt(1, Location::RequiresRegister());
2412 locations->SetOut(Location::SameAsFirstInput());
2413 locations->AddTemp(Location::RequiresFpuRegister());
2414}
2415
2416void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2417 LocationSummary* locations = neg->GetLocations();
2418 Location out = locations->Out();
2419 DCHECK(locations->InAt(0).Equals(out));
2420
2421 Register constant_area = locations->InAt(1).AsRegister<Register>();
2422 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002424 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2425 neg->GetBaseMethodAddress(),
2426 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002427 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2428 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002429 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2430 neg->GetBaseMethodAddress(),
2431 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002432 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2433 }
2434}
2435
Roland Levillaindff1f282014-11-05 14:15:05 +00002436void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 DataType::Type result_type = conversion->GetResultType();
2438 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002439 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2440 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002441
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002442 // The float-to-long and double-to-long type conversions rely on a
2443 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002444 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002445 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2446 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002447 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002448 : LocationSummary::kNoCall;
2449 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002450 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002451
Roland Levillaindff1f282014-11-05 14:15:05 +00002452 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002453 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002454 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002455 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002456 case DataType::Type::kUint8:
2457 case DataType::Type::kInt8:
2458 case DataType::Type::kUint16:
2459 case DataType::Type::kInt16:
2460 case DataType::Type::kInt32:
2461 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2462 // Make the output overlap to please the register allocator. This greatly simplifies
2463 // the validation of the linear scan implementation
2464 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2465 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002467 HInstruction* input = conversion->InputAt(0);
2468 Location input_location = input->IsConstant()
2469 ? Location::ConstantLocation(input->AsConstant())
2470 : Location::RegisterPairLocation(EAX, EDX);
2471 locations->SetInAt(0, input_location);
2472 // Make the output overlap to please the register allocator. This greatly simplifies
2473 // the validation of the linear scan implementation
2474 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2475 break;
2476 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002477
2478 default:
2479 LOG(FATAL) << "Unexpected type conversion from " << input_type
2480 << " to " << result_type;
2481 }
2482 break;
2483
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002484 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002486 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2487 locations->SetInAt(0, Location::Any());
2488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002489 break;
2490
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002492 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002494 locations->SetInAt(0, Location::Any());
2495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2496 break;
2497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002499 locations->SetInAt(0, Location::RequiresFpuRegister());
2500 locations->SetOut(Location::RequiresRegister());
2501 locations->AddTemp(Location::RequiresFpuRegister());
2502 break;
2503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002505 locations->SetInAt(0, Location::RequiresFpuRegister());
2506 locations->SetOut(Location::RequiresRegister());
2507 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002508 break;
2509
2510 default:
2511 LOG(FATAL) << "Unexpected type conversion from " << input_type
2512 << " to " << result_type;
2513 }
2514 break;
2515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002519 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002521 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 case DataType::Type::kInt16:
2523 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002524 locations->SetInAt(0, Location::RegisterLocation(EAX));
2525 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2526 break;
2527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 case DataType::Type::kFloat32:
2529 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002530 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002531 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2532 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2533
Vladimir Marko949c91f2015-01-27 10:48:44 +00002534 // The runtime helper puts the result in EAX, EDX.
2535 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002536 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002537 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002538
2539 default:
2540 LOG(FATAL) << "Unexpected type conversion from " << input_type
2541 << " to " << result_type;
2542 }
2543 break;
2544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002545 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002546 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002548 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002550 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kInt16:
2552 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002553 locations->SetInAt(0, Location::RequiresRegister());
2554 locations->SetOut(Location::RequiresFpuRegister());
2555 break;
2556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002558 locations->SetInAt(0, Location::Any());
2559 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560 break;
2561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002563 locations->SetInAt(0, Location::RequiresFpuRegister());
2564 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002565 break;
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type conversion from " << input_type
2569 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002570 }
Roland Levillaincff13742014-11-17 14:32:17 +00002571 break;
2572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002573 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002574 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002576 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002578 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 case DataType::Type::kInt16:
2580 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002581 locations->SetInAt(0, Location::RequiresRegister());
2582 locations->SetOut(Location::RequiresFpuRegister());
2583 break;
2584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002585 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002586 locations->SetInAt(0, Location::Any());
2587 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002588 break;
2589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002591 locations->SetInAt(0, Location::RequiresFpuRegister());
2592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected type conversion from " << input_type
2597 << " to " << result_type;
2598 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002599 break;
2600
2601 default:
2602 LOG(FATAL) << "Unexpected type conversion from " << input_type
2603 << " to " << result_type;
2604 }
2605}
2606
2607void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2608 LocationSummary* locations = conversion->GetLocations();
2609 Location out = locations->Out();
2610 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 DataType::Type result_type = conversion->GetResultType();
2612 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002613 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2614 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002615 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002616 case DataType::Type::kUint8:
2617 switch (input_type) {
2618 case DataType::Type::kInt8:
2619 case DataType::Type::kUint16:
2620 case DataType::Type::kInt16:
2621 case DataType::Type::kInt32:
2622 if (in.IsRegister()) {
2623 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2624 } else {
2625 DCHECK(in.GetConstant()->IsIntConstant());
2626 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2627 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2628 }
2629 break;
2630 case DataType::Type::kInt64:
2631 if (in.IsRegisterPair()) {
2632 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2633 } else {
2634 DCHECK(in.GetConstant()->IsLongConstant());
2635 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2636 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2637 }
2638 break;
2639
2640 default:
2641 LOG(FATAL) << "Unexpected type conversion from " << input_type
2642 << " to " << result_type;
2643 }
2644 break;
2645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002647 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002648 case DataType::Type::kUint8:
2649 case DataType::Type::kUint16:
2650 case DataType::Type::kInt16:
2651 case DataType::Type::kInt32:
2652 if (in.IsRegister()) {
2653 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2654 } else {
2655 DCHECK(in.GetConstant()->IsIntConstant());
2656 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2657 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2658 }
2659 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002661 if (in.IsRegisterPair()) {
2662 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2663 } else {
2664 DCHECK(in.GetConstant()->IsLongConstant());
2665 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2666 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2667 }
2668 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002669
2670 default:
2671 LOG(FATAL) << "Unexpected type conversion from " << input_type
2672 << " to " << result_type;
2673 }
2674 break;
2675
2676 case DataType::Type::kUint16:
2677 switch (input_type) {
2678 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 case DataType::Type::kInt16:
2680 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002681 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002682 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2683 } else if (in.IsStackSlot()) {
2684 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002685 } else {
2686 DCHECK(in.GetConstant()->IsIntConstant());
2687 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002688 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2689 }
2690 break;
2691 case DataType::Type::kInt64:
2692 if (in.IsRegisterPair()) {
2693 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2694 } else if (in.IsDoubleStackSlot()) {
2695 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2696 } else {
2697 DCHECK(in.GetConstant()->IsLongConstant());
2698 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2699 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002700 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002701 break;
2702
2703 default:
2704 LOG(FATAL) << "Unexpected type conversion from " << input_type
2705 << " to " << result_type;
2706 }
2707 break;
2708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002710 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002712 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002713 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002714 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002715 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002717 } else {
2718 DCHECK(in.GetConstant()->IsIntConstant());
2719 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002721 }
2722 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002723 case DataType::Type::kInt64:
2724 if (in.IsRegisterPair()) {
2725 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2726 } else if (in.IsDoubleStackSlot()) {
2727 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2728 } else {
2729 DCHECK(in.GetConstant()->IsLongConstant());
2730 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2731 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2732 }
2733 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002734
2735 default:
2736 LOG(FATAL) << "Unexpected type conversion from " << input_type
2737 << " to " << result_type;
2738 }
2739 break;
2740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002742 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002744 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002745 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002746 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002748 } else {
2749 DCHECK(in.IsConstant());
2750 DCHECK(in.GetConstant()->IsLongConstant());
2751 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002752 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002753 }
2754 break;
2755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002757 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2758 Register output = out.AsRegister<Register>();
2759 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002760 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002761
2762 __ movl(output, Immediate(kPrimIntMax));
2763 // temp = int-to-float(output)
2764 __ cvtsi2ss(temp, output);
2765 // if input >= temp goto done
2766 __ comiss(input, temp);
2767 __ j(kAboveEqual, &done);
2768 // if input == NaN goto nan
2769 __ j(kUnordered, &nan);
2770 // output = float-to-int-truncate(input)
2771 __ cvttss2si(output, input);
2772 __ jmp(&done);
2773 __ Bind(&nan);
2774 // output = 0
2775 __ xorl(output, output);
2776 __ Bind(&done);
2777 break;
2778 }
2779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002781 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2782 Register output = out.AsRegister<Register>();
2783 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002784 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002785
2786 __ movl(output, Immediate(kPrimIntMax));
2787 // temp = int-to-double(output)
2788 __ cvtsi2sd(temp, output);
2789 // if input >= temp goto done
2790 __ comisd(input, temp);
2791 __ j(kAboveEqual, &done);
2792 // if input == NaN goto nan
2793 __ j(kUnordered, &nan);
2794 // output = double-to-int-truncate(input)
2795 __ cvttsd2si(output, input);
2796 __ jmp(&done);
2797 __ Bind(&nan);
2798 // output = 0
2799 __ xorl(output, output);
2800 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002801 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002802 }
Roland Levillain946e1432014-11-11 17:35:19 +00002803
2804 default:
2805 LOG(FATAL) << "Unexpected type conversion from " << input_type
2806 << " to " << result_type;
2807 }
2808 break;
2809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002811 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002813 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002815 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kInt16:
2817 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002818 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2819 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 __ cdq();
2822 break;
2823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002825 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002826 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002827 break;
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002830 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002831 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002832 break;
2833
2834 default:
2835 LOG(FATAL) << "Unexpected type conversion from " << input_type
2836 << " to " << result_type;
2837 }
2838 break;
2839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002841 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002843 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002845 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 case DataType::Type::kInt16:
2847 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002849 break;
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002852 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002853
Roland Levillain232ade02015-04-20 15:14:36 +01002854 // Create stack space for the call to
2855 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2856 // TODO: enhance register allocator to ask for stack temporaries.
2857 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002859 __ subl(ESP, Immediate(adjustment));
2860 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002861
Roland Levillain232ade02015-04-20 15:14:36 +01002862 // Load the value to the FP stack, using temporaries if needed.
2863 PushOntoFPStack(in, 0, adjustment, false, true);
2864
2865 if (out.IsStackSlot()) {
2866 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2867 } else {
2868 __ fstps(Address(ESP, 0));
2869 Location stack_temp = Location::StackSlot(0);
2870 codegen_->Move32(out, stack_temp);
2871 }
2872
2873 // Remove the temporary stack space we allocated.
2874 if (adjustment != 0) {
2875 __ addl(ESP, Immediate(adjustment));
2876 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002877 break;
2878 }
2879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002881 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002882 break;
2883
2884 default:
2885 LOG(FATAL) << "Unexpected type conversion from " << input_type
2886 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002887 }
Roland Levillaincff13742014-11-17 14:32:17 +00002888 break;
2889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002891 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002893 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002895 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kInt16:
2897 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002898 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002899 break;
2900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002902 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002903
Roland Levillain232ade02015-04-20 15:14:36 +01002904 // Create stack space for the call to
2905 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2906 // TODO: enhance register allocator to ask for stack temporaries.
2907 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002909 __ subl(ESP, Immediate(adjustment));
2910 }
2911
2912 // Load the value to the FP stack, using temporaries if needed.
2913 PushOntoFPStack(in, 0, adjustment, false, true);
2914
2915 if (out.IsDoubleStackSlot()) {
2916 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2917 } else {
2918 __ fstpl(Address(ESP, 0));
2919 Location stack_temp = Location::DoubleStackSlot(0);
2920 codegen_->Move64(out, stack_temp);
2921 }
2922
2923 // Remove the temporary stack space we allocated.
2924 if (adjustment != 0) {
2925 __ addl(ESP, Immediate(adjustment));
2926 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002927 break;
2928 }
2929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002931 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002932 break;
2933
2934 default:
2935 LOG(FATAL) << "Unexpected type conversion from " << input_type
2936 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002937 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002938 break;
2939
2940 default:
2941 LOG(FATAL) << "Unexpected type conversion from " << input_type
2942 << " to " << result_type;
2943 }
2944}
2945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002946void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002947 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002948 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002951 locations->SetInAt(0, Location::RequiresRegister());
2952 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2954 break;
2955 }
2956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002958 locations->SetInAt(0, Location::RequiresRegister());
2959 locations->SetInAt(1, Location::Any());
2960 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002961 break;
2962 }
2963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 case DataType::Type::kFloat32:
2965 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002966 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002967 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2968 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002969 } else if (add->InputAt(1)->IsConstant()) {
2970 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002971 } else {
2972 locations->SetInAt(1, Location::Any());
2973 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002975 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002976 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002977
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002979 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2980 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002981 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002982}
2983
2984void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2985 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 Location first = locations->InAt(0);
2987 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002988 Location out = locations->Out();
2989
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002992 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002993 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2994 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002995 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2996 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002997 } else {
2998 __ leal(out.AsRegister<Register>(), Address(
2999 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3000 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003001 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003002 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3003 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3004 __ addl(out.AsRegister<Register>(), Immediate(value));
3005 } else {
3006 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3007 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003008 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003009 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003011 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003012 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003013 }
3014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003016 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003017 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3018 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003020 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3021 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003022 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003023 } else {
3024 DCHECK(second.IsConstant()) << second;
3025 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3026 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3027 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003028 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003029 break;
3030 }
3031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003033 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003035 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3036 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003037 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003038 __ addss(first.AsFpuRegister<XmmRegister>(),
3039 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003040 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3041 const_area->GetBaseMethodAddress(),
3042 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003043 } else {
3044 DCHECK(second.IsStackSlot());
3045 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003046 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003047 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003048 }
3049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003050 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3054 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003055 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003056 __ addsd(first.AsFpuRegister<XmmRegister>(),
3057 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003058 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3059 const_area->GetBaseMethodAddress(),
3060 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003061 } else {
3062 DCHECK(second.IsDoubleStackSlot());
3063 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003064 }
3065 break;
3066 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003067
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003068 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003069 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003070 }
3071}
3072
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003073void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003074 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003075 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003076 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt32:
3078 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003079 locations->SetInAt(0, Location::RequiresRegister());
3080 locations->SetInAt(1, Location::Any());
3081 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003082 break;
3083 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003084 case DataType::Type::kFloat32:
3085 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003086 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003087 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3088 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003089 } else if (sub->InputAt(1)->IsConstant()) {
3090 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003091 } else {
3092 locations->SetInAt(1, Location::Any());
3093 }
Calin Juravle11351682014-10-23 15:38:15 +01003094 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003095 break;
Calin Juravle11351682014-10-23 15:38:15 +01003096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003097
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003098 default:
Calin Juravle11351682014-10-23 15:38:15 +01003099 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003100 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003101}
3102
3103void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3104 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003105 Location first = locations->InAt(0);
3106 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003107 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003108 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003109 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003110 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003112 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003113 __ subl(first.AsRegister<Register>(),
3114 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003115 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003116 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003117 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003118 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119 }
3120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003122 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003123 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3124 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003125 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003126 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003127 __ sbbl(first.AsRegisterPairHigh<Register>(),
3128 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003129 } else {
3130 DCHECK(second.IsConstant()) << second;
3131 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3132 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3133 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003134 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003135 break;
3136 }
3137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003138 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003139 if (second.IsFpuRegister()) {
3140 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3141 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3142 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003143 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003144 __ subss(first.AsFpuRegister<XmmRegister>(),
3145 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003146 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3147 const_area->GetBaseMethodAddress(),
3148 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003149 } else {
3150 DCHECK(second.IsStackSlot());
3151 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3152 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003153 break;
Calin Juravle11351682014-10-23 15:38:15 +01003154 }
3155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003156 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003157 if (second.IsFpuRegister()) {
3158 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3159 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3160 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003161 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003162 __ subsd(first.AsFpuRegister<XmmRegister>(),
3163 codegen_->LiteralDoubleAddress(
3164 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003165 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003166 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3167 } else {
3168 DCHECK(second.IsDoubleStackSlot());
3169 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3170 }
Calin Juravle11351682014-10-23 15:38:15 +01003171 break;
3172 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003173
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003174 default:
Calin Juravle11351682014-10-23 15:38:15 +01003175 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003176 }
3177}
3178
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179void LocationsBuilderX86::VisitMul(HMul* mul) {
3180 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003181 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 locations->SetInAt(0, Location::RequiresRegister());
3185 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003186 if (mul->InputAt(1)->IsIntConstant()) {
3187 // Can use 3 operand multiply.
3188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3189 } else {
3190 locations->SetOut(Location::SameAsFirstInput());
3191 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003193 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 locations->SetInAt(1, Location::Any());
3196 locations->SetOut(Location::SameAsFirstInput());
3197 // Needed for imul on 32bits with 64bits output.
3198 locations->AddTemp(Location::RegisterLocation(EAX));
3199 locations->AddTemp(Location::RegisterLocation(EDX));
3200 break;
3201 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003202 case DataType::Type::kFloat32:
3203 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003204 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003205 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3206 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003207 } else if (mul->InputAt(1)->IsConstant()) {
3208 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003209 } else {
3210 locations->SetInAt(1, Location::Any());
3211 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003212 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003213 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003214 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215
3216 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003217 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218 }
3219}
3220
3221void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3222 LocationSummary* locations = mul->GetLocations();
3223 Location first = locations->InAt(0);
3224 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003225 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226
3227 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003229 // The constant may have ended up in a register, so test explicitly to avoid
3230 // problems where the output may not be the same as the first operand.
3231 if (mul->InputAt(1)->IsIntConstant()) {
3232 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3233 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3234 } else if (second.IsRegister()) {
3235 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237 } else {
3238 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003239 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003240 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003241 }
3242 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003243
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003244 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003245 Register in1_hi = first.AsRegisterPairHigh<Register>();
3246 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 Register eax = locations->GetTemp(0).AsRegister<Register>();
3248 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249
3250 DCHECK_EQ(EAX, eax);
3251 DCHECK_EQ(EDX, edx);
3252
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003253 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003254 // output: in1
3255 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3256 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3257 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003258 if (second.IsConstant()) {
3259 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003260
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003261 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3262 int32_t low_value = Low32Bits(value);
3263 int32_t high_value = High32Bits(value);
3264 Immediate low(low_value);
3265 Immediate high(high_value);
3266
3267 __ movl(eax, high);
3268 // eax <- in1.lo * in2.hi
3269 __ imull(eax, in1_lo);
3270 // in1.hi <- in1.hi * in2.lo
3271 __ imull(in1_hi, low);
3272 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3273 __ addl(in1_hi, eax);
3274 // move in2_lo to eax to prepare for double precision
3275 __ movl(eax, low);
3276 // edx:eax <- in1.lo * in2.lo
3277 __ mull(in1_lo);
3278 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3279 __ addl(in1_hi, edx);
3280 // in1.lo <- (in1.lo * in2.lo)[31:0];
3281 __ movl(in1_lo, eax);
3282 } else if (second.IsRegisterPair()) {
3283 Register in2_hi = second.AsRegisterPairHigh<Register>();
3284 Register in2_lo = second.AsRegisterPairLow<Register>();
3285
3286 __ movl(eax, in2_hi);
3287 // eax <- in1.lo * in2.hi
3288 __ imull(eax, in1_lo);
3289 // in1.hi <- in1.hi * in2.lo
3290 __ imull(in1_hi, in2_lo);
3291 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3292 __ addl(in1_hi, eax);
3293 // move in1_lo to eax to prepare for double precision
3294 __ movl(eax, in1_lo);
3295 // edx:eax <- in1.lo * in2.lo
3296 __ mull(in2_lo);
3297 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3298 __ addl(in1_hi, edx);
3299 // in1.lo <- (in1.lo * in2.lo)[31:0];
3300 __ movl(in1_lo, eax);
3301 } else {
3302 DCHECK(second.IsDoubleStackSlot()) << second;
3303 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3304 Address in2_lo(ESP, second.GetStackIndex());
3305
3306 __ movl(eax, in2_hi);
3307 // eax <- in1.lo * in2.hi
3308 __ imull(eax, in1_lo);
3309 // in1.hi <- in1.hi * in2.lo
3310 __ imull(in1_hi, in2_lo);
3311 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3312 __ addl(in1_hi, eax);
3313 // move in1_lo to eax to prepare for double precision
3314 __ movl(eax, in1_lo);
3315 // edx:eax <- in1.lo * in2.lo
3316 __ mull(in2_lo);
3317 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3318 __ addl(in1_hi, edx);
3319 // in1.lo <- (in1.lo * in2.lo)[31:0];
3320 __ movl(in1_lo, eax);
3321 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003322
3323 break;
3324 }
3325
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003326 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003327 DCHECK(first.Equals(locations->Out()));
3328 if (second.IsFpuRegister()) {
3329 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3330 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3331 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003332 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003333 __ mulss(first.AsFpuRegister<XmmRegister>(),
3334 codegen_->LiteralFloatAddress(
3335 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003336 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003337 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3338 } else {
3339 DCHECK(second.IsStackSlot());
3340 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3341 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003342 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003343 }
3344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003345 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003346 DCHECK(first.Equals(locations->Out()));
3347 if (second.IsFpuRegister()) {
3348 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3349 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3350 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003351 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003352 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3353 codegen_->LiteralDoubleAddress(
3354 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003355 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003356 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3357 } else {
3358 DCHECK(second.IsDoubleStackSlot());
3359 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3360 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003361 break;
3362 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003363
3364 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003365 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003366 }
3367}
3368
Roland Levillain232ade02015-04-20 15:14:36 +01003369void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3370 uint32_t temp_offset,
3371 uint32_t stack_adjustment,
3372 bool is_fp,
3373 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003374 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003375 DCHECK(!is_wide);
3376 if (is_fp) {
3377 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3378 } else {
3379 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3380 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003381 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003382 DCHECK(is_wide);
3383 if (is_fp) {
3384 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3385 } else {
3386 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3387 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003388 } else {
3389 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003390 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 Location stack_temp = Location::StackSlot(temp_offset);
3392 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003393 if (is_fp) {
3394 __ flds(Address(ESP, temp_offset));
3395 } else {
3396 __ filds(Address(ESP, temp_offset));
3397 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003398 } else {
3399 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3400 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003401 if (is_fp) {
3402 __ fldl(Address(ESP, temp_offset));
3403 } else {
3404 __ fildl(Address(ESP, temp_offset));
3405 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003406 }
3407 }
3408}
3409
3410void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 DataType::Type type = rem->GetResultType();
3412 bool is_float = type == DataType::Type::kFloat32;
3413 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003414 LocationSummary* locations = rem->GetLocations();
3415 Location first = locations->InAt(0);
3416 Location second = locations->InAt(1);
3417 Location out = locations->Out();
3418
3419 // Create stack space for 2 elements.
3420 // TODO: enhance register allocator to ask for stack temporaries.
3421 __ subl(ESP, Immediate(2 * elem_size));
3422
3423 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003424 const bool is_wide = !is_float;
3425 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3426 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003427
3428 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003429 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003430 __ Bind(&retry);
3431 __ fprem();
3432
3433 // Move FP status to AX.
3434 __ fstsw();
3435
3436 // And see if the argument reduction is complete. This is signaled by the
3437 // C2 FPU flag bit set to 0.
3438 __ andl(EAX, Immediate(kC2ConditionMask));
3439 __ j(kNotEqual, &retry);
3440
3441 // We have settled on the final value. Retrieve it into an XMM register.
3442 // Store FP top of stack to real stack.
3443 if (is_float) {
3444 __ fsts(Address(ESP, 0));
3445 } else {
3446 __ fstl(Address(ESP, 0));
3447 }
3448
3449 // Pop the 2 items from the FP stack.
3450 __ fucompp();
3451
3452 // Load the value from the stack into an XMM register.
3453 DCHECK(out.IsFpuRegister()) << out;
3454 if (is_float) {
3455 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3456 } else {
3457 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3458 }
3459
3460 // And remove the temporary stack space we allocated.
3461 __ addl(ESP, Immediate(2 * elem_size));
3462}
3463
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464
3465void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3466 DCHECK(instruction->IsDiv() || instruction->IsRem());
3467
3468 LocationSummary* locations = instruction->GetLocations();
3469 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003470 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471
3472 Register out_register = locations->Out().AsRegister<Register>();
3473 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003474 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475
3476 DCHECK(imm == 1 || imm == -1);
3477
3478 if (instruction->IsRem()) {
3479 __ xorl(out_register, out_register);
3480 } else {
3481 __ movl(out_register, input_register);
3482 if (imm == -1) {
3483 __ negl(out_register);
3484 }
3485 }
3486}
3487
3488
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003489void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 LocationSummary* locations = instruction->GetLocations();
3491
3492 Register out_register = locations->Out().AsRegister<Register>();
3493 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003494 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003495 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3496 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 Register num = locations->GetTemp(0).AsRegister<Register>();
3499
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003500 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 __ testl(input_register, input_register);
3502 __ cmovl(kGreaterEqual, num, input_register);
3503 int shift = CTZ(imm);
3504 __ sarl(num, Immediate(shift));
3505
3506 if (imm < 0) {
3507 __ negl(num);
3508 }
3509
3510 __ movl(out_register, num);
3511}
3512
3513void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3514 DCHECK(instruction->IsDiv() || instruction->IsRem());
3515
3516 LocationSummary* locations = instruction->GetLocations();
3517 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3518
3519 Register eax = locations->InAt(0).AsRegister<Register>();
3520 Register out = locations->Out().AsRegister<Register>();
3521 Register num;
3522 Register edx;
3523
3524 if (instruction->IsDiv()) {
3525 edx = locations->GetTemp(0).AsRegister<Register>();
3526 num = locations->GetTemp(1).AsRegister<Register>();
3527 } else {
3528 edx = locations->Out().AsRegister<Register>();
3529 num = locations->GetTemp(0).AsRegister<Register>();
3530 }
3531
3532 DCHECK_EQ(EAX, eax);
3533 DCHECK_EQ(EDX, edx);
3534 if (instruction->IsDiv()) {
3535 DCHECK_EQ(EAX, out);
3536 } else {
3537 DCHECK_EQ(EDX, out);
3538 }
3539
3540 int64_t magic;
3541 int shift;
3542 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3543
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 // Save the numerator.
3545 __ movl(num, eax);
3546
3547 // EAX = magic
3548 __ movl(eax, Immediate(magic));
3549
3550 // EDX:EAX = magic * numerator
3551 __ imull(num);
3552
3553 if (imm > 0 && magic < 0) {
3554 // EDX += num
3555 __ addl(edx, num);
3556 } else if (imm < 0 && magic > 0) {
3557 __ subl(edx, num);
3558 }
3559
3560 // Shift if needed.
3561 if (shift != 0) {
3562 __ sarl(edx, Immediate(shift));
3563 }
3564
3565 // EDX += 1 if EDX < 0
3566 __ movl(eax, edx);
3567 __ shrl(edx, Immediate(31));
3568 __ addl(edx, eax);
3569
3570 if (instruction->IsRem()) {
3571 __ movl(eax, num);
3572 __ imull(edx, Immediate(imm));
3573 __ subl(eax, edx);
3574 __ movl(edx, eax);
3575 } else {
3576 __ movl(eax, edx);
3577 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578}
3579
Calin Juravlebacfec32014-11-14 15:54:36 +00003580void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3581 DCHECK(instruction->IsDiv() || instruction->IsRem());
3582
3583 LocationSummary* locations = instruction->GetLocations();
3584 Location out = locations->Out();
3585 Location first = locations->InAt(0);
3586 Location second = locations->InAt(1);
3587 bool is_div = instruction->IsDiv();
3588
3589 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003590 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 DCHECK_EQ(EAX, first.AsRegister<Register>());
3592 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003593
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003594 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003595 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003596
3597 if (imm == 0) {
3598 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3599 } else if (imm == 1 || imm == -1) {
3600 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003601 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003602 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003603 } else {
3604 DCHECK(imm <= -2 || imm >= 2);
3605 GenerateDivRemWithAnyConstant(instruction);
3606 }
3607 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003608 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003609 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003610 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003611
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003612 Register second_reg = second.AsRegister<Register>();
3613 // 0x80000000/-1 triggers an arithmetic exception!
3614 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3615 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003616
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003617 __ cmpl(second_reg, Immediate(-1));
3618 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003619
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 // edx:eax <- sign-extended of eax
3621 __ cdq();
3622 // eax = quotient, edx = remainder
3623 __ idivl(second_reg);
3624 __ Bind(slow_path->GetExitLabel());
3625 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 break;
3627 }
3628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 InvokeRuntimeCallingConvention calling_convention;
3631 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3632 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3633 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3634 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3635 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3636 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3637
3638 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003639 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003640 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003641 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003642 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003643 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003644 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003645 break;
3646 }
3647
3648 default:
3649 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3650 }
3651}
3652
Calin Juravle7c4954d2014-10-28 16:57:40 +00003653void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003654 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003655 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003656 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003657 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003658
Calin Juravle7c4954d2014-10-28 16:57:40 +00003659 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003661 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003662 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003663 locations->SetOut(Location::SameAsFirstInput());
3664 // Intel uses edx:eax as the dividend.
3665 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003666 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3667 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3668 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003669 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003670 locations->AddTemp(Location::RequiresRegister());
3671 }
Calin Juravled0d48522014-11-04 16:40:20 +00003672 break;
3673 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003675 InvokeRuntimeCallingConvention calling_convention;
3676 locations->SetInAt(0, Location::RegisterPairLocation(
3677 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3678 locations->SetInAt(1, Location::RegisterPairLocation(
3679 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3680 // Runtime helper puts the result in EAX, EDX.
3681 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003682 break;
3683 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003684 case DataType::Type::kFloat32:
3685 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003686 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003687 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3688 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003689 } else if (div->InputAt(1)->IsConstant()) {
3690 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003691 } else {
3692 locations->SetInAt(1, Location::Any());
3693 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 locations->SetOut(Location::SameAsFirstInput());
3695 break;
3696 }
3697
3698 default:
3699 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3700 }
3701}
3702
3703void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3704 LocationSummary* locations = div->GetLocations();
3705 Location first = locations->InAt(0);
3706 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003707
3708 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003709 case DataType::Type::kInt32:
3710 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003711 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003712 break;
3713 }
3714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003715 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003716 if (second.IsFpuRegister()) {
3717 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3718 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3719 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003720 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003721 __ divss(first.AsFpuRegister<XmmRegister>(),
3722 codegen_->LiteralFloatAddress(
3723 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003724 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003725 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3726 } else {
3727 DCHECK(second.IsStackSlot());
3728 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3729 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003730 break;
3731 }
3732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003733 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003734 if (second.IsFpuRegister()) {
3735 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3736 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3737 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003738 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003739 __ divsd(first.AsFpuRegister<XmmRegister>(),
3740 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003741 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3742 const_area->GetBaseMethodAddress(),
3743 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003744 } else {
3745 DCHECK(second.IsDoubleStackSlot());
3746 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3747 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003748 break;
3749 }
3750
3751 default:
3752 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3753 }
3754}
3755
Calin Juravlebacfec32014-11-14 15:54:36 +00003756void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003759 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003760 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003761 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003762 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003763
Calin Juravled2ec87d2014-12-08 14:24:46 +00003764 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003766 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003767 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003768 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003769 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3770 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3771 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003772 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003773 locations->AddTemp(Location::RequiresRegister());
3774 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 break;
3776 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 InvokeRuntimeCallingConvention calling_convention;
3779 locations->SetInAt(0, Location::RegisterPairLocation(
3780 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3781 locations->SetInAt(1, Location::RegisterPairLocation(
3782 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3783 // Runtime helper puts the result in EAX, EDX.
3784 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3785 break;
3786 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kFloat64:
3788 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003789 locations->SetInAt(0, Location::Any());
3790 locations->SetInAt(1, Location::Any());
3791 locations->SetOut(Location::RequiresFpuRegister());
3792 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003793 break;
3794 }
3795
3796 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003797 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 }
3799}
3800
3801void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003803 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kInt32:
3805 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003806 GenerateDivRemIntegral(rem);
3807 break;
3808 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kFloat32:
3810 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003811 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003812 break;
3813 }
3814 default:
3815 LOG(FATAL) << "Unexpected rem type " << type;
3816 }
3817}
3818
Aart Bik1f8d51b2018-02-15 10:42:37 -08003819static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3820 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3821 switch (minmax->GetResultType()) {
3822 case DataType::Type::kInt32:
3823 locations->SetInAt(0, Location::RequiresRegister());
3824 locations->SetInAt(1, Location::RequiresRegister());
3825 locations->SetOut(Location::SameAsFirstInput());
3826 break;
3827 case DataType::Type::kInt64:
3828 locations->SetInAt(0, Location::RequiresRegister());
3829 locations->SetInAt(1, Location::RequiresRegister());
3830 locations->SetOut(Location::SameAsFirstInput());
3831 // Register to use to perform a long subtract to set cc.
3832 locations->AddTemp(Location::RequiresRegister());
3833 break;
3834 case DataType::Type::kFloat32:
3835 locations->SetInAt(0, Location::RequiresFpuRegister());
3836 locations->SetInAt(1, Location::RequiresFpuRegister());
3837 locations->SetOut(Location::SameAsFirstInput());
3838 locations->AddTemp(Location::RequiresRegister());
3839 break;
3840 case DataType::Type::kFloat64:
3841 locations->SetInAt(0, Location::RequiresFpuRegister());
3842 locations->SetInAt(1, Location::RequiresFpuRegister());
3843 locations->SetOut(Location::SameAsFirstInput());
3844 break;
3845 default:
3846 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3847 }
3848}
3849
Aart Bik351df3e2018-03-07 11:54:57 -08003850void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3851 bool is_min,
3852 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003853 Location op1_loc = locations->InAt(0);
3854 Location op2_loc = locations->InAt(1);
3855
3856 // Shortcut for same input locations.
3857 if (op1_loc.Equals(op2_loc)) {
3858 // Can return immediately, as op1_loc == out_loc.
3859 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3860 // a copy here.
3861 DCHECK(locations->Out().Equals(op1_loc));
3862 return;
3863 }
3864
3865 if (type == DataType::Type::kInt64) {
3866 // Need to perform a subtract to get the sign right.
3867 // op1 is already in the same location as the output.
3868 Location output = locations->Out();
3869 Register output_lo = output.AsRegisterPairLow<Register>();
3870 Register output_hi = output.AsRegisterPairHigh<Register>();
3871
3872 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3873 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3874
3875 // The comparison is performed by subtracting the second operand from
3876 // the first operand and then setting the status flags in the same
3877 // manner as the SUB instruction."
3878 __ cmpl(output_lo, op2_lo);
3879
3880 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3881 Register temp = locations->GetTemp(0).AsRegister<Register>();
3882 __ movl(temp, output_hi);
3883 __ sbbl(temp, op2_hi);
3884
3885 // Now the condition code is correct.
3886 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3887 __ cmovl(cond, output_lo, op2_lo);
3888 __ cmovl(cond, output_hi, op2_hi);
3889 } else {
3890 DCHECK_EQ(type, DataType::Type::kInt32);
3891 Register out = locations->Out().AsRegister<Register>();
3892 Register op2 = op2_loc.AsRegister<Register>();
3893
3894 // (out := op1)
3895 // out <=? op2
3896 // if out is min jmp done
3897 // out := op2
3898 // done:
3899
3900 __ cmpl(out, op2);
3901 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3902 __ cmovl(cond, out, op2);
3903 }
3904}
3905
3906void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3907 bool is_min,
3908 DataType::Type type) {
3909 Location op1_loc = locations->InAt(0);
3910 Location op2_loc = locations->InAt(1);
3911 Location out_loc = locations->Out();
3912 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3913
3914 // Shortcut for same input locations.
3915 if (op1_loc.Equals(op2_loc)) {
3916 DCHECK(out_loc.Equals(op1_loc));
3917 return;
3918 }
3919
3920 // (out := op1)
3921 // out <=? op2
3922 // if Nan jmp Nan_label
3923 // if out is min jmp done
3924 // if op2 is min jmp op2_label
3925 // handle -0/+0
3926 // jmp done
3927 // Nan_label:
3928 // out := NaN
3929 // op2_label:
3930 // out := op2
3931 // done:
3932 //
3933 // This removes one jmp, but needs to copy one input (op1) to out.
3934 //
3935 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3936
3937 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3938
3939 NearLabel nan, done, op2_label;
3940 if (type == DataType::Type::kFloat64) {
3941 __ ucomisd(out, op2);
3942 } else {
3943 DCHECK_EQ(type, DataType::Type::kFloat32);
3944 __ ucomiss(out, op2);
3945 }
3946
3947 __ j(Condition::kParityEven, &nan);
3948
3949 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3950 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3951
3952 // Handle 0.0/-0.0.
3953 if (is_min) {
3954 if (type == DataType::Type::kFloat64) {
3955 __ orpd(out, op2);
3956 } else {
3957 __ orps(out, op2);
3958 }
3959 } else {
3960 if (type == DataType::Type::kFloat64) {
3961 __ andpd(out, op2);
3962 } else {
3963 __ andps(out, op2);
3964 }
3965 }
3966 __ jmp(&done);
3967
3968 // NaN handling.
3969 __ Bind(&nan);
3970 if (type == DataType::Type::kFloat64) {
3971 // TODO: Use a constant from the constant table (requires extra input).
3972 __ LoadLongConstant(out, kDoubleNaN);
3973 } else {
3974 Register constant = locations->GetTemp(0).AsRegister<Register>();
3975 __ movl(constant, Immediate(kFloatNaN));
3976 __ movd(out, constant);
3977 }
3978 __ jmp(&done);
3979
3980 // out := op2;
3981 __ Bind(&op2_label);
3982 if (type == DataType::Type::kFloat64) {
3983 __ movsd(out, op2);
3984 } else {
3985 __ movss(out, op2);
3986 }
3987
3988 // Done.
3989 __ Bind(&done);
3990}
3991
Aart Bik351df3e2018-03-07 11:54:57 -08003992void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3993 DataType::Type type = minmax->GetResultType();
3994 switch (type) {
3995 case DataType::Type::kInt32:
3996 case DataType::Type::kInt64:
3997 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
3998 break;
3999 case DataType::Type::kFloat32:
4000 case DataType::Type::kFloat64:
4001 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4002 break;
4003 default:
4004 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4005 }
4006}
4007
Aart Bik1f8d51b2018-02-15 10:42:37 -08004008void LocationsBuilderX86::VisitMin(HMin* min) {
4009 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4010}
4011
4012void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004013 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004014}
4015
4016void LocationsBuilderX86::VisitMax(HMax* max) {
4017 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4018}
4019
4020void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004021 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004022}
4023
Aart Bik3dad3412018-02-28 12:01:46 -08004024void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4025 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4026 switch (abs->GetResultType()) {
4027 case DataType::Type::kInt32:
4028 locations->SetInAt(0, Location::RegisterLocation(EAX));
4029 locations->SetOut(Location::SameAsFirstInput());
4030 locations->AddTemp(Location::RegisterLocation(EDX));
4031 break;
4032 case DataType::Type::kInt64:
4033 locations->SetInAt(0, Location::RequiresRegister());
4034 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4035 locations->AddTemp(Location::RequiresRegister());
4036 break;
4037 case DataType::Type::kFloat32:
4038 locations->SetInAt(0, Location::RequiresFpuRegister());
4039 locations->SetOut(Location::SameAsFirstInput());
4040 locations->AddTemp(Location::RequiresFpuRegister());
4041 locations->AddTemp(Location::RequiresRegister());
4042 break;
4043 case DataType::Type::kFloat64:
4044 locations->SetInAt(0, Location::RequiresFpuRegister());
4045 locations->SetOut(Location::SameAsFirstInput());
4046 locations->AddTemp(Location::RequiresFpuRegister());
4047 break;
4048 default:
4049 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4050 }
4051}
4052
4053void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4054 LocationSummary* locations = abs->GetLocations();
4055 switch (abs->GetResultType()) {
4056 case DataType::Type::kInt32: {
4057 Register out = locations->Out().AsRegister<Register>();
4058 DCHECK_EQ(out, EAX);
4059 Register temp = locations->GetTemp(0).AsRegister<Register>();
4060 DCHECK_EQ(temp, EDX);
4061 // Sign extend EAX into EDX.
4062 __ cdq();
4063 // XOR EAX with sign.
4064 __ xorl(EAX, EDX);
4065 // Subtract out sign to correct.
4066 __ subl(EAX, EDX);
4067 // The result is in EAX.
4068 break;
4069 }
4070 case DataType::Type::kInt64: {
4071 Location input = locations->InAt(0);
4072 Register input_lo = input.AsRegisterPairLow<Register>();
4073 Register input_hi = input.AsRegisterPairHigh<Register>();
4074 Location output = locations->Out();
4075 Register output_lo = output.AsRegisterPairLow<Register>();
4076 Register output_hi = output.AsRegisterPairHigh<Register>();
4077 Register temp = locations->GetTemp(0).AsRegister<Register>();
4078 // Compute the sign into the temporary.
4079 __ movl(temp, input_hi);
4080 __ sarl(temp, Immediate(31));
4081 // Store the sign into the output.
4082 __ movl(output_lo, temp);
4083 __ movl(output_hi, temp);
4084 // XOR the input to the output.
4085 __ xorl(output_lo, input_lo);
4086 __ xorl(output_hi, input_hi);
4087 // Subtract the sign.
4088 __ subl(output_lo, temp);
4089 __ sbbl(output_hi, temp);
4090 break;
4091 }
4092 case DataType::Type::kFloat32: {
4093 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4094 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4095 Register constant = locations->GetTemp(1).AsRegister<Register>();
4096 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4097 __ movd(temp, constant);
4098 __ andps(out, temp);
4099 break;
4100 }
4101 case DataType::Type::kFloat64: {
4102 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4103 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4104 // TODO: Use a constant from the constant table (requires extra input).
4105 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4106 __ andpd(out, temp);
4107 break;
4108 }
4109 default:
4110 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4111 }
4112}
4113
Calin Juravled0d48522014-11-04 16:40:20 +00004114void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004115 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004116 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004117 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004118 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 case DataType::Type::kInt8:
4120 case DataType::Type::kUint16:
4121 case DataType::Type::kInt16:
4122 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004123 locations->SetInAt(0, Location::Any());
4124 break;
4125 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004126 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004127 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4128 if (!instruction->IsConstant()) {
4129 locations->AddTemp(Location::RequiresRegister());
4130 }
4131 break;
4132 }
4133 default:
4134 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4135 }
Calin Juravled0d48522014-11-04 16:40:20 +00004136}
4137
4138void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004139 SlowPathCode* slow_path =
4140 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004141 codegen_->AddSlowPath(slow_path);
4142
4143 LocationSummary* locations = instruction->GetLocations();
4144 Location value = locations->InAt(0);
4145
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004146 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004148 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 case DataType::Type::kInt8:
4150 case DataType::Type::kUint16:
4151 case DataType::Type::kInt16:
4152 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004153 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004155 __ j(kEqual, slow_path->GetEntryLabel());
4156 } else if (value.IsStackSlot()) {
4157 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4158 __ j(kEqual, slow_path->GetEntryLabel());
4159 } else {
4160 DCHECK(value.IsConstant()) << value;
4161 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004162 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004163 }
4164 }
4165 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004166 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004167 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004168 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004169 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004170 __ movl(temp, value.AsRegisterPairLow<Register>());
4171 __ orl(temp, value.AsRegisterPairHigh<Register>());
4172 __ j(kEqual, slow_path->GetEntryLabel());
4173 } else {
4174 DCHECK(value.IsConstant()) << value;
4175 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4176 __ jmp(slow_path->GetEntryLabel());
4177 }
4178 }
4179 break;
4180 }
4181 default:
4182 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004183 }
Calin Juravled0d48522014-11-04 16:40:20 +00004184}
4185
Calin Juravle9aec02f2014-11-18 23:06:35 +00004186void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4187 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4188
4189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004190 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004191
4192 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004193 case DataType::Type::kInt32:
4194 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004195 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004196 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004197 // The shift count needs to be in CL or a constant.
4198 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004199 locations->SetOut(Location::SameAsFirstInput());
4200 break;
4201 }
4202 default:
4203 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4204 }
4205}
4206
4207void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4208 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4209
4210 LocationSummary* locations = op->GetLocations();
4211 Location first = locations->InAt(0);
4212 Location second = locations->InAt(1);
4213 DCHECK(first.Equals(locations->Out()));
4214
4215 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004216 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004217 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004218 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004219 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004220 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004221 DCHECK_EQ(ECX, second_reg);
4222 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004223 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004224 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004225 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004226 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004227 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004228 }
4229 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004230 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004231 if (shift == 0) {
4232 return;
4233 }
4234 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004235 if (op->IsShl()) {
4236 __ shll(first_reg, imm);
4237 } else if (op->IsShr()) {
4238 __ sarl(first_reg, imm);
4239 } else {
4240 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004241 }
4242 }
4243 break;
4244 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004245 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004246 if (second.IsRegister()) {
4247 Register second_reg = second.AsRegister<Register>();
4248 DCHECK_EQ(ECX, second_reg);
4249 if (op->IsShl()) {
4250 GenerateShlLong(first, second_reg);
4251 } else if (op->IsShr()) {
4252 GenerateShrLong(first, second_reg);
4253 } else {
4254 GenerateUShrLong(first, second_reg);
4255 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004256 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004257 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004258 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004259 // Nothing to do if the shift is 0, as the input is already the output.
4260 if (shift != 0) {
4261 if (op->IsShl()) {
4262 GenerateShlLong(first, shift);
4263 } else if (op->IsShr()) {
4264 GenerateShrLong(first, shift);
4265 } else {
4266 GenerateUShrLong(first, shift);
4267 }
4268 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004269 }
4270 break;
4271 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004272 default:
4273 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4274 }
4275}
4276
Mark P Mendell73945692015-04-29 14:56:17 +00004277void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4278 Register low = loc.AsRegisterPairLow<Register>();
4279 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004280 if (shift == 1) {
4281 // This is just an addition.
4282 __ addl(low, low);
4283 __ adcl(high, high);
4284 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004285 // Shift by 32 is easy. High gets low, and low gets 0.
4286 codegen_->EmitParallelMoves(
4287 loc.ToLow(),
4288 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004289 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004290 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4291 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004292 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004293 } else if (shift > 32) {
4294 // Low part becomes 0. High part is low part << (shift-32).
4295 __ movl(high, low);
4296 __ shll(high, Immediate(shift - 32));
4297 __ xorl(low, low);
4298 } else {
4299 // Between 1 and 31.
4300 __ shld(high, low, Immediate(shift));
4301 __ shll(low, Immediate(shift));
4302 }
4303}
4304
Calin Juravle9aec02f2014-11-18 23:06:35 +00004305void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004306 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004307 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4308 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4309 __ testl(shifter, Immediate(32));
4310 __ j(kEqual, &done);
4311 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4312 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4313 __ Bind(&done);
4314}
4315
Mark P Mendell73945692015-04-29 14:56:17 +00004316void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4317 Register low = loc.AsRegisterPairLow<Register>();
4318 Register high = loc.AsRegisterPairHigh<Register>();
4319 if (shift == 32) {
4320 // Need to copy the sign.
4321 DCHECK_NE(low, high);
4322 __ movl(low, high);
4323 __ sarl(high, Immediate(31));
4324 } else if (shift > 32) {
4325 DCHECK_NE(low, high);
4326 // High part becomes sign. Low part is shifted by shift - 32.
4327 __ movl(low, high);
4328 __ sarl(high, Immediate(31));
4329 __ sarl(low, Immediate(shift - 32));
4330 } else {
4331 // Between 1 and 31.
4332 __ shrd(low, high, Immediate(shift));
4333 __ sarl(high, Immediate(shift));
4334 }
4335}
4336
Calin Juravle9aec02f2014-11-18 23:06:35 +00004337void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004338 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004339 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4340 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4341 __ testl(shifter, Immediate(32));
4342 __ j(kEqual, &done);
4343 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4344 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4345 __ Bind(&done);
4346}
4347
Mark P Mendell73945692015-04-29 14:56:17 +00004348void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4349 Register low = loc.AsRegisterPairLow<Register>();
4350 Register high = loc.AsRegisterPairHigh<Register>();
4351 if (shift == 32) {
4352 // Shift by 32 is easy. Low gets high, and high gets 0.
4353 codegen_->EmitParallelMoves(
4354 loc.ToHigh(),
4355 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004356 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004357 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4358 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004360 } else if (shift > 32) {
4361 // Low part is high >> (shift - 32). High part becomes 0.
4362 __ movl(low, high);
4363 __ shrl(low, Immediate(shift - 32));
4364 __ xorl(high, high);
4365 } else {
4366 // Between 1 and 31.
4367 __ shrd(low, high, Immediate(shift));
4368 __ shrl(high, Immediate(shift));
4369 }
4370}
4371
Calin Juravle9aec02f2014-11-18 23:06:35 +00004372void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004373 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004374 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4375 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4376 __ testl(shifter, Immediate(32));
4377 __ j(kEqual, &done);
4378 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4379 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4380 __ Bind(&done);
4381}
4382
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004383void LocationsBuilderX86::VisitRor(HRor* ror) {
4384 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004385 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004386
4387 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004388 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004389 // Add the temporary needed.
4390 locations->AddTemp(Location::RequiresRegister());
4391 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004392 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004393 locations->SetInAt(0, Location::RequiresRegister());
4394 // The shift count needs to be in CL (unless it is a constant).
4395 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4396 locations->SetOut(Location::SameAsFirstInput());
4397 break;
4398 default:
4399 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4400 UNREACHABLE();
4401 }
4402}
4403
4404void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4405 LocationSummary* locations = ror->GetLocations();
4406 Location first = locations->InAt(0);
4407 Location second = locations->InAt(1);
4408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004409 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004410 Register first_reg = first.AsRegister<Register>();
4411 if (second.IsRegister()) {
4412 Register second_reg = second.AsRegister<Register>();
4413 __ rorl(first_reg, second_reg);
4414 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004415 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004416 __ rorl(first_reg, imm);
4417 }
4418 return;
4419 }
4420
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004421 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004422 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4423 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4424 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4425 if (second.IsRegister()) {
4426 Register second_reg = second.AsRegister<Register>();
4427 DCHECK_EQ(second_reg, ECX);
4428 __ movl(temp_reg, first_reg_hi);
4429 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4430 __ shrd(first_reg_lo, temp_reg, second_reg);
4431 __ movl(temp_reg, first_reg_hi);
4432 __ testl(second_reg, Immediate(32));
4433 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4434 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4435 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004436 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004437 if (shift_amt == 0) {
4438 // Already fine.
4439 return;
4440 }
4441 if (shift_amt == 32) {
4442 // Just swap.
4443 __ movl(temp_reg, first_reg_lo);
4444 __ movl(first_reg_lo, first_reg_hi);
4445 __ movl(first_reg_hi, temp_reg);
4446 return;
4447 }
4448
4449 Immediate imm(shift_amt);
4450 // Save the constents of the low value.
4451 __ movl(temp_reg, first_reg_lo);
4452
4453 // Shift right into low, feeding bits from high.
4454 __ shrd(first_reg_lo, first_reg_hi, imm);
4455
4456 // Shift right into high, feeding bits from the original low.
4457 __ shrd(first_reg_hi, temp_reg, imm);
4458
4459 // Swap if needed.
4460 if (shift_amt > 32) {
4461 __ movl(temp_reg, first_reg_lo);
4462 __ movl(first_reg_lo, first_reg_hi);
4463 __ movl(first_reg_hi, temp_reg);
4464 }
4465 }
4466}
4467
Calin Juravle9aec02f2014-11-18 23:06:35 +00004468void LocationsBuilderX86::VisitShl(HShl* shl) {
4469 HandleShift(shl);
4470}
4471
4472void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4473 HandleShift(shl);
4474}
4475
4476void LocationsBuilderX86::VisitShr(HShr* shr) {
4477 HandleShift(shr);
4478}
4479
4480void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4481 HandleShift(shr);
4482}
4483
4484void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4485 HandleShift(ushr);
4486}
4487
4488void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4489 HandleShift(ushr);
4490}
4491
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004492void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004493 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4494 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004495 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004496 if (instruction->IsStringAlloc()) {
4497 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4498 } else {
4499 InvokeRuntimeCallingConvention calling_convention;
4500 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004501 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004502}
4503
4504void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004505 // Note: if heap poisoning is enabled, the entry point takes cares
4506 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004507 if (instruction->IsStringAlloc()) {
4508 // String is allocated through StringFactory. Call NewEmptyString entry point.
4509 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004510 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004511 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4512 __ call(Address(temp, code_offset.Int32Value()));
4513 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4514 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004515 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004516 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004517 DCHECK(!codegen_->IsLeafMethod());
4518 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004519}
4520
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004521void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004522 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4523 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004524 locations->SetOut(Location::RegisterLocation(EAX));
4525 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004526 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4527 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004528}
4529
4530void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004531 // Note: if heap poisoning is enabled, the entry point takes cares
4532 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004533 QuickEntrypointEnum entrypoint =
4534 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4535 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004536 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004537 DCHECK(!codegen_->IsLeafMethod());
4538}
4539
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004540void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004541 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004542 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004543 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4544 if (location.IsStackSlot()) {
4545 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4546 } else if (location.IsDoubleStackSlot()) {
4547 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004548 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004549 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004550}
4551
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004552void InstructionCodeGeneratorX86::VisitParameterValue(
4553 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4554}
4555
4556void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4557 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004558 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004559 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4560}
4561
4562void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004563}
4564
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004565void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4566 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004567 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004568 locations->SetInAt(0, Location::RequiresRegister());
4569 locations->SetOut(Location::RequiresRegister());
4570}
4571
4572void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4573 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004574 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004575 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004576 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004577 __ movl(locations->Out().AsRegister<Register>(),
4578 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004579 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004580 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004581 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004582 __ movl(locations->Out().AsRegister<Register>(),
4583 Address(locations->InAt(0).AsRegister<Register>(),
4584 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4585 // temp = temp->GetImtEntryAt(method_offset);
4586 __ movl(locations->Out().AsRegister<Register>(),
4587 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004588 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004589}
4590
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004591void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004592 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004593 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004594 locations->SetInAt(0, Location::RequiresRegister());
4595 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004596}
4597
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004598void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4599 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004600 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004601 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004602 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004603 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004605 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004606 break;
4607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004608 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004609 __ notl(out.AsRegisterPairLow<Register>());
4610 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004611 break;
4612
4613 default:
4614 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4615 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004616}
4617
David Brazdil66d126e2015-04-03 16:02:44 +01004618void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4619 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004620 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004621 locations->SetInAt(0, Location::RequiresRegister());
4622 locations->SetOut(Location::SameAsFirstInput());
4623}
4624
4625void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004626 LocationSummary* locations = bool_not->GetLocations();
4627 Location in = locations->InAt(0);
4628 Location out = locations->Out();
4629 DCHECK(in.Equals(out));
4630 __ xorl(out.AsRegister<Register>(), Immediate(1));
4631}
4632
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004633void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004634 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004635 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004636 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004638 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004640 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004641 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 case DataType::Type::kInt32:
4643 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004644 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004645 locations->SetInAt(1, Location::Any());
4646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4647 break;
4648 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004649 case DataType::Type::kFloat32:
4650 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004651 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004652 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4653 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4654 } else if (compare->InputAt(1)->IsConstant()) {
4655 locations->SetInAt(1, Location::RequiresFpuRegister());
4656 } else {
4657 locations->SetInAt(1, Location::Any());
4658 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004659 locations->SetOut(Location::RequiresRegister());
4660 break;
4661 }
4662 default:
4663 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4664 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004665}
4666
4667void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004668 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004669 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004670 Location left = locations->InAt(0);
4671 Location right = locations->InAt(1);
4672
Mark Mendell0c9497d2015-08-21 09:30:05 -04004673 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004674 Condition less_cond = kLess;
4675
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004676 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004678 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004679 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004680 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004681 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004683 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004684 break;
4685 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004686 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004687 Register left_low = left.AsRegisterPairLow<Register>();
4688 Register left_high = left.AsRegisterPairHigh<Register>();
4689 int32_t val_low = 0;
4690 int32_t val_high = 0;
4691 bool right_is_const = false;
4692
4693 if (right.IsConstant()) {
4694 DCHECK(right.GetConstant()->IsLongConstant());
4695 right_is_const = true;
4696 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4697 val_low = Low32Bits(val);
4698 val_high = High32Bits(val);
4699 }
4700
Calin Juravleddb7df22014-11-25 20:56:51 +00004701 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004702 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004703 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004704 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004705 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004706 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004707 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004708 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004709 __ j(kLess, &less); // Signed compare.
4710 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004711 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004712 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004713 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004714 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004715 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004716 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004717 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004718 }
Aart Bika19616e2016-02-01 18:57:58 -08004719 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004720 break;
4721 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004722 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004723 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004724 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004725 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004726 break;
4727 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004728 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004729 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004730 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004731 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004732 break;
4733 }
4734 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004735 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004736 }
Aart Bika19616e2016-02-01 18:57:58 -08004737
Calin Juravleddb7df22014-11-25 20:56:51 +00004738 __ movl(out, Immediate(0));
4739 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004740 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004741
4742 __ Bind(&greater);
4743 __ movl(out, Immediate(1));
4744 __ jmp(&done);
4745
4746 __ Bind(&less);
4747 __ movl(out, Immediate(-1));
4748
4749 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004750}
4751
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004752void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004753 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004754 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004755 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004756 locations->SetInAt(i, Location::Any());
4757 }
4758 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004759}
4760
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004761void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004762 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004763}
4764
Roland Levillain7c1559a2015-12-15 10:55:36 +00004765void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004766 /*
4767 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4768 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4769 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4770 */
4771 switch (kind) {
4772 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004773 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004774 break;
4775 }
4776 case MemBarrierKind::kAnyStore:
4777 case MemBarrierKind::kLoadAny:
4778 case MemBarrierKind::kStoreStore: {
4779 // nop
4780 break;
4781 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004782 case MemBarrierKind::kNTStoreStore:
4783 // Non-Temporal Store/Store needs an explicit fence.
4784 MemoryFence(/* non-temporal */ true);
4785 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004786 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004787}
4788
Vladimir Markodc151b22015-10-15 18:02:30 +01004789HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4790 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004791 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004792 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004793}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004795Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4796 Register temp) {
4797 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004798 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004799 if (!invoke->GetLocations()->Intrinsified()) {
4800 return location.AsRegister<Register>();
4801 }
4802 // For intrinsics we allow any location, so it may be on the stack.
4803 if (!location.IsRegister()) {
4804 __ movl(temp, Address(ESP, location.GetStackIndex()));
4805 return temp;
4806 }
4807 // For register locations, check if the register was saved. If so, get it from the stack.
4808 // Note: There is a chance that the register was saved but not overwritten, so we could
4809 // save one load. However, since this is just an intrinsic slow path we prefer this
4810 // simple and more robust approach rather that trying to determine if that's the case.
4811 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004812 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4813 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4814 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4815 __ movl(temp, Address(ESP, stack_offset));
4816 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004817 }
4818 return location.AsRegister<Register>();
4819}
4820
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004821void CodeGeneratorX86::GenerateStaticOrDirectCall(
4822 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004823 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4824 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004825 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004826 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004827 uint32_t offset =
4828 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4829 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004830 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004831 }
Vladimir Marko58155012015-08-19 12:49:41 +00004832 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004833 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004834 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004835 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4836 DCHECK(GetCompilerOptions().IsBootImage());
4837 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4838 temp.AsRegister<Register>());
4839 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004840 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004841 break;
4842 }
Vladimir Marko58155012015-08-19 12:49:41 +00004843 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4844 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4845 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004846 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4847 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4848 temp.AsRegister<Register>());
4849 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4850 RecordBootImageRelRoPatch(
4851 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004852 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004853 break;
4854 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004855 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004856 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4857 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004858 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004859 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004860 break;
4861 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004862 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4863 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4864 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004865 }
Vladimir Marko58155012015-08-19 12:49:41 +00004866 }
4867
4868 switch (invoke->GetCodePtrLocation()) {
4869 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4870 __ call(GetFrameEntryLabel());
4871 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004872 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4873 // (callee_method + offset_of_quick_compiled_code)()
4874 __ call(Address(callee_method.AsRegister<Register>(),
4875 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004876 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004877 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004878 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004879 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004880
4881 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004882}
4883
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004884void CodeGeneratorX86::GenerateVirtualCall(
4885 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004886 Register temp = temp_in.AsRegister<Register>();
4887 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4888 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004889
4890 // Use the calling convention instead of the location of the receiver, as
4891 // intrinsics may have put the receiver in a different register. In the intrinsics
4892 // slow path, the arguments have been moved to the right place, so here we are
4893 // guaranteed that the receiver is the first register of the calling convention.
4894 InvokeDexCallingConvention calling_convention;
4895 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004896 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004897 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004898 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004899 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004900 // Instead of simply (possibly) unpoisoning `temp` here, we should
4901 // emit a read barrier for the previous class reference load.
4902 // However this is not required in practice, as this is an
4903 // intermediate/temporary reference and because the current
4904 // concurrent copying collector keeps the from-space memory
4905 // intact/accessible until the end of the marking phase (the
4906 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004907 __ MaybeUnpoisonHeapReference(temp);
4908 // temp = temp->GetMethodAt(method_offset);
4909 __ movl(temp, Address(temp, method_offset));
4910 // call temp->GetEntryPoint();
4911 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004912 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004913 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004914}
4915
Vladimir Markob066d432018-01-03 13:14:37 +00004916void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4917 uint32_t boot_image_offset) {
4918 boot_image_method_patches_.emplace_back(
4919 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4920 __ Bind(&boot_image_method_patches_.back().label);
4921}
4922
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004923void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004924 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004925 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004926 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004927 boot_image_method_patches_.emplace_back(
4928 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004929 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004930}
4931
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004932void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4933 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4934 HX86ComputeBaseMethodAddress* method_address =
4935 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004936 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004937 method_bss_entry_patches_.emplace_back(
4938 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4939 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004940}
4941
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004942void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4943 HX86ComputeBaseMethodAddress* method_address =
4944 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4945 boot_image_type_patches_.emplace_back(
4946 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004947 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004948}
4949
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004950Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004951 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004952 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4953 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004954 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004955 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004956}
4957
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004958void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4959 HX86ComputeBaseMethodAddress* method_address =
4960 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4961 boot_image_string_patches_.emplace_back(
4962 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4963 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004964}
4965
Vladimir Markoaad75c62016-10-03 08:46:48 +00004966Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4967 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004968 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004969 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004970 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004971 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004972 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004973}
4974
Vladimir Markoeebb8212018-06-05 14:57:24 +01004975void CodeGeneratorX86::LoadBootImageAddress(Register reg,
4976 uint32_t boot_image_offset,
4977 HInvokeStaticOrDirect* invoke) {
4978 DCHECK(!GetCompilerOptions().IsBootImage());
4979 if (GetCompilerOptions().GetCompilePic()) {
4980 DCHECK(Runtime::Current()->IsAotCompiler());
4981 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4982 HX86ComputeBaseMethodAddress* method_address =
4983 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4984 DCHECK(method_address != nullptr);
4985 Register method_address_reg =
4986 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4987 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
4988 RecordBootImageRelRoPatch(method_address, boot_image_offset);
4989 } else {
4990 gc::Heap* heap = Runtime::Current()->GetHeap();
4991 DCHECK(!heap->GetBootImageSpaces().empty());
4992 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_offset;
4993 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
4994 }
4995}
4996
Vladimir Markoaad75c62016-10-03 08:46:48 +00004997// The label points to the end of the "movl" or another instruction but the literal offset
4998// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4999constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5000
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005001template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005002inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005003 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005004 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005005 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005006 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005007 linker_patches->push_back(Factory(literal_offset,
5008 info.target_dex_file,
5009 GetMethodAddressOffset(info.method_address),
5010 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005011 }
5012}
5013
Vladimir Markob066d432018-01-03 13:14:37 +00005014linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
5015 const DexFile* target_dex_file,
5016 uint32_t pc_insn_offset,
5017 uint32_t boot_image_offset) {
5018 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
5019 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
5020}
5021
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005022void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005023 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005024 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005025 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005026 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005027 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005028 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005029 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005030 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005031 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005032 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005033 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5034 boot_image_method_patches_, linker_patches);
5035 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5036 boot_image_type_patches_, linker_patches);
5037 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005038 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005039 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00005040 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
5041 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005042 DCHECK(boot_image_type_patches_.empty());
5043 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005044 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005045 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5046 method_bss_entry_patches_, linker_patches);
5047 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5048 type_bss_entry_patches_, linker_patches);
5049 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5050 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005051 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005052}
5053
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005054void CodeGeneratorX86::MarkGCCard(Register temp,
5055 Register card,
5056 Register object,
5057 Register value,
5058 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005059 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005060 if (value_can_be_null) {
5061 __ testl(value, value);
5062 __ j(kEqual, &is_null);
5063 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005064 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065 __ movl(temp, object);
5066 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005067 __ movb(Address(temp, card, TIMES_1, 0),
5068 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005069 if (value_can_be_null) {
5070 __ Bind(&is_null);
5071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072}
5073
Calin Juravle52c48962014-12-16 17:02:57 +00005074void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5075 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076
5077 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005078 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005080 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5081 kEmitCompilerReadBarrier
5082 ? LocationSummary::kCallOnSlowPath
5083 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005084 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005085 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005086 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005087 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005089 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005090 locations->SetOut(Location::RequiresFpuRegister());
5091 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005092 // The output overlaps in case of long: we don't want the low move
5093 // to overwrite the object's location. Likewise, in the case of
5094 // an object field get with read barriers enabled, we do not want
5095 // the move to overwrite the object's location, as we need it to emit
5096 // the read barrier.
5097 locations->SetOut(
5098 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005099 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005100 Location::kOutputOverlap :
5101 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005102 }
Calin Juravle52c48962014-12-16 17:02:57 +00005103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005104 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005105 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005106 // So we use an XMM register as a temp to achieve atomicity (first
5107 // load the temp into the XMM and then copy the XMM into the
5108 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005109 locations->AddTemp(Location::RequiresFpuRegister());
5110 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111}
5112
Calin Juravle52c48962014-12-16 17:02:57 +00005113void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5114 const FieldInfo& field_info) {
5115 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005116
Calin Juravle52c48962014-12-16 17:02:57 +00005117 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005118 Location base_loc = locations->InAt(0);
5119 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005120 Location out = locations->Out();
5121 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005122 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5123 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005124 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5125
Vladimir Marko61b92282017-10-11 13:23:17 +01005126 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005127 case DataType::Type::kBool:
5128 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005129 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005130 break;
5131 }
5132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005134 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135 break;
5136 }
5137
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005138 case DataType::Type::kUint16: {
5139 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140 break;
5141 }
5142
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005143 case DataType::Type::kInt16: {
5144 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145 break;
5146 }
5147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005148 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005149 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005152 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 // /* HeapReference<Object> */ out = *(base + offset)
5154 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005155 // Note that a potential implicit null check is handled in this
5156 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5157 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005158 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005159 if (is_volatile) {
5160 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5161 }
5162 } else {
5163 __ movl(out.AsRegister<Register>(), Address(base, offset));
5164 codegen_->MaybeRecordImplicitNullCheck(instruction);
5165 if (is_volatile) {
5166 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5167 }
5168 // If read barriers are enabled, emit read barriers other than
5169 // Baker's using a slow path (and also unpoison the loaded
5170 // reference, if heap poisoning is enabled).
5171 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5172 }
5173 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005174 }
5175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005177 if (is_volatile) {
5178 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5179 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005181 __ movd(out.AsRegisterPairLow<Register>(), temp);
5182 __ psrlq(temp, Immediate(32));
5183 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5184 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005185 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005186 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005187 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005188 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5189 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 break;
5191 }
5192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005193 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005194 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005195 break;
5196 }
5197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005198 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005199 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005200 break;
5201 }
5202
Aart Bik66c158e2018-01-31 12:55:04 -08005203 case DataType::Type::kUint32:
5204 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005206 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005207 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208 }
Calin Juravle52c48962014-12-16 17:02:57 +00005209
Vladimir Marko61b92282017-10-11 13:23:17 +01005210 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005211 // Potential implicit null checks, in the case of reference or
5212 // long fields, are handled in the previous switch statement.
5213 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 codegen_->MaybeRecordImplicitNullCheck(instruction);
5215 }
5216
Calin Juravle52c48962014-12-16 17:02:57 +00005217 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005218 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005219 // Memory barriers, in the case of references, are also handled
5220 // in the previous switch statement.
5221 } else {
5222 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5223 }
Roland Levillain4d027112015-07-01 15:41:14 +01005224 }
Calin Juravle52c48962014-12-16 17:02:57 +00005225}
5226
5227void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5228 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5229
5230 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005231 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005232 locations->SetInAt(0, Location::RequiresRegister());
5233 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005234 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005235 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005236
5237 // The register allocator does not support multiple
5238 // inputs that die at entry with one in a specific register.
5239 if (is_byte_type) {
5240 // Ensure the value is in a byte register.
5241 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 } else if (DataType::IsFloatingPointType(field_type)) {
5243 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005244 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5245 locations->SetInAt(1, Location::RequiresFpuRegister());
5246 } else {
5247 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5248 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005249 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005250 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005251 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005252
Calin Juravle52c48962014-12-16 17:02:57 +00005253 // 64bits value can be atomically written to an address with movsd and an XMM register.
5254 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5255 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5256 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5257 // isolated cases when we need this it isn't worth adding the extra complexity.
5258 locations->AddTemp(Location::RequiresFpuRegister());
5259 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005260 } else {
5261 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5262
5263 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5264 // Temporary registers for the write barrier.
5265 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5266 // Ensure the card is in a byte register.
5267 locations->AddTemp(Location::RegisterLocation(ECX));
5268 }
Calin Juravle52c48962014-12-16 17:02:57 +00005269 }
5270}
5271
5272void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005273 const FieldInfo& field_info,
5274 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005275 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5276
5277 LocationSummary* locations = instruction->GetLocations();
5278 Register base = locations->InAt(0).AsRegister<Register>();
5279 Location value = locations->InAt(1);
5280 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005281 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005282 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005283 bool needs_write_barrier =
5284 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005285
5286 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005288 }
5289
Mark Mendell81489372015-11-04 11:30:41 -05005290 bool maybe_record_implicit_null_check_done = false;
5291
Calin Juravle52c48962014-12-16 17:02:57 +00005292 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005293 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005294 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005295 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005296 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5297 break;
5298 }
5299
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005300 case DataType::Type::kUint16:
5301 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005302 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005303 __ movw(Address(base, offset),
5304 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005305 } else {
5306 __ movw(Address(base, offset), value.AsRegister<Register>());
5307 }
Calin Juravle52c48962014-12-16 17:02:57 +00005308 break;
5309 }
5310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kInt32:
5312 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005313 if (kPoisonHeapReferences && needs_write_barrier) {
5314 // Note that in the case where `value` is a null reference,
5315 // we do not enter this block, as the reference does not
5316 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005318 Register temp = locations->GetTemp(0).AsRegister<Register>();
5319 __ movl(temp, value.AsRegister<Register>());
5320 __ PoisonHeapReference(temp);
5321 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005322 } else if (value.IsConstant()) {
5323 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5324 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005325 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005326 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005327 __ movl(Address(base, offset), value.AsRegister<Register>());
5328 }
Calin Juravle52c48962014-12-16 17:02:57 +00005329 break;
5330 }
5331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005332 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005333 if (is_volatile) {
5334 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5335 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5336 __ movd(temp1, value.AsRegisterPairLow<Register>());
5337 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5338 __ punpckldq(temp1, temp2);
5339 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005341 } else if (value.IsConstant()) {
5342 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5343 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5344 codegen_->MaybeRecordImplicitNullCheck(instruction);
5345 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005346 } else {
5347 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005348 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005349 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5350 }
Mark Mendell81489372015-11-04 11:30:41 -05005351 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005352 break;
5353 }
5354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005355 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005356 if (value.IsConstant()) {
5357 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5358 __ movl(Address(base, offset), Immediate(v));
5359 } else {
5360 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5361 }
Calin Juravle52c48962014-12-16 17:02:57 +00005362 break;
5363 }
5364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005365 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005366 if (value.IsConstant()) {
5367 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5368 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5369 codegen_->MaybeRecordImplicitNullCheck(instruction);
5370 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5371 maybe_record_implicit_null_check_done = true;
5372 } else {
5373 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5374 }
Calin Juravle52c48962014-12-16 17:02:57 +00005375 break;
5376 }
5377
Aart Bik66c158e2018-01-31 12:55:04 -08005378 case DataType::Type::kUint32:
5379 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005380 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005381 LOG(FATAL) << "Unreachable type " << field_type;
5382 UNREACHABLE();
5383 }
5384
Mark Mendell81489372015-11-04 11:30:41 -05005385 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005386 codegen_->MaybeRecordImplicitNullCheck(instruction);
5387 }
5388
Roland Levillain4d027112015-07-01 15:41:14 +01005389 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005390 Register temp = locations->GetTemp(0).AsRegister<Register>();
5391 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005392 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005393 }
5394
Calin Juravle52c48962014-12-16 17:02:57 +00005395 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005396 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005397 }
5398}
5399
5400void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5401 HandleFieldGet(instruction, instruction->GetFieldInfo());
5402}
5403
5404void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5405 HandleFieldGet(instruction, instruction->GetFieldInfo());
5406}
5407
5408void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5409 HandleFieldSet(instruction, instruction->GetFieldInfo());
5410}
5411
5412void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005413 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005414}
5415
5416void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5417 HandleFieldSet(instruction, instruction->GetFieldInfo());
5418}
5419
5420void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005421 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005422}
5423
5424void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5425 HandleFieldGet(instruction, instruction->GetFieldInfo());
5426}
5427
5428void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5429 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005430}
5431
Calin Juravlee460d1d2015-09-29 04:52:17 +01005432void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5433 HUnresolvedInstanceFieldGet* instruction) {
5434 FieldAccessCallingConventionX86 calling_convention;
5435 codegen_->CreateUnresolvedFieldLocationSummary(
5436 instruction, instruction->GetFieldType(), calling_convention);
5437}
5438
5439void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5440 HUnresolvedInstanceFieldGet* instruction) {
5441 FieldAccessCallingConventionX86 calling_convention;
5442 codegen_->GenerateUnresolvedFieldAccess(instruction,
5443 instruction->GetFieldType(),
5444 instruction->GetFieldIndex(),
5445 instruction->GetDexPc(),
5446 calling_convention);
5447}
5448
5449void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5450 HUnresolvedInstanceFieldSet* instruction) {
5451 FieldAccessCallingConventionX86 calling_convention;
5452 codegen_->CreateUnresolvedFieldLocationSummary(
5453 instruction, instruction->GetFieldType(), calling_convention);
5454}
5455
5456void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5457 HUnresolvedInstanceFieldSet* instruction) {
5458 FieldAccessCallingConventionX86 calling_convention;
5459 codegen_->GenerateUnresolvedFieldAccess(instruction,
5460 instruction->GetFieldType(),
5461 instruction->GetFieldIndex(),
5462 instruction->GetDexPc(),
5463 calling_convention);
5464}
5465
5466void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5467 HUnresolvedStaticFieldGet* instruction) {
5468 FieldAccessCallingConventionX86 calling_convention;
5469 codegen_->CreateUnresolvedFieldLocationSummary(
5470 instruction, instruction->GetFieldType(), calling_convention);
5471}
5472
5473void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5474 HUnresolvedStaticFieldGet* 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::VisitUnresolvedStaticFieldSet(
5484 HUnresolvedStaticFieldSet* instruction) {
5485 FieldAccessCallingConventionX86 calling_convention;
5486 codegen_->CreateUnresolvedFieldLocationSummary(
5487 instruction, instruction->GetFieldType(), calling_convention);
5488}
5489
5490void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5491 HUnresolvedStaticFieldSet* instruction) {
5492 FieldAccessCallingConventionX86 calling_convention;
5493 codegen_->GenerateUnresolvedFieldAccess(instruction,
5494 instruction->GetFieldType(),
5495 instruction->GetFieldIndex(),
5496 instruction->GetDexPc(),
5497 calling_convention);
5498}
5499
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005500void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005501 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5502 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5503 ? Location::RequiresRegister()
5504 : Location::Any();
5505 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005506}
5507
Calin Juravle2ae48182016-03-16 14:05:09 +00005508void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5509 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005510 return;
5511 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005512 LocationSummary* locations = instruction->GetLocations();
5513 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005514
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005515 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005516 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005517}
5518
Calin Juravle2ae48182016-03-16 14:05:09 +00005519void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005520 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005521 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005522
5523 LocationSummary* locations = instruction->GetLocations();
5524 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005525
5526 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005527 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005528 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005529 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005530 } else {
5531 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005532 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005533 __ jmp(slow_path->GetEntryLabel());
5534 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005535 }
5536 __ j(kEqual, slow_path->GetEntryLabel());
5537}
5538
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005539void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005540 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005541}
5542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005544 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005546 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005547 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5548 object_array_get_with_read_barrier
5549 ? LocationSummary::kCallOnSlowPath
5550 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005551 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005552 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005553 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005554 locations->SetInAt(0, Location::RequiresRegister());
5555 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005556 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5558 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005559 // The output overlaps in case of long: we don't want the low move
5560 // to overwrite the array's location. Likewise, in the case of an
5561 // object array get with read barriers enabled, we do not want the
5562 // move to overwrite the array's location, as we need it to emit
5563 // the read barrier.
5564 locations->SetOut(
5565 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5567 ? Location::kOutputOverlap
5568 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005569 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005570}
5571
5572void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5573 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005574 Location obj_loc = locations->InAt(0);
5575 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005577 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005578 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005580 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005581 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005582 case DataType::Type::kBool:
5583 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005584 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005585 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586 break;
5587 }
5588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005590 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005591 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592 break;
5593 }
5594
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005595 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005596 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005597 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5598 // Branch cases into compressed and uncompressed for each index's type.
5599 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5600 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005601 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005602 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005603 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5604 "Expecting 0=compressed, 1=uncompressed");
5605 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005606 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5607 __ jmp(&done);
5608 __ Bind(&not_compressed);
5609 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5610 __ Bind(&done);
5611 } else {
5612 // Common case for charAt of array of char or when string compression's
5613 // feature is turned off.
5614 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5615 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616 break;
5617 }
5618
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005619 case DataType::Type::kInt16: {
5620 Register out = out_loc.AsRegister<Register>();
5621 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5622 break;
5623 }
5624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005626 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005627 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005628 break;
5629 }
5630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005631 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005632 static_assert(
5633 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5634 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005635 // /* HeapReference<Object> */ out =
5636 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5637 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005638 // Note that a potential implicit null check is handled in this
5639 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5640 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005641 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005642 } else {
5643 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005644 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5645 codegen_->MaybeRecordImplicitNullCheck(instruction);
5646 // If read barriers are enabled, emit read barriers other than
5647 // Baker's using a slow path (and also unpoison the loaded
5648 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005649 if (index.IsConstant()) {
5650 uint32_t offset =
5651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005652 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5653 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005654 codegen_->MaybeGenerateReadBarrierSlow(
5655 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5656 }
5657 }
5658 break;
5659 }
5660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005662 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005663 __ movl(out_loc.AsRegisterPairLow<Register>(),
5664 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5665 codegen_->MaybeRecordImplicitNullCheck(instruction);
5666 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5667 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005668 break;
5669 }
5670
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005671 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005672 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005673 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005674 break;
5675 }
5676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005678 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005679 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005680 break;
5681 }
5682
Aart Bik66c158e2018-01-31 12:55:04 -08005683 case DataType::Type::kUint32:
5684 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005685 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005686 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005687 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005688 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005690 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005691 // Potential implicit null checks, in the case of reference or
5692 // long arrays, are handled in the previous switch statement.
5693 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005694 codegen_->MaybeRecordImplicitNullCheck(instruction);
5695 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005696}
5697
5698void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005699 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005700
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005701 bool needs_write_barrier =
5702 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005703 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005704
Vladimir Markoca6fff82017-10-03 14:49:14 +01005705 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005706 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005707 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005708 LocationSummary::kCallOnSlowPath :
5709 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005710
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005711 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005712 // We need the inputs to be different than the output in case of long operation.
5713 // In case of a byte operation, the register allocator does not support multiple
5714 // inputs that die at entry with one in a specific register.
5715 locations->SetInAt(0, Location::RequiresRegister());
5716 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5717 if (is_byte_type) {
5718 // Ensure the value is in a byte register.
5719 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005721 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005723 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5724 }
5725 if (needs_write_barrier) {
5726 // Temporary registers for the write barrier.
5727 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5728 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005729 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005730 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005731}
5732
5733void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5734 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735 Location array_loc = locations->InAt(0);
5736 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005737 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005738 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005739 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005740 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5741 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5742 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005744 bool needs_write_barrier =
5745 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005746
5747 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005748 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005749 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005750 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005751 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005752 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005753 if (value.IsRegister()) {
5754 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005755 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005756 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005757 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005758 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005759 break;
5760 }
5761
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005762 case DataType::Type::kUint16:
5763 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005764 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005765 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005766 if (value.IsRegister()) {
5767 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005768 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005769 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005770 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005771 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005772 break;
5773 }
5774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005776 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005777 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005779 if (!value.IsRegister()) {
5780 // Just setting null.
5781 DCHECK(instruction->InputAt(2)->IsNullConstant());
5782 DCHECK(value.IsConstant()) << value;
5783 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005784 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005785 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005787 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005788 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005789
5790 DCHECK(needs_write_barrier);
5791 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005792 // We cannot use a NearLabel for `done`, as its range may be too
5793 // short when Baker read barriers are enabled.
5794 Label done;
5795 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005796 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005797 Location temp_loc = locations->GetTemp(0);
5798 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005800 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005801 codegen_->AddSlowPath(slow_path);
5802 if (instruction->GetValueCanBeNull()) {
5803 __ testl(register_value, register_value);
5804 __ j(kNotEqual, &not_null);
5805 __ movl(address, Immediate(0));
5806 codegen_->MaybeRecordImplicitNullCheck(instruction);
5807 __ jmp(&done);
5808 __ Bind(&not_null);
5809 }
5810
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005811 // Note that when Baker read barriers are enabled, the type
5812 // checks are performed without read barriers. This is fine,
5813 // even in the case where a class object is in the from-space
5814 // after the flip, as a comparison involving such a type would
5815 // not produce a false positive; it may of course produce a
5816 // false negative, in which case we would take the ArraySet
5817 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005818
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005819 // /* HeapReference<Class> */ temp = array->klass_
5820 __ movl(temp, Address(array, class_offset));
5821 codegen_->MaybeRecordImplicitNullCheck(instruction);
5822 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005823
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005824 // /* HeapReference<Class> */ temp = temp->component_type_
5825 __ movl(temp, Address(temp, component_offset));
5826 // If heap poisoning is enabled, no need to unpoison `temp`
5827 // nor the object reference in `register_value->klass`, as
5828 // we are comparing two poisoned references.
5829 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005830
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005831 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5832 __ j(kEqual, &do_put);
5833 // If heap poisoning is enabled, the `temp` reference has
5834 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835 __ MaybeUnpoisonHeapReference(temp);
5836
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005837 // If heap poisoning is enabled, no need to unpoison the
5838 // heap reference loaded below, as it is only used for a
5839 // comparison with null.
5840 __ cmpl(Address(temp, super_offset), Immediate(0));
5841 __ j(kNotEqual, slow_path->GetEntryLabel());
5842 __ Bind(&do_put);
5843 } else {
5844 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005845 }
5846 }
5847
5848 if (kPoisonHeapReferences) {
5849 __ movl(temp, register_value);
5850 __ PoisonHeapReference(temp);
5851 __ movl(address, temp);
5852 } else {
5853 __ movl(address, register_value);
5854 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005855 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005856 codegen_->MaybeRecordImplicitNullCheck(instruction);
5857 }
5858
5859 Register card = locations->GetTemp(1).AsRegister<Register>();
5860 codegen_->MarkGCCard(
5861 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5862 __ Bind(&done);
5863
5864 if (slow_path != nullptr) {
5865 __ Bind(slow_path->GetExitLabel());
5866 }
5867
5868 break;
5869 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005871 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005872 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005873 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005874 if (value.IsRegister()) {
5875 __ movl(address, value.AsRegister<Register>());
5876 } else {
5877 DCHECK(value.IsConstant()) << value;
5878 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5879 __ movl(address, Immediate(v));
5880 }
5881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005882 break;
5883 }
5884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005886 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005887 if (value.IsRegisterPair()) {
5888 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5889 value.AsRegisterPairLow<Register>());
5890 codegen_->MaybeRecordImplicitNullCheck(instruction);
5891 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5892 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005893 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005894 DCHECK(value.IsConstant());
5895 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5896 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5897 Immediate(Low32Bits(val)));
5898 codegen_->MaybeRecordImplicitNullCheck(instruction);
5899 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5900 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005901 }
5902 break;
5903 }
5904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005905 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005906 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005907 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005908 if (value.IsFpuRegister()) {
5909 __ movss(address, value.AsFpuRegister<XmmRegister>());
5910 } else {
5911 DCHECK(value.IsConstant());
5912 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5913 __ movl(address, Immediate(v));
5914 }
5915 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005916 break;
5917 }
5918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005920 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005921 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005922 if (value.IsFpuRegister()) {
5923 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5924 } else {
5925 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005926 Address address_hi =
5927 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005928 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5929 __ movl(address, Immediate(Low32Bits(v)));
5930 codegen_->MaybeRecordImplicitNullCheck(instruction);
5931 __ movl(address_hi, Immediate(High32Bits(v)));
5932 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005933 break;
5934 }
5935
Aart Bik66c158e2018-01-31 12:55:04 -08005936 case DataType::Type::kUint32:
5937 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005939 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005940 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005941 }
5942}
5943
5944void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005945 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005946 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005947 if (!instruction->IsEmittedAtUseSite()) {
5948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5949 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005950}
5951
5952void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005953 if (instruction->IsEmittedAtUseSite()) {
5954 return;
5955 }
5956
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005957 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005958 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005959 Register obj = locations->InAt(0).AsRegister<Register>();
5960 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005961 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005962 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005963 // Mask out most significant bit in case the array is String's array of char.
5964 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005965 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005966 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005967}
5968
5969void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005970 RegisterSet caller_saves = RegisterSet::Empty();
5971 InvokeRuntimeCallingConvention calling_convention;
5972 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5973 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5974 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005975 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005976 HInstruction* length = instruction->InputAt(1);
5977 if (!length->IsEmittedAtUseSite()) {
5978 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5979 }
jessicahandojo4877b792016-09-08 19:49:13 -07005980 // Need register to see array's length.
5981 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5982 locations->AddTemp(Location::RequiresRegister());
5983 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005984}
5985
5986void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005987 const bool is_string_compressed_char_at =
5988 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005989 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005990 Location index_loc = locations->InAt(0);
5991 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005992 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005993 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994
Mark Mendell99dbd682015-04-22 16:18:52 -04005995 if (length_loc.IsConstant()) {
5996 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5997 if (index_loc.IsConstant()) {
5998 // BCE will remove the bounds check if we are guarenteed to pass.
5999 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6000 if (index < 0 || index >= length) {
6001 codegen_->AddSlowPath(slow_path);
6002 __ jmp(slow_path->GetEntryLabel());
6003 } else {
6004 // Some optimization after BCE may have generated this, and we should not
6005 // generate a bounds check if it is a valid range.
6006 }
6007 return;
6008 }
6009
6010 // We have to reverse the jump condition because the length is the constant.
6011 Register index_reg = index_loc.AsRegister<Register>();
6012 __ cmpl(index_reg, Immediate(length));
6013 codegen_->AddSlowPath(slow_path);
6014 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006015 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006016 HInstruction* array_length = instruction->InputAt(1);
6017 if (array_length->IsEmittedAtUseSite()) {
6018 // Address the length field in the array.
6019 DCHECK(array_length->IsArrayLength());
6020 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6021 Location array_loc = array_length->GetLocations()->InAt(0);
6022 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006023 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006024 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6025 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006026 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6027 __ movl(length_reg, array_len);
6028 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006029 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006030 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006031 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006032 // Checking bounds for general case:
6033 // Array of char or string's array with feature compression off.
6034 if (index_loc.IsConstant()) {
6035 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6036 __ cmpl(array_len, Immediate(value));
6037 } else {
6038 __ cmpl(array_len, index_loc.AsRegister<Register>());
6039 }
6040 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006041 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006042 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006043 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006044 }
6045 codegen_->AddSlowPath(slow_path);
6046 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006047 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006048}
6049
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006050void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006051 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006052}
6053
6054void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006055 if (instruction->GetNext()->IsSuspendCheck() &&
6056 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6057 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6058 // The back edge will generate the suspend check.
6059 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6060 }
6061
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006062 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6063}
6064
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006065void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006066 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6067 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006068 // In suspend check slow path, usually there are no caller-save registers at all.
6069 // If SIMD instructions are present, however, we force spilling all live SIMD
6070 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006071 locations->SetCustomSlowPathCallerSaves(
6072 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006073}
6074
6075void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006076 HBasicBlock* block = instruction->GetBlock();
6077 if (block->GetLoopInformation() != nullptr) {
6078 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6079 // The back edge will generate the suspend check.
6080 return;
6081 }
6082 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6083 // The goto will generate the suspend check.
6084 return;
6085 }
6086 GenerateSuspendCheck(instruction, nullptr);
6087}
6088
6089void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6090 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006091 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006092 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6093 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006094 slow_path =
6095 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006096 instruction->SetSlowPath(slow_path);
6097 codegen_->AddSlowPath(slow_path);
6098 if (successor != nullptr) {
6099 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006100 }
6101 } else {
6102 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6103 }
6104
Andreas Gampe542451c2016-07-26 09:02:02 -07006105 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006106 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006107 if (successor == nullptr) {
6108 __ j(kNotEqual, slow_path->GetEntryLabel());
6109 __ Bind(slow_path->GetReturnLabel());
6110 } else {
6111 __ j(kEqual, codegen_->GetLabelOf(successor));
6112 __ jmp(slow_path->GetEntryLabel());
6113 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006114}
6115
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006116X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6117 return codegen_->GetAssembler();
6118}
6119
Aart Bikcfe50bb2017-12-12 14:54:12 -08006120void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006121 ScratchRegisterScope ensure_scratch(
6122 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6123 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6124 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006125
Aart Bikcfe50bb2017-12-12 14:54:12 -08006126 // Now that temp register is available (possibly spilled), move blocks of memory.
6127 for (int i = 0; i < number_of_words; i++) {
6128 __ movl(temp_reg, Address(ESP, src + stack_offset));
6129 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6130 stack_offset += kX86WordSize;
6131 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006132}
6133
6134void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006135 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006136 Location source = move->GetSource();
6137 Location destination = move->GetDestination();
6138
6139 if (source.IsRegister()) {
6140 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006141 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006142 } else if (destination.IsFpuRegister()) {
6143 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006144 } else {
6145 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006146 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006147 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006148 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006149 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006150 // Create stack space for 2 elements.
6151 __ subl(ESP, Immediate(2 * elem_size));
6152 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6153 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6154 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6155 // And remove the temporary stack space we allocated.
6156 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006157 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006158 if (destination.IsRegister()) {
6159 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6160 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006161 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006162 } else if (destination.IsRegisterPair()) {
6163 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6164 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6165 __ psrlq(src_reg, Immediate(32));
6166 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006167 } else if (destination.IsStackSlot()) {
6168 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006169 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006170 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006171 } else {
6172 DCHECK(destination.IsSIMDStackSlot());
6173 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006174 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006175 } else if (source.IsStackSlot()) {
6176 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006177 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006178 } else if (destination.IsFpuRegister()) {
6179 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006180 } else {
6181 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006182 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006183 }
6184 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006185 if (destination.IsRegisterPair()) {
6186 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6187 __ movl(destination.AsRegisterPairHigh<Register>(),
6188 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6189 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006190 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6191 } else {
6192 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006193 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006194 }
Aart Bik5576f372017-03-23 16:17:37 -07006195 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006196 if (destination.IsFpuRegister()) {
6197 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6198 } else {
6199 DCHECK(destination.IsSIMDStackSlot());
6200 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6201 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006202 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006203 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006204 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006205 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006206 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006207 if (value == 0) {
6208 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6209 } else {
6210 __ movl(destination.AsRegister<Register>(), Immediate(value));
6211 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006212 } else {
6213 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006214 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006215 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006216 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006217 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006218 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006219 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006220 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006221 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6222 if (value == 0) {
6223 // Easy handling of 0.0.
6224 __ xorps(dest, dest);
6225 } else {
6226 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006227 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6228 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6229 __ movl(temp, Immediate(value));
6230 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006231 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006232 } else {
6233 DCHECK(destination.IsStackSlot()) << destination;
6234 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6235 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006236 } else if (constant->IsLongConstant()) {
6237 int64_t value = constant->AsLongConstant()->GetValue();
6238 int32_t low_value = Low32Bits(value);
6239 int32_t high_value = High32Bits(value);
6240 Immediate low(low_value);
6241 Immediate high(high_value);
6242 if (destination.IsDoubleStackSlot()) {
6243 __ movl(Address(ESP, destination.GetStackIndex()), low);
6244 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6245 } else {
6246 __ movl(destination.AsRegisterPairLow<Register>(), low);
6247 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6248 }
6249 } else {
6250 DCHECK(constant->IsDoubleConstant());
6251 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006252 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006253 int32_t low_value = Low32Bits(value);
6254 int32_t high_value = High32Bits(value);
6255 Immediate low(low_value);
6256 Immediate high(high_value);
6257 if (destination.IsFpuRegister()) {
6258 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6259 if (value == 0) {
6260 // Easy handling of 0.0.
6261 __ xorpd(dest, dest);
6262 } else {
6263 __ pushl(high);
6264 __ pushl(low);
6265 __ movsd(dest, Address(ESP, 0));
6266 __ addl(ESP, Immediate(8));
6267 }
6268 } else {
6269 DCHECK(destination.IsDoubleStackSlot()) << destination;
6270 __ movl(Address(ESP, destination.GetStackIndex()), low);
6271 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6272 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006273 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006274 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006275 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006276 }
6277}
6278
Mark Mendella5c19ce2015-04-01 12:51:05 -04006279void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006280 Register suggested_scratch = reg == EAX ? EBX : EAX;
6281 ScratchRegisterScope ensure_scratch(
6282 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6283
6284 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6285 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6286 __ movl(Address(ESP, mem + stack_offset), reg);
6287 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006288}
6289
Mark Mendell7c8d0092015-01-26 11:21:33 -05006290void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006291 ScratchRegisterScope ensure_scratch(
6292 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6293
6294 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6295 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6296 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6297 __ movss(Address(ESP, mem + stack_offset), reg);
6298 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006299}
6300
Aart Bikcfe50bb2017-12-12 14:54:12 -08006301void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6302 size_t extra_slot = 4 * kX86WordSize;
6303 __ subl(ESP, Immediate(extra_slot));
6304 __ movups(Address(ESP, 0), XmmRegister(reg));
6305 ExchangeMemory(0, mem + extra_slot, 4);
6306 __ movups(XmmRegister(reg), Address(ESP, 0));
6307 __ addl(ESP, Immediate(extra_slot));
6308}
6309
6310void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006311 ScratchRegisterScope ensure_scratch1(
6312 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006313
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006314 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6315 ScratchRegisterScope ensure_scratch2(
6316 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006317
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006318 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6319 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006320
6321 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6322 for (int i = 0; i < number_of_words; i++) {
6323 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6324 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6325 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6326 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6327 stack_offset += kX86WordSize;
6328 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006329}
6330
6331void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006332 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006333 Location source = move->GetSource();
6334 Location destination = move->GetDestination();
6335
6336 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006337 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6338 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6339 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6340 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6341 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006342 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006343 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006344 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006345 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006346 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006347 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006348 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6349 // Use XOR Swap algorithm to avoid a temporary.
6350 DCHECK_NE(source.reg(), destination.reg());
6351 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6352 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6353 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6354 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6355 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6356 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6357 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006358 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6359 // Take advantage of the 16 bytes in the XMM register.
6360 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6361 Address stack(ESP, destination.GetStackIndex());
6362 // Load the double into the high doubleword.
6363 __ movhpd(reg, stack);
6364
6365 // Store the low double into the destination.
6366 __ movsd(stack, reg);
6367
6368 // Move the high double to the low double.
6369 __ psrldq(reg, Immediate(8));
6370 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6371 // Take advantage of the 16 bytes in the XMM register.
6372 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6373 Address stack(ESP, source.GetStackIndex());
6374 // Load the double into the high doubleword.
6375 __ movhpd(reg, stack);
6376
6377 // Store the low double into the destination.
6378 __ movsd(stack, reg);
6379
6380 // Move the high double to the low double.
6381 __ psrldq(reg, Immediate(8));
6382 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006383 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6384 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6385 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6386 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6387 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6388 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6389 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006390 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006391 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006392 }
6393}
6394
6395void ParallelMoveResolverX86::SpillScratch(int reg) {
6396 __ pushl(static_cast<Register>(reg));
6397}
6398
6399void ParallelMoveResolverX86::RestoreScratch(int reg) {
6400 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006401}
6402
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006403HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6404 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006405 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006406 case HLoadClass::LoadKind::kInvalid:
6407 LOG(FATAL) << "UNREACHABLE";
6408 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006409 case HLoadClass::LoadKind::kReferrersClass:
6410 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006412 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006413 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006414 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006415 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006416 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006417 DCHECK(Runtime::Current()->UseJitCompilation());
6418 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006419 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006420 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006421 break;
6422 }
6423 return desired_class_load_kind;
6424}
6425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006426void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006427 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006428 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006429 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006430 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006431 cls,
6432 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006433 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006434 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006435 return;
6436 }
Vladimir Marko41559982017-01-06 14:04:23 +00006437 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006438
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006439 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6440 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006441 ? LocationSummary::kCallOnSlowPath
6442 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006444 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006445 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006446 }
6447
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006448 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006449 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006450 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006451 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 locations->SetInAt(0, Location::RequiresRegister());
6453 }
6454 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006455 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6456 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6457 // Rely on the type resolution and/or initialization to save everything.
6458 RegisterSet caller_saves = RegisterSet::Empty();
6459 InvokeRuntimeCallingConvention calling_convention;
6460 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6461 locations->SetCustomSlowPathCallerSaves(caller_saves);
6462 } else {
6463 // For non-Baker read barrier we have a temp-clobbering call.
6464 }
6465 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006466}
6467
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006468Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006469 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006470 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006471 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006472 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006473 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006474 PatchInfo<Label>* info = &jit_class_patches_.back();
6475 return &info->label;
6476}
6477
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006478// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6479// move.
6480void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006481 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006482 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006483 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006484 return;
6485 }
Vladimir Marko41559982017-01-06 14:04:23 +00006486 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006487
Vladimir Marko41559982017-01-06 14:04:23 +00006488 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489 Location out_loc = locations->Out();
6490 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006492 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006493 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6494 ? kWithoutReadBarrier
6495 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006496 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006497 case HLoadClass::LoadKind::kReferrersClass: {
6498 DCHECK(!cls->CanCallRuntime());
6499 DCHECK(!cls->MustGenerateClinitCheck());
6500 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6501 Register current_method = locations->InAt(0).AsRegister<Register>();
6502 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006503 cls,
6504 out_loc,
6505 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006506 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006507 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006508 break;
6509 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006510 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006511 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006512 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006513 Register method_address = locations->InAt(0).AsRegister<Register>();
6514 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006515 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006516 break;
6517 }
6518 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006519 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006520 uint32_t address = dchecked_integral_cast<uint32_t>(
6521 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6522 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006523 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006524 break;
6525 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006526 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006527 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6528 Register method_address = locations->InAt(0).AsRegister<Register>();
6529 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006530 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6531 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006532 break;
6533 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006534 case HLoadClass::LoadKind::kBssEntry: {
6535 Register method_address = locations->InAt(0).AsRegister<Register>();
6536 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6537 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6538 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6539 generate_null_check = true;
6540 break;
6541 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006542 case HLoadClass::LoadKind::kJitTableAddress: {
6543 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6544 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006545 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006546 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006547 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006548 break;
6549 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006550 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006551 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006552 LOG(FATAL) << "UNREACHABLE";
6553 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006554 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006555
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006556 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6557 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006558 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006559 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6560 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006561
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006562 if (generate_null_check) {
6563 __ testl(out, out);
6564 __ j(kEqual, slow_path->GetEntryLabel());
6565 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006566
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006567 if (cls->MustGenerateClinitCheck()) {
6568 GenerateClassInitializationCheck(slow_path, out);
6569 } else {
6570 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006571 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006572 }
6573}
6574
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006575void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6576 InvokeRuntimeCallingConvention calling_convention;
6577 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6578 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6579}
6580
6581void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6582 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6583}
6584
Orion Hodson18259d72018-04-12 11:18:23 +01006585void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6586 InvokeRuntimeCallingConvention calling_convention;
6587 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6588 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6589}
6590
6591void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6592 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6593}
6594
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006595void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6596 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006597 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006598 locations->SetInAt(0, Location::RequiresRegister());
6599 if (check->HasUses()) {
6600 locations->SetOut(Location::SameAsFirstInput());
6601 }
6602}
6603
6604void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006605 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006606 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006607 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006608 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006609 GenerateClassInitializationCheck(slow_path,
6610 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006611}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006612
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006613void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006614 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006615 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6616 const size_t status_byte_offset =
6617 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6618 constexpr uint32_t shifted_initialized_value =
6619 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6620
6621 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006622 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006623 __ Bind(slow_path->GetExitLabel());
6624 // No need for memory fence, thanks to the X86 memory model.
6625}
6626
Vladimir Marko175e7862018-03-27 09:03:13 +00006627void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6628 Register temp) {
6629 uint32_t path_to_root = check->GetBitstringPathToRoot();
6630 uint32_t mask = check->GetBitstringMask();
6631 DCHECK(IsPowerOfTwo(mask + 1));
6632 size_t mask_bits = WhichPowerOf2(mask + 1);
6633
6634 if (mask_bits == 16u) {
6635 // Compare the bitstring in memory.
6636 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6637 } else {
6638 // /* uint32_t */ temp = temp->status_
6639 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6640 // Compare the bitstring bits using SUB.
6641 __ subl(temp, Immediate(path_to_root));
6642 // Shift out bits that do not contribute to the comparison.
6643 __ shll(temp, Immediate(32u - mask_bits));
6644 }
6645}
6646
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006647HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6648 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006649 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006650 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006651 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006652 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006653 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006654 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006655 case HLoadString::LoadKind::kJitTableAddress:
6656 DCHECK(Runtime::Current()->UseJitCompilation());
6657 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006658 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006659 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006660 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006661 }
6662 return desired_string_load_kind;
6663}
6664
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006665void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006666 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006667 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006668 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006669 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006670 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006671 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006672 locations->SetInAt(0, Location::RequiresRegister());
6673 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006674 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006675 locations->SetOut(Location::RegisterLocation(EAX));
6676 } else {
6677 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006678 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6679 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006680 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006681 RegisterSet caller_saves = RegisterSet::Empty();
6682 InvokeRuntimeCallingConvention calling_convention;
6683 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6684 locations->SetCustomSlowPathCallerSaves(caller_saves);
6685 } else {
6686 // For non-Baker read barrier we have a temp-clobbering call.
6687 }
6688 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006689 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006690}
6691
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006692Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006693 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006694 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006695 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006696 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006697 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006698 PatchInfo<Label>* info = &jit_string_patches_.back();
6699 return &info->label;
6700}
6701
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006702// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6703// move.
6704void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006705 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006706 Location out_loc = locations->Out();
6707 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006709 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006710 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006711 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006712 Register method_address = locations->InAt(0).AsRegister<Register>();
6713 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006714 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006715 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006716 }
6717 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006718 uint32_t address = dchecked_integral_cast<uint32_t>(
6719 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6720 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006721 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006722 return;
6723 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006724 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006725 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6726 Register method_address = locations->InAt(0).AsRegister<Register>();
6727 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006728 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6729 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006730 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006731 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006732 case HLoadString::LoadKind::kBssEntry: {
6733 Register method_address = locations->InAt(0).AsRegister<Register>();
6734 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6735 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006736 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006737 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006738 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006739 codegen_->AddSlowPath(slow_path);
6740 __ testl(out, out);
6741 __ j(kEqual, slow_path->GetEntryLabel());
6742 __ Bind(slow_path->GetExitLabel());
6743 return;
6744 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006745 case HLoadString::LoadKind::kJitTableAddress: {
6746 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6747 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006748 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006749 // /* GcRoot<mirror::String> */ out = *address
6750 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6751 return;
6752 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006753 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006754 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006755 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006756
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006757 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006758 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006759 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006760 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006761 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6762 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006763}
6764
David Brazdilcb1c0552015-08-04 16:22:25 +01006765static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006766 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006767}
6768
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006769void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6770 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006771 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006772 locations->SetOut(Location::RequiresRegister());
6773}
6774
6775void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006776 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6777}
6778
6779void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006780 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006781}
6782
6783void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6784 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006785}
6786
6787void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006788 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6789 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006790 InvokeRuntimeCallingConvention calling_convention;
6791 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6792}
6793
6794void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006795 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006796 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006797}
6798
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006799// Temp is used for read barrier.
6800static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6801 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006802 !kUseBakerReadBarrier &&
6803 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006804 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006805 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6806 return 1;
6807 }
6808 return 0;
6809}
6810
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006811// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6812// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006813// The other checks have one temp for loading the object's class.
6814static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006815 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006816 return 2;
6817 }
6818 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006819}
6820
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006821void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006822 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006823 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006824 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006825 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006826 case TypeCheckKind::kExactCheck:
6827 case TypeCheckKind::kAbstractClassCheck:
6828 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006829 case TypeCheckKind::kArrayObjectCheck: {
6830 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6831 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6832 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006833 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006834 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006835 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836 case TypeCheckKind::kUnresolvedCheck:
6837 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006838 call_kind = LocationSummary::kCallOnSlowPath;
6839 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006840 case TypeCheckKind::kBitstringCheck:
6841 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006842 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006843
Vladimir Markoca6fff82017-10-03 14:49:14 +01006844 LocationSummary* locations =
6845 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006846 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006847 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006848 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006849 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006850 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6851 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6852 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6853 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6854 } else {
6855 locations->SetInAt(1, Location::Any());
6856 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006857 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6858 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006859 // When read barriers are enabled, we need a temporary register for some cases.
6860 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006861}
6862
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006863void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006864 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006865 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006866 Location obj_loc = locations->InAt(0);
6867 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006868 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006869 Location out_loc = locations->Out();
6870 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6872 DCHECK_LE(num_temps, 1u);
6873 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006874 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006875 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6876 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6877 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006878 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006879 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006880
6881 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006882 // Avoid null check if we know obj is not null.
6883 if (instruction->MustDoNullCheck()) {
6884 __ testl(obj, obj);
6885 __ j(kEqual, &zero);
6886 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006887
Roland Levillain7c1559a2015-12-15 10:55:36 +00006888 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006889 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006890 ReadBarrierOption read_barrier_option =
6891 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006892 // /* HeapReference<Class> */ out = obj->klass_
6893 GenerateReferenceLoadTwoRegisters(instruction,
6894 out_loc,
6895 obj_loc,
6896 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006897 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006898 if (cls.IsRegister()) {
6899 __ cmpl(out, cls.AsRegister<Register>());
6900 } else {
6901 DCHECK(cls.IsStackSlot()) << cls;
6902 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6903 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006904
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006905 // Classes must be equal for the instanceof to succeed.
6906 __ j(kNotEqual, &zero);
6907 __ movl(out, Immediate(1));
6908 __ jmp(&done);
6909 break;
6910 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006911
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006912 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006913 ReadBarrierOption read_barrier_option =
6914 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006915 // /* HeapReference<Class> */ out = obj->klass_
6916 GenerateReferenceLoadTwoRegisters(instruction,
6917 out_loc,
6918 obj_loc,
6919 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006920 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006921 // If the class is abstract, we eagerly fetch the super class of the
6922 // object to avoid doing a comparison we know will fail.
6923 NearLabel loop;
6924 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006925 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006926 GenerateReferenceLoadOneRegister(instruction,
6927 out_loc,
6928 super_offset,
6929 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006930 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006931 __ testl(out, out);
6932 // If `out` is null, we use it for the result, and jump to `done`.
6933 __ j(kEqual, &done);
6934 if (cls.IsRegister()) {
6935 __ cmpl(out, cls.AsRegister<Register>());
6936 } else {
6937 DCHECK(cls.IsStackSlot()) << cls;
6938 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6939 }
6940 __ j(kNotEqual, &loop);
6941 __ movl(out, Immediate(1));
6942 if (zero.IsLinked()) {
6943 __ jmp(&done);
6944 }
6945 break;
6946 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006948 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006949 ReadBarrierOption read_barrier_option =
6950 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006951 // /* HeapReference<Class> */ out = obj->klass_
6952 GenerateReferenceLoadTwoRegisters(instruction,
6953 out_loc,
6954 obj_loc,
6955 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006956 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006957 // Walk over the class hierarchy to find a match.
6958 NearLabel loop, success;
6959 __ Bind(&loop);
6960 if (cls.IsRegister()) {
6961 __ cmpl(out, cls.AsRegister<Register>());
6962 } else {
6963 DCHECK(cls.IsStackSlot()) << cls;
6964 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6965 }
6966 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006967 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006968 GenerateReferenceLoadOneRegister(instruction,
6969 out_loc,
6970 super_offset,
6971 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006972 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006973 __ testl(out, out);
6974 __ j(kNotEqual, &loop);
6975 // If `out` is null, we use it for the result, and jump to `done`.
6976 __ jmp(&done);
6977 __ Bind(&success);
6978 __ movl(out, Immediate(1));
6979 if (zero.IsLinked()) {
6980 __ jmp(&done);
6981 }
6982 break;
6983 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006985 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006986 ReadBarrierOption read_barrier_option =
6987 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006988 // /* HeapReference<Class> */ out = obj->klass_
6989 GenerateReferenceLoadTwoRegisters(instruction,
6990 out_loc,
6991 obj_loc,
6992 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006993 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006994 // Do an exact check.
6995 NearLabel exact_check;
6996 if (cls.IsRegister()) {
6997 __ cmpl(out, cls.AsRegister<Register>());
6998 } else {
6999 DCHECK(cls.IsStackSlot()) << cls;
7000 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7001 }
7002 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007003 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007004 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007005 GenerateReferenceLoadOneRegister(instruction,
7006 out_loc,
7007 component_offset,
7008 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007009 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007010 __ testl(out, out);
7011 // If `out` is null, we use it for the result, and jump to `done`.
7012 __ j(kEqual, &done);
7013 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7014 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007015 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007016 __ movl(out, Immediate(1));
7017 __ jmp(&done);
7018 break;
7019 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007020
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007021 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007022 // No read barrier since the slow path will retry upon failure.
7023 // /* HeapReference<Class> */ out = obj->klass_
7024 GenerateReferenceLoadTwoRegisters(instruction,
7025 out_loc,
7026 obj_loc,
7027 class_offset,
7028 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007029 if (cls.IsRegister()) {
7030 __ cmpl(out, cls.AsRegister<Register>());
7031 } else {
7032 DCHECK(cls.IsStackSlot()) << cls;
7033 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7034 }
7035 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007036 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7037 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007038 codegen_->AddSlowPath(slow_path);
7039 __ j(kNotEqual, slow_path->GetEntryLabel());
7040 __ movl(out, Immediate(1));
7041 if (zero.IsLinked()) {
7042 __ jmp(&done);
7043 }
7044 break;
7045 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007046
Calin Juravle98893e12015-10-02 21:05:03 +01007047 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007048 case TypeCheckKind::kInterfaceCheck: {
7049 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007050 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007051 // cases.
7052 //
7053 // We cannot directly call the InstanceofNonTrivial runtime
7054 // entry point without resorting to a type checking slow path
7055 // here (i.e. by calling InvokeRuntime directly), as it would
7056 // require to assign fixed registers for the inputs of this
7057 // HInstanceOf instruction (following the runtime calling
7058 // convention), which might be cluttered by the potential first
7059 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007060 //
7061 // TODO: Introduce a new runtime entry point taking the object
7062 // to test (instead of its class) as argument, and let it deal
7063 // with the read barrier issues. This will let us refactor this
7064 // case of the `switch` code as it was previously (with a direct
7065 // call to the runtime not using a type checking slow path).
7066 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007067 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007068 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7069 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007070 codegen_->AddSlowPath(slow_path);
7071 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007072 if (zero.IsLinked()) {
7073 __ jmp(&done);
7074 }
7075 break;
7076 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007077
7078 case TypeCheckKind::kBitstringCheck: {
7079 // /* HeapReference<Class> */ temp = obj->klass_
7080 GenerateReferenceLoadTwoRegisters(instruction,
7081 out_loc,
7082 obj_loc,
7083 class_offset,
7084 kWithoutReadBarrier);
7085
7086 GenerateBitstringTypeCheckCompare(instruction, out);
7087 __ j(kNotEqual, &zero);
7088 __ movl(out, Immediate(1));
7089 __ jmp(&done);
7090 break;
7091 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007092 }
7093
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007094 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007095 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007096 __ xorl(out, out);
7097 }
7098
7099 if (done.IsLinked()) {
7100 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007101 }
7102
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007103 if (slow_path != nullptr) {
7104 __ Bind(slow_path->GetExitLabel());
7105 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007106}
7107
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007108void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007109 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007110 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007111 LocationSummary* locations =
7112 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007113 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007114 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7115 // Require a register for the interface check since there is a loop that compares the class to
7116 // a memory address.
7117 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007118 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7119 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7120 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7121 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007122 } else {
7123 locations->SetInAt(1, Location::Any());
7124 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007125 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007126 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7127}
7128
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007129void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007130 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007131 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007132 Location obj_loc = locations->InAt(0);
7133 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007134 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 Location temp_loc = locations->GetTemp(0);
7136 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007137 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7138 DCHECK_GE(num_temps, 1u);
7139 DCHECK_LE(num_temps, 2u);
7140 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7141 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7142 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7143 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7144 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7145 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7146 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7147 const uint32_t object_array_data_offset =
7148 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007149
Vladimir Marko87584542017-12-12 17:47:52 +00007150 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007151 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007152 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7153 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007154 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007155
Roland Levillain0d5a2812015-11-13 10:07:31 +00007156 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007157 // Avoid null check if we know obj is not null.
7158 if (instruction->MustDoNullCheck()) {
7159 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007160 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007161 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007162
Roland Levillain0d5a2812015-11-13 10:07:31 +00007163 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007164 case TypeCheckKind::kExactCheck:
7165 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007166 // /* HeapReference<Class> */ temp = obj->klass_
7167 GenerateReferenceLoadTwoRegisters(instruction,
7168 temp_loc,
7169 obj_loc,
7170 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007171 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007172
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007173 if (cls.IsRegister()) {
7174 __ cmpl(temp, cls.AsRegister<Register>());
7175 } else {
7176 DCHECK(cls.IsStackSlot()) << cls;
7177 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7178 }
7179 // Jump to slow path for throwing the exception or doing a
7180 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007181 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007182 break;
7183 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007184
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007185 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007186 // /* HeapReference<Class> */ temp = obj->klass_
7187 GenerateReferenceLoadTwoRegisters(instruction,
7188 temp_loc,
7189 obj_loc,
7190 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007191 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007192
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007193 // If the class is abstract, we eagerly fetch the super class of the
7194 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007195 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007196 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007197 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007198 GenerateReferenceLoadOneRegister(instruction,
7199 temp_loc,
7200 super_offset,
7201 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007202 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007203
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007204 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7205 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007206 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007207 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007208
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007209 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007210 if (cls.IsRegister()) {
7211 __ cmpl(temp, cls.AsRegister<Register>());
7212 } else {
7213 DCHECK(cls.IsStackSlot()) << cls;
7214 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7215 }
7216 __ j(kNotEqual, &loop);
7217 break;
7218 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007219
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007220 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007221 // /* HeapReference<Class> */ temp = obj->klass_
7222 GenerateReferenceLoadTwoRegisters(instruction,
7223 temp_loc,
7224 obj_loc,
7225 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007226 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007227
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007228 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007229 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007230 __ Bind(&loop);
7231 if (cls.IsRegister()) {
7232 __ cmpl(temp, cls.AsRegister<Register>());
7233 } else {
7234 DCHECK(cls.IsStackSlot()) << cls;
7235 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7236 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007237 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007238
Roland Levillain0d5a2812015-11-13 10:07:31 +00007239 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007240 GenerateReferenceLoadOneRegister(instruction,
7241 temp_loc,
7242 super_offset,
7243 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007244 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007245
7246 // If the class reference currently in `temp` is not null, jump
7247 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007248 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007249 __ j(kNotZero, &loop);
7250 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007251 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007252 break;
7253 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007254
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007255 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007256 // /* HeapReference<Class> */ temp = obj->klass_
7257 GenerateReferenceLoadTwoRegisters(instruction,
7258 temp_loc,
7259 obj_loc,
7260 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007261 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007262
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007263 // Do an exact check.
7264 if (cls.IsRegister()) {
7265 __ cmpl(temp, cls.AsRegister<Register>());
7266 } else {
7267 DCHECK(cls.IsStackSlot()) << cls;
7268 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7269 }
7270 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007271
7272 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007273 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007274 GenerateReferenceLoadOneRegister(instruction,
7275 temp_loc,
7276 component_offset,
7277 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007278 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007279
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007280 // If the component type is null (i.e. the object not an array), jump to the slow path to
7281 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007282 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007283 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007284
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007285 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007286 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007287 break;
7288 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007289
Calin Juravle98893e12015-10-02 21:05:03 +01007290 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007291 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007292 // We cannot directly call the CheckCast runtime entry point
7293 // without resorting to a type checking slow path here (i.e. by
7294 // calling InvokeRuntime directly), as it would require to
7295 // assign fixed registers for the inputs of this HInstanceOf
7296 // instruction (following the runtime calling convention), which
7297 // might be cluttered by the potential first read barrier
7298 // emission at the beginning of this method.
7299 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007301
7302 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007303 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7304 // We can not get false positives by doing this.
7305 // /* HeapReference<Class> */ temp = obj->klass_
7306 GenerateReferenceLoadTwoRegisters(instruction,
7307 temp_loc,
7308 obj_loc,
7309 class_offset,
7310 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007311
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007312 // /* HeapReference<Class> */ temp = temp->iftable_
7313 GenerateReferenceLoadTwoRegisters(instruction,
7314 temp_loc,
7315 temp_loc,
7316 iftable_offset,
7317 kWithoutReadBarrier);
7318 // Iftable is never null.
7319 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7320 // Maybe poison the `cls` for direct comparison with memory.
7321 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7322 // Loop through the iftable and check if any class matches.
7323 NearLabel start_loop;
7324 __ Bind(&start_loop);
7325 // Need to subtract first to handle the empty array case.
7326 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7327 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7328 // Go to next interface if the classes do not match.
7329 __ cmpl(cls.AsRegister<Register>(),
7330 CodeGeneratorX86::ArrayAddress(temp,
7331 maybe_temp2_loc,
7332 TIMES_4,
7333 object_array_data_offset));
7334 __ j(kNotEqual, &start_loop);
7335 // If `cls` was poisoned above, unpoison it.
7336 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007337 break;
7338 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007339
7340 case TypeCheckKind::kBitstringCheck: {
7341 // /* HeapReference<Class> */ temp = obj->klass_
7342 GenerateReferenceLoadTwoRegisters(instruction,
7343 temp_loc,
7344 obj_loc,
7345 class_offset,
7346 kWithoutReadBarrier);
7347
7348 GenerateBitstringTypeCheckCompare(instruction, temp);
7349 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7350 break;
7351 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007352 }
7353 __ Bind(&done);
7354
Roland Levillain0d5a2812015-11-13 10:07:31 +00007355 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007356}
7357
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007358void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007359 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7360 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007361 InvokeRuntimeCallingConvention calling_convention;
7362 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7363}
7364
7365void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007366 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7367 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007368 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007369 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007370 if (instruction->IsEnter()) {
7371 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7372 } else {
7373 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7374 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007375}
7376
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007377void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7378void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7379void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7380
7381void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007383 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007384 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7385 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007386 locations->SetInAt(0, Location::RequiresRegister());
7387 locations->SetInAt(1, Location::Any());
7388 locations->SetOut(Location::SameAsFirstInput());
7389}
7390
7391void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7392 HandleBitwiseOperation(instruction);
7393}
7394
7395void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7396 HandleBitwiseOperation(instruction);
7397}
7398
7399void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7400 HandleBitwiseOperation(instruction);
7401}
7402
7403void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7404 LocationSummary* locations = instruction->GetLocations();
7405 Location first = locations->InAt(0);
7406 Location second = locations->InAt(1);
7407 DCHECK(first.Equals(locations->Out()));
7408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007409 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007410 if (second.IsRegister()) {
7411 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007412 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007413 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007414 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007415 } else {
7416 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007417 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007418 }
7419 } else if (second.IsConstant()) {
7420 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007421 __ andl(first.AsRegister<Register>(),
7422 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007423 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007424 __ orl(first.AsRegister<Register>(),
7425 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007426 } else {
7427 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007428 __ xorl(first.AsRegister<Register>(),
7429 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007430 }
7431 } else {
7432 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007433 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007434 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007435 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007436 } else {
7437 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007438 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007439 }
7440 }
7441 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007442 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007443 if (second.IsRegisterPair()) {
7444 if (instruction->IsAnd()) {
7445 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7446 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7447 } else if (instruction->IsOr()) {
7448 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7449 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7450 } else {
7451 DCHECK(instruction->IsXor());
7452 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7453 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7454 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007455 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007456 if (instruction->IsAnd()) {
7457 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7458 __ andl(first.AsRegisterPairHigh<Register>(),
7459 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7460 } else if (instruction->IsOr()) {
7461 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7462 __ orl(first.AsRegisterPairHigh<Register>(),
7463 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7464 } else {
7465 DCHECK(instruction->IsXor());
7466 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7467 __ xorl(first.AsRegisterPairHigh<Register>(),
7468 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7469 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007470 } else {
7471 DCHECK(second.IsConstant()) << second;
7472 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007473 int32_t low_value = Low32Bits(value);
7474 int32_t high_value = High32Bits(value);
7475 Immediate low(low_value);
7476 Immediate high(high_value);
7477 Register first_low = first.AsRegisterPairLow<Register>();
7478 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007479 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007480 if (low_value == 0) {
7481 __ xorl(first_low, first_low);
7482 } else if (low_value != -1) {
7483 __ andl(first_low, low);
7484 }
7485 if (high_value == 0) {
7486 __ xorl(first_high, first_high);
7487 } else if (high_value != -1) {
7488 __ andl(first_high, high);
7489 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007490 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007491 if (low_value != 0) {
7492 __ orl(first_low, low);
7493 }
7494 if (high_value != 0) {
7495 __ orl(first_high, high);
7496 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007497 } else {
7498 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007499 if (low_value != 0) {
7500 __ xorl(first_low, low);
7501 }
7502 if (high_value != 0) {
7503 __ xorl(first_high, high);
7504 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007505 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007506 }
7507 }
7508}
7509
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007510void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7511 HInstruction* instruction,
7512 Location out,
7513 uint32_t offset,
7514 Location maybe_temp,
7515 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007516 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007517 if (read_barrier_option == kWithReadBarrier) {
7518 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007519 if (kUseBakerReadBarrier) {
7520 // Load with fast path based Baker's read barrier.
7521 // /* HeapReference<Object> */ out = *(out + offset)
7522 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007523 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007524 } else {
7525 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007526 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007527 // in the following move operation, as we will need it for the
7528 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007529 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007530 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007531 // /* HeapReference<Object> */ out = *(out + offset)
7532 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007533 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007534 }
7535 } else {
7536 // Plain load with no read barrier.
7537 // /* HeapReference<Object> */ out = *(out + offset)
7538 __ movl(out_reg, Address(out_reg, offset));
7539 __ MaybeUnpoisonHeapReference(out_reg);
7540 }
7541}
7542
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007543void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7544 HInstruction* instruction,
7545 Location out,
7546 Location obj,
7547 uint32_t offset,
7548 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007549 Register out_reg = out.AsRegister<Register>();
7550 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007551 if (read_barrier_option == kWithReadBarrier) {
7552 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007553 if (kUseBakerReadBarrier) {
7554 // Load with fast path based Baker's read barrier.
7555 // /* HeapReference<Object> */ out = *(obj + offset)
7556 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007557 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007558 } else {
7559 // Load with slow path based read barrier.
7560 // /* HeapReference<Object> */ out = *(obj + offset)
7561 __ movl(out_reg, Address(obj_reg, offset));
7562 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7563 }
7564 } else {
7565 // Plain load with no read barrier.
7566 // /* HeapReference<Object> */ out = *(obj + offset)
7567 __ movl(out_reg, Address(obj_reg, offset));
7568 __ MaybeUnpoisonHeapReference(out_reg);
7569 }
7570}
7571
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007572void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7573 HInstruction* instruction,
7574 Location root,
7575 const Address& address,
7576 Label* fixup_label,
7577 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007578 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007579 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007580 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007581 if (kUseBakerReadBarrier) {
7582 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7583 // Baker's read barrier are used:
7584 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007585 // root = obj.field;
7586 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7587 // if (temp != null) {
7588 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007589 // }
7590
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007591 // /* GcRoot<mirror::Object> */ root = *address
7592 __ movl(root_reg, address);
7593 if (fixup_label != nullptr) {
7594 __ Bind(fixup_label);
7595 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007596 static_assert(
7597 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7598 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7599 "have different sizes.");
7600 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7601 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7602 "have different sizes.");
7603
Vladimir Marko953437b2016-08-24 08:30:46 +00007604 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007605 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007606 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007607 codegen_->AddSlowPath(slow_path);
7608
Roland Levillaind966ce72017-02-09 16:20:14 +00007609 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7610 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007611 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007612 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7613 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007614 __ j(kNotEqual, slow_path->GetEntryLabel());
7615 __ Bind(slow_path->GetExitLabel());
7616 } else {
7617 // GC root loaded through a slow path for read barriers other
7618 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007619 // /* GcRoot<mirror::Object>* */ root = address
7620 __ leal(root_reg, address);
7621 if (fixup_label != nullptr) {
7622 __ Bind(fixup_label);
7623 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007624 // /* mirror::Object* */ root = root->Read()
7625 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7626 }
7627 } else {
7628 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007629 // /* GcRoot<mirror::Object> */ root = *address
7630 __ movl(root_reg, address);
7631 if (fixup_label != nullptr) {
7632 __ Bind(fixup_label);
7633 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007634 // Note that GC roots are not affected by heap poisoning, thus we
7635 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007636 }
7637}
7638
7639void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7640 Location ref,
7641 Register obj,
7642 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007643 bool needs_null_check) {
7644 DCHECK(kEmitCompilerReadBarrier);
7645 DCHECK(kUseBakerReadBarrier);
7646
7647 // /* HeapReference<Object> */ ref = *(obj + offset)
7648 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007649 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007650}
7651
7652void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7653 Location ref,
7654 Register obj,
7655 uint32_t data_offset,
7656 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007657 bool needs_null_check) {
7658 DCHECK(kEmitCompilerReadBarrier);
7659 DCHECK(kUseBakerReadBarrier);
7660
Roland Levillain3d312422016-06-23 13:53:42 +01007661 static_assert(
7662 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7663 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007664 // /* HeapReference<Object> */ ref =
7665 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007666 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007667 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007668}
7669
7670void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7671 Location ref,
7672 Register obj,
7673 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007674 bool needs_null_check,
7675 bool always_update_field,
7676 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007677 DCHECK(kEmitCompilerReadBarrier);
7678 DCHECK(kUseBakerReadBarrier);
7679
7680 // In slow path based read barriers, the read barrier call is
7681 // inserted after the original load. However, in fast path based
7682 // Baker's read barriers, we need to perform the load of
7683 // mirror::Object::monitor_ *before* the original reference load.
7684 // This load-load ordering is required by the read barrier.
7685 // The fast path/slow path (for Baker's algorithm) should look like:
7686 //
7687 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7688 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7689 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007690 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007691 // if (is_gray) {
7692 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7693 // }
7694 //
7695 // Note: the original implementation in ReadBarrier::Barrier is
7696 // slightly more complex as:
7697 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007698 // the high-bits of rb_state, which are expected to be all zeroes
7699 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7700 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007701 // - it performs additional checks that we do not do here for
7702 // performance reasons.
7703
7704 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007705 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7706
Vladimir Marko953437b2016-08-24 08:30:46 +00007707 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007708 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7709 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007710 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7711 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7712 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7713
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007714 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007715 // ref = ReadBarrier::Mark(ref);
7716 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7717 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007718 if (needs_null_check) {
7719 MaybeRecordImplicitNullCheck(instruction);
7720 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007721
7722 // Load fence to prevent load-load reordering.
7723 // Note that this is a no-op, thanks to the x86 memory model.
7724 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7725
7726 // The actual reference load.
7727 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007728 __ movl(ref_reg, src); // Flags are unaffected.
7729
7730 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7731 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007732 SlowPathCode* slow_path;
7733 if (always_update_field) {
7734 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007735 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007736 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7737 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007738 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007739 instruction, ref, /* unpoison_ref_before_marking */ true);
7740 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007741 AddSlowPath(slow_path);
7742
7743 // We have done the "if" of the gray bit check above, now branch based on the flags.
7744 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007745
7746 // Object* ref = ref_addr->AsMirrorPtr()
7747 __ MaybeUnpoisonHeapReference(ref_reg);
7748
Roland Levillain7c1559a2015-12-15 10:55:36 +00007749 __ Bind(slow_path->GetExitLabel());
7750}
7751
7752void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7753 Location out,
7754 Location ref,
7755 Location obj,
7756 uint32_t offset,
7757 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007758 DCHECK(kEmitCompilerReadBarrier);
7759
Roland Levillain7c1559a2015-12-15 10:55:36 +00007760 // Insert a slow path based read barrier *after* the reference load.
7761 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007762 // If heap poisoning is enabled, the unpoisoning of the loaded
7763 // reference will be carried out by the runtime within the slow
7764 // path.
7765 //
7766 // Note that `ref` currently does not get unpoisoned (when heap
7767 // poisoning is enabled), which is alright as the `ref` argument is
7768 // not used by the artReadBarrierSlow entry point.
7769 //
7770 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007771 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007772 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7773 AddSlowPath(slow_path);
7774
Roland Levillain0d5a2812015-11-13 10:07:31 +00007775 __ jmp(slow_path->GetEntryLabel());
7776 __ Bind(slow_path->GetExitLabel());
7777}
7778
Roland Levillain7c1559a2015-12-15 10:55:36 +00007779void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7780 Location out,
7781 Location ref,
7782 Location obj,
7783 uint32_t offset,
7784 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007785 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007786 // Baker's read barriers shall be handled by the fast path
7787 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7788 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007789 // If heap poisoning is enabled, unpoisoning will be taken care of
7790 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007791 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007792 } else if (kPoisonHeapReferences) {
7793 __ UnpoisonHeapReference(out.AsRegister<Register>());
7794 }
7795}
7796
Roland Levillain7c1559a2015-12-15 10:55:36 +00007797void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7798 Location out,
7799 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007800 DCHECK(kEmitCompilerReadBarrier);
7801
Roland Levillain7c1559a2015-12-15 10:55:36 +00007802 // Insert a slow path based read barrier *after* the GC root load.
7803 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007804 // Note that GC roots are not affected by heap poisoning, so we do
7805 // not need to do anything special for this here.
7806 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007807 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007808 AddSlowPath(slow_path);
7809
Roland Levillain0d5a2812015-11-13 10:07:31 +00007810 __ jmp(slow_path->GetEntryLabel());
7811 __ Bind(slow_path->GetExitLabel());
7812}
7813
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007814void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007815 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007816 LOG(FATAL) << "Unreachable";
7817}
7818
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007819void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007820 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007821 LOG(FATAL) << "Unreachable";
7822}
7823
Mark Mendellfe57faa2015-09-18 09:26:15 -04007824// Simple implementation of packed switch - generate cascaded compare/jumps.
7825void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7826 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007827 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007828 locations->SetInAt(0, Location::RequiresRegister());
7829}
7830
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007831void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7832 int32_t lower_bound,
7833 uint32_t num_entries,
7834 HBasicBlock* switch_block,
7835 HBasicBlock* default_block) {
7836 // Figure out the correct compare values and jump conditions.
7837 // Handle the first compare/branch as a special case because it might
7838 // jump to the default case.
7839 DCHECK_GT(num_entries, 2u);
7840 Condition first_condition;
7841 uint32_t index;
7842 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7843 if (lower_bound != 0) {
7844 first_condition = kLess;
7845 __ cmpl(value_reg, Immediate(lower_bound));
7846 __ j(first_condition, codegen_->GetLabelOf(default_block));
7847 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007848
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007849 index = 1;
7850 } else {
7851 // Handle all the compare/jumps below.
7852 first_condition = kBelow;
7853 index = 0;
7854 }
7855
7856 // Handle the rest of the compare/jumps.
7857 for (; index + 1 < num_entries; index += 2) {
7858 int32_t compare_to_value = lower_bound + index + 1;
7859 __ cmpl(value_reg, Immediate(compare_to_value));
7860 // Jump to successors[index] if value < case_value[index].
7861 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7862 // Jump to successors[index + 1] if value == case_value[index + 1].
7863 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7864 }
7865
7866 if (index != num_entries) {
7867 // There are an odd number of entries. Handle the last one.
7868 DCHECK_EQ(index + 1, num_entries);
7869 __ cmpl(value_reg, Immediate(lower_bound + index));
7870 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007871 }
7872
7873 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007874 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7875 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007876 }
7877}
7878
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007879void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7880 int32_t lower_bound = switch_instr->GetStartValue();
7881 uint32_t num_entries = switch_instr->GetNumEntries();
7882 LocationSummary* locations = switch_instr->GetLocations();
7883 Register value_reg = locations->InAt(0).AsRegister<Register>();
7884
7885 GenPackedSwitchWithCompares(value_reg,
7886 lower_bound,
7887 num_entries,
7888 switch_instr->GetBlock(),
7889 switch_instr->GetDefaultBlock());
7890}
7891
Mark Mendell805b3b52015-09-18 14:10:29 -04007892void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7893 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007894 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007895 locations->SetInAt(0, Location::RequiresRegister());
7896
7897 // Constant area pointer.
7898 locations->SetInAt(1, Location::RequiresRegister());
7899
7900 // And the temporary we need.
7901 locations->AddTemp(Location::RequiresRegister());
7902}
7903
7904void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7905 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007906 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007907 LocationSummary* locations = switch_instr->GetLocations();
7908 Register value_reg = locations->InAt(0).AsRegister<Register>();
7909 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7910
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007911 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7912 GenPackedSwitchWithCompares(value_reg,
7913 lower_bound,
7914 num_entries,
7915 switch_instr->GetBlock(),
7916 default_block);
7917 return;
7918 }
7919
Mark Mendell805b3b52015-09-18 14:10:29 -04007920 // Optimizing has a jump area.
7921 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7922 Register constant_area = locations->InAt(1).AsRegister<Register>();
7923
7924 // Remove the bias, if needed.
7925 if (lower_bound != 0) {
7926 __ leal(temp_reg, Address(value_reg, -lower_bound));
7927 value_reg = temp_reg;
7928 }
7929
7930 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007931 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007932 __ cmpl(value_reg, Immediate(num_entries - 1));
7933 __ j(kAbove, codegen_->GetLabelOf(default_block));
7934
7935 // We are in the range of the table.
7936 // Load (target-constant_area) from the jump table, indexing by the value.
7937 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7938
7939 // Compute the actual target address by adding in constant_area.
7940 __ addl(temp_reg, constant_area);
7941
7942 // And jump.
7943 __ jmp(temp_reg);
7944}
7945
Mark Mendell0616ae02015-04-17 12:49:27 -04007946void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7947 HX86ComputeBaseMethodAddress* insn) {
7948 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007949 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007950 locations->SetOut(Location::RequiresRegister());
7951}
7952
7953void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7954 HX86ComputeBaseMethodAddress* insn) {
7955 LocationSummary* locations = insn->GetLocations();
7956 Register reg = locations->Out().AsRegister<Register>();
7957
7958 // Generate call to next instruction.
7959 Label next_instruction;
7960 __ call(&next_instruction);
7961 __ Bind(&next_instruction);
7962
7963 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007964 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007965
7966 // Grab the return address off the stack.
7967 __ popl(reg);
7968}
7969
7970void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7971 HX86LoadFromConstantTable* insn) {
7972 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007973 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007974
7975 locations->SetInAt(0, Location::RequiresRegister());
7976 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7977
7978 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007979 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007980 return;
7981 }
7982
7983 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007984 case DataType::Type::kFloat32:
7985 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007986 locations->SetOut(Location::RequiresFpuRegister());
7987 break;
7988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007989 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007990 locations->SetOut(Location::RequiresRegister());
7991 break;
7992
7993 default:
7994 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7995 }
7996}
7997
7998void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007999 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008000 return;
8001 }
8002
8003 LocationSummary* locations = insn->GetLocations();
8004 Location out = locations->Out();
8005 Register const_area = locations->InAt(0).AsRegister<Register>();
8006 HConstant *value = insn->GetConstant();
8007
8008 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008009 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008010 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008011 codegen_->LiteralFloatAddress(
8012 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008013 break;
8014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008015 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008016 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008017 codegen_->LiteralDoubleAddress(
8018 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008019 break;
8020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008021 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008022 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008023 codegen_->LiteralInt32Address(
8024 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008025 break;
8026
8027 default:
8028 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8029 }
8030}
8031
Mark Mendell0616ae02015-04-17 12:49:27 -04008032/**
8033 * Class to handle late fixup of offsets into constant area.
8034 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008035class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008036 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008037 RIPFixup(CodeGeneratorX86& codegen,
8038 HX86ComputeBaseMethodAddress* base_method_address,
8039 size_t offset)
8040 : codegen_(&codegen),
8041 base_method_address_(base_method_address),
8042 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008043
8044 protected:
8045 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8046
8047 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008048 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008049
8050 private:
8051 void Process(const MemoryRegion& region, int pos) OVERRIDE {
8052 // Patch the correct offset for the instruction. The place to patch is the
8053 // last 4 bytes of the instruction.
8054 // The value to patch is the distance from the offset in the constant area
8055 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008056 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008057 int32_t relative_position =
8058 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008059
8060 // Patch in the right value.
8061 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8062 }
8063
Mark Mendell0616ae02015-04-17 12:49:27 -04008064 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008065 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008066};
8067
Mark Mendell805b3b52015-09-18 14:10:29 -04008068/**
8069 * Class to handle late fixup of offsets to a jump table that will be created in the
8070 * constant area.
8071 */
8072class JumpTableRIPFixup : public RIPFixup {
8073 public:
8074 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008075 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8076 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008077
8078 void CreateJumpTable() {
8079 X86Assembler* assembler = codegen_->GetAssembler();
8080
8081 // Ensure that the reference to the jump table has the correct offset.
8082 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8083 SetOffset(offset_in_constant_table);
8084
8085 // The label values in the jump table are computed relative to the
8086 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008087 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008088
8089 // Populate the jump table with the correct values for the jump table.
8090 int32_t num_entries = switch_instr_->GetNumEntries();
8091 HBasicBlock* block = switch_instr_->GetBlock();
8092 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8093 // The value that we want is the target offset - the position of the table.
8094 for (int32_t i = 0; i < num_entries; i++) {
8095 HBasicBlock* b = successors[i];
8096 Label* l = codegen_->GetLabelOf(b);
8097 DCHECK(l->IsBound());
8098 int32_t offset_to_block = l->Position() - relative_offset;
8099 assembler->AppendInt32(offset_to_block);
8100 }
8101 }
8102
8103 private:
8104 const HX86PackedSwitch* switch_instr_;
8105};
8106
8107void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8108 // Generate the constant area if needed.
8109 X86Assembler* assembler = GetAssembler();
8110 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8111 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8112 // byte values.
8113 assembler->Align(4, 0);
8114 constant_area_start_ = assembler->CodeSize();
8115
8116 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008117 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008118 jump_table->CreateJumpTable();
8119 }
8120
8121 // And now add the constant area to the generated code.
8122 assembler->AddConstantArea();
8123 }
8124
8125 // And finish up.
8126 CodeGenerator::Finalize(allocator);
8127}
8128
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008129Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8130 HX86ComputeBaseMethodAddress* method_base,
8131 Register reg) {
8132 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008133 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008134 return Address(reg, kDummy32BitOffset, fixup);
8135}
8136
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008137Address CodeGeneratorX86::LiteralFloatAddress(float v,
8138 HX86ComputeBaseMethodAddress* method_base,
8139 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008140 AssemblerFixup* fixup =
8141 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008142 return Address(reg, kDummy32BitOffset, fixup);
8143}
8144
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008145Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8146 HX86ComputeBaseMethodAddress* method_base,
8147 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008148 AssemblerFixup* fixup =
8149 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008150 return Address(reg, kDummy32BitOffset, fixup);
8151}
8152
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008153Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8154 HX86ComputeBaseMethodAddress* method_base,
8155 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008156 AssemblerFixup* fixup =
8157 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008158 return Address(reg, kDummy32BitOffset, fixup);
8159}
8160
Aart Bika19616e2016-02-01 18:57:58 -08008161void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8162 if (value == 0) {
8163 __ xorl(dest, dest);
8164 } else {
8165 __ movl(dest, Immediate(value));
8166 }
8167}
8168
8169void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8170 if (value == 0) {
8171 __ testl(dest, dest);
8172 } else {
8173 __ cmpl(dest, Immediate(value));
8174 }
8175}
8176
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008177void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8178 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008179 GenerateIntCompare(lhs_reg, rhs);
8180}
8181
8182void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008183 if (rhs.IsConstant()) {
8184 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008185 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008186 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008187 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008188 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008189 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008190 }
8191}
8192
8193Address CodeGeneratorX86::ArrayAddress(Register obj,
8194 Location index,
8195 ScaleFactor scale,
8196 uint32_t data_offset) {
8197 return index.IsConstant() ?
8198 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8199 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8200}
8201
Mark Mendell805b3b52015-09-18 14:10:29 -04008202Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8203 Register reg,
8204 Register value) {
8205 // Create a fixup to be used to create and address the jump table.
8206 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008207 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008208
8209 // We have to populate the jump tables.
8210 fixups_to_jump_tables_.push_back(table_fixup);
8211
8212 // We want a scaled address, as we are extracting the correct offset from the table.
8213 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8214}
8215
Andreas Gampe85b62f22015-09-09 13:15:38 -07008216// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008217void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008218 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008219 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008220 return;
8221 }
8222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008223 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008224
8225 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8226 if (target.Equals(return_loc)) {
8227 return;
8228 }
8229
8230 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8231 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008232 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008233 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008234 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8235 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008236 GetMoveResolver()->EmitNativeCode(&parallel_move);
8237 } else {
8238 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008239 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008240 parallel_move.AddMove(return_loc, target, type, nullptr);
8241 GetMoveResolver()->EmitNativeCode(&parallel_move);
8242 }
8243}
8244
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008245void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8246 const uint8_t* roots_data,
8247 const PatchInfo<Label>& info,
8248 uint64_t index_in_table) const {
8249 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8250 uintptr_t address =
8251 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8252 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8253 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8254 dchecked_integral_cast<uint32_t>(address);
8255}
8256
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008257void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8258 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008259 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008260 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008261 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008262 }
8263
8264 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008265 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008266 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008267 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008268 }
8269}
8270
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008271void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8272 ATTRIBUTE_UNUSED) {
8273 LOG(FATAL) << "Unreachable";
8274}
8275
8276void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8277 ATTRIBUTE_UNUSED) {
8278 LOG(FATAL) << "Unreachable";
8279}
8280
Roland Levillain4d027112015-07-01 15:41:14 +01008281#undef __
8282
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008283} // namespace x86
8284} // namespace art