blob: d189476a4802e49eb986b6bf14e0b9a94066db82 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "intrinsics.h"
29#include "intrinsics_x86.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070032#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010034#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010038#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041
Roland Levillain0d5a2812015-11-13 10:07:31 +000042template<class MirrorType>
43class GcRoot;
44
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000045namespace x86 {
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050049static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Mark Mendell24f2dfa2015-01-14 19:51:45 -050051static constexpr int kC2ConditionMask = 0x400;
52
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000053static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000054
Aart Bik1f8d51b2018-02-15 10:42:37 -080055static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
56static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
57
Roland Levillain7cbd27f2016-08-11 23:53:33 +010058// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
59#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000064 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames2ed20af2015-03-06 13:55:35 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010067 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010073 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010074 instruction_,
75 instruction_->GetDexPc(),
76 this);
Roland Levillain888d0672015-11-23 18:53:50 +000077 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 }
79
Alexandre Rames8158f282015-08-07 10:26:17 +010080 bool IsFatal() const OVERRIDE { return true; }
81
Alexandre Rames9931f312015-06-19 14:47:01 +010082 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
83
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Andreas Gampe85b62f22015-09-09 13:15:38 -070088class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000089 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000090 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000091
Alexandre Rames2ed20af2015-03-06 13:55:35 +000092 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010093 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000094 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010095 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000109 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
110 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
125 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Andreas Gampe85b62f22015-09-09 13:15:38 -0700130class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000132 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100135 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000140 if (instruction_->CanThrowIntoCatchBlock()) {
141 // Live registers will be restored in the catch block if caught.
142 SaveLiveRegisters(codegen, instruction_->GetLocations());
143 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400144
145 // Are we using an array length from memory?
146 HInstruction* array_length = instruction_->InputAt(1);
147 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400149 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
150 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100151 HArrayLength* length = array_length->AsArrayLength();
152 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400153 Location array_loc = array_length->GetLocations()->InAt(0);
154 Address array_len(array_loc.AsRegister<Register>(), len_offset);
155 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
156 // Check for conflicts with index.
157 if (length_loc.Equals(locations->InAt(0))) {
158 // We know we aren't using parameter 2.
159 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
160 }
161 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100162 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100163 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700164 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000166 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100167 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100169 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400170 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100172 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100173 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? kQuickThrowStringBounds
175 : kQuickThrowArrayBounds;
176 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100177 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000178 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 }
180
Alexandre Rames8158f282015-08-07 10:26:17 +0100181 bool IsFatal() const OVERRIDE { return true; }
182
Alexandre Rames9931f312015-06-19 14:47:01 +0100183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
187};
188
Andreas Gampe85b62f22015-09-09 13:15:38 -0700189class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000191 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700195 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100196 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000197 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700198 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100199 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000200 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700201 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 if (successor_ == nullptr) {
203 __ jmp(GetReturnLabel());
204 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100205 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 }
208
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 Label* GetReturnLabel() {
210 DCHECK(successor_ == nullptr);
211 return &return_label_;
212 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100214 HBasicBlock* GetSuccessor() const {
215 return successor_;
216 }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
219
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000220 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000222 Label return_label_;
223
224 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
225};
226
Vladimir Markoaad75c62016-10-03 08:46:48 +0000227class LoadStringSlowPathX86 : public SlowPathCode {
228 public:
229 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 LocationSummary* locations = instruction_->GetLocations();
233 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
234
235 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
236 __ Bind(GetEntryLabel());
237 SaveLiveRegisters(codegen, locations);
238
239 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
241 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000242 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
243 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
244 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
245 RestoreLiveRegisters(codegen, locations);
246
Vladimir Markoaad75c62016-10-03 08:46:48 +0000247 __ jmp(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
251
252 private:
253 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
254};
255
Andreas Gampe85b62f22015-09-09 13:15:38 -0700256class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257 public:
258 LoadClassSlowPathX86(HLoadClass* cls,
259 HInstruction* at,
260 uint32_t dex_pc,
261 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
264 }
265
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000266 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000267 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
269 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000270 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271
272 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000273 dex::TypeIndex type_index = cls_->GetTypeIndex();
274 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100275 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
276 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 instruction_,
278 dex_pc_,
279 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 if (do_clinit_) {
281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
282 } else {
283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
284 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000292 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000293 __ jmp(GetExitLabel());
294 }
295
Alexandre Rames9931f312015-06-19 14:47:01 +0100296 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
297
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000298 private:
299 // The class this slow path will load.
300 HLoadClass* const cls_;
301
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000302 // The dex PC of `at_`.
303 const uint32_t dex_pc_;
304
305 // Whether to initialize the class.
306 const bool do_clinit_;
307
308 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
309};
310
Andreas Gampe85b62f22015-09-09 13:15:38 -0700311class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000314 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000316 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 DCHECK(instruction_->IsCheckCast()
319 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
322 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000323
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000324 if (kPoisonHeapReferences &&
325 instruction_->IsCheckCast() &&
326 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
327 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
328 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
329 }
330
Vladimir Marko87584542017-12-12 17:47:52 +0000331 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 SaveLiveRegisters(codegen, locations);
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 // We're moving two locations to locations that could overlap, so we need a parallel
336 // move resolver.
337 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800338 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800339 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100340 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800341 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800342 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100343 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100345 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100346 instruction_,
347 instruction_->GetDexPc(),
348 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800349 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000350 } else {
351 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800352 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
353 instruction_,
354 instruction_->GetDexPc(),
355 this);
356 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000357 }
358
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 if (!is_fatal_) {
360 if (instruction_->IsInstanceOf()) {
361 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
362 }
363 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 __ jmp(GetExitLabel());
366 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000367 }
368
Alexandre Rames9931f312015-06-19 14:47:01 +0100369 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100371
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000374
375 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
376};
377
Andreas Gampe85b62f22015-09-09 13:15:38 -0700378class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 public:
Aart Bik42249c32016-01-07 15:33:50 -0800380 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000381 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382
383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100384 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100386 LocationSummary* locations = instruction_->GetLocations();
387 SaveLiveRegisters(codegen, locations);
388 InvokeRuntimeCallingConvention calling_convention;
389 x86_codegen->Load32BitValue(
390 calling_convention.GetRegisterAt(0),
391 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100392 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100393 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
397
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
400};
401
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402class ArraySetSlowPathX86 : public SlowPathCode {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405
406 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
407 LocationSummary* locations = instruction_->GetLocations();
408 __ Bind(GetEntryLabel());
409 SaveLiveRegisters(codegen, locations);
410
411 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100412 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413 parallel_move.AddMove(
414 locations->InAt(0),
415 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100416 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417 nullptr);
418 parallel_move.AddMove(
419 locations->InAt(1),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100421 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(2),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100426 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 nullptr);
428 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
429
430 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100431 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 RestoreLiveRegisters(codegen, locations);
434 __ jmp(GetExitLabel());
435 }
436
437 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
438
439 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100440 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
441};
442
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100443// Slow path marking an object reference `ref` during a read
444// barrier. The field `obj.field` in the object `obj` holding this
445// reference does not get updated by this slow path after marking (see
446// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
447//
448// This means that after the execution of this slow path, `ref` will
449// always be up-to-date, but `obj.field` may not; i.e., after the
450// flip, `ref` will be a to-space reference, but `obj.field` will
451// probably still be a from-space reference (unless it gets updated by
452// another thread, or if another thread installed another object
453// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
455 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100456 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
457 Location ref,
458 bool unpoison_ref_before_marking)
459 : SlowPathCode(instruction),
460 ref_(ref),
461 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(kEmitCompilerReadBarrier);
463 }
464
465 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100469 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000470 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100471 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000472 DCHECK(instruction_->IsInstanceFieldGet() ||
473 instruction_->IsStaticFieldGet() ||
474 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100475 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000476 instruction_->IsLoadClass() ||
477 instruction_->IsLoadString() ||
478 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100479 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100480 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
481 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 << "Unexpected instruction in read barrier marking slow path: "
483 << instruction_->DebugName();
484
485 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000487 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100488 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000489 }
Roland Levillain4359e612016-07-20 11:32:19 +0100490 // No need to save live registers; it's taken care of by the
491 // entrypoint. Also, there is no need to update the stack mask,
492 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100494 DCHECK_NE(ref_reg, ESP);
495 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100496 // "Compact" slow path, saving two moves.
497 //
498 // Instead of using the standard runtime calling convention (input
499 // and output in EAX):
500 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100501 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100502 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100504 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100506 // of a dedicated entrypoint:
507 //
508 // rX <- ReadBarrierMarkRegX(rX)
509 //
Roland Levillain97c46462017-05-11 14:04:03 +0100510 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100511 // This runtime call does not require a stack map.
512 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000513 __ jmp(GetExitLabel());
514 }
515
516 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // The location (register) of the marked object reference.
518 const Location ref_;
519 // Should the reference in `ref_` be unpoisoned prior to marking it?
520 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000521
522 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
523};
524
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100525// Slow path marking an object reference `ref` during a read barrier,
526// and if needed, atomically updating the field `obj.field` in the
527// object `obj` holding this reference after marking (contrary to
528// ReadBarrierMarkSlowPathX86 above, which never tries to update
529// `obj.field`).
530//
531// This means that after the execution of this slow path, both `ref`
532// and `obj.field` will be up-to-date; i.e., after the flip, both will
533// hold the same to-space reference (unless another thread installed
534// another object reference (different from `ref`) in `obj.field`).
535class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
536 public:
537 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
538 Location ref,
539 Register obj,
540 const Address& field_addr,
541 bool unpoison_ref_before_marking,
542 Register temp)
543 : SlowPathCode(instruction),
544 ref_(ref),
545 obj_(obj),
546 field_addr_(field_addr),
547 unpoison_ref_before_marking_(unpoison_ref_before_marking),
548 temp_(temp) {
549 DCHECK(kEmitCompilerReadBarrier);
550 }
551
552 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
553
554 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
555 LocationSummary* locations = instruction_->GetLocations();
556 Register ref_reg = ref_.AsRegister<Register>();
557 DCHECK(locations->CanCall());
558 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
559 // This slow path is only used by the UnsafeCASObject intrinsic.
560 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
561 << "Unexpected instruction in read barrier marking and field updating slow path: "
562 << instruction_->DebugName();
563 DCHECK(instruction_->GetLocations()->Intrinsified());
564 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
565
566 __ Bind(GetEntryLabel());
567 if (unpoison_ref_before_marking_) {
568 // Object* ref = ref_addr->AsMirrorPtr()
569 __ MaybeUnpoisonHeapReference(ref_reg);
570 }
571
572 // Save the old (unpoisoned) reference.
573 __ movl(temp_, ref_reg);
574
575 // No need to save live registers; it's taken care of by the
576 // entrypoint. Also, there is no need to update the stack mask,
577 // as this runtime call will not trigger a garbage collection.
578 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
579 DCHECK_NE(ref_reg, ESP);
580 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
581 // "Compact" slow path, saving two moves.
582 //
583 // Instead of using the standard runtime calling convention (input
584 // and output in EAX):
585 //
586 // EAX <- ref
587 // EAX <- ReadBarrierMark(EAX)
588 // ref <- EAX
589 //
590 // we just use rX (the register containing `ref`) as input and output
591 // of a dedicated entrypoint:
592 //
593 // rX <- ReadBarrierMarkRegX(rX)
594 //
Roland Levillain97c46462017-05-11 14:04:03 +0100595 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100596 // This runtime call does not require a stack map.
597 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
598
599 // If the new reference is different from the old reference,
600 // update the field in the holder (`*field_addr`).
601 //
602 // Note that this field could also hold a different object, if
603 // another thread had concurrently changed it. In that case, the
604 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
605 // operation below would abort the CAS, leaving the field as-is.
606 NearLabel done;
607 __ cmpl(temp_, ref_reg);
608 __ j(kEqual, &done);
609
610 // Update the the holder's field atomically. This may fail if
611 // mutator updates before us, but it's OK. This is achieved
612 // using a strong compare-and-set (CAS) operation with relaxed
613 // memory synchronization ordering, where the expected value is
614 // the old reference and the desired value is the new reference.
615 // This operation is implemented with a 32-bit LOCK CMPXLCHG
616 // instruction, which requires the expected value (the old
617 // reference) to be in EAX. Save EAX beforehand, and move the
618 // expected value (stored in `temp_`) into EAX.
619 __ pushl(EAX);
620 __ movl(EAX, temp_);
621
622 // Convenience aliases.
623 Register base = obj_;
624 Register expected = EAX;
625 Register value = ref_reg;
626
627 bool base_equals_value = (base == value);
628 if (kPoisonHeapReferences) {
629 if (base_equals_value) {
630 // If `base` and `value` are the same register location, move
631 // `value` to a temporary register. This way, poisoning
632 // `value` won't invalidate `base`.
633 value = temp_;
634 __ movl(value, base);
635 }
636
637 // Check that the register allocator did not assign the location
638 // of `expected` (EAX) to `value` nor to `base`, so that heap
639 // poisoning (when enabled) works as intended below.
640 // - If `value` were equal to `expected`, both references would
641 // be poisoned twice, meaning they would not be poisoned at
642 // all, as heap poisoning uses address negation.
643 // - If `base` were equal to `expected`, poisoning `expected`
644 // would invalidate `base`.
645 DCHECK_NE(value, expected);
646 DCHECK_NE(base, expected);
647
648 __ PoisonHeapReference(expected);
649 __ PoisonHeapReference(value);
650 }
651
652 __ LockCmpxchgl(field_addr_, value);
653
654 // If heap poisoning is enabled, we need to unpoison the values
655 // that were poisoned earlier.
656 if (kPoisonHeapReferences) {
657 if (base_equals_value) {
658 // `value` has been moved to a temporary register, no need
659 // to unpoison it.
660 } else {
661 __ UnpoisonHeapReference(value);
662 }
663 // No need to unpoison `expected` (EAX), as it is be overwritten below.
664 }
665
666 // Restore EAX.
667 __ popl(EAX);
668
669 __ Bind(&done);
670 __ jmp(GetExitLabel());
671 }
672
673 private:
674 // The location (register) of the marked object reference.
675 const Location ref_;
676 // The register containing the object holding the marked object reference field.
677 const Register obj_;
678 // The address of the marked reference field. The base of this address must be `obj_`.
679 const Address field_addr_;
680
681 // Should the reference in `ref_` be unpoisoned prior to marking it?
682 const bool unpoison_ref_before_marking_;
683
684 const Register temp_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
687};
688
Roland Levillain0d5a2812015-11-13 10:07:31 +0000689// Slow path generating a read barrier for a heap reference.
690class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
691 public:
692 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
693 Location out,
694 Location ref,
695 Location obj,
696 uint32_t offset,
697 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000698 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000699 out_(out),
700 ref_(ref),
701 obj_(obj),
702 offset_(offset),
703 index_(index) {
704 DCHECK(kEmitCompilerReadBarrier);
705 // If `obj` is equal to `out` or `ref`, it means the initial object
706 // has been overwritten by (or after) the heap object reference load
707 // to be instrumented, e.g.:
708 //
709 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000710 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711 //
712 // In that case, we have lost the information about the original
713 // object, and the emitted read barrier cannot work properly.
714 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
715 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
720 LocationSummary* locations = instruction_->GetLocations();
721 Register reg_out = out_.AsRegister<Register>();
722 DCHECK(locations->CanCall());
723 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100724 DCHECK(instruction_->IsInstanceFieldGet() ||
725 instruction_->IsStaticFieldGet() ||
726 instruction_->IsArrayGet() ||
727 instruction_->IsInstanceOf() ||
728 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700729 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000730 << "Unexpected instruction in read barrier for heap reference slow path: "
731 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000732
733 __ Bind(GetEntryLabel());
734 SaveLiveRegisters(codegen, locations);
735
736 // We may have to change the index's value, but as `index_` is a
737 // constant member (like other "inputs" of this slow path),
738 // introduce a copy of it, `index`.
739 Location index = index_;
740 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100741 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000742 if (instruction_->IsArrayGet()) {
743 // Compute the actual memory offset and store it in `index`.
744 Register index_reg = index_.AsRegister<Register>();
745 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
746 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
747 // We are about to change the value of `index_reg` (see the
748 // calls to art::x86::X86Assembler::shll and
749 // art::x86::X86Assembler::AddImmediate below), but it has
750 // not been saved by the previous call to
751 // art::SlowPathCode::SaveLiveRegisters, as it is a
752 // callee-save register --
753 // art::SlowPathCode::SaveLiveRegisters does not consider
754 // callee-save registers, as it has been designed with the
755 // assumption that callee-save registers are supposed to be
756 // handled by the called function. So, as a callee-save
757 // register, `index_reg` _would_ eventually be saved onto
758 // the stack, but it would be too late: we would have
759 // changed its value earlier. Therefore, we manually save
760 // it here into another freely available register,
761 // `free_reg`, chosen of course among the caller-save
762 // registers (as a callee-save `free_reg` register would
763 // exhibit the same problem).
764 //
765 // Note we could have requested a temporary register from
766 // the register allocator instead; but we prefer not to, as
767 // this is a slow path, and we know we can find a
768 // caller-save register that is available.
769 Register free_reg = FindAvailableCallerSaveRegister(codegen);
770 __ movl(free_reg, index_reg);
771 index_reg = free_reg;
772 index = Location::RegisterLocation(index_reg);
773 } else {
774 // The initial register stored in `index_` has already been
775 // saved in the call to art::SlowPathCode::SaveLiveRegisters
776 // (as it is not a callee-save register), so we can freely
777 // use it.
778 }
779 // Shifting the index value contained in `index_reg` by the scale
780 // factor (2) cannot overflow in practice, as the runtime is
781 // unable to allocate object arrays with a size larger than
782 // 2^26 - 1 (that is, 2^28 - 4 bytes).
783 __ shll(index_reg, Immediate(TIMES_4));
784 static_assert(
785 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
786 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
787 __ AddImmediate(index_reg, Immediate(offset_));
788 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100789 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
790 // intrinsics, `index_` is not shifted by a scale factor of 2
791 // (as in the case of ArrayGet), as it is actually an offset
792 // to an object field within an object.
793 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000794 DCHECK(instruction_->GetLocations()->Intrinsified());
795 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
796 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
797 << instruction_->AsInvoke()->GetIntrinsic();
798 DCHECK_EQ(offset_, 0U);
799 DCHECK(index_.IsRegisterPair());
800 // UnsafeGet's offset location is a register pair, the low
801 // part contains the correct offset.
802 index = index_.ToLow();
803 }
804 }
805
806 // We're moving two or three locations to locations that could
807 // overlap, so we need a parallel move resolver.
808 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100809 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000810 parallel_move.AddMove(ref_,
811 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100812 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000813 nullptr);
814 parallel_move.AddMove(obj_,
815 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100816 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000817 nullptr);
818 if (index.IsValid()) {
819 parallel_move.AddMove(index,
820 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100821 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 nullptr);
823 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
824 } else {
825 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
826 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
827 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100828 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 CheckEntrypointTypes<
830 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
831 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
832
833 RestoreLiveRegisters(codegen, locations);
834 __ jmp(GetExitLabel());
835 }
836
837 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
838
839 private:
840 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
841 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
842 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
843 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
844 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
845 return static_cast<Register>(i);
846 }
847 }
848 // We shall never fail to find a free caller-save register, as
849 // there are more than two core caller-save registers on x86
850 // (meaning it is possible to find one which is different from
851 // `ref` and `obj`).
852 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
853 LOG(FATAL) << "Could not find a free caller-save register";
854 UNREACHABLE();
855 }
856
Roland Levillain0d5a2812015-11-13 10:07:31 +0000857 const Location out_;
858 const Location ref_;
859 const Location obj_;
860 const uint32_t offset_;
861 // An additional location containing an index to an array.
862 // Only used for HArrayGet and the UnsafeGetObject &
863 // UnsafeGetObjectVolatile intrinsics.
864 const Location index_;
865
866 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
867};
868
869// Slow path generating a read barrier for a GC root.
870class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
871 public:
872 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000873 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000874 DCHECK(kEmitCompilerReadBarrier);
875 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000876
877 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
878 LocationSummary* locations = instruction_->GetLocations();
879 Register reg_out = out_.AsRegister<Register>();
880 DCHECK(locations->CanCall());
881 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000882 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
883 << "Unexpected instruction in read barrier for GC root slow path: "
884 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000885
886 __ Bind(GetEntryLabel());
887 SaveLiveRegisters(codegen, locations);
888
889 InvokeRuntimeCallingConvention calling_convention;
890 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
891 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100892 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000893 instruction_,
894 instruction_->GetDexPc(),
895 this);
896 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
897 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
898
899 RestoreLiveRegisters(codegen, locations);
900 __ jmp(GetExitLabel());
901 }
902
903 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
904
905 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000906 const Location out_;
907 const Location root_;
908
909 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
910};
911
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100912#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100913// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
914#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100915
Aart Bike9f37602015-10-09 11:15:55 -0700916inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700917 switch (cond) {
918 case kCondEQ: return kEqual;
919 case kCondNE: return kNotEqual;
920 case kCondLT: return kLess;
921 case kCondLE: return kLessEqual;
922 case kCondGT: return kGreater;
923 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700924 case kCondB: return kBelow;
925 case kCondBE: return kBelowEqual;
926 case kCondA: return kAbove;
927 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700928 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100929 LOG(FATAL) << "Unreachable";
930 UNREACHABLE();
931}
932
Aart Bike9f37602015-10-09 11:15:55 -0700933// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100934inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
935 switch (cond) {
936 case kCondEQ: return kEqual;
937 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700938 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100939 case kCondLT: return kBelow;
940 case kCondLE: return kBelowEqual;
941 case kCondGT: return kAbove;
942 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700943 // Unsigned remain unchanged.
944 case kCondB: return kBelow;
945 case kCondBE: return kBelowEqual;
946 case kCondA: return kAbove;
947 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 }
949 LOG(FATAL) << "Unreachable";
950 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700951}
952
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100953void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100954 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100955}
956
957void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959}
960
Vladimir Markoa0431112018-06-25 09:32:54 +0100961const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
962 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
967 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100968}
969
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100970size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
971 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
972 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100973}
974
Mark Mendell7c8d0092015-01-26 11:21:33 -0500975size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700976 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700977 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700978 } else {
979 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
980 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500981 return GetFloatingPointSpillSlotSize();
982}
983
984size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700985 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700986 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 } else {
988 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
989 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500990 return GetFloatingPointSpillSlotSize();
991}
992
Calin Juravle175dc732015-08-25 15:42:32 +0100993void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
994 HInstruction* instruction,
995 uint32_t dex_pc,
996 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100997 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100998 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
999 if (EntrypointRequiresStackMap(entrypoint)) {
1000 RecordPcInfo(instruction, dex_pc, slow_path);
1001 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001002}
1003
Roland Levillaindec8f632016-07-22 17:10:06 +01001004void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1005 HInstruction* instruction,
1006 SlowPathCode* slow_path) {
1007 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001008 GenerateInvokeRuntime(entry_point_offset);
1009}
1010
1011void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001012 __ fs()->call(Address::Absolute(entry_point_offset));
1013}
1014
Mark Mendellfb8d2792015-03-31 22:16:59 -04001015CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001016 const CompilerOptions& compiler_options,
1017 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001018 : CodeGenerator(graph,
1019 kNumberOfCpuRegisters,
1020 kNumberOfXmmRegisters,
1021 kNumberOfRegisterPairs,
1022 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1023 arraysize(kCoreCalleeSaves))
1024 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001025 0,
1026 compiler_options,
1027 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001028 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001030 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001031 move_resolver_(graph->GetAllocator(), this),
1032 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001033 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1034 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1035 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1036 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001037 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001038 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001039 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1041 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001042 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001043 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001044 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001045 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001046 // Use a fake return address register to mimic Quick.
1047 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049
David Brazdil58282f42016-01-14 12:45:10 +00001050void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001052 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001053}
1054
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001056 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001057 assembler_(codegen->GetAssembler()),
1058 codegen_(codegen) {}
1059
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001061 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062}
1063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001066 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001067 bool skip_overflow_check =
1068 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001070
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001071 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1072 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1073 Immediate(1));
1074 }
1075
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001076 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001077 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1078 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001079 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001080 }
1081
Mark Mendell5f874182015-03-04 15:42:45 -05001082 if (HasEmptyFrame()) {
1083 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001084 }
Mark Mendell5f874182015-03-04 15:42:45 -05001085
1086 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1087 Register reg = kCoreCalleeSaves[i];
1088 if (allocated_registers_.ContainsCoreRegister(reg)) {
1089 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001090 __ cfi().AdjustCFAOffset(kX86WordSize);
1091 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001092 }
1093 }
1094
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001095 int adjust = GetFrameSize() - FrameEntrySpillSize();
1096 __ subl(ESP, Immediate(adjust));
1097 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001098 // Save the current method if we need it. Note that we do not
1099 // do this in HCurrentMethod, as the instruction might have been removed
1100 // in the SSA graph.
1101 if (RequiresCurrentMethod()) {
1102 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1103 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001104
1105 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1106 // Initialize should_deoptimize flag to 0.
1107 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1108 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001109}
1110
1111void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001112 __ cfi().RememberState();
1113 if (!HasEmptyFrame()) {
1114 int adjust = GetFrameSize() - FrameEntrySpillSize();
1115 __ addl(ESP, Immediate(adjust));
1116 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001117
David Srbeckyc34dc932015-04-12 09:27:43 +01001118 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1119 Register reg = kCoreCalleeSaves[i];
1120 if (allocated_registers_.ContainsCoreRegister(reg)) {
1121 __ popl(reg);
1122 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1123 __ cfi().Restore(DWARFReg(reg));
1124 }
Mark Mendell5f874182015-03-04 15:42:45 -05001125 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001126 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001127 __ ret();
1128 __ cfi().RestoreState();
1129 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001130}
1131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001132void CodeGeneratorX86::Bind(HBasicBlock* block) {
1133 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001134}
1135
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001136Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001137 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001138 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001139 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001140 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 case DataType::Type::kInt8:
1142 case DataType::Type::kUint16:
1143 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001144 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::RegisterLocation(EAX);
1147
Aart Bik66c158e2018-01-31 12:55:04 -08001148 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001149 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150 return Location::RegisterPairLocation(EAX, EDX);
1151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001152 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001153 return Location::NoLocation();
1154
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001155 case DataType::Type::kFloat64:
1156 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001157 return Location::FpuRegisterLocation(XMM0);
1158 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001159
1160 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001161}
1162
1163Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1164 return Location::RegisterLocation(kMethodRegisterArgument);
1165}
1166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001169 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001170 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001171 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001172 case DataType::Type::kInt8:
1173 case DataType::Type::kUint16:
1174 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001175 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001181 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001182 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001183 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001185 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186 uint32_t index = gp_index_;
1187 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001188 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001190 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1191 calling_convention.GetRegisterPairAt(index));
1192 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001193 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001194 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1195 }
1196 }
1197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001198 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001199 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001200 stack_index_++;
1201 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1202 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1203 } else {
1204 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1205 }
1206 }
1207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001209 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001210 stack_index_ += 2;
1211 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1212 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1213 } else {
1214 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001215 }
1216 }
1217
Aart Bik66c158e2018-01-31 12:55:04 -08001218 case DataType::Type::kUint32:
1219 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001221 LOG(FATAL) << "Unexpected parameter type " << type;
1222 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001223 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001224 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001225}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001226
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227void CodeGeneratorX86::Move32(Location destination, Location source) {
1228 if (source.Equals(destination)) {
1229 return;
1230 }
1231 if (destination.IsRegister()) {
1232 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 } else {
1237 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (destination.IsFpuRegister()) {
1241 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 } else {
1246 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001250 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001252 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001254 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001255 } else if (source.IsConstant()) {
1256 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001257 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001258 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 } else {
1260 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001261 __ pushl(Address(ESP, source.GetStackIndex()));
1262 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 }
1264 }
1265}
1266
1267void CodeGeneratorX86::Move64(Location destination, Location source) {
1268 if (source.Equals(destination)) {
1269 return;
1270 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 if (destination.IsRegisterPair()) {
1272 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 EmitParallelMoves(
1274 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1275 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001276 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001277 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001278 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1282 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1283 __ psrlq(src_reg, Immediate(32));
1284 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001285 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001286 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001287 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001288 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1289 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001290 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1291 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001293 if (source.IsFpuRegister()) {
1294 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1295 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001296 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001297 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001298 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001299 // Create stack space for 2 elements.
1300 __ subl(ESP, Immediate(2 * elem_size));
1301 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1302 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1303 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1304 // And remove the temporary stack space we allocated.
1305 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else {
1307 LOG(FATAL) << "Unimplemented";
1308 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001309 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001310 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001311 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001312 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001315 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001316 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001317 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001318 } else if (source.IsConstant()) {
1319 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001320 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1321 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001322 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001323 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1324 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001325 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001326 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001327 EmitParallelMoves(
1328 Location::StackSlot(source.GetStackIndex()),
1329 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001330 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001331 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001332 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001334 }
1335 }
1336}
1337
Calin Juravle175dc732015-08-25 15:42:32 +01001338void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1339 DCHECK(location.IsRegister());
1340 __ movl(location.AsRegister<Register>(), Immediate(value));
1341}
1342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001343void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001344 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001345 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1346 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1347 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001348 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001349 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001350 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001351 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001352}
1353
1354void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1355 if (location.IsRegister()) {
1356 locations->AddTemp(location);
1357 } else if (location.IsRegisterPair()) {
1358 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1359 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1360 } else {
1361 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1362 }
1363}
1364
David Brazdilfc6a86a2015-06-26 10:33:45 +00001365void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001366 if (successor->IsExitBlock()) {
1367 DCHECK(got->GetPrevious()->AlwaysThrows());
1368 return; // no code needed
1369 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001370
1371 HBasicBlock* block = got->GetBlock();
1372 HInstruction* previous = got->GetPrevious();
1373
1374 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001375 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001376 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1377 __ pushl(EAX);
1378 __ movl(EAX, Address(ESP, kX86WordSize));
1379 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1380 __ popl(EAX);
1381 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001382 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1383 return;
1384 }
1385
1386 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1387 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1388 }
1389 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001390 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001391 }
1392}
1393
David Brazdilfc6a86a2015-06-26 10:33:45 +00001394void LocationsBuilderX86::VisitGoto(HGoto* got) {
1395 got->SetLocations(nullptr);
1396}
1397
1398void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1399 HandleGoto(got, got->GetSuccessor());
1400}
1401
1402void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1403 try_boundary->SetLocations(nullptr);
1404}
1405
1406void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1407 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1408 if (!successor->IsExitBlock()) {
1409 HandleGoto(try_boundary, successor);
1410 }
1411}
1412
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001413void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001414 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001415}
1416
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001417void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001418}
1419
Mark Mendell152408f2015-12-31 12:28:50 -05001420template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001421void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001422 LabelType* true_label,
1423 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 if (cond->IsFPConditionTrueIfNaN()) {
1425 __ j(kUnordered, true_label);
1426 } else if (cond->IsFPConditionFalseIfNaN()) {
1427 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001428 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001429 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001430}
1431
Mark Mendell152408f2015-12-31 12:28:50 -05001432template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001433void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001434 LabelType* true_label,
1435 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001436 LocationSummary* locations = cond->GetLocations();
1437 Location left = locations->InAt(0);
1438 Location right = locations->InAt(1);
1439 IfCondition if_cond = cond->GetCondition();
1440
Mark Mendellc4701932015-04-10 13:18:51 -04001441 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001442 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001443 IfCondition true_high_cond = if_cond;
1444 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001445 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001446
1447 // Set the conditions for the test, remembering that == needs to be
1448 // decided using the low words.
1449 switch (if_cond) {
1450 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001451 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001453 break;
1454 case kCondLT:
1455 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001456 break;
1457 case kCondLE:
1458 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001459 break;
1460 case kCondGT:
1461 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001462 break;
1463 case kCondGE:
1464 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001465 break;
Aart Bike9f37602015-10-09 11:15:55 -07001466 case kCondB:
1467 false_high_cond = kCondA;
1468 break;
1469 case kCondBE:
1470 true_high_cond = kCondB;
1471 break;
1472 case kCondA:
1473 false_high_cond = kCondB;
1474 break;
1475 case kCondAE:
1476 true_high_cond = kCondA;
1477 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001478 }
1479
1480 if (right.IsConstant()) {
1481 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001482 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001483 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001484
Aart Bika19616e2016-02-01 18:57:58 -08001485 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001488 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001489 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001490 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001491 __ j(X86Condition(true_high_cond), true_label);
1492 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001493 }
1494 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001495 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001496 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001497 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001498 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001499
1500 __ cmpl(left_high, right_high);
1501 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001502 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001503 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001504 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001505 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001506 __ j(X86Condition(true_high_cond), true_label);
1507 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001508 }
1509 // Must be equal high, so compare the lows.
1510 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001511 } else {
1512 DCHECK(right.IsDoubleStackSlot());
1513 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1514 if (if_cond == kCondNE) {
1515 __ j(X86Condition(true_high_cond), true_label);
1516 } else if (if_cond == kCondEQ) {
1517 __ j(X86Condition(false_high_cond), false_label);
1518 } else {
1519 __ j(X86Condition(true_high_cond), true_label);
1520 __ j(X86Condition(false_high_cond), false_label);
1521 }
1522 // Must be equal high, so compare the lows.
1523 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001524 }
1525 // The last comparison might be unsigned.
1526 __ j(final_condition, true_label);
1527}
1528
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001529void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1530 Location rhs,
1531 HInstruction* insn,
1532 bool is_double) {
1533 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1534 if (is_double) {
1535 if (rhs.IsFpuRegister()) {
1536 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1537 } else if (const_area != nullptr) {
1538 DCHECK(const_area->IsEmittedAtUseSite());
1539 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1540 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001541 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1542 const_area->GetBaseMethodAddress(),
1543 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001544 } else {
1545 DCHECK(rhs.IsDoubleStackSlot());
1546 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1547 }
1548 } else {
1549 if (rhs.IsFpuRegister()) {
1550 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1551 } else if (const_area != nullptr) {
1552 DCHECK(const_area->IsEmittedAtUseSite());
1553 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1554 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001555 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1556 const_area->GetBaseMethodAddress(),
1557 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001558 } else {
1559 DCHECK(rhs.IsStackSlot());
1560 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1561 }
1562 }
1563}
1564
Mark Mendell152408f2015-12-31 12:28:50 -05001565template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001566void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001567 LabelType* true_target_in,
1568 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001569 // Generated branching requires both targets to be explicit. If either of the
1570 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001571 LabelType fallthrough_target;
1572 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1573 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001574
Mark Mendellc4701932015-04-10 13:18:51 -04001575 LocationSummary* locations = condition->GetLocations();
1576 Location left = locations->InAt(0);
1577 Location right = locations->InAt(1);
1578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001579 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001580 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001581 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001582 GenerateLongComparesAndJumps(condition, true_target, false_target);
1583 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001585 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001586 GenerateFPJumps(condition, true_target, false_target);
1587 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001588 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001589 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001590 GenerateFPJumps(condition, true_target, false_target);
1591 break;
1592 default:
1593 LOG(FATAL) << "Unexpected compare type " << type;
1594 }
1595
David Brazdil0debae72015-11-12 18:37:00 +00001596 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001597 __ jmp(false_target);
1598 }
David Brazdil0debae72015-11-12 18:37:00 +00001599
1600 if (fallthrough_target.IsLinked()) {
1601 __ Bind(&fallthrough_target);
1602 }
Mark Mendellc4701932015-04-10 13:18:51 -04001603}
1604
David Brazdil0debae72015-11-12 18:37:00 +00001605static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1606 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1607 // are set only strictly before `branch`. We can't use the eflags on long/FP
1608 // conditions if they are materialized due to the complex branching.
1609 return cond->IsCondition() &&
1610 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1612 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001613}
1614
Mark Mendell152408f2015-12-31 12:28:50 -05001615template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001616void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001617 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001618 LabelType* true_target,
1619 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001620 HInstruction* cond = instruction->InputAt(condition_input_index);
1621
1622 if (true_target == nullptr && false_target == nullptr) {
1623 // Nothing to do. The code always falls through.
1624 return;
1625 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001626 // Constant condition, statically compared against "true" (integer value 1).
1627 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001628 if (true_target != nullptr) {
1629 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001630 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001631 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001632 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001633 if (false_target != nullptr) {
1634 __ jmp(false_target);
1635 }
1636 }
1637 return;
1638 }
1639
1640 // The following code generates these patterns:
1641 // (1) true_target == nullptr && false_target != nullptr
1642 // - opposite condition true => branch to false_target
1643 // (2) true_target != nullptr && false_target == nullptr
1644 // - condition true => branch to true_target
1645 // (3) true_target != nullptr && false_target != nullptr
1646 // - condition true => branch to true_target
1647 // - branch to false_target
1648 if (IsBooleanValueOrMaterializedCondition(cond)) {
1649 if (AreEflagsSetFrom(cond, instruction)) {
1650 if (true_target == nullptr) {
1651 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1652 } else {
1653 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1654 }
1655 } else {
1656 // Materialized condition, compare against 0.
1657 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1658 if (lhs.IsRegister()) {
1659 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1660 } else {
1661 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1662 }
1663 if (true_target == nullptr) {
1664 __ j(kEqual, false_target);
1665 } else {
1666 __ j(kNotEqual, true_target);
1667 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001668 }
1669 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001670 // Condition has not been materialized, use its inputs as the comparison and
1671 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001672 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001673
1674 // If this is a long or FP comparison that has been folded into
1675 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 DataType::Type type = condition->InputAt(0)->GetType();
1677 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001678 GenerateCompareTestAndBranch(condition, true_target, false_target);
1679 return;
1680 }
1681
1682 Location lhs = condition->GetLocations()->InAt(0);
1683 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001684 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001685 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001686 if (true_target == nullptr) {
1687 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1688 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001689 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001690 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001691 }
David Brazdil0debae72015-11-12 18:37:00 +00001692
1693 // If neither branch falls through (case 3), the conditional branch to `true_target`
1694 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1695 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697 }
1698}
1699
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001701 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001702 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001703 locations->SetInAt(0, Location::Any());
1704 }
1705}
1706
1707void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001708 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1709 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1710 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1711 nullptr : codegen_->GetLabelOf(true_successor);
1712 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1713 nullptr : codegen_->GetLabelOf(false_successor);
1714 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715}
1716
1717void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001718 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001719 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001720 InvokeRuntimeCallingConvention calling_convention;
1721 RegisterSet caller_saves = RegisterSet::Empty();
1722 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1723 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001724 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001725 locations->SetInAt(0, Location::Any());
1726 }
1727}
1728
1729void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001730 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001731 GenerateTestAndBranch<Label>(deoptimize,
1732 /* condition_input_index */ 0,
1733 slow_path->GetEntryLabel(),
1734 /* false_target */ nullptr);
1735}
1736
Mingyao Yang063fc772016-08-02 11:02:54 -07001737void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001738 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001739 LocationSummary(flag, LocationSummary::kNoCall);
1740 locations->SetOut(Location::RequiresRegister());
1741}
1742
1743void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1744 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1745 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1746}
1747
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001748static bool SelectCanUseCMOV(HSelect* select) {
1749 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001751 return false;
1752 }
1753
1754 // A FP condition doesn't generate the single CC that we need.
1755 // In 32 bit mode, a long condition doesn't generate a single CC either.
1756 HInstruction* condition = select->GetCondition();
1757 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 DataType::Type compare_type = condition->InputAt(0)->GetType();
1759 if (compare_type == DataType::Type::kInt64 ||
1760 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001761 return false;
1762 }
1763 }
1764
1765 // We can generate a CMOV for this Select.
1766 return true;
1767}
1768
David Brazdil74eb1b22015-12-14 11:44:01 +00001769void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001770 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001772 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001773 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001774 } else {
1775 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001776 if (SelectCanUseCMOV(select)) {
1777 if (select->InputAt(1)->IsConstant()) {
1778 // Cmov can't handle a constant value.
1779 locations->SetInAt(1, Location::RequiresRegister());
1780 } else {
1781 locations->SetInAt(1, Location::Any());
1782 }
1783 } else {
1784 locations->SetInAt(1, Location::Any());
1785 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001786 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001787 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1788 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001789 }
1790 locations->SetOut(Location::SameAsFirstInput());
1791}
1792
1793void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1794 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001795 DCHECK(locations->InAt(0).Equals(locations->Out()));
1796 if (SelectCanUseCMOV(select)) {
1797 // If both the condition and the source types are integer, we can generate
1798 // a CMOV to implement Select.
1799
1800 HInstruction* select_condition = select->GetCondition();
1801 Condition cond = kNotEqual;
1802
1803 // Figure out how to test the 'condition'.
1804 if (select_condition->IsCondition()) {
1805 HCondition* condition = select_condition->AsCondition();
1806 if (!condition->IsEmittedAtUseSite()) {
1807 // This was a previously materialized condition.
1808 // Can we use the existing condition code?
1809 if (AreEflagsSetFrom(condition, select)) {
1810 // Materialization was the previous instruction. Condition codes are right.
1811 cond = X86Condition(condition->GetCondition());
1812 } else {
1813 // No, we have to recreate the condition code.
1814 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1815 __ testl(cond_reg, cond_reg);
1816 }
1817 } else {
1818 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1820 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001821 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001822 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001823 cond = X86Condition(condition->GetCondition());
1824 }
1825 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001826 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001827 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1828 __ testl(cond_reg, cond_reg);
1829 }
1830
1831 // If the condition is true, overwrite the output, which already contains false.
1832 Location false_loc = locations->InAt(0);
1833 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001834 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001835 // 64 bit conditional move.
1836 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1837 Register false_low = false_loc.AsRegisterPairLow<Register>();
1838 if (true_loc.IsRegisterPair()) {
1839 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1840 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1841 } else {
1842 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1843 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1844 }
1845 } else {
1846 // 32 bit conditional move.
1847 Register false_reg = false_loc.AsRegister<Register>();
1848 if (true_loc.IsRegister()) {
1849 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1850 } else {
1851 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1852 }
1853 }
1854 } else {
1855 NearLabel false_target;
1856 GenerateTestAndBranch<NearLabel>(
1857 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1858 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1859 __ Bind(&false_target);
1860 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001861}
1862
David Srbecky0cf44932015-12-09 14:09:59 +00001863void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001864 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001865}
1866
David Srbeckyd28f4a02016-03-14 17:14:24 +00001867void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1868 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001869}
1870
1871void CodeGeneratorX86::GenerateNop() {
1872 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001873}
1874
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001875void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001876 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001877 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001878 // Handle the long/FP comparisons made in instruction simplification.
1879 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001881 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001882 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 locations->SetOut(Location::RequiresRegister());
1885 }
1886 break;
1887 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 case DataType::Type::kFloat32:
1889 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001890 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001891 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1892 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1893 } else if (cond->InputAt(1)->IsConstant()) {
1894 locations->SetInAt(1, Location::RequiresFpuRegister());
1895 } else {
1896 locations->SetInAt(1, Location::Any());
1897 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001898 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001899 locations->SetOut(Location::RequiresRegister());
1900 }
1901 break;
1902 }
1903 default:
1904 locations->SetInAt(0, Location::RequiresRegister());
1905 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001906 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001907 // We need a byte register.
1908 locations->SetOut(Location::RegisterLocation(ECX));
1909 }
1910 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001911 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001912}
1913
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001914void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001915 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001916 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001917 }
Mark Mendellc4701932015-04-10 13:18:51 -04001918
1919 LocationSummary* locations = cond->GetLocations();
1920 Location lhs = locations->InAt(0);
1921 Location rhs = locations->InAt(1);
1922 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001923 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001924
1925 switch (cond->InputAt(0)->GetType()) {
1926 default: {
1927 // Integer case.
1928
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001929 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001930 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001931 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001932 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001933 return;
1934 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001936 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1937 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001939 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001940 GenerateFPJumps(cond, &true_label, &false_label);
1941 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001942 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001943 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001944 GenerateFPJumps(cond, &true_label, &false_label);
1945 break;
1946 }
1947
1948 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001949 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001950
Roland Levillain4fa13f62015-07-06 18:11:54 +01001951 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001952 __ Bind(&false_label);
1953 __ xorl(reg, reg);
1954 __ jmp(&done_label);
1955
Roland Levillain4fa13f62015-07-06 18:11:54 +01001956 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001957 __ Bind(&true_label);
1958 __ movl(reg, Immediate(1));
1959 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002004}
2005
2006void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008}
2009
Aart Bike9f37602015-10-09 11:15:55 -07002010void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
2038void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002040}
2041
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002042void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002044 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002045 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002046}
2047
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002048void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002049 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002050}
2051
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002052void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2053 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002054 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002055 locations->SetOut(Location::ConstantLocation(constant));
2056}
2057
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002058void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002060}
2061
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002063 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002064 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002065 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066}
2067
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002068void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 // Will be generated at use site.
2070}
2071
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2073 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002074 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 locations->SetOut(Location::ConstantLocation(constant));
2076}
2077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002078void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002079 // Will be generated at use site.
2080}
2081
2082void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2083 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 locations->SetOut(Location::ConstantLocation(constant));
2086}
2087
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002088void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002089 // Will be generated at use site.
2090}
2091
Igor Murashkind01745e2017-04-05 16:40:31 -07002092void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2093 constructor_fence->SetLocations(nullptr);
2094}
2095
2096void InstructionCodeGeneratorX86::VisitConstructorFence(
2097 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2098 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2099}
2100
Calin Juravle27df7582015-04-17 19:12:31 +01002101void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2102 memory_barrier->SetLocations(nullptr);
2103}
2104
2105void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002106 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002107}
2108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002111}
2112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002113void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002114 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002115}
2116
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002118 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002119 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002121 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002123 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 case DataType::Type::kInt8:
2125 case DataType::Type::kUint16:
2126 case DataType::Type::kInt16:
2127 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 break;
2130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002132 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002133 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134 break;
2135
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 case DataType::Type::kFloat32:
2137 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002138 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 break;
2141
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002142 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002144 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002145}
2146
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002147void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 if (kIsDebugBuild) {
2149 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002150 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002151 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002152 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kInt8:
2154 case DataType::Type::kUint16:
2155 case DataType::Type::kInt16:
2156 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002157 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002158 break;
2159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002161 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2162 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163 break;
2164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002165 case DataType::Type::kFloat32:
2166 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002167 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002168 break;
2169
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002170 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002171 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002172 }
2173 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002174 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002175}
2176
Calin Juravle175dc732015-08-25 15:42:32 +01002177void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2178 // The trampoline uses the same calling convention as dex calling conventions,
2179 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2180 // the method_idx.
2181 HandleInvoke(invoke);
2182}
2183
2184void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2185 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2186}
2187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002188void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002189 // Explicit clinit checks triggered by static invokes must have been pruned by
2190 // art::PrepareForRegisterAllocation.
2191 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002192
Mark Mendellfb8d2792015-03-31 22:16:59 -04002193 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002194 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002195 if (invoke->GetLocations()->CanCall() &&
2196 invoke->HasPcRelativeMethodLoadKind() &&
2197 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002198 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002199 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002200 return;
2201 }
2202
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002203 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002204
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002205 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002206 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002207 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002208 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002209}
2210
Mark Mendell09ed1a32015-03-25 08:30:06 -04002211static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2212 if (invoke->GetLocations()->Intrinsified()) {
2213 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2214 intrinsic.Dispatch(invoke);
2215 return true;
2216 }
2217 return false;
2218}
2219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002220void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002221 // Explicit clinit checks triggered by static invokes must have been pruned by
2222 // art::PrepareForRegisterAllocation.
2223 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002224
Mark Mendell09ed1a32015-03-25 08:30:06 -04002225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2226 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002227 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002228
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002229 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002230 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002231 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002232}
2233
2234void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002235 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2236 if (intrinsic.TryDispatch(invoke)) {
2237 return;
2238 }
2239
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002240 HandleInvoke(invoke);
2241}
2242
2243void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002244 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002245 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002246}
2247
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002248void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002249 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2250 return;
2251 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002252
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002253 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002254 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002255}
2256
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // This call to HandleInvoke allocates a temporary (core) register
2259 // which is also used to transfer the hidden argument from FP to
2260 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261 HandleInvoke(invoke);
2262 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002263 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264}
2265
2266void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2267 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002268 LocationSummary* locations = invoke->GetLocations();
2269 Register temp = locations->GetTemp(0).AsRegister<Register>();
2270 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 Location receiver = locations->InAt(0);
2272 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2273
Roland Levillain0d5a2812015-11-13 10:07:31 +00002274 // Set the hidden argument. This is safe to do this here, as XMM7
2275 // won't be modified thereafter, before the `call` instruction.
2276 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002278 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 if (receiver.IsStackSlot()) {
2281 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002282 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283 __ movl(temp, Address(temp, class_offset));
2284 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002285 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002286 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002287 }
Roland Levillain4d027112015-07-01 15:41:14 +01002288 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002289 // Instead of simply (possibly) unpoisoning `temp` here, we should
2290 // emit a read barrier for the previous class reference load.
2291 // However this is not required in practice, as this is an
2292 // intermediate/temporary reference and because the current
2293 // concurrent copying collector keeps the from-space memory
2294 // intact/accessible until the end of the marking phase (the
2295 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002296 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002297 // temp = temp->GetAddressOfIMT()
2298 __ movl(temp,
2299 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002300 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002301 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002302 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002303 __ movl(temp, Address(temp, method_offset));
2304 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002305 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002306 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002307
2308 DCHECK(!codegen_->IsLeafMethod());
2309 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2310}
2311
Orion Hodsonac141392017-01-13 11:53:47 +00002312void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2313 HandleInvoke(invoke);
2314}
2315
2316void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2317 codegen_->GenerateInvokePolymorphicCall(invoke);
2318}
2319
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002320void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2321 HandleInvoke(invoke);
2322}
2323
2324void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2325 codegen_->GenerateInvokeCustomCall(invoke);
2326}
2327
Roland Levillain88cb1752014-10-20 16:36:47 +01002328void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2329 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002330 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002331 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 case DataType::Type::kInt32:
2333 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetOut(Location::SameAsFirstInput());
2336 break;
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002339 locations->SetInAt(0, Location::RequiresFpuRegister());
2340 locations->SetOut(Location::SameAsFirstInput());
2341 locations->AddTemp(Location::RequiresRegister());
2342 locations->AddTemp(Location::RequiresFpuRegister());
2343 break;
2344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002346 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002347 locations->SetOut(Location::SameAsFirstInput());
2348 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002349 break;
2350
2351 default:
2352 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2353 }
2354}
2355
2356void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2357 LocationSummary* locations = neg->GetLocations();
2358 Location out = locations->Out();
2359 Location in = locations->InAt(0);
2360 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002362 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002363 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002364 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002365 break;
2366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002368 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002369 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002370 __ negl(out.AsRegisterPairLow<Register>());
2371 // Negation is similar to subtraction from zero. The least
2372 // significant byte triggers a borrow when it is different from
2373 // zero; to take it into account, add 1 to the most significant
2374 // byte if the carry flag (CF) is set to 1 after the first NEGL
2375 // operation.
2376 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2377 __ negl(out.AsRegisterPairHigh<Register>());
2378 break;
2379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002380 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002381 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 Register constant = locations->GetTemp(0).AsRegister<Register>();
2383 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002384 // Implement float negation with an exclusive or with value
2385 // 0x80000000 (mask for bit 31, representing the sign of a
2386 // single-precision floating-point number).
2387 __ movl(constant, Immediate(INT32_C(0x80000000)));
2388 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002389 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002390 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002391 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002392
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002394 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002395 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002396 // Implement double negation with an exclusive or with value
2397 // 0x8000000000000000 (mask for bit 63, representing the sign of
2398 // a double-precision floating-point number).
2399 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002400 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002402 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002403
2404 default:
2405 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2406 }
2407}
2408
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002409void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2410 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002411 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002413 locations->SetInAt(0, Location::RequiresFpuRegister());
2414 locations->SetInAt(1, Location::RequiresRegister());
2415 locations->SetOut(Location::SameAsFirstInput());
2416 locations->AddTemp(Location::RequiresFpuRegister());
2417}
2418
2419void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2420 LocationSummary* locations = neg->GetLocations();
2421 Location out = locations->Out();
2422 DCHECK(locations->InAt(0).Equals(out));
2423
2424 Register constant_area = locations->InAt(1).AsRegister<Register>();
2425 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002427 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2428 neg->GetBaseMethodAddress(),
2429 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002430 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2431 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002432 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2433 neg->GetBaseMethodAddress(),
2434 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002435 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2436 }
2437}
2438
Roland Levillaindff1f282014-11-05 14:15:05 +00002439void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 DataType::Type result_type = conversion->GetResultType();
2441 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002442 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2443 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002444
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002445 // The float-to-long and double-to-long type conversions rely on a
2446 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002447 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002448 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2449 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002450 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002451 : LocationSummary::kNoCall;
2452 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002453 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002454
Roland Levillaindff1f282014-11-05 14:15:05 +00002455 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002456 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002458 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002459 case DataType::Type::kUint8:
2460 case DataType::Type::kInt8:
2461 case DataType::Type::kUint16:
2462 case DataType::Type::kInt16:
2463 case DataType::Type::kInt32:
2464 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2465 // Make the output overlap to please the register allocator. This greatly simplifies
2466 // the validation of the linear scan implementation
2467 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2468 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002470 HInstruction* input = conversion->InputAt(0);
2471 Location input_location = input->IsConstant()
2472 ? Location::ConstantLocation(input->AsConstant())
2473 : Location::RegisterPairLocation(EAX, EDX);
2474 locations->SetInAt(0, input_location);
2475 // Make the output overlap to please the register allocator. This greatly simplifies
2476 // the validation of the linear scan implementation
2477 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2478 break;
2479 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485 break;
2486
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002487 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002489 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2490 locations->SetInAt(0, Location::Any());
2491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002492 break;
2493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002495 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002497 locations->SetInAt(0, Location::Any());
2498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2499 break;
2500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002502 locations->SetInAt(0, Location::RequiresFpuRegister());
2503 locations->SetOut(Location::RequiresRegister());
2504 locations->AddTemp(Location::RequiresFpuRegister());
2505 break;
2506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002508 locations->SetInAt(0, Location::RequiresFpuRegister());
2509 locations->SetOut(Location::RequiresRegister());
2510 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected type conversion from " << input_type
2515 << " to " << result_type;
2516 }
2517 break;
2518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002520 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002522 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002524 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kInt16:
2526 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 locations->SetInAt(0, Location::RegisterLocation(EAX));
2528 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2529 break;
2530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 case DataType::Type::kFloat32:
2532 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002533 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002534 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2535 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2536
Vladimir Marko949c91f2015-01-27 10:48:44 +00002537 // The runtime helper puts the result in EAX, EDX.
2538 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002539 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002540 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002541
2542 default:
2543 LOG(FATAL) << "Unexpected type conversion from " << input_type
2544 << " to " << result_type;
2545 }
2546 break;
2547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002549 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002551 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002553 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt16:
2555 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002556 locations->SetInAt(0, Location::RequiresRegister());
2557 locations->SetOut(Location::RequiresFpuRegister());
2558 break;
2559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002561 locations->SetInAt(0, Location::Any());
2562 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002563 break;
2564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002566 locations->SetInAt(0, Location::RequiresFpuRegister());
2567 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002568 break;
2569
2570 default:
2571 LOG(FATAL) << "Unexpected type conversion from " << input_type
2572 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002573 }
Roland Levillaincff13742014-11-17 14:32:17 +00002574 break;
2575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002577 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002581 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kInt16:
2583 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002584 locations->SetInAt(0, Location::RequiresRegister());
2585 locations->SetOut(Location::RequiresFpuRegister());
2586 break;
2587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002589 locations->SetInAt(0, Location::Any());
2590 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002591 break;
2592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002594 locations->SetInAt(0, Location::RequiresFpuRegister());
2595 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002596 break;
2597
2598 default:
2599 LOG(FATAL) << "Unexpected type conversion from " << input_type
2600 << " to " << result_type;
2601 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 break;
2603
2604 default:
2605 LOG(FATAL) << "Unexpected type conversion from " << input_type
2606 << " to " << result_type;
2607 }
2608}
2609
2610void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2611 LocationSummary* locations = conversion->GetLocations();
2612 Location out = locations->Out();
2613 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DataType::Type result_type = conversion->GetResultType();
2615 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002616 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2617 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002618 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002619 case DataType::Type::kUint8:
2620 switch (input_type) {
2621 case DataType::Type::kInt8:
2622 case DataType::Type::kUint16:
2623 case DataType::Type::kInt16:
2624 case DataType::Type::kInt32:
2625 if (in.IsRegister()) {
2626 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2627 } else {
2628 DCHECK(in.GetConstant()->IsIntConstant());
2629 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2630 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2631 }
2632 break;
2633 case DataType::Type::kInt64:
2634 if (in.IsRegisterPair()) {
2635 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2636 } else {
2637 DCHECK(in.GetConstant()->IsLongConstant());
2638 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2640 }
2641 break;
2642
2643 default:
2644 LOG(FATAL) << "Unexpected type conversion from " << input_type
2645 << " to " << result_type;
2646 }
2647 break;
2648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002650 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002651 case DataType::Type::kUint8:
2652 case DataType::Type::kUint16:
2653 case DataType::Type::kInt16:
2654 case DataType::Type::kInt32:
2655 if (in.IsRegister()) {
2656 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2657 } else {
2658 DCHECK(in.GetConstant()->IsIntConstant());
2659 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2660 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2661 }
2662 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002664 if (in.IsRegisterPair()) {
2665 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2666 } else {
2667 DCHECK(in.GetConstant()->IsLongConstant());
2668 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2669 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2670 }
2671 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002672
2673 default:
2674 LOG(FATAL) << "Unexpected type conversion from " << input_type
2675 << " to " << result_type;
2676 }
2677 break;
2678
2679 case DataType::Type::kUint16:
2680 switch (input_type) {
2681 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 case DataType::Type::kInt16:
2683 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002684 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002685 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2686 } else if (in.IsStackSlot()) {
2687 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002688 } else {
2689 DCHECK(in.GetConstant()->IsIntConstant());
2690 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002691 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2692 }
2693 break;
2694 case DataType::Type::kInt64:
2695 if (in.IsRegisterPair()) {
2696 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2697 } else if (in.IsDoubleStackSlot()) {
2698 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2699 } else {
2700 DCHECK(in.GetConstant()->IsLongConstant());
2701 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2702 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002703 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002704 break;
2705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 }
2710 break;
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002713 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002715 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002716 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002718 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002719 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002720 } else {
2721 DCHECK(in.GetConstant()->IsIntConstant());
2722 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002724 }
2725 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002726 case DataType::Type::kInt64:
2727 if (in.IsRegisterPair()) {
2728 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2729 } else if (in.IsDoubleStackSlot()) {
2730 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2731 } else {
2732 DCHECK(in.GetConstant()->IsLongConstant());
2733 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2734 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2735 }
2736 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002737
2738 default:
2739 LOG(FATAL) << "Unexpected type conversion from " << input_type
2740 << " to " << result_type;
2741 }
2742 break;
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002745 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002747 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002749 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002750 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002751 } else {
2752 DCHECK(in.IsConstant());
2753 DCHECK(in.GetConstant()->IsLongConstant());
2754 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002756 }
2757 break;
2758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002760 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2761 Register output = out.AsRegister<Register>();
2762 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002763 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002764
2765 __ movl(output, Immediate(kPrimIntMax));
2766 // temp = int-to-float(output)
2767 __ cvtsi2ss(temp, output);
2768 // if input >= temp goto done
2769 __ comiss(input, temp);
2770 __ j(kAboveEqual, &done);
2771 // if input == NaN goto nan
2772 __ j(kUnordered, &nan);
2773 // output = float-to-int-truncate(input)
2774 __ cvttss2si(output, input);
2775 __ jmp(&done);
2776 __ Bind(&nan);
2777 // output = 0
2778 __ xorl(output, output);
2779 __ Bind(&done);
2780 break;
2781 }
2782
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002784 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2785 Register output = out.AsRegister<Register>();
2786 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002787 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002788
2789 __ movl(output, Immediate(kPrimIntMax));
2790 // temp = int-to-double(output)
2791 __ cvtsi2sd(temp, output);
2792 // if input >= temp goto done
2793 __ comisd(input, temp);
2794 __ j(kAboveEqual, &done);
2795 // if input == NaN goto nan
2796 __ j(kUnordered, &nan);
2797 // output = double-to-int-truncate(input)
2798 __ cvttsd2si(output, input);
2799 __ jmp(&done);
2800 __ Bind(&nan);
2801 // output = 0
2802 __ xorl(output, output);
2803 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002804 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002805 }
Roland Levillain946e1432014-11-11 17:35:19 +00002806
2807 default:
2808 LOG(FATAL) << "Unexpected type conversion from " << input_type
2809 << " to " << result_type;
2810 }
2811 break;
2812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002814 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002816 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002818 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 case DataType::Type::kInt16:
2820 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2822 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002823 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002824 __ cdq();
2825 break;
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002828 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002829 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002830 break;
2831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002833 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002834 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002835 break;
2836
2837 default:
2838 LOG(FATAL) << "Unexpected type conversion from " << input_type
2839 << " to " << result_type;
2840 }
2841 break;
2842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002844 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002846 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002848 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002849 case DataType::Type::kInt16:
2850 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002851 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002852 break;
2853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002854 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002855 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002856
Roland Levillain232ade02015-04-20 15:14:36 +01002857 // Create stack space for the call to
2858 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2859 // TODO: enhance register allocator to ask for stack temporaries.
2860 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002862 __ subl(ESP, Immediate(adjustment));
2863 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002864
Roland Levillain232ade02015-04-20 15:14:36 +01002865 // Load the value to the FP stack, using temporaries if needed.
2866 PushOntoFPStack(in, 0, adjustment, false, true);
2867
2868 if (out.IsStackSlot()) {
2869 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2870 } else {
2871 __ fstps(Address(ESP, 0));
2872 Location stack_temp = Location::StackSlot(0);
2873 codegen_->Move32(out, stack_temp);
2874 }
2875
2876 // Remove the temporary stack space we allocated.
2877 if (adjustment != 0) {
2878 __ addl(ESP, Immediate(adjustment));
2879 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002880 break;
2881 }
2882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002884 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002885 break;
2886
2887 default:
2888 LOG(FATAL) << "Unexpected type conversion from " << input_type
2889 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002890 }
Roland Levillaincff13742014-11-17 14:32:17 +00002891 break;
2892
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002894 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002896 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002898 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kInt16:
2900 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002901 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002902 break;
2903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002905 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002906
Roland Levillain232ade02015-04-20 15:14:36 +01002907 // Create stack space for the call to
2908 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2909 // TODO: enhance register allocator to ask for stack temporaries.
2910 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002912 __ subl(ESP, Immediate(adjustment));
2913 }
2914
2915 // Load the value to the FP stack, using temporaries if needed.
2916 PushOntoFPStack(in, 0, adjustment, false, true);
2917
2918 if (out.IsDoubleStackSlot()) {
2919 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2920 } else {
2921 __ fstpl(Address(ESP, 0));
2922 Location stack_temp = Location::DoubleStackSlot(0);
2923 codegen_->Move64(out, stack_temp);
2924 }
2925
2926 // Remove the temporary stack space we allocated.
2927 if (adjustment != 0) {
2928 __ addl(ESP, Immediate(adjustment));
2929 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002930 break;
2931 }
2932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002934 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002935 break;
2936
2937 default:
2938 LOG(FATAL) << "Unexpected type conversion from " << input_type
2939 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002940 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002941 break;
2942
2943 default:
2944 LOG(FATAL) << "Unexpected type conversion from " << input_type
2945 << " to " << result_type;
2946 }
2947}
2948
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002951 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002952 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002954 locations->SetInAt(0, Location::RequiresRegister());
2955 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2957 break;
2958 }
2959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002961 locations->SetInAt(0, Location::RequiresRegister());
2962 locations->SetInAt(1, Location::Any());
2963 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002964 break;
2965 }
2966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kFloat32:
2968 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002969 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002970 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2971 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002972 } else if (add->InputAt(1)->IsConstant()) {
2973 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002974 } else {
2975 locations->SetInAt(1, Location::Any());
2976 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002978 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002979 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002980
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002981 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2983 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002984 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002985}
2986
2987void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2988 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 Location first = locations->InAt(0);
2990 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002991 Location out = locations->Out();
2992
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002996 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2997 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002998 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2999 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003000 } else {
3001 __ leal(out.AsRegister<Register>(), Address(
3002 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3003 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003005 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3006 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3007 __ addl(out.AsRegister<Register>(), Immediate(value));
3008 } else {
3009 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3010 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003011 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003012 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003014 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003015 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003016 }
3017
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003019 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003020 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3021 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003022 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003023 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3024 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003025 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003026 } else {
3027 DCHECK(second.IsConstant()) << second;
3028 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3029 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3030 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003031 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003032 break;
3033 }
3034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003035 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003036 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003038 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3039 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003040 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003041 __ addss(first.AsFpuRegister<XmmRegister>(),
3042 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003043 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3044 const_area->GetBaseMethodAddress(),
3045 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003046 } else {
3047 DCHECK(second.IsStackSlot());
3048 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003049 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003050 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 }
3052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003054 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003056 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3057 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003058 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003059 __ addsd(first.AsFpuRegister<XmmRegister>(),
3060 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003061 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3062 const_area->GetBaseMethodAddress(),
3063 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003064 } else {
3065 DCHECK(second.IsDoubleStackSlot());
3066 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003067 }
3068 break;
3069 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003070
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003071 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003072 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003073 }
3074}
3075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003076void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003077 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003078 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003079 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kInt32:
3081 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003082 locations->SetInAt(0, Location::RequiresRegister());
3083 locations->SetInAt(1, Location::Any());
3084 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003085 break;
3086 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003087 case DataType::Type::kFloat32:
3088 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003089 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003090 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3091 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003092 } else if (sub->InputAt(1)->IsConstant()) {
3093 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003094 } else {
3095 locations->SetInAt(1, Location::Any());
3096 }
Calin Juravle11351682014-10-23 15:38:15 +01003097 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003098 break;
Calin Juravle11351682014-10-23 15:38:15 +01003099 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003100
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003101 default:
Calin Juravle11351682014-10-23 15:38:15 +01003102 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003103 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003104}
3105
3106void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3107 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003108 Location first = locations->InAt(0);
3109 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003110 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003113 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003115 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003116 __ subl(first.AsRegister<Register>(),
3117 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003120 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003121 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003122 }
3123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003124 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003125 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003126 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3127 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003128 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003129 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003130 __ sbbl(first.AsRegisterPairHigh<Register>(),
3131 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003132 } else {
3133 DCHECK(second.IsConstant()) << second;
3134 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3135 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3136 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003137 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003138 break;
3139 }
3140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003142 if (second.IsFpuRegister()) {
3143 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3144 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3145 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003146 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 __ subss(first.AsFpuRegister<XmmRegister>(),
3148 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003149 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3150 const_area->GetBaseMethodAddress(),
3151 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003152 } else {
3153 DCHECK(second.IsStackSlot());
3154 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3155 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003156 break;
Calin Juravle11351682014-10-23 15:38:15 +01003157 }
3158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003159 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003160 if (second.IsFpuRegister()) {
3161 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3162 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3163 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003164 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 __ subsd(first.AsFpuRegister<XmmRegister>(),
3166 codegen_->LiteralDoubleAddress(
3167 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003168 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003169 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3170 } else {
3171 DCHECK(second.IsDoubleStackSlot());
3172 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3173 }
Calin Juravle11351682014-10-23 15:38:15 +01003174 break;
3175 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003176
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003177 default:
Calin Juravle11351682014-10-23 15:38:15 +01003178 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003179 }
3180}
3181
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182void LocationsBuilderX86::VisitMul(HMul* mul) {
3183 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003184 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003185 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003186 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 locations->SetInAt(0, Location::RequiresRegister());
3188 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003189 if (mul->InputAt(1)->IsIntConstant()) {
3190 // Can use 3 operand multiply.
3191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3192 } else {
3193 locations->SetOut(Location::SameAsFirstInput());
3194 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198 locations->SetInAt(1, Location::Any());
3199 locations->SetOut(Location::SameAsFirstInput());
3200 // Needed for imul on 32bits with 64bits output.
3201 locations->AddTemp(Location::RegisterLocation(EAX));
3202 locations->AddTemp(Location::RegisterLocation(EDX));
3203 break;
3204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kFloat32:
3206 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003207 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003208 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3209 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003210 } else if (mul->InputAt(1)->IsConstant()) {
3211 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003212 } else {
3213 locations->SetInAt(1, Location::Any());
3214 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003215 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003217 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218
3219 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003220 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221 }
3222}
3223
3224void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3225 LocationSummary* locations = mul->GetLocations();
3226 Location first = locations->InAt(0);
3227 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003228 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003229
3230 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003232 // The constant may have ended up in a register, so test explicitly to avoid
3233 // problems where the output may not be the same as the first operand.
3234 if (mul->InputAt(1)->IsIntConstant()) {
3235 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3236 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3237 } else if (second.IsRegister()) {
3238 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003240 } else {
3241 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003242 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003244 }
3245 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003248 Register in1_hi = first.AsRegisterPairHigh<Register>();
3249 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 Register eax = locations->GetTemp(0).AsRegister<Register>();
3251 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252
3253 DCHECK_EQ(EAX, eax);
3254 DCHECK_EQ(EDX, edx);
3255
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003256 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257 // output: in1
3258 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3259 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3260 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003261 if (second.IsConstant()) {
3262 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003263
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003264 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3265 int32_t low_value = Low32Bits(value);
3266 int32_t high_value = High32Bits(value);
3267 Immediate low(low_value);
3268 Immediate high(high_value);
3269
3270 __ movl(eax, high);
3271 // eax <- in1.lo * in2.hi
3272 __ imull(eax, in1_lo);
3273 // in1.hi <- in1.hi * in2.lo
3274 __ imull(in1_hi, low);
3275 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3276 __ addl(in1_hi, eax);
3277 // move in2_lo to eax to prepare for double precision
3278 __ movl(eax, low);
3279 // edx:eax <- in1.lo * in2.lo
3280 __ mull(in1_lo);
3281 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3282 __ addl(in1_hi, edx);
3283 // in1.lo <- (in1.lo * in2.lo)[31:0];
3284 __ movl(in1_lo, eax);
3285 } else if (second.IsRegisterPair()) {
3286 Register in2_hi = second.AsRegisterPairHigh<Register>();
3287 Register in2_lo = second.AsRegisterPairLow<Register>();
3288
3289 __ movl(eax, in2_hi);
3290 // eax <- in1.lo * in2.hi
3291 __ imull(eax, in1_lo);
3292 // in1.hi <- in1.hi * in2.lo
3293 __ imull(in1_hi, in2_lo);
3294 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3295 __ addl(in1_hi, eax);
3296 // move in1_lo to eax to prepare for double precision
3297 __ movl(eax, in1_lo);
3298 // edx:eax <- in1.lo * in2.lo
3299 __ mull(in2_lo);
3300 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3301 __ addl(in1_hi, edx);
3302 // in1.lo <- (in1.lo * in2.lo)[31:0];
3303 __ movl(in1_lo, eax);
3304 } else {
3305 DCHECK(second.IsDoubleStackSlot()) << second;
3306 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3307 Address in2_lo(ESP, second.GetStackIndex());
3308
3309 __ movl(eax, in2_hi);
3310 // eax <- in1.lo * in2.hi
3311 __ imull(eax, in1_lo);
3312 // in1.hi <- in1.hi * in2.lo
3313 __ imull(in1_hi, in2_lo);
3314 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3315 __ addl(in1_hi, eax);
3316 // move in1_lo to eax to prepare for double precision
3317 __ movl(eax, in1_lo);
3318 // edx:eax <- in1.lo * in2.lo
3319 __ mull(in2_lo);
3320 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3321 __ addl(in1_hi, edx);
3322 // in1.lo <- (in1.lo * in2.lo)[31:0];
3323 __ movl(in1_lo, eax);
3324 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003325
3326 break;
3327 }
3328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003330 DCHECK(first.Equals(locations->Out()));
3331 if (second.IsFpuRegister()) {
3332 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3333 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3334 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003335 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003336 __ mulss(first.AsFpuRegister<XmmRegister>(),
3337 codegen_->LiteralFloatAddress(
3338 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003339 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003340 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3341 } else {
3342 DCHECK(second.IsStackSlot());
3343 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3344 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003345 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003346 }
3347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003348 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 DCHECK(first.Equals(locations->Out()));
3350 if (second.IsFpuRegister()) {
3351 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3352 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3353 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003354 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003355 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3356 codegen_->LiteralDoubleAddress(
3357 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003358 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003359 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3360 } else {
3361 DCHECK(second.IsDoubleStackSlot());
3362 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3363 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003364 break;
3365 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003366
3367 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003368 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003369 }
3370}
3371
Roland Levillain232ade02015-04-20 15:14:36 +01003372void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3373 uint32_t temp_offset,
3374 uint32_t stack_adjustment,
3375 bool is_fp,
3376 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003377 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003378 DCHECK(!is_wide);
3379 if (is_fp) {
3380 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3381 } else {
3382 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3383 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003385 DCHECK(is_wide);
3386 if (is_fp) {
3387 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3388 } else {
3389 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 } else {
3392 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003393 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003394 Location stack_temp = Location::StackSlot(temp_offset);
3395 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003396 if (is_fp) {
3397 __ flds(Address(ESP, temp_offset));
3398 } else {
3399 __ filds(Address(ESP, temp_offset));
3400 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003401 } else {
3402 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3403 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003404 if (is_fp) {
3405 __ fldl(Address(ESP, temp_offset));
3406 } else {
3407 __ fildl(Address(ESP, temp_offset));
3408 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003409 }
3410 }
3411}
3412
3413void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 DataType::Type type = rem->GetResultType();
3415 bool is_float = type == DataType::Type::kFloat32;
3416 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003417 LocationSummary* locations = rem->GetLocations();
3418 Location first = locations->InAt(0);
3419 Location second = locations->InAt(1);
3420 Location out = locations->Out();
3421
3422 // Create stack space for 2 elements.
3423 // TODO: enhance register allocator to ask for stack temporaries.
3424 __ subl(ESP, Immediate(2 * elem_size));
3425
3426 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003427 const bool is_wide = !is_float;
3428 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3429 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003430
3431 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003432 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003433 __ Bind(&retry);
3434 __ fprem();
3435
3436 // Move FP status to AX.
3437 __ fstsw();
3438
3439 // And see if the argument reduction is complete. This is signaled by the
3440 // C2 FPU flag bit set to 0.
3441 __ andl(EAX, Immediate(kC2ConditionMask));
3442 __ j(kNotEqual, &retry);
3443
3444 // We have settled on the final value. Retrieve it into an XMM register.
3445 // Store FP top of stack to real stack.
3446 if (is_float) {
3447 __ fsts(Address(ESP, 0));
3448 } else {
3449 __ fstl(Address(ESP, 0));
3450 }
3451
3452 // Pop the 2 items from the FP stack.
3453 __ fucompp();
3454
3455 // Load the value from the stack into an XMM register.
3456 DCHECK(out.IsFpuRegister()) << out;
3457 if (is_float) {
3458 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3459 } else {
3460 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3461 }
3462
3463 // And remove the temporary stack space we allocated.
3464 __ addl(ESP, Immediate(2 * elem_size));
3465}
3466
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003467
3468void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3469 DCHECK(instruction->IsDiv() || instruction->IsRem());
3470
3471 LocationSummary* locations = instruction->GetLocations();
3472 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003473 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475 Register out_register = locations->Out().AsRegister<Register>();
3476 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003477 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003478
3479 DCHECK(imm == 1 || imm == -1);
3480
3481 if (instruction->IsRem()) {
3482 __ xorl(out_register, out_register);
3483 } else {
3484 __ movl(out_register, input_register);
3485 if (imm == -1) {
3486 __ negl(out_register);
3487 }
3488 }
3489}
3490
3491
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003492void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 LocationSummary* locations = instruction->GetLocations();
3494
3495 Register out_register = locations->Out().AsRegister<Register>();
3496 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003497 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003498 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3499 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 Register num = locations->GetTemp(0).AsRegister<Register>();
3502
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003503 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 __ testl(input_register, input_register);
3505 __ cmovl(kGreaterEqual, num, input_register);
3506 int shift = CTZ(imm);
3507 __ sarl(num, Immediate(shift));
3508
3509 if (imm < 0) {
3510 __ negl(num);
3511 }
3512
3513 __ movl(out_register, num);
3514}
3515
3516void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3517 DCHECK(instruction->IsDiv() || instruction->IsRem());
3518
3519 LocationSummary* locations = instruction->GetLocations();
3520 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3521
3522 Register eax = locations->InAt(0).AsRegister<Register>();
3523 Register out = locations->Out().AsRegister<Register>();
3524 Register num;
3525 Register edx;
3526
3527 if (instruction->IsDiv()) {
3528 edx = locations->GetTemp(0).AsRegister<Register>();
3529 num = locations->GetTemp(1).AsRegister<Register>();
3530 } else {
3531 edx = locations->Out().AsRegister<Register>();
3532 num = locations->GetTemp(0).AsRegister<Register>();
3533 }
3534
3535 DCHECK_EQ(EAX, eax);
3536 DCHECK_EQ(EDX, edx);
3537 if (instruction->IsDiv()) {
3538 DCHECK_EQ(EAX, out);
3539 } else {
3540 DCHECK_EQ(EDX, out);
3541 }
3542
3543 int64_t magic;
3544 int shift;
3545 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3546
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003547 // Save the numerator.
3548 __ movl(num, eax);
3549
3550 // EAX = magic
3551 __ movl(eax, Immediate(magic));
3552
3553 // EDX:EAX = magic * numerator
3554 __ imull(num);
3555
3556 if (imm > 0 && magic < 0) {
3557 // EDX += num
3558 __ addl(edx, num);
3559 } else if (imm < 0 && magic > 0) {
3560 __ subl(edx, num);
3561 }
3562
3563 // Shift if needed.
3564 if (shift != 0) {
3565 __ sarl(edx, Immediate(shift));
3566 }
3567
3568 // EDX += 1 if EDX < 0
3569 __ movl(eax, edx);
3570 __ shrl(edx, Immediate(31));
3571 __ addl(edx, eax);
3572
3573 if (instruction->IsRem()) {
3574 __ movl(eax, num);
3575 __ imull(edx, Immediate(imm));
3576 __ subl(eax, edx);
3577 __ movl(edx, eax);
3578 } else {
3579 __ movl(eax, edx);
3580 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581}
3582
Calin Juravlebacfec32014-11-14 15:54:36 +00003583void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3584 DCHECK(instruction->IsDiv() || instruction->IsRem());
3585
3586 LocationSummary* locations = instruction->GetLocations();
3587 Location out = locations->Out();
3588 Location first = locations->InAt(0);
3589 Location second = locations->InAt(1);
3590 bool is_div = instruction->IsDiv();
3591
3592 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003593 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003594 DCHECK_EQ(EAX, first.AsRegister<Register>());
3595 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003596
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003597 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003598 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599
3600 if (imm == 0) {
3601 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3602 } else if (imm == 1 || imm == -1) {
3603 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003604 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003605 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 } else {
3607 DCHECK(imm <= -2 || imm >= 2);
3608 GenerateDivRemWithAnyConstant(instruction);
3609 }
3610 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003611 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003612 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003614
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 Register second_reg = second.AsRegister<Register>();
3616 // 0x80000000/-1 triggers an arithmetic exception!
3617 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3618 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003619
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 __ cmpl(second_reg, Immediate(-1));
3621 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003622
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 // edx:eax <- sign-extended of eax
3624 __ cdq();
3625 // eax = quotient, edx = remainder
3626 __ idivl(second_reg);
3627 __ Bind(slow_path->GetExitLabel());
3628 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003629 break;
3630 }
3631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003632 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 InvokeRuntimeCallingConvention calling_convention;
3634 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3635 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3636 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3637 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3638 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3639 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3640
3641 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003642 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003643 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003644 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003645 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003646 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003647 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 break;
3649 }
3650
3651 default:
3652 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3653 }
3654}
3655
Calin Juravle7c4954d2014-10-28 16:57:40 +00003656void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003657 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003658 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003659 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003660 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661
Calin Juravle7c4954d2014-10-28 16:57:40 +00003662 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003664 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003665 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003666 locations->SetOut(Location::SameAsFirstInput());
3667 // Intel uses edx:eax as the dividend.
3668 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003669 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3670 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3671 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003672 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003673 locations->AddTemp(Location::RequiresRegister());
3674 }
Calin Juravled0d48522014-11-04 16:40:20 +00003675 break;
3676 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003677 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003678 InvokeRuntimeCallingConvention calling_convention;
3679 locations->SetInAt(0, Location::RegisterPairLocation(
3680 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3681 locations->SetInAt(1, Location::RegisterPairLocation(
3682 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3683 // Runtime helper puts the result in EAX, EDX.
3684 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003685 break;
3686 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 case DataType::Type::kFloat32:
3688 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003689 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003690 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3691 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003692 } else if (div->InputAt(1)->IsConstant()) {
3693 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003694 } else {
3695 locations->SetInAt(1, Location::Any());
3696 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003697 locations->SetOut(Location::SameAsFirstInput());
3698 break;
3699 }
3700
3701 default:
3702 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3703 }
3704}
3705
3706void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3707 LocationSummary* locations = div->GetLocations();
3708 Location first = locations->InAt(0);
3709 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003710
3711 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003712 case DataType::Type::kInt32:
3713 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003714 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003715 break;
3716 }
3717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003719 if (second.IsFpuRegister()) {
3720 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3721 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3722 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003723 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003724 __ divss(first.AsFpuRegister<XmmRegister>(),
3725 codegen_->LiteralFloatAddress(
3726 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003727 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3729 } else {
3730 DCHECK(second.IsStackSlot());
3731 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3732 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003733 break;
3734 }
3735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 if (second.IsFpuRegister()) {
3738 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3739 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3740 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003741 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003742 __ divsd(first.AsFpuRegister<XmmRegister>(),
3743 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003744 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3745 const_area->GetBaseMethodAddress(),
3746 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003747 } else {
3748 DCHECK(second.IsDoubleStackSlot());
3749 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3750 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003751 break;
3752 }
3753
3754 default:
3755 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3756 }
3757}
3758
Calin Juravlebacfec32014-11-14 15:54:36 +00003759void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003762 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003763 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003764 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003765 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003766
Calin Juravled2ec87d2014-12-08 14:24:46 +00003767 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003768 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003769 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003771 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003772 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3773 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3774 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003775 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003776 locations->AddTemp(Location::RequiresRegister());
3777 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 break;
3779 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 InvokeRuntimeCallingConvention calling_convention;
3782 locations->SetInAt(0, Location::RegisterPairLocation(
3783 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3784 locations->SetInAt(1, Location::RegisterPairLocation(
3785 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3786 // Runtime helper puts the result in EAX, EDX.
3787 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3788 break;
3789 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 case DataType::Type::kFloat64:
3791 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003792 locations->SetInAt(0, Location::Any());
3793 locations->SetInAt(1, Location::Any());
3794 locations->SetOut(Location::RequiresFpuRegister());
3795 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003796 break;
3797 }
3798
3799 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003800 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 }
3802}
3803
3804void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003805 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003806 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 case DataType::Type::kInt32:
3808 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 GenerateDivRemIntegral(rem);
3810 break;
3811 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003812 case DataType::Type::kFloat32:
3813 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003814 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003815 break;
3816 }
3817 default:
3818 LOG(FATAL) << "Unexpected rem type " << type;
3819 }
3820}
3821
Aart Bik1f8d51b2018-02-15 10:42:37 -08003822static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3823 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3824 switch (minmax->GetResultType()) {
3825 case DataType::Type::kInt32:
3826 locations->SetInAt(0, Location::RequiresRegister());
3827 locations->SetInAt(1, Location::RequiresRegister());
3828 locations->SetOut(Location::SameAsFirstInput());
3829 break;
3830 case DataType::Type::kInt64:
3831 locations->SetInAt(0, Location::RequiresRegister());
3832 locations->SetInAt(1, Location::RequiresRegister());
3833 locations->SetOut(Location::SameAsFirstInput());
3834 // Register to use to perform a long subtract to set cc.
3835 locations->AddTemp(Location::RequiresRegister());
3836 break;
3837 case DataType::Type::kFloat32:
3838 locations->SetInAt(0, Location::RequiresFpuRegister());
3839 locations->SetInAt(1, Location::RequiresFpuRegister());
3840 locations->SetOut(Location::SameAsFirstInput());
3841 locations->AddTemp(Location::RequiresRegister());
3842 break;
3843 case DataType::Type::kFloat64:
3844 locations->SetInAt(0, Location::RequiresFpuRegister());
3845 locations->SetInAt(1, Location::RequiresFpuRegister());
3846 locations->SetOut(Location::SameAsFirstInput());
3847 break;
3848 default:
3849 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3850 }
3851}
3852
Aart Bik351df3e2018-03-07 11:54:57 -08003853void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
3854 bool is_min,
3855 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003856 Location op1_loc = locations->InAt(0);
3857 Location op2_loc = locations->InAt(1);
3858
3859 // Shortcut for same input locations.
3860 if (op1_loc.Equals(op2_loc)) {
3861 // Can return immediately, as op1_loc == out_loc.
3862 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3863 // a copy here.
3864 DCHECK(locations->Out().Equals(op1_loc));
3865 return;
3866 }
3867
3868 if (type == DataType::Type::kInt64) {
3869 // Need to perform a subtract to get the sign right.
3870 // op1 is already in the same location as the output.
3871 Location output = locations->Out();
3872 Register output_lo = output.AsRegisterPairLow<Register>();
3873 Register output_hi = output.AsRegisterPairHigh<Register>();
3874
3875 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
3876 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
3877
3878 // The comparison is performed by subtracting the second operand from
3879 // the first operand and then setting the status flags in the same
3880 // manner as the SUB instruction."
3881 __ cmpl(output_lo, op2_lo);
3882
3883 // Now use a temp and the borrow to finish the subtraction of op2_hi.
3884 Register temp = locations->GetTemp(0).AsRegister<Register>();
3885 __ movl(temp, output_hi);
3886 __ sbbl(temp, op2_hi);
3887
3888 // Now the condition code is correct.
3889 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
3890 __ cmovl(cond, output_lo, op2_lo);
3891 __ cmovl(cond, output_hi, op2_hi);
3892 } else {
3893 DCHECK_EQ(type, DataType::Type::kInt32);
3894 Register out = locations->Out().AsRegister<Register>();
3895 Register op2 = op2_loc.AsRegister<Register>();
3896
3897 // (out := op1)
3898 // out <=? op2
3899 // if out is min jmp done
3900 // out := op2
3901 // done:
3902
3903 __ cmpl(out, op2);
3904 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
3905 __ cmovl(cond, out, op2);
3906 }
3907}
3908
3909void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
3910 bool is_min,
3911 DataType::Type type) {
3912 Location op1_loc = locations->InAt(0);
3913 Location op2_loc = locations->InAt(1);
3914 Location out_loc = locations->Out();
3915 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3916
3917 // Shortcut for same input locations.
3918 if (op1_loc.Equals(op2_loc)) {
3919 DCHECK(out_loc.Equals(op1_loc));
3920 return;
3921 }
3922
3923 // (out := op1)
3924 // out <=? op2
3925 // if Nan jmp Nan_label
3926 // if out is min jmp done
3927 // if op2 is min jmp op2_label
3928 // handle -0/+0
3929 // jmp done
3930 // Nan_label:
3931 // out := NaN
3932 // op2_label:
3933 // out := op2
3934 // done:
3935 //
3936 // This removes one jmp, but needs to copy one input (op1) to out.
3937 //
3938 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
3939
3940 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3941
3942 NearLabel nan, done, op2_label;
3943 if (type == DataType::Type::kFloat64) {
3944 __ ucomisd(out, op2);
3945 } else {
3946 DCHECK_EQ(type, DataType::Type::kFloat32);
3947 __ ucomiss(out, op2);
3948 }
3949
3950 __ j(Condition::kParityEven, &nan);
3951
3952 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3953 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3954
3955 // Handle 0.0/-0.0.
3956 if (is_min) {
3957 if (type == DataType::Type::kFloat64) {
3958 __ orpd(out, op2);
3959 } else {
3960 __ orps(out, op2);
3961 }
3962 } else {
3963 if (type == DataType::Type::kFloat64) {
3964 __ andpd(out, op2);
3965 } else {
3966 __ andps(out, op2);
3967 }
3968 }
3969 __ jmp(&done);
3970
3971 // NaN handling.
3972 __ Bind(&nan);
3973 if (type == DataType::Type::kFloat64) {
3974 // TODO: Use a constant from the constant table (requires extra input).
3975 __ LoadLongConstant(out, kDoubleNaN);
3976 } else {
3977 Register constant = locations->GetTemp(0).AsRegister<Register>();
3978 __ movl(constant, Immediate(kFloatNaN));
3979 __ movd(out, constant);
3980 }
3981 __ jmp(&done);
3982
3983 // out := op2;
3984 __ Bind(&op2_label);
3985 if (type == DataType::Type::kFloat64) {
3986 __ movsd(out, op2);
3987 } else {
3988 __ movss(out, op2);
3989 }
3990
3991 // Done.
3992 __ Bind(&done);
3993}
3994
Aart Bik351df3e2018-03-07 11:54:57 -08003995void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3996 DataType::Type type = minmax->GetResultType();
3997 switch (type) {
3998 case DataType::Type::kInt32:
3999 case DataType::Type::kInt64:
4000 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4001 break;
4002 case DataType::Type::kFloat32:
4003 case DataType::Type::kFloat64:
4004 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4005 break;
4006 default:
4007 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4008 }
4009}
4010
Aart Bik1f8d51b2018-02-15 10:42:37 -08004011void LocationsBuilderX86::VisitMin(HMin* min) {
4012 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4013}
4014
4015void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004016 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004017}
4018
4019void LocationsBuilderX86::VisitMax(HMax* max) {
4020 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4021}
4022
4023void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004024 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004025}
4026
Aart Bik3dad3412018-02-28 12:01:46 -08004027void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4028 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4029 switch (abs->GetResultType()) {
4030 case DataType::Type::kInt32:
4031 locations->SetInAt(0, Location::RegisterLocation(EAX));
4032 locations->SetOut(Location::SameAsFirstInput());
4033 locations->AddTemp(Location::RegisterLocation(EDX));
4034 break;
4035 case DataType::Type::kInt64:
4036 locations->SetInAt(0, Location::RequiresRegister());
4037 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4038 locations->AddTemp(Location::RequiresRegister());
4039 break;
4040 case DataType::Type::kFloat32:
4041 locations->SetInAt(0, Location::RequiresFpuRegister());
4042 locations->SetOut(Location::SameAsFirstInput());
4043 locations->AddTemp(Location::RequiresFpuRegister());
4044 locations->AddTemp(Location::RequiresRegister());
4045 break;
4046 case DataType::Type::kFloat64:
4047 locations->SetInAt(0, Location::RequiresFpuRegister());
4048 locations->SetOut(Location::SameAsFirstInput());
4049 locations->AddTemp(Location::RequiresFpuRegister());
4050 break;
4051 default:
4052 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4053 }
4054}
4055
4056void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4057 LocationSummary* locations = abs->GetLocations();
4058 switch (abs->GetResultType()) {
4059 case DataType::Type::kInt32: {
4060 Register out = locations->Out().AsRegister<Register>();
4061 DCHECK_EQ(out, EAX);
4062 Register temp = locations->GetTemp(0).AsRegister<Register>();
4063 DCHECK_EQ(temp, EDX);
4064 // Sign extend EAX into EDX.
4065 __ cdq();
4066 // XOR EAX with sign.
4067 __ xorl(EAX, EDX);
4068 // Subtract out sign to correct.
4069 __ subl(EAX, EDX);
4070 // The result is in EAX.
4071 break;
4072 }
4073 case DataType::Type::kInt64: {
4074 Location input = locations->InAt(0);
4075 Register input_lo = input.AsRegisterPairLow<Register>();
4076 Register input_hi = input.AsRegisterPairHigh<Register>();
4077 Location output = locations->Out();
4078 Register output_lo = output.AsRegisterPairLow<Register>();
4079 Register output_hi = output.AsRegisterPairHigh<Register>();
4080 Register temp = locations->GetTemp(0).AsRegister<Register>();
4081 // Compute the sign into the temporary.
4082 __ movl(temp, input_hi);
4083 __ sarl(temp, Immediate(31));
4084 // Store the sign into the output.
4085 __ movl(output_lo, temp);
4086 __ movl(output_hi, temp);
4087 // XOR the input to the output.
4088 __ xorl(output_lo, input_lo);
4089 __ xorl(output_hi, input_hi);
4090 // Subtract the sign.
4091 __ subl(output_lo, temp);
4092 __ sbbl(output_hi, temp);
4093 break;
4094 }
4095 case DataType::Type::kFloat32: {
4096 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4097 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4098 Register constant = locations->GetTemp(1).AsRegister<Register>();
4099 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4100 __ movd(temp, constant);
4101 __ andps(out, temp);
4102 break;
4103 }
4104 case DataType::Type::kFloat64: {
4105 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4106 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4107 // TODO: Use a constant from the constant table (requires extra input).
4108 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4109 __ andpd(out, temp);
4110 break;
4111 }
4112 default:
4113 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4114 }
4115}
4116
Calin Juravled0d48522014-11-04 16:40:20 +00004117void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004118 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004119 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004121 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 case DataType::Type::kInt8:
4123 case DataType::Type::kUint16:
4124 case DataType::Type::kInt16:
4125 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004126 locations->SetInAt(0, Location::Any());
4127 break;
4128 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004129 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004130 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4131 if (!instruction->IsConstant()) {
4132 locations->AddTemp(Location::RequiresRegister());
4133 }
4134 break;
4135 }
4136 default:
4137 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4138 }
Calin Juravled0d48522014-11-04 16:40:20 +00004139}
4140
4141void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004142 SlowPathCode* slow_path =
4143 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004144 codegen_->AddSlowPath(slow_path);
4145
4146 LocationSummary* locations = instruction->GetLocations();
4147 Location value = locations->InAt(0);
4148
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004149 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004151 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 case DataType::Type::kInt8:
4153 case DataType::Type::kUint16:
4154 case DataType::Type::kInt16:
4155 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004156 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004158 __ j(kEqual, slow_path->GetEntryLabel());
4159 } else if (value.IsStackSlot()) {
4160 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4161 __ j(kEqual, slow_path->GetEntryLabel());
4162 } else {
4163 DCHECK(value.IsConstant()) << value;
4164 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004165 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004166 }
4167 }
4168 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004169 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004171 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004172 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004173 __ movl(temp, value.AsRegisterPairLow<Register>());
4174 __ orl(temp, value.AsRegisterPairHigh<Register>());
4175 __ j(kEqual, slow_path->GetEntryLabel());
4176 } else {
4177 DCHECK(value.IsConstant()) << value;
4178 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4179 __ jmp(slow_path->GetEntryLabel());
4180 }
4181 }
4182 break;
4183 }
4184 default:
4185 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004186 }
Calin Juravled0d48522014-11-04 16:40:20 +00004187}
4188
Calin Juravle9aec02f2014-11-18 23:06:35 +00004189void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4190 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4191
4192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004193 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004194
4195 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004196 case DataType::Type::kInt32:
4197 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004198 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004199 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004200 // The shift count needs to be in CL or a constant.
4201 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004202 locations->SetOut(Location::SameAsFirstInput());
4203 break;
4204 }
4205 default:
4206 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4207 }
4208}
4209
4210void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4211 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4212
4213 LocationSummary* locations = op->GetLocations();
4214 Location first = locations->InAt(0);
4215 Location second = locations->InAt(1);
4216 DCHECK(first.Equals(locations->Out()));
4217
4218 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004220 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004221 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004222 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004224 DCHECK_EQ(ECX, second_reg);
4225 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004226 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004227 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004228 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004229 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004230 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004231 }
4232 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004233 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004234 if (shift == 0) {
4235 return;
4236 }
4237 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004238 if (op->IsShl()) {
4239 __ shll(first_reg, imm);
4240 } else if (op->IsShr()) {
4241 __ sarl(first_reg, imm);
4242 } else {
4243 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004244 }
4245 }
4246 break;
4247 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004248 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004249 if (second.IsRegister()) {
4250 Register second_reg = second.AsRegister<Register>();
4251 DCHECK_EQ(ECX, second_reg);
4252 if (op->IsShl()) {
4253 GenerateShlLong(first, second_reg);
4254 } else if (op->IsShr()) {
4255 GenerateShrLong(first, second_reg);
4256 } else {
4257 GenerateUShrLong(first, second_reg);
4258 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004259 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004260 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004261 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004262 // Nothing to do if the shift is 0, as the input is already the output.
4263 if (shift != 0) {
4264 if (op->IsShl()) {
4265 GenerateShlLong(first, shift);
4266 } else if (op->IsShr()) {
4267 GenerateShrLong(first, shift);
4268 } else {
4269 GenerateUShrLong(first, shift);
4270 }
4271 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004272 }
4273 break;
4274 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004275 default:
4276 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4277 }
4278}
4279
Mark P Mendell73945692015-04-29 14:56:17 +00004280void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4281 Register low = loc.AsRegisterPairLow<Register>();
4282 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004283 if (shift == 1) {
4284 // This is just an addition.
4285 __ addl(low, low);
4286 __ adcl(high, high);
4287 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004288 // Shift by 32 is easy. High gets low, and low gets 0.
4289 codegen_->EmitParallelMoves(
4290 loc.ToLow(),
4291 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004292 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004293 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4294 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004296 } else if (shift > 32) {
4297 // Low part becomes 0. High part is low part << (shift-32).
4298 __ movl(high, low);
4299 __ shll(high, Immediate(shift - 32));
4300 __ xorl(low, low);
4301 } else {
4302 // Between 1 and 31.
4303 __ shld(high, low, Immediate(shift));
4304 __ shll(low, Immediate(shift));
4305 }
4306}
4307
Calin Juravle9aec02f2014-11-18 23:06:35 +00004308void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004309 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004310 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4311 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4312 __ testl(shifter, Immediate(32));
4313 __ j(kEqual, &done);
4314 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4315 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4316 __ Bind(&done);
4317}
4318
Mark P Mendell73945692015-04-29 14:56:17 +00004319void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4320 Register low = loc.AsRegisterPairLow<Register>();
4321 Register high = loc.AsRegisterPairHigh<Register>();
4322 if (shift == 32) {
4323 // Need to copy the sign.
4324 DCHECK_NE(low, high);
4325 __ movl(low, high);
4326 __ sarl(high, Immediate(31));
4327 } else if (shift > 32) {
4328 DCHECK_NE(low, high);
4329 // High part becomes sign. Low part is shifted by shift - 32.
4330 __ movl(low, high);
4331 __ sarl(high, Immediate(31));
4332 __ sarl(low, Immediate(shift - 32));
4333 } else {
4334 // Between 1 and 31.
4335 __ shrd(low, high, Immediate(shift));
4336 __ sarl(high, Immediate(shift));
4337 }
4338}
4339
Calin Juravle9aec02f2014-11-18 23:06:35 +00004340void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004341 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004342 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4343 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4344 __ testl(shifter, Immediate(32));
4345 __ j(kEqual, &done);
4346 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4347 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4348 __ Bind(&done);
4349}
4350
Mark P Mendell73945692015-04-29 14:56:17 +00004351void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4352 Register low = loc.AsRegisterPairLow<Register>();
4353 Register high = loc.AsRegisterPairHigh<Register>();
4354 if (shift == 32) {
4355 // Shift by 32 is easy. Low gets high, and high gets 0.
4356 codegen_->EmitParallelMoves(
4357 loc.ToHigh(),
4358 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004360 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4361 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004363 } else if (shift > 32) {
4364 // Low part is high >> (shift - 32). High part becomes 0.
4365 __ movl(low, high);
4366 __ shrl(low, Immediate(shift - 32));
4367 __ xorl(high, high);
4368 } else {
4369 // Between 1 and 31.
4370 __ shrd(low, high, Immediate(shift));
4371 __ shrl(high, Immediate(shift));
4372 }
4373}
4374
Calin Juravle9aec02f2014-11-18 23:06:35 +00004375void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004376 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004377 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4378 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4379 __ testl(shifter, Immediate(32));
4380 __ j(kEqual, &done);
4381 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4382 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4383 __ Bind(&done);
4384}
4385
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004386void LocationsBuilderX86::VisitRor(HRor* ror) {
4387 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004388 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004389
4390 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004391 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004392 // Add the temporary needed.
4393 locations->AddTemp(Location::RequiresRegister());
4394 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004395 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004396 locations->SetInAt(0, Location::RequiresRegister());
4397 // The shift count needs to be in CL (unless it is a constant).
4398 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4399 locations->SetOut(Location::SameAsFirstInput());
4400 break;
4401 default:
4402 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4403 UNREACHABLE();
4404 }
4405}
4406
4407void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4408 LocationSummary* locations = ror->GetLocations();
4409 Location first = locations->InAt(0);
4410 Location second = locations->InAt(1);
4411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004412 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004413 Register first_reg = first.AsRegister<Register>();
4414 if (second.IsRegister()) {
4415 Register second_reg = second.AsRegister<Register>();
4416 __ rorl(first_reg, second_reg);
4417 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004418 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004419 __ rorl(first_reg, imm);
4420 }
4421 return;
4422 }
4423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004425 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4426 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4427 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4428 if (second.IsRegister()) {
4429 Register second_reg = second.AsRegister<Register>();
4430 DCHECK_EQ(second_reg, ECX);
4431 __ movl(temp_reg, first_reg_hi);
4432 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4433 __ shrd(first_reg_lo, temp_reg, second_reg);
4434 __ movl(temp_reg, first_reg_hi);
4435 __ testl(second_reg, Immediate(32));
4436 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4437 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4438 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004439 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004440 if (shift_amt == 0) {
4441 // Already fine.
4442 return;
4443 }
4444 if (shift_amt == 32) {
4445 // Just swap.
4446 __ movl(temp_reg, first_reg_lo);
4447 __ movl(first_reg_lo, first_reg_hi);
4448 __ movl(first_reg_hi, temp_reg);
4449 return;
4450 }
4451
4452 Immediate imm(shift_amt);
4453 // Save the constents of the low value.
4454 __ movl(temp_reg, first_reg_lo);
4455
4456 // Shift right into low, feeding bits from high.
4457 __ shrd(first_reg_lo, first_reg_hi, imm);
4458
4459 // Shift right into high, feeding bits from the original low.
4460 __ shrd(first_reg_hi, temp_reg, imm);
4461
4462 // Swap if needed.
4463 if (shift_amt > 32) {
4464 __ movl(temp_reg, first_reg_lo);
4465 __ movl(first_reg_lo, first_reg_hi);
4466 __ movl(first_reg_hi, temp_reg);
4467 }
4468 }
4469}
4470
Calin Juravle9aec02f2014-11-18 23:06:35 +00004471void LocationsBuilderX86::VisitShl(HShl* shl) {
4472 HandleShift(shl);
4473}
4474
4475void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4476 HandleShift(shl);
4477}
4478
4479void LocationsBuilderX86::VisitShr(HShr* shr) {
4480 HandleShift(shr);
4481}
4482
4483void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4484 HandleShift(shr);
4485}
4486
4487void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4488 HandleShift(ushr);
4489}
4490
4491void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4492 HandleShift(ushr);
4493}
4494
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004495void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004496 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4497 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004498 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004499 if (instruction->IsStringAlloc()) {
4500 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4501 } else {
4502 InvokeRuntimeCallingConvention calling_convention;
4503 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004504 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004505}
4506
4507void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004508 // Note: if heap poisoning is enabled, the entry point takes cares
4509 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004510 if (instruction->IsStringAlloc()) {
4511 // String is allocated through StringFactory. Call NewEmptyString entry point.
4512 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004513 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004514 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4515 __ call(Address(temp, code_offset.Int32Value()));
4516 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4517 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004518 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004519 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004520 DCHECK(!codegen_->IsLeafMethod());
4521 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004522}
4523
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004524void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004525 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4526 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004527 locations->SetOut(Location::RegisterLocation(EAX));
4528 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004529 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4530 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004531}
4532
4533void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004534 // Note: if heap poisoning is enabled, the entry point takes cares
4535 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004536 QuickEntrypointEnum entrypoint =
4537 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4538 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004539 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004540 DCHECK(!codegen_->IsLeafMethod());
4541}
4542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004543void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004544 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004545 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004546 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4547 if (location.IsStackSlot()) {
4548 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4549 } else if (location.IsDoubleStackSlot()) {
4550 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004551 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004552 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004553}
4554
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004555void InstructionCodeGeneratorX86::VisitParameterValue(
4556 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4557}
4558
4559void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4560 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004561 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004562 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4563}
4564
4565void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004566}
4567
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004568void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4569 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004570 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004571 locations->SetInAt(0, Location::RequiresRegister());
4572 locations->SetOut(Location::RequiresRegister());
4573}
4574
4575void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4576 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004577 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004578 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004579 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004580 __ movl(locations->Out().AsRegister<Register>(),
4581 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004582 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004583 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004584 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004585 __ movl(locations->Out().AsRegister<Register>(),
4586 Address(locations->InAt(0).AsRegister<Register>(),
4587 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4588 // temp = temp->GetImtEntryAt(method_offset);
4589 __ movl(locations->Out().AsRegister<Register>(),
4590 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004591 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004592}
4593
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004594void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004595 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004596 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004597 locations->SetInAt(0, Location::RequiresRegister());
4598 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004599}
4600
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004601void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4602 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004603 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004604 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004605 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004606 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004607 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004608 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004609 break;
4610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004611 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004612 __ notl(out.AsRegisterPairLow<Register>());
4613 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004614 break;
4615
4616 default:
4617 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4618 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004619}
4620
David Brazdil66d126e2015-04-03 16:02:44 +01004621void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4622 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004623 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004624 locations->SetInAt(0, Location::RequiresRegister());
4625 locations->SetOut(Location::SameAsFirstInput());
4626}
4627
4628void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004629 LocationSummary* locations = bool_not->GetLocations();
4630 Location in = locations->InAt(0);
4631 Location out = locations->Out();
4632 DCHECK(in.Equals(out));
4633 __ xorl(out.AsRegister<Register>(), Immediate(1));
4634}
4635
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004636void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004637 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004638 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004639 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004640 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004641 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004644 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 case DataType::Type::kInt32:
4646 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004647 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004648 locations->SetInAt(1, Location::Any());
4649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4650 break;
4651 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004652 case DataType::Type::kFloat32:
4653 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004654 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004655 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4656 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4657 } else if (compare->InputAt(1)->IsConstant()) {
4658 locations->SetInAt(1, Location::RequiresFpuRegister());
4659 } else {
4660 locations->SetInAt(1, Location::Any());
4661 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004662 locations->SetOut(Location::RequiresRegister());
4663 break;
4664 }
4665 default:
4666 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4667 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004668}
4669
4670void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004671 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004672 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004673 Location left = locations->InAt(0);
4674 Location right = locations->InAt(1);
4675
Mark Mendell0c9497d2015-08-21 09:30:05 -04004676 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004677 Condition less_cond = kLess;
4678
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004679 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004680 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004681 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004683 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004684 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004685 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004686 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004687 break;
4688 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004689 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004690 Register left_low = left.AsRegisterPairLow<Register>();
4691 Register left_high = left.AsRegisterPairHigh<Register>();
4692 int32_t val_low = 0;
4693 int32_t val_high = 0;
4694 bool right_is_const = false;
4695
4696 if (right.IsConstant()) {
4697 DCHECK(right.GetConstant()->IsLongConstant());
4698 right_is_const = true;
4699 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4700 val_low = Low32Bits(val);
4701 val_high = High32Bits(val);
4702 }
4703
Calin Juravleddb7df22014-11-25 20:56:51 +00004704 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004705 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004706 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004707 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004708 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004709 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004710 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004711 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004712 __ j(kLess, &less); // Signed compare.
4713 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004714 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004715 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004716 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004717 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004718 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004719 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004720 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004721 }
Aart Bika19616e2016-02-01 18:57:58 -08004722 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004723 break;
4724 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004726 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004727 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004728 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004729 break;
4730 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004731 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004732 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004733 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004734 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004735 break;
4736 }
4737 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004738 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004739 }
Aart Bika19616e2016-02-01 18:57:58 -08004740
Calin Juravleddb7df22014-11-25 20:56:51 +00004741 __ movl(out, Immediate(0));
4742 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004743 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004744
4745 __ Bind(&greater);
4746 __ movl(out, Immediate(1));
4747 __ jmp(&done);
4748
4749 __ Bind(&less);
4750 __ movl(out, Immediate(-1));
4751
4752 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004753}
4754
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004755void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004756 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004757 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004758 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004759 locations->SetInAt(i, Location::Any());
4760 }
4761 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004762}
4763
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004764void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004765 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004766}
4767
Roland Levillain7c1559a2015-12-15 10:55:36 +00004768void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004769 /*
4770 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4771 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4772 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4773 */
4774 switch (kind) {
4775 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004776 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004777 break;
4778 }
4779 case MemBarrierKind::kAnyStore:
4780 case MemBarrierKind::kLoadAny:
4781 case MemBarrierKind::kStoreStore: {
4782 // nop
4783 break;
4784 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004785 case MemBarrierKind::kNTStoreStore:
4786 // Non-Temporal Store/Store needs an explicit fence.
4787 MemoryFence(/* non-temporal */ true);
4788 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004789 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004790}
4791
Vladimir Markodc151b22015-10-15 18:02:30 +01004792HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4793 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004794 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004795 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004796}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004797
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004798Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4799 Register temp) {
4800 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004801 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004802 if (!invoke->GetLocations()->Intrinsified()) {
4803 return location.AsRegister<Register>();
4804 }
4805 // For intrinsics we allow any location, so it may be on the stack.
4806 if (!location.IsRegister()) {
4807 __ movl(temp, Address(ESP, location.GetStackIndex()));
4808 return temp;
4809 }
4810 // For register locations, check if the register was saved. If so, get it from the stack.
4811 // Note: There is a chance that the register was saved but not overwritten, so we could
4812 // save one load. However, since this is just an intrinsic slow path we prefer this
4813 // simple and more robust approach rather that trying to determine if that's the case.
4814 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004815 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4816 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4817 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4818 __ movl(temp, Address(ESP, stack_offset));
4819 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004820 }
4821 return location.AsRegister<Register>();
4822}
4823
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004824void CodeGeneratorX86::GenerateStaticOrDirectCall(
4825 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004826 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4827 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004828 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004829 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004830 uint32_t offset =
4831 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4832 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004833 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004834 }
Vladimir Marko58155012015-08-19 12:49:41 +00004835 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004836 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004837 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004838 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4839 DCHECK(GetCompilerOptions().IsBootImage());
4840 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4841 temp.AsRegister<Register>());
4842 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004843 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004844 break;
4845 }
Vladimir Marko58155012015-08-19 12:49:41 +00004846 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4847 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4848 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004849 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4850 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4851 temp.AsRegister<Register>());
4852 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4853 RecordBootImageRelRoPatch(
4854 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004855 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004856 break;
4857 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004858 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004859 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4860 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004861 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004862 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004863 break;
4864 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004865 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4866 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4867 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004868 }
Vladimir Marko58155012015-08-19 12:49:41 +00004869 }
4870
4871 switch (invoke->GetCodePtrLocation()) {
4872 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4873 __ call(GetFrameEntryLabel());
4874 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004875 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4876 // (callee_method + offset_of_quick_compiled_code)()
4877 __ call(Address(callee_method.AsRegister<Register>(),
4878 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004879 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004880 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004881 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004882 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004883
4884 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004885}
4886
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004887void CodeGeneratorX86::GenerateVirtualCall(
4888 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004889 Register temp = temp_in.AsRegister<Register>();
4890 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4891 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004892
4893 // Use the calling convention instead of the location of the receiver, as
4894 // intrinsics may have put the receiver in a different register. In the intrinsics
4895 // slow path, the arguments have been moved to the right place, so here we are
4896 // guaranteed that the receiver is the first register of the calling convention.
4897 InvokeDexCallingConvention calling_convention;
4898 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004899 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004900 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004901 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004902 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004903 // Instead of simply (possibly) unpoisoning `temp` here, we should
4904 // emit a read barrier for the previous class reference load.
4905 // However this is not required in practice, as this is an
4906 // intermediate/temporary reference and because the current
4907 // concurrent copying collector keeps the from-space memory
4908 // intact/accessible until the end of the marking phase (the
4909 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004910 __ MaybeUnpoisonHeapReference(temp);
4911 // temp = temp->GetMethodAt(method_offset);
4912 __ movl(temp, Address(temp, method_offset));
4913 // call temp->GetEntryPoint();
4914 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004915 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004916 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004917}
4918
Vladimir Marko6fd16062018-06-26 11:02:04 +01004919void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
4920 uint32_t intrinsic_data) {
4921 boot_image_intrinsic_patches_.emplace_back(
4922 method_address, /* target_dex_file */ nullptr, intrinsic_data);
4923 __ Bind(&boot_image_intrinsic_patches_.back().label);
4924}
4925
Vladimir Markob066d432018-01-03 13:14:37 +00004926void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4927 uint32_t boot_image_offset) {
4928 boot_image_method_patches_.emplace_back(
4929 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4930 __ Bind(&boot_image_method_patches_.back().label);
4931}
4932
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004933void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004934 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004935 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004936 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004937 boot_image_method_patches_.emplace_back(
4938 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004939 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004940}
4941
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004942void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4943 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4944 HX86ComputeBaseMethodAddress* method_address =
4945 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004946 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004947 method_bss_entry_patches_.emplace_back(
4948 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4949 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004950}
4951
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004952void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4953 HX86ComputeBaseMethodAddress* method_address =
4954 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4955 boot_image_type_patches_.emplace_back(
4956 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004957 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004958}
4959
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004960Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004961 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004962 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4963 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004964 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004965 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004966}
4967
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004968void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4969 HX86ComputeBaseMethodAddress* method_address =
4970 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4971 boot_image_string_patches_.emplace_back(
4972 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4973 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004974}
4975
Vladimir Markoaad75c62016-10-03 08:46:48 +00004976Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4977 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004978 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004979 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004980 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004981 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004982 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004983}
4984
Vladimir Markoeebb8212018-06-05 14:57:24 +01004985void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004986 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01004987 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004988 if (GetCompilerOptions().IsBootImage()) {
4989 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4990 HX86ComputeBaseMethodAddress* method_address =
4991 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4992 DCHECK(method_address != nullptr);
4993 Register method_address_reg =
4994 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
4995 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
4996 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
4997 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004998 DCHECK(Runtime::Current()->IsAotCompiler());
4999 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5000 HX86ComputeBaseMethodAddress* method_address =
5001 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5002 DCHECK(method_address != nullptr);
5003 Register method_address_reg =
5004 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5005 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005006 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005007 } else {
5008 gc::Heap* heap = Runtime::Current()->GetHeap();
5009 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005010 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005011 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5012 }
5013}
5014
Vladimir Marko6fd16062018-06-26 11:02:04 +01005015void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5016 uint32_t boot_image_offset) {
5017 DCHECK(invoke->IsStatic());
5018 InvokeRuntimeCallingConvention calling_convention;
5019 Register argument = calling_convention.GetRegisterAt(0);
5020 if (GetCompilerOptions().IsBootImage()) {
5021 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5022 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5023 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5024 HX86ComputeBaseMethodAddress* method_address =
5025 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5026 DCHECK(method_address != nullptr);
5027 Register method_address_reg =
5028 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5029 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5030 MethodReference target_method = invoke->GetTargetMethod();
5031 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5032 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5033 __ Bind(&boot_image_type_patches_.back().label);
5034 } else {
5035 LoadBootImageAddress(argument, boot_image_offset, invoke);
5036 }
5037 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5038 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5039}
5040
Vladimir Markoaad75c62016-10-03 08:46:48 +00005041// The label points to the end of the "movl" or another instruction but the literal offset
5042// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5043constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5044
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005045template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005046inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005047 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005048 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005049 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005050 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005051 linker_patches->push_back(Factory(literal_offset,
5052 info.target_dex_file,
5053 GetMethodAddressOffset(info.method_address),
5054 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005055 }
5056}
5057
Vladimir Marko6fd16062018-06-26 11:02:04 +01005058template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5059linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5060 const DexFile* target_dex_file,
5061 uint32_t pc_insn_offset,
5062 uint32_t boot_image_offset) {
5063 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5064 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005065}
5066
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005067void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005068 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005069 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005070 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005071 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005072 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005073 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005074 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005075 string_bss_entry_patches_.size() +
5076 boot_image_intrinsic_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005077 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01005078 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005079 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5080 boot_image_method_patches_, linker_patches);
5081 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5082 boot_image_type_patches_, linker_patches);
5083 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005084 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005085 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5086 boot_image_intrinsic_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005087 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005088 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00005089 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005090 DCHECK(boot_image_type_patches_.empty());
5091 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005092 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005093 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005094 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5095 method_bss_entry_patches_, linker_patches);
5096 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5097 type_bss_entry_patches_, linker_patches);
5098 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5099 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005100 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005101}
5102
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005103void CodeGeneratorX86::MarkGCCard(Register temp,
5104 Register card,
5105 Register object,
5106 Register value,
5107 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005108 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005109 if (value_can_be_null) {
5110 __ testl(value, value);
5111 __ j(kEqual, &is_null);
5112 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005113 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 __ movl(temp, object);
5115 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005116 __ movb(Address(temp, card, TIMES_1, 0),
5117 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005118 if (value_can_be_null) {
5119 __ Bind(&is_null);
5120 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121}
5122
Calin Juravle52c48962014-12-16 17:02:57 +00005123void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5124 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005125
5126 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005128 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005129 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5130 kEmitCompilerReadBarrier
5131 ? LocationSummary::kCallOnSlowPath
5132 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005133 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005134 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005135 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005136 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005139 locations->SetOut(Location::RequiresFpuRegister());
5140 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005141 // The output overlaps in case of long: we don't want the low move
5142 // to overwrite the object's location. Likewise, in the case of
5143 // an object field get with read barriers enabled, we do not want
5144 // the move to overwrite the object's location, as we need it to emit
5145 // the read barrier.
5146 locations->SetOut(
5147 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005148 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005149 Location::kOutputOverlap :
5150 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005151 }
Calin Juravle52c48962014-12-16 17:02:57 +00005152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005153 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005154 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005155 // So we use an XMM register as a temp to achieve atomicity (first
5156 // load the temp into the XMM and then copy the XMM into the
5157 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005158 locations->AddTemp(Location::RequiresFpuRegister());
5159 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005160}
5161
Calin Juravle52c48962014-12-16 17:02:57 +00005162void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5163 const FieldInfo& field_info) {
5164 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165
Calin Juravle52c48962014-12-16 17:02:57 +00005166 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005167 Location base_loc = locations->InAt(0);
5168 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005169 Location out = locations->Out();
5170 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005171 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5172 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005173 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5174
Vladimir Marko61b92282017-10-11 13:23:17 +01005175 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005176 case DataType::Type::kBool:
5177 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005178 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179 break;
5180 }
5181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005183 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184 break;
5185 }
5186
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005187 case DataType::Type::kUint16: {
5188 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005189 break;
5190 }
5191
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005192 case DataType::Type::kInt16: {
5193 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 break;
5195 }
5196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005197 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005198 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005199 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005201 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005202 // /* HeapReference<Object> */ out = *(base + offset)
5203 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005204 // Note that a potential implicit null check is handled in this
5205 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5206 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005207 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005208 if (is_volatile) {
5209 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5210 }
5211 } else {
5212 __ movl(out.AsRegister<Register>(), Address(base, offset));
5213 codegen_->MaybeRecordImplicitNullCheck(instruction);
5214 if (is_volatile) {
5215 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5216 }
5217 // If read barriers are enabled, emit read barriers other than
5218 // Baker's using a slow path (and also unpoison the loaded
5219 // reference, if heap poisoning is enabled).
5220 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5221 }
5222 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005223 }
5224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005225 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005226 if (is_volatile) {
5227 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5228 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005229 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005230 __ movd(out.AsRegisterPairLow<Register>(), temp);
5231 __ psrlq(temp, Immediate(32));
5232 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5233 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005234 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005235 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005236 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005237 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5238 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005239 break;
5240 }
5241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005243 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005244 break;
5245 }
5246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005247 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005248 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005249 break;
5250 }
5251
Aart Bik66c158e2018-01-31 12:55:04 -08005252 case DataType::Type::kUint32:
5253 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005254 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005255 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005256 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005257 }
Calin Juravle52c48962014-12-16 17:02:57 +00005258
Vladimir Marko61b92282017-10-11 13:23:17 +01005259 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005260 // Potential implicit null checks, in the case of reference or
5261 // long fields, are handled in the previous switch statement.
5262 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005263 codegen_->MaybeRecordImplicitNullCheck(instruction);
5264 }
5265
Calin Juravle52c48962014-12-16 17:02:57 +00005266 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005267 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 // Memory barriers, in the case of references, are also handled
5269 // in the previous switch statement.
5270 } else {
5271 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5272 }
Roland Levillain4d027112015-07-01 15:41:14 +01005273 }
Calin Juravle52c48962014-12-16 17:02:57 +00005274}
5275
5276void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5277 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5278
5279 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005280 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005281 locations->SetInAt(0, Location::RequiresRegister());
5282 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005283 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005284 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005285
5286 // The register allocator does not support multiple
5287 // inputs that die at entry with one in a specific register.
5288 if (is_byte_type) {
5289 // Ensure the value is in a byte register.
5290 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 } else if (DataType::IsFloatingPointType(field_type)) {
5292 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005293 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5294 locations->SetInAt(1, Location::RequiresFpuRegister());
5295 } else {
5296 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5297 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005298 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005299 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005300 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005301
Calin Juravle52c48962014-12-16 17:02:57 +00005302 // 64bits value can be atomically written to an address with movsd and an XMM register.
5303 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5304 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5305 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5306 // isolated cases when we need this it isn't worth adding the extra complexity.
5307 locations->AddTemp(Location::RequiresFpuRegister());
5308 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005309 } else {
5310 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5311
5312 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5313 // Temporary registers for the write barrier.
5314 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5315 // Ensure the card is in a byte register.
5316 locations->AddTemp(Location::RegisterLocation(ECX));
5317 }
Calin Juravle52c48962014-12-16 17:02:57 +00005318 }
5319}
5320
5321void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005322 const FieldInfo& field_info,
5323 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005324 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5325
5326 LocationSummary* locations = instruction->GetLocations();
5327 Register base = locations->InAt(0).AsRegister<Register>();
5328 Location value = locations->InAt(1);
5329 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005331 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005332 bool needs_write_barrier =
5333 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005334
5335 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005336 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005337 }
5338
Mark Mendell81489372015-11-04 11:30:41 -05005339 bool maybe_record_implicit_null_check_done = false;
5340
Calin Juravle52c48962014-12-16 17:02:57 +00005341 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005342 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005343 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005344 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005345 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5346 break;
5347 }
5348
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005349 case DataType::Type::kUint16:
5350 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005351 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005352 __ movw(Address(base, offset),
5353 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005354 } else {
5355 __ movw(Address(base, offset), value.AsRegister<Register>());
5356 }
Calin Juravle52c48962014-12-16 17:02:57 +00005357 break;
5358 }
5359
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005360 case DataType::Type::kInt32:
5361 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005362 if (kPoisonHeapReferences && needs_write_barrier) {
5363 // Note that in the case where `value` is a null reference,
5364 // we do not enter this block, as the reference does not
5365 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005367 Register temp = locations->GetTemp(0).AsRegister<Register>();
5368 __ movl(temp, value.AsRegister<Register>());
5369 __ PoisonHeapReference(temp);
5370 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005371 } else if (value.IsConstant()) {
5372 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5373 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005374 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005375 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005376 __ movl(Address(base, offset), value.AsRegister<Register>());
5377 }
Calin Juravle52c48962014-12-16 17:02:57 +00005378 break;
5379 }
5380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005382 if (is_volatile) {
5383 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5384 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5385 __ movd(temp1, value.AsRegisterPairLow<Register>());
5386 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5387 __ punpckldq(temp1, temp2);
5388 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005390 } else if (value.IsConstant()) {
5391 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5392 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5393 codegen_->MaybeRecordImplicitNullCheck(instruction);
5394 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005395 } else {
5396 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005397 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005398 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5399 }
Mark Mendell81489372015-11-04 11:30:41 -05005400 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005401 break;
5402 }
5403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005404 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005405 if (value.IsConstant()) {
5406 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5407 __ movl(Address(base, offset), Immediate(v));
5408 } else {
5409 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5410 }
Calin Juravle52c48962014-12-16 17:02:57 +00005411 break;
5412 }
5413
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005414 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005415 if (value.IsConstant()) {
5416 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5417 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5418 codegen_->MaybeRecordImplicitNullCheck(instruction);
5419 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5420 maybe_record_implicit_null_check_done = true;
5421 } else {
5422 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5423 }
Calin Juravle52c48962014-12-16 17:02:57 +00005424 break;
5425 }
5426
Aart Bik66c158e2018-01-31 12:55:04 -08005427 case DataType::Type::kUint32:
5428 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005430 LOG(FATAL) << "Unreachable type " << field_type;
5431 UNREACHABLE();
5432 }
5433
Mark Mendell81489372015-11-04 11:30:41 -05005434 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005435 codegen_->MaybeRecordImplicitNullCheck(instruction);
5436 }
5437
Roland Levillain4d027112015-07-01 15:41:14 +01005438 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005439 Register temp = locations->GetTemp(0).AsRegister<Register>();
5440 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005441 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005442 }
5443
Calin Juravle52c48962014-12-16 17:02:57 +00005444 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005445 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005446 }
5447}
5448
5449void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5450 HandleFieldGet(instruction, instruction->GetFieldInfo());
5451}
5452
5453void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5454 HandleFieldGet(instruction, instruction->GetFieldInfo());
5455}
5456
5457void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5458 HandleFieldSet(instruction, instruction->GetFieldInfo());
5459}
5460
5461void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005462 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005463}
5464
5465void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5466 HandleFieldSet(instruction, instruction->GetFieldInfo());
5467}
5468
5469void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005470 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005471}
5472
5473void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5474 HandleFieldGet(instruction, instruction->GetFieldInfo());
5475}
5476
5477void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5478 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005479}
5480
Calin Juravlee460d1d2015-09-29 04:52:17 +01005481void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5482 HUnresolvedInstanceFieldGet* instruction) {
5483 FieldAccessCallingConventionX86 calling_convention;
5484 codegen_->CreateUnresolvedFieldLocationSummary(
5485 instruction, instruction->GetFieldType(), calling_convention);
5486}
5487
5488void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5489 HUnresolvedInstanceFieldGet* instruction) {
5490 FieldAccessCallingConventionX86 calling_convention;
5491 codegen_->GenerateUnresolvedFieldAccess(instruction,
5492 instruction->GetFieldType(),
5493 instruction->GetFieldIndex(),
5494 instruction->GetDexPc(),
5495 calling_convention);
5496}
5497
5498void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5499 HUnresolvedInstanceFieldSet* instruction) {
5500 FieldAccessCallingConventionX86 calling_convention;
5501 codegen_->CreateUnresolvedFieldLocationSummary(
5502 instruction, instruction->GetFieldType(), calling_convention);
5503}
5504
5505void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5506 HUnresolvedInstanceFieldSet* instruction) {
5507 FieldAccessCallingConventionX86 calling_convention;
5508 codegen_->GenerateUnresolvedFieldAccess(instruction,
5509 instruction->GetFieldType(),
5510 instruction->GetFieldIndex(),
5511 instruction->GetDexPc(),
5512 calling_convention);
5513}
5514
5515void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5516 HUnresolvedStaticFieldGet* instruction) {
5517 FieldAccessCallingConventionX86 calling_convention;
5518 codegen_->CreateUnresolvedFieldLocationSummary(
5519 instruction, instruction->GetFieldType(), calling_convention);
5520}
5521
5522void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5523 HUnresolvedStaticFieldGet* instruction) {
5524 FieldAccessCallingConventionX86 calling_convention;
5525 codegen_->GenerateUnresolvedFieldAccess(instruction,
5526 instruction->GetFieldType(),
5527 instruction->GetFieldIndex(),
5528 instruction->GetDexPc(),
5529 calling_convention);
5530}
5531
5532void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5533 HUnresolvedStaticFieldSet* instruction) {
5534 FieldAccessCallingConventionX86 calling_convention;
5535 codegen_->CreateUnresolvedFieldLocationSummary(
5536 instruction, instruction->GetFieldType(), calling_convention);
5537}
5538
5539void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5540 HUnresolvedStaticFieldSet* instruction) {
5541 FieldAccessCallingConventionX86 calling_convention;
5542 codegen_->GenerateUnresolvedFieldAccess(instruction,
5543 instruction->GetFieldType(),
5544 instruction->GetFieldIndex(),
5545 instruction->GetDexPc(),
5546 calling_convention);
5547}
5548
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005549void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005550 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5551 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5552 ? Location::RequiresRegister()
5553 : Location::Any();
5554 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005555}
5556
Calin Juravle2ae48182016-03-16 14:05:09 +00005557void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5558 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005559 return;
5560 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005561 LocationSummary* locations = instruction->GetLocations();
5562 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005563
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005564 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005565 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005566}
5567
Calin Juravle2ae48182016-03-16 14:05:09 +00005568void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005569 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005570 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005571
5572 LocationSummary* locations = instruction->GetLocations();
5573 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005574
5575 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005576 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005577 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005578 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005579 } else {
5580 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005581 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005582 __ jmp(slow_path->GetEntryLabel());
5583 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005584 }
5585 __ j(kEqual, slow_path->GetEntryLabel());
5586}
5587
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005588void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005589 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005590}
5591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005593 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005594 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005595 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005596 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5597 object_array_get_with_read_barrier
5598 ? LocationSummary::kCallOnSlowPath
5599 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005600 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005601 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005602 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005603 locations->SetInAt(0, Location::RequiresRegister());
5604 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005605 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005606 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5607 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005608 // The output overlaps in case of long: we don't want the low move
5609 // to overwrite the array's location. Likewise, in the case of an
5610 // object array get with read barriers enabled, we do not want the
5611 // move to overwrite the array's location, as we need it to emit
5612 // the read barrier.
5613 locations->SetOut(
5614 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005615 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5616 ? Location::kOutputOverlap
5617 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005619}
5620
5621void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5622 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005623 Location obj_loc = locations->InAt(0);
5624 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005626 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005627 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005630 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005631 case DataType::Type::kBool:
5632 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005633 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005634 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635 break;
5636 }
5637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005639 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005640 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005641 break;
5642 }
5643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005645 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005646 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5647 // Branch cases into compressed and uncompressed for each index's type.
5648 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5649 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005650 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005651 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005652 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5653 "Expecting 0=compressed, 1=uncompressed");
5654 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005655 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5656 __ jmp(&done);
5657 __ Bind(&not_compressed);
5658 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5659 __ Bind(&done);
5660 } else {
5661 // Common case for charAt of array of char or when string compression's
5662 // feature is turned off.
5663 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5664 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005665 break;
5666 }
5667
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005668 case DataType::Type::kInt16: {
5669 Register out = out_loc.AsRegister<Register>();
5670 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5671 break;
5672 }
5673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005675 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005676 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005677 break;
5678 }
5679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005681 static_assert(
5682 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5683 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005684 // /* HeapReference<Object> */ out =
5685 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5686 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005687 // Note that a potential implicit null check is handled in this
5688 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5689 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005690 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005691 } else {
5692 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005693 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5694 codegen_->MaybeRecordImplicitNullCheck(instruction);
5695 // If read barriers are enabled, emit read barriers other than
5696 // Baker's using a slow path (and also unpoison the loaded
5697 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005698 if (index.IsConstant()) {
5699 uint32_t offset =
5700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005701 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5702 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005703 codegen_->MaybeGenerateReadBarrierSlow(
5704 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5705 }
5706 }
5707 break;
5708 }
5709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005710 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005711 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005712 __ movl(out_loc.AsRegisterPairLow<Register>(),
5713 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5714 codegen_->MaybeRecordImplicitNullCheck(instruction);
5715 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5716 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005717 break;
5718 }
5719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005721 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005722 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005723 break;
5724 }
5725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005727 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005728 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005729 break;
5730 }
5731
Aart Bik66c158e2018-01-31 12:55:04 -08005732 case DataType::Type::kUint32:
5733 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005735 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005736 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005737 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005739 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005740 // Potential implicit null checks, in the case of reference or
5741 // long arrays, are handled in the previous switch statement.
5742 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005743 codegen_->MaybeRecordImplicitNullCheck(instruction);
5744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005745}
5746
5747void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005748 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005750 bool needs_write_barrier =
5751 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005752 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005753
Vladimir Markoca6fff82017-10-03 14:49:14 +01005754 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005755 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005756 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005757 LocationSummary::kCallOnSlowPath :
5758 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005759
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005760 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005761 // We need the inputs to be different than the output in case of long operation.
5762 // In case of a byte operation, the register allocator does not support multiple
5763 // inputs that die at entry with one in a specific register.
5764 locations->SetInAt(0, Location::RequiresRegister());
5765 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5766 if (is_byte_type) {
5767 // Ensure the value is in a byte register.
5768 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005770 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005771 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005772 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5773 }
5774 if (needs_write_barrier) {
5775 // Temporary registers for the write barrier.
5776 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5777 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005778 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005779 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005780}
5781
5782void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5783 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005784 Location array_loc = locations->InAt(0);
5785 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005786 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005787 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005788 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005789 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5790 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5791 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005793 bool needs_write_barrier =
5794 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005795
5796 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005798 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005799 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005800 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005801 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005802 if (value.IsRegister()) {
5803 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005804 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005805 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005806 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005807 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005808 break;
5809 }
5810
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005811 case DataType::Type::kUint16:
5812 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005813 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005814 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005815 if (value.IsRegister()) {
5816 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005817 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005818 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005819 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005820 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005821 break;
5822 }
5823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005824 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005825 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005826 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005828 if (!value.IsRegister()) {
5829 // Just setting null.
5830 DCHECK(instruction->InputAt(2)->IsNullConstant());
5831 DCHECK(value.IsConstant()) << value;
5832 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005834 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005836 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005837 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005838
5839 DCHECK(needs_write_barrier);
5840 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005841 // We cannot use a NearLabel for `done`, as its range may be too
5842 // short when Baker read barriers are enabled.
5843 Label done;
5844 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005845 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005846 Location temp_loc = locations->GetTemp(0);
5847 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005848 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005849 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005850 codegen_->AddSlowPath(slow_path);
5851 if (instruction->GetValueCanBeNull()) {
5852 __ testl(register_value, register_value);
5853 __ j(kNotEqual, &not_null);
5854 __ movl(address, Immediate(0));
5855 codegen_->MaybeRecordImplicitNullCheck(instruction);
5856 __ jmp(&done);
5857 __ Bind(&not_null);
5858 }
5859
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005860 // Note that when Baker read barriers are enabled, the type
5861 // checks are performed without read barriers. This is fine,
5862 // even in the case where a class object is in the from-space
5863 // after the flip, as a comparison involving such a type would
5864 // not produce a false positive; it may of course produce a
5865 // false negative, in which case we would take the ArraySet
5866 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005867
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005868 // /* HeapReference<Class> */ temp = array->klass_
5869 __ movl(temp, Address(array, class_offset));
5870 codegen_->MaybeRecordImplicitNullCheck(instruction);
5871 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005872
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005873 // /* HeapReference<Class> */ temp = temp->component_type_
5874 __ movl(temp, Address(temp, component_offset));
5875 // If heap poisoning is enabled, no need to unpoison `temp`
5876 // nor the object reference in `register_value->klass`, as
5877 // we are comparing two poisoned references.
5878 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005879
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005880 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5881 __ j(kEqual, &do_put);
5882 // If heap poisoning is enabled, the `temp` reference has
5883 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884 __ MaybeUnpoisonHeapReference(temp);
5885
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005886 // If heap poisoning is enabled, no need to unpoison the
5887 // heap reference loaded below, as it is only used for a
5888 // comparison with null.
5889 __ cmpl(Address(temp, super_offset), Immediate(0));
5890 __ j(kNotEqual, slow_path->GetEntryLabel());
5891 __ Bind(&do_put);
5892 } else {
5893 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005894 }
5895 }
5896
5897 if (kPoisonHeapReferences) {
5898 __ movl(temp, register_value);
5899 __ PoisonHeapReference(temp);
5900 __ movl(address, temp);
5901 } else {
5902 __ movl(address, register_value);
5903 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005904 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005905 codegen_->MaybeRecordImplicitNullCheck(instruction);
5906 }
5907
5908 Register card = locations->GetTemp(1).AsRegister<Register>();
5909 codegen_->MarkGCCard(
5910 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5911 __ Bind(&done);
5912
5913 if (slow_path != nullptr) {
5914 __ Bind(slow_path->GetExitLabel());
5915 }
5916
5917 break;
5918 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005920 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005921 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005922 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005923 if (value.IsRegister()) {
5924 __ movl(address, value.AsRegister<Register>());
5925 } else {
5926 DCHECK(value.IsConstant()) << value;
5927 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5928 __ movl(address, Immediate(v));
5929 }
5930 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005931 break;
5932 }
5933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005934 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005935 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005936 if (value.IsRegisterPair()) {
5937 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5938 value.AsRegisterPairLow<Register>());
5939 codegen_->MaybeRecordImplicitNullCheck(instruction);
5940 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5941 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005942 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005943 DCHECK(value.IsConstant());
5944 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5945 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5946 Immediate(Low32Bits(val)));
5947 codegen_->MaybeRecordImplicitNullCheck(instruction);
5948 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5949 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005950 }
5951 break;
5952 }
5953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005955 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005956 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005957 if (value.IsFpuRegister()) {
5958 __ movss(address, value.AsFpuRegister<XmmRegister>());
5959 } else {
5960 DCHECK(value.IsConstant());
5961 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5962 __ movl(address, Immediate(v));
5963 }
5964 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005965 break;
5966 }
5967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005968 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005969 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005970 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005971 if (value.IsFpuRegister()) {
5972 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5973 } else {
5974 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005975 Address address_hi =
5976 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005977 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5978 __ movl(address, Immediate(Low32Bits(v)));
5979 codegen_->MaybeRecordImplicitNullCheck(instruction);
5980 __ movl(address_hi, Immediate(High32Bits(v)));
5981 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005982 break;
5983 }
5984
Aart Bik66c158e2018-01-31 12:55:04 -08005985 case DataType::Type::kUint32:
5986 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005988 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005989 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005990 }
5991}
5992
5993void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005994 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005995 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005996 if (!instruction->IsEmittedAtUseSite()) {
5997 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5998 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005999}
6000
6001void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006002 if (instruction->IsEmittedAtUseSite()) {
6003 return;
6004 }
6005
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006006 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006007 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006008 Register obj = locations->InAt(0).AsRegister<Register>();
6009 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006010 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006011 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006012 // Mask out most significant bit in case the array is String's array of char.
6013 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006014 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006015 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016}
6017
6018void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006019 RegisterSet caller_saves = RegisterSet::Empty();
6020 InvokeRuntimeCallingConvention calling_convention;
6021 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6022 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6023 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006024 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006025 HInstruction* length = instruction->InputAt(1);
6026 if (!length->IsEmittedAtUseSite()) {
6027 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6028 }
jessicahandojo4877b792016-09-08 19:49:13 -07006029 // Need register to see array's length.
6030 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6031 locations->AddTemp(Location::RequiresRegister());
6032 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033}
6034
6035void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006036 const bool is_string_compressed_char_at =
6037 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006038 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006039 Location index_loc = locations->InAt(0);
6040 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006041 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006042 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006043
Mark Mendell99dbd682015-04-22 16:18:52 -04006044 if (length_loc.IsConstant()) {
6045 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6046 if (index_loc.IsConstant()) {
6047 // BCE will remove the bounds check if we are guarenteed to pass.
6048 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6049 if (index < 0 || index >= length) {
6050 codegen_->AddSlowPath(slow_path);
6051 __ jmp(slow_path->GetEntryLabel());
6052 } else {
6053 // Some optimization after BCE may have generated this, and we should not
6054 // generate a bounds check if it is a valid range.
6055 }
6056 return;
6057 }
6058
6059 // We have to reverse the jump condition because the length is the constant.
6060 Register index_reg = index_loc.AsRegister<Register>();
6061 __ cmpl(index_reg, Immediate(length));
6062 codegen_->AddSlowPath(slow_path);
6063 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006064 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006065 HInstruction* array_length = instruction->InputAt(1);
6066 if (array_length->IsEmittedAtUseSite()) {
6067 // Address the length field in the array.
6068 DCHECK(array_length->IsArrayLength());
6069 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6070 Location array_loc = array_length->GetLocations()->InAt(0);
6071 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006072 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006073 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6074 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006075 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6076 __ movl(length_reg, array_len);
6077 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006078 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006079 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006080 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006081 // Checking bounds for general case:
6082 // Array of char or string's array with feature compression off.
6083 if (index_loc.IsConstant()) {
6084 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6085 __ cmpl(array_len, Immediate(value));
6086 } else {
6087 __ cmpl(array_len, index_loc.AsRegister<Register>());
6088 }
6089 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006090 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006091 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006092 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006093 }
6094 codegen_->AddSlowPath(slow_path);
6095 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006097}
6098
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006099void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006100 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006101}
6102
6103void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006104 if (instruction->GetNext()->IsSuspendCheck() &&
6105 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6106 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6107 // The back edge will generate the suspend check.
6108 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6109 }
6110
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006111 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6112}
6113
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006114void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006115 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6116 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006117 // In suspend check slow path, usually there are no caller-save registers at all.
6118 // If SIMD instructions are present, however, we force spilling all live SIMD
6119 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006120 locations->SetCustomSlowPathCallerSaves(
6121 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006122}
6123
6124void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006125 HBasicBlock* block = instruction->GetBlock();
6126 if (block->GetLoopInformation() != nullptr) {
6127 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6128 // The back edge will generate the suspend check.
6129 return;
6130 }
6131 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6132 // The goto will generate the suspend check.
6133 return;
6134 }
6135 GenerateSuspendCheck(instruction, nullptr);
6136}
6137
6138void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6139 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006140 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006141 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6142 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006143 slow_path =
6144 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006145 instruction->SetSlowPath(slow_path);
6146 codegen_->AddSlowPath(slow_path);
6147 if (successor != nullptr) {
6148 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006149 }
6150 } else {
6151 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6152 }
6153
Andreas Gampe542451c2016-07-26 09:02:02 -07006154 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006155 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006156 if (successor == nullptr) {
6157 __ j(kNotEqual, slow_path->GetEntryLabel());
6158 __ Bind(slow_path->GetReturnLabel());
6159 } else {
6160 __ j(kEqual, codegen_->GetLabelOf(successor));
6161 __ jmp(slow_path->GetEntryLabel());
6162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006163}
6164
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006165X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6166 return codegen_->GetAssembler();
6167}
6168
Aart Bikcfe50bb2017-12-12 14:54:12 -08006169void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006170 ScratchRegisterScope ensure_scratch(
6171 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6172 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6173 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006174
Aart Bikcfe50bb2017-12-12 14:54:12 -08006175 // Now that temp register is available (possibly spilled), move blocks of memory.
6176 for (int i = 0; i < number_of_words; i++) {
6177 __ movl(temp_reg, Address(ESP, src + stack_offset));
6178 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6179 stack_offset += kX86WordSize;
6180 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006181}
6182
6183void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006184 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006185 Location source = move->GetSource();
6186 Location destination = move->GetDestination();
6187
6188 if (source.IsRegister()) {
6189 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006190 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006191 } else if (destination.IsFpuRegister()) {
6192 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006193 } else {
6194 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006195 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006196 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006197 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006199 // Create stack space for 2 elements.
6200 __ subl(ESP, Immediate(2 * elem_size));
6201 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6202 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6203 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6204 // And remove the temporary stack space we allocated.
6205 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006206 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006207 if (destination.IsRegister()) {
6208 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6209 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006210 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006211 } else if (destination.IsRegisterPair()) {
6212 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6213 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6214 __ psrlq(src_reg, Immediate(32));
6215 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006216 } else if (destination.IsStackSlot()) {
6217 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006218 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006219 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006220 } else {
6221 DCHECK(destination.IsSIMDStackSlot());
6222 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006223 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006224 } else if (source.IsStackSlot()) {
6225 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006226 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006227 } else if (destination.IsFpuRegister()) {
6228 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006229 } else {
6230 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006231 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006232 }
6233 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006234 if (destination.IsRegisterPair()) {
6235 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6236 __ movl(destination.AsRegisterPairHigh<Register>(),
6237 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6238 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006239 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6240 } else {
6241 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006242 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006243 }
Aart Bik5576f372017-03-23 16:17:37 -07006244 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006245 if (destination.IsFpuRegister()) {
6246 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6247 } else {
6248 DCHECK(destination.IsSIMDStackSlot());
6249 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6250 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006251 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006252 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006253 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006254 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006255 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006256 if (value == 0) {
6257 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6258 } else {
6259 __ movl(destination.AsRegister<Register>(), Immediate(value));
6260 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006261 } else {
6262 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006263 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006264 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006265 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006266 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006267 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006268 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006269 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006270 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6271 if (value == 0) {
6272 // Easy handling of 0.0.
6273 __ xorps(dest, dest);
6274 } else {
6275 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006276 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6277 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6278 __ movl(temp, Immediate(value));
6279 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006280 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006281 } else {
6282 DCHECK(destination.IsStackSlot()) << destination;
6283 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6284 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006285 } else if (constant->IsLongConstant()) {
6286 int64_t value = constant->AsLongConstant()->GetValue();
6287 int32_t low_value = Low32Bits(value);
6288 int32_t high_value = High32Bits(value);
6289 Immediate low(low_value);
6290 Immediate high(high_value);
6291 if (destination.IsDoubleStackSlot()) {
6292 __ movl(Address(ESP, destination.GetStackIndex()), low);
6293 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6294 } else {
6295 __ movl(destination.AsRegisterPairLow<Register>(), low);
6296 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6297 }
6298 } else {
6299 DCHECK(constant->IsDoubleConstant());
6300 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006301 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006302 int32_t low_value = Low32Bits(value);
6303 int32_t high_value = High32Bits(value);
6304 Immediate low(low_value);
6305 Immediate high(high_value);
6306 if (destination.IsFpuRegister()) {
6307 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6308 if (value == 0) {
6309 // Easy handling of 0.0.
6310 __ xorpd(dest, dest);
6311 } else {
6312 __ pushl(high);
6313 __ pushl(low);
6314 __ movsd(dest, Address(ESP, 0));
6315 __ addl(ESP, Immediate(8));
6316 }
6317 } else {
6318 DCHECK(destination.IsDoubleStackSlot()) << destination;
6319 __ movl(Address(ESP, destination.GetStackIndex()), low);
6320 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6321 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006322 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006323 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006324 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006325 }
6326}
6327
Mark Mendella5c19ce2015-04-01 12:51:05 -04006328void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006329 Register suggested_scratch = reg == EAX ? EBX : EAX;
6330 ScratchRegisterScope ensure_scratch(
6331 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6332
6333 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6334 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6335 __ movl(Address(ESP, mem + stack_offset), reg);
6336 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006337}
6338
Mark Mendell7c8d0092015-01-26 11:21:33 -05006339void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006340 ScratchRegisterScope ensure_scratch(
6341 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6342
6343 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6344 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6345 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6346 __ movss(Address(ESP, mem + stack_offset), reg);
6347 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006348}
6349
Aart Bikcfe50bb2017-12-12 14:54:12 -08006350void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6351 size_t extra_slot = 4 * kX86WordSize;
6352 __ subl(ESP, Immediate(extra_slot));
6353 __ movups(Address(ESP, 0), XmmRegister(reg));
6354 ExchangeMemory(0, mem + extra_slot, 4);
6355 __ movups(XmmRegister(reg), Address(ESP, 0));
6356 __ addl(ESP, Immediate(extra_slot));
6357}
6358
6359void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006360 ScratchRegisterScope ensure_scratch1(
6361 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006362
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006363 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6364 ScratchRegisterScope ensure_scratch2(
6365 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006366
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006367 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6368 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006369
6370 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6371 for (int i = 0; i < number_of_words; i++) {
6372 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6373 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6374 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6375 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6376 stack_offset += kX86WordSize;
6377 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006378}
6379
6380void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006381 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006382 Location source = move->GetSource();
6383 Location destination = move->GetDestination();
6384
6385 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006386 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6387 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6388 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6389 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6390 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006391 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006392 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006393 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006394 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006395 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006396 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006397 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6398 // Use XOR Swap algorithm to avoid a temporary.
6399 DCHECK_NE(source.reg(), destination.reg());
6400 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6401 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6402 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6403 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6404 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6405 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6406 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006407 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6408 // Take advantage of the 16 bytes in the XMM register.
6409 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6410 Address stack(ESP, destination.GetStackIndex());
6411 // Load the double into the high doubleword.
6412 __ movhpd(reg, stack);
6413
6414 // Store the low double into the destination.
6415 __ movsd(stack, reg);
6416
6417 // Move the high double to the low double.
6418 __ psrldq(reg, Immediate(8));
6419 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6420 // Take advantage of the 16 bytes in the XMM register.
6421 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6422 Address stack(ESP, source.GetStackIndex());
6423 // Load the double into the high doubleword.
6424 __ movhpd(reg, stack);
6425
6426 // Store the low double into the destination.
6427 __ movsd(stack, reg);
6428
6429 // Move the high double to the low double.
6430 __ psrldq(reg, Immediate(8));
6431 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006432 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6433 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6434 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6435 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6436 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6437 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6438 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006439 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006440 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006441 }
6442}
6443
6444void ParallelMoveResolverX86::SpillScratch(int reg) {
6445 __ pushl(static_cast<Register>(reg));
6446}
6447
6448void ParallelMoveResolverX86::RestoreScratch(int reg) {
6449 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006450}
6451
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6453 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006454 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006455 case HLoadClass::LoadKind::kInvalid:
6456 LOG(FATAL) << "UNREACHABLE";
6457 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006458 case HLoadClass::LoadKind::kReferrersClass:
6459 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006460 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006461 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006462 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006463 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006465 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006466 DCHECK(Runtime::Current()->UseJitCompilation());
6467 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006468 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006469 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006470 break;
6471 }
6472 return desired_class_load_kind;
6473}
6474
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006475void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006476 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006477 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006478 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006479 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006480 cls,
6481 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006482 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006483 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006484 return;
6485 }
Vladimir Marko41559982017-01-06 14:04:23 +00006486 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006487
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006488 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6489 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006490 ? LocationSummary::kCallOnSlowPath
6491 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006492 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006493 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006494 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006495 }
6496
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006497 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006498 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006499 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006500 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006501 locations->SetInAt(0, Location::RequiresRegister());
6502 }
6503 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006504 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6505 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6506 // Rely on the type resolution and/or initialization to save everything.
6507 RegisterSet caller_saves = RegisterSet::Empty();
6508 InvokeRuntimeCallingConvention calling_convention;
6509 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6510 locations->SetCustomSlowPathCallerSaves(caller_saves);
6511 } else {
6512 // For non-Baker read barrier we have a temp-clobbering call.
6513 }
6514 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006515}
6516
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006517Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006518 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006519 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006520 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006521 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006522 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006523 PatchInfo<Label>* info = &jit_class_patches_.back();
6524 return &info->label;
6525}
6526
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006527// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6528// move.
6529void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006530 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006531 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006532 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006533 return;
6534 }
Vladimir Marko41559982017-01-06 14:04:23 +00006535 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006536
Vladimir Marko41559982017-01-06 14:04:23 +00006537 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006538 Location out_loc = locations->Out();
6539 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006540
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006541 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006542 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6543 ? kWithoutReadBarrier
6544 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006545 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006546 case HLoadClass::LoadKind::kReferrersClass: {
6547 DCHECK(!cls->CanCallRuntime());
6548 DCHECK(!cls->MustGenerateClinitCheck());
6549 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6550 Register current_method = locations->InAt(0).AsRegister<Register>();
6551 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006552 cls,
6553 out_loc,
6554 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006555 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006556 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006557 break;
6558 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006559 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006560 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006561 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006562 Register method_address = locations->InAt(0).AsRegister<Register>();
6563 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006564 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006565 break;
6566 }
6567 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006568 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006569 uint32_t address = dchecked_integral_cast<uint32_t>(
6570 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6571 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006572 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006573 break;
6574 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006575 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006576 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6577 Register method_address = locations->InAt(0).AsRegister<Register>();
6578 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006579 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6580 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006581 break;
6582 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006583 case HLoadClass::LoadKind::kBssEntry: {
6584 Register method_address = locations->InAt(0).AsRegister<Register>();
6585 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6586 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6587 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6588 generate_null_check = true;
6589 break;
6590 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006591 case HLoadClass::LoadKind::kJitTableAddress: {
6592 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6593 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006594 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006595 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006596 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006597 break;
6598 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006599 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006600 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006601 LOG(FATAL) << "UNREACHABLE";
6602 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006603 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006604
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006605 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6606 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006607 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006608 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6609 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006610
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006611 if (generate_null_check) {
6612 __ testl(out, out);
6613 __ j(kEqual, slow_path->GetEntryLabel());
6614 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006615
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006616 if (cls->MustGenerateClinitCheck()) {
6617 GenerateClassInitializationCheck(slow_path, out);
6618 } else {
6619 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006620 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006621 }
6622}
6623
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006624void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6625 InvokeRuntimeCallingConvention calling_convention;
6626 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6627 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6628}
6629
6630void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6631 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6632}
6633
Orion Hodson18259d72018-04-12 11:18:23 +01006634void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6635 InvokeRuntimeCallingConvention calling_convention;
6636 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6637 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6638}
6639
6640void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6641 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6642}
6643
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006644void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6645 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006646 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006647 locations->SetInAt(0, Location::RequiresRegister());
6648 if (check->HasUses()) {
6649 locations->SetOut(Location::SameAsFirstInput());
6650 }
6651}
6652
6653void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006654 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006655 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006656 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006657 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006658 GenerateClassInitializationCheck(slow_path,
6659 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006660}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006661
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006662void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006663 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006664 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6665 const size_t status_byte_offset =
6666 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6667 constexpr uint32_t shifted_initialized_value =
6668 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6669
6670 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006671 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006672 __ Bind(slow_path->GetExitLabel());
6673 // No need for memory fence, thanks to the X86 memory model.
6674}
6675
Vladimir Marko175e7862018-03-27 09:03:13 +00006676void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6677 Register temp) {
6678 uint32_t path_to_root = check->GetBitstringPathToRoot();
6679 uint32_t mask = check->GetBitstringMask();
6680 DCHECK(IsPowerOfTwo(mask + 1));
6681 size_t mask_bits = WhichPowerOf2(mask + 1);
6682
6683 if (mask_bits == 16u) {
6684 // Compare the bitstring in memory.
6685 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6686 } else {
6687 // /* uint32_t */ temp = temp->status_
6688 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6689 // Compare the bitstring bits using SUB.
6690 __ subl(temp, Immediate(path_to_root));
6691 // Shift out bits that do not contribute to the comparison.
6692 __ shll(temp, Immediate(32u - mask_bits));
6693 }
6694}
6695
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006696HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6697 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006698 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006699 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006700 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006701 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006702 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006703 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006704 case HLoadString::LoadKind::kJitTableAddress:
6705 DCHECK(Runtime::Current()->UseJitCompilation());
6706 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006707 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006708 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006709 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006710 }
6711 return desired_string_load_kind;
6712}
6713
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006714void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006715 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006716 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006717 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006718 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006719 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006720 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006721 locations->SetInAt(0, Location::RequiresRegister());
6722 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006723 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006724 locations->SetOut(Location::RegisterLocation(EAX));
6725 } else {
6726 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006727 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6728 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006729 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006730 RegisterSet caller_saves = RegisterSet::Empty();
6731 InvokeRuntimeCallingConvention calling_convention;
6732 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6733 locations->SetCustomSlowPathCallerSaves(caller_saves);
6734 } else {
6735 // For non-Baker read barrier we have a temp-clobbering call.
6736 }
6737 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006738 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006739}
6740
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006741Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006742 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006743 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006744 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006745 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006746 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006747 PatchInfo<Label>* info = &jit_string_patches_.back();
6748 return &info->label;
6749}
6750
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006751// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6752// move.
6753void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006754 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006755 Location out_loc = locations->Out();
6756 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006758 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006759 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006760 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006761 Register method_address = locations->InAt(0).AsRegister<Register>();
6762 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006763 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006764 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006765 }
6766 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006767 uint32_t address = dchecked_integral_cast<uint32_t>(
6768 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6769 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006770 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006771 return;
6772 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006773 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006774 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6775 Register method_address = locations->InAt(0).AsRegister<Register>();
6776 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006777 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6778 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006779 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006780 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006781 case HLoadString::LoadKind::kBssEntry: {
6782 Register method_address = locations->InAt(0).AsRegister<Register>();
6783 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6784 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006785 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006786 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006787 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006788 codegen_->AddSlowPath(slow_path);
6789 __ testl(out, out);
6790 __ j(kEqual, slow_path->GetEntryLabel());
6791 __ Bind(slow_path->GetExitLabel());
6792 return;
6793 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006794 case HLoadString::LoadKind::kJitTableAddress: {
6795 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6796 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006797 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006798 // /* GcRoot<mirror::String> */ out = *address
6799 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6800 return;
6801 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006802 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006803 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006804 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006805
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006806 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006807 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006808 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006809 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006810 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6811 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006812}
6813
David Brazdilcb1c0552015-08-04 16:22:25 +01006814static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006815 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006816}
6817
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006818void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6819 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006820 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006821 locations->SetOut(Location::RequiresRegister());
6822}
6823
6824void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006825 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6826}
6827
6828void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006829 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006830}
6831
6832void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6833 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006834}
6835
6836void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6838 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006839 InvokeRuntimeCallingConvention calling_convention;
6840 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6841}
6842
6843void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006844 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006845 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006846}
6847
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006848// Temp is used for read barrier.
6849static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6850 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006851 !kUseBakerReadBarrier &&
6852 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006853 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006854 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6855 return 1;
6856 }
6857 return 0;
6858}
6859
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006860// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6861// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006862// The other checks have one temp for loading the object's class.
6863static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006864 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006865 return 2;
6866 }
6867 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006868}
6869
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006870void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006871 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006872 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006873 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006874 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006875 case TypeCheckKind::kExactCheck:
6876 case TypeCheckKind::kAbstractClassCheck:
6877 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006878 case TypeCheckKind::kArrayObjectCheck: {
6879 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6880 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6881 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006882 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006883 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006884 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006885 case TypeCheckKind::kUnresolvedCheck:
6886 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006887 call_kind = LocationSummary::kCallOnSlowPath;
6888 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006889 case TypeCheckKind::kBitstringCheck:
6890 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006891 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006892
Vladimir Markoca6fff82017-10-03 14:49:14 +01006893 LocationSummary* locations =
6894 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006895 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006896 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006897 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006898 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006899 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6900 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6901 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6902 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6903 } else {
6904 locations->SetInAt(1, Location::Any());
6905 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006906 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6907 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006908 // When read barriers are enabled, we need a temporary register for some cases.
6909 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006910}
6911
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006912void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006913 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006914 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006915 Location obj_loc = locations->InAt(0);
6916 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006917 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006918 Location out_loc = locations->Out();
6919 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006920 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6921 DCHECK_LE(num_temps, 1u);
6922 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006923 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006924 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6925 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6926 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006927 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006928 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006929
6930 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006931 // Avoid null check if we know obj is not null.
6932 if (instruction->MustDoNullCheck()) {
6933 __ testl(obj, obj);
6934 __ j(kEqual, &zero);
6935 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006936
Roland Levillain7c1559a2015-12-15 10:55:36 +00006937 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006938 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006939 ReadBarrierOption read_barrier_option =
6940 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006941 // /* HeapReference<Class> */ out = obj->klass_
6942 GenerateReferenceLoadTwoRegisters(instruction,
6943 out_loc,
6944 obj_loc,
6945 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006946 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006947 if (cls.IsRegister()) {
6948 __ cmpl(out, cls.AsRegister<Register>());
6949 } else {
6950 DCHECK(cls.IsStackSlot()) << cls;
6951 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6952 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006953
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006954 // Classes must be equal for the instanceof to succeed.
6955 __ j(kNotEqual, &zero);
6956 __ movl(out, Immediate(1));
6957 __ jmp(&done);
6958 break;
6959 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006961 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006962 ReadBarrierOption read_barrier_option =
6963 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006964 // /* HeapReference<Class> */ out = obj->klass_
6965 GenerateReferenceLoadTwoRegisters(instruction,
6966 out_loc,
6967 obj_loc,
6968 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006969 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006970 // If the class is abstract, we eagerly fetch the super class of the
6971 // object to avoid doing a comparison we know will fail.
6972 NearLabel loop;
6973 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006974 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006975 GenerateReferenceLoadOneRegister(instruction,
6976 out_loc,
6977 super_offset,
6978 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006979 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006980 __ testl(out, out);
6981 // If `out` is null, we use it for the result, and jump to `done`.
6982 __ j(kEqual, &done);
6983 if (cls.IsRegister()) {
6984 __ cmpl(out, cls.AsRegister<Register>());
6985 } else {
6986 DCHECK(cls.IsStackSlot()) << cls;
6987 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6988 }
6989 __ j(kNotEqual, &loop);
6990 __ movl(out, Immediate(1));
6991 if (zero.IsLinked()) {
6992 __ jmp(&done);
6993 }
6994 break;
6995 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006996
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006997 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006998 ReadBarrierOption read_barrier_option =
6999 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007000 // /* HeapReference<Class> */ out = obj->klass_
7001 GenerateReferenceLoadTwoRegisters(instruction,
7002 out_loc,
7003 obj_loc,
7004 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007005 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007006 // Walk over the class hierarchy to find a match.
7007 NearLabel loop, success;
7008 __ Bind(&loop);
7009 if (cls.IsRegister()) {
7010 __ cmpl(out, cls.AsRegister<Register>());
7011 } else {
7012 DCHECK(cls.IsStackSlot()) << cls;
7013 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7014 }
7015 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007016 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007017 GenerateReferenceLoadOneRegister(instruction,
7018 out_loc,
7019 super_offset,
7020 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007021 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007022 __ testl(out, out);
7023 __ j(kNotEqual, &loop);
7024 // If `out` is null, we use it for the result, and jump to `done`.
7025 __ jmp(&done);
7026 __ Bind(&success);
7027 __ movl(out, Immediate(1));
7028 if (zero.IsLinked()) {
7029 __ jmp(&done);
7030 }
7031 break;
7032 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007033
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007034 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007035 ReadBarrierOption read_barrier_option =
7036 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007037 // /* HeapReference<Class> */ out = obj->klass_
7038 GenerateReferenceLoadTwoRegisters(instruction,
7039 out_loc,
7040 obj_loc,
7041 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007042 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007043 // Do an exact check.
7044 NearLabel exact_check;
7045 if (cls.IsRegister()) {
7046 __ cmpl(out, cls.AsRegister<Register>());
7047 } else {
7048 DCHECK(cls.IsStackSlot()) << cls;
7049 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7050 }
7051 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007052 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007053 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007054 GenerateReferenceLoadOneRegister(instruction,
7055 out_loc,
7056 component_offset,
7057 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007058 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007059 __ testl(out, out);
7060 // If `out` is null, we use it for the result, and jump to `done`.
7061 __ j(kEqual, &done);
7062 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7063 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007064 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007065 __ movl(out, Immediate(1));
7066 __ jmp(&done);
7067 break;
7068 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007069
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007070 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007071 // No read barrier since the slow path will retry upon failure.
7072 // /* HeapReference<Class> */ out = obj->klass_
7073 GenerateReferenceLoadTwoRegisters(instruction,
7074 out_loc,
7075 obj_loc,
7076 class_offset,
7077 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007078 if (cls.IsRegister()) {
7079 __ cmpl(out, cls.AsRegister<Register>());
7080 } else {
7081 DCHECK(cls.IsStackSlot()) << cls;
7082 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7083 }
7084 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007085 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7086 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007087 codegen_->AddSlowPath(slow_path);
7088 __ j(kNotEqual, slow_path->GetEntryLabel());
7089 __ movl(out, Immediate(1));
7090 if (zero.IsLinked()) {
7091 __ jmp(&done);
7092 }
7093 break;
7094 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007095
Calin Juravle98893e12015-10-02 21:05:03 +01007096 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007097 case TypeCheckKind::kInterfaceCheck: {
7098 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007099 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007100 // cases.
7101 //
7102 // We cannot directly call the InstanceofNonTrivial runtime
7103 // entry point without resorting to a type checking slow path
7104 // here (i.e. by calling InvokeRuntime directly), as it would
7105 // require to assign fixed registers for the inputs of this
7106 // HInstanceOf instruction (following the runtime calling
7107 // convention), which might be cluttered by the potential first
7108 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007109 //
7110 // TODO: Introduce a new runtime entry point taking the object
7111 // to test (instead of its class) as argument, and let it deal
7112 // with the read barrier issues. This will let us refactor this
7113 // case of the `switch` code as it was previously (with a direct
7114 // call to the runtime not using a type checking slow path).
7115 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007116 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007117 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7118 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007119 codegen_->AddSlowPath(slow_path);
7120 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007121 if (zero.IsLinked()) {
7122 __ jmp(&done);
7123 }
7124 break;
7125 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007126
7127 case TypeCheckKind::kBitstringCheck: {
7128 // /* HeapReference<Class> */ temp = obj->klass_
7129 GenerateReferenceLoadTwoRegisters(instruction,
7130 out_loc,
7131 obj_loc,
7132 class_offset,
7133 kWithoutReadBarrier);
7134
7135 GenerateBitstringTypeCheckCompare(instruction, out);
7136 __ j(kNotEqual, &zero);
7137 __ movl(out, Immediate(1));
7138 __ jmp(&done);
7139 break;
7140 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007141 }
7142
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007143 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007144 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007145 __ xorl(out, out);
7146 }
7147
7148 if (done.IsLinked()) {
7149 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007150 }
7151
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007152 if (slow_path != nullptr) {
7153 __ Bind(slow_path->GetExitLabel());
7154 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007155}
7156
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007157void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007158 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007159 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007160 LocationSummary* locations =
7161 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007162 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007163 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7164 // Require a register for the interface check since there is a loop that compares the class to
7165 // a memory address.
7166 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007167 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7168 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7169 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7170 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007171 } else {
7172 locations->SetInAt(1, Location::Any());
7173 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007174 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007175 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7176}
7177
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007178void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007179 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007180 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007181 Location obj_loc = locations->InAt(0);
7182 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007183 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007184 Location temp_loc = locations->GetTemp(0);
7185 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007186 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7187 DCHECK_GE(num_temps, 1u);
7188 DCHECK_LE(num_temps, 2u);
7189 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7190 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7191 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7192 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7193 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7194 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7195 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7196 const uint32_t object_array_data_offset =
7197 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007198
Vladimir Marko87584542017-12-12 17:47:52 +00007199 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007200 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007201 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7202 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007203 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007204
Roland Levillain0d5a2812015-11-13 10:07:31 +00007205 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007206 // Avoid null check if we know obj is not null.
7207 if (instruction->MustDoNullCheck()) {
7208 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007209 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007210 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007211
Roland Levillain0d5a2812015-11-13 10:07:31 +00007212 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007213 case TypeCheckKind::kExactCheck:
7214 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007215 // /* HeapReference<Class> */ temp = obj->klass_
7216 GenerateReferenceLoadTwoRegisters(instruction,
7217 temp_loc,
7218 obj_loc,
7219 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007220 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007222 if (cls.IsRegister()) {
7223 __ cmpl(temp, cls.AsRegister<Register>());
7224 } else {
7225 DCHECK(cls.IsStackSlot()) << cls;
7226 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7227 }
7228 // Jump to slow path for throwing the exception or doing a
7229 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007230 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007231 break;
7232 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007234 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007235 // /* HeapReference<Class> */ temp = obj->klass_
7236 GenerateReferenceLoadTwoRegisters(instruction,
7237 temp_loc,
7238 obj_loc,
7239 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007240 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007242 // If the class is abstract, we eagerly fetch the super class of the
7243 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007244 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007245 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007246 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007247 GenerateReferenceLoadOneRegister(instruction,
7248 temp_loc,
7249 super_offset,
7250 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007251 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007252
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007253 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7254 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007255 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007256 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007257
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007258 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007259 if (cls.IsRegister()) {
7260 __ cmpl(temp, cls.AsRegister<Register>());
7261 } else {
7262 DCHECK(cls.IsStackSlot()) << cls;
7263 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7264 }
7265 __ j(kNotEqual, &loop);
7266 break;
7267 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007269 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007270 // /* HeapReference<Class> */ temp = obj->klass_
7271 GenerateReferenceLoadTwoRegisters(instruction,
7272 temp_loc,
7273 obj_loc,
7274 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007275 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007276
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007277 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007278 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007279 __ Bind(&loop);
7280 if (cls.IsRegister()) {
7281 __ cmpl(temp, cls.AsRegister<Register>());
7282 } else {
7283 DCHECK(cls.IsStackSlot()) << cls;
7284 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7285 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007286 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007287
Roland Levillain0d5a2812015-11-13 10:07:31 +00007288 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007289 GenerateReferenceLoadOneRegister(instruction,
7290 temp_loc,
7291 super_offset,
7292 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007293 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007294
7295 // If the class reference currently in `temp` is not null, jump
7296 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007297 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007298 __ j(kNotZero, &loop);
7299 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007300 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007301 break;
7302 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007303
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007304 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007305 // /* HeapReference<Class> */ temp = obj->klass_
7306 GenerateReferenceLoadTwoRegisters(instruction,
7307 temp_loc,
7308 obj_loc,
7309 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007310 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007311
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007312 // Do an exact check.
7313 if (cls.IsRegister()) {
7314 __ cmpl(temp, cls.AsRegister<Register>());
7315 } else {
7316 DCHECK(cls.IsStackSlot()) << cls;
7317 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7318 }
7319 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007320
7321 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007322 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007323 GenerateReferenceLoadOneRegister(instruction,
7324 temp_loc,
7325 component_offset,
7326 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007327 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007329 // If the component type is null (i.e. the object not an array), jump to the slow path to
7330 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007331 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007332 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007334 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007335 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007336 break;
7337 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007338
Calin Juravle98893e12015-10-02 21:05:03 +01007339 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007340 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007341 // We cannot directly call the CheckCast runtime entry point
7342 // without resorting to a type checking slow path here (i.e. by
7343 // calling InvokeRuntime directly), as it would require to
7344 // assign fixed registers for the inputs of this HInstanceOf
7345 // instruction (following the runtime calling convention), which
7346 // might be cluttered by the potential first read barrier
7347 // emission at the beginning of this method.
7348 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007349 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007350
7351 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007352 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7353 // We can not get false positives by doing this.
7354 // /* HeapReference<Class> */ temp = obj->klass_
7355 GenerateReferenceLoadTwoRegisters(instruction,
7356 temp_loc,
7357 obj_loc,
7358 class_offset,
7359 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007360
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007361 // /* HeapReference<Class> */ temp = temp->iftable_
7362 GenerateReferenceLoadTwoRegisters(instruction,
7363 temp_loc,
7364 temp_loc,
7365 iftable_offset,
7366 kWithoutReadBarrier);
7367 // Iftable is never null.
7368 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7369 // Maybe poison the `cls` for direct comparison with memory.
7370 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7371 // Loop through the iftable and check if any class matches.
7372 NearLabel start_loop;
7373 __ Bind(&start_loop);
7374 // Need to subtract first to handle the empty array case.
7375 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7376 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7377 // Go to next interface if the classes do not match.
7378 __ cmpl(cls.AsRegister<Register>(),
7379 CodeGeneratorX86::ArrayAddress(temp,
7380 maybe_temp2_loc,
7381 TIMES_4,
7382 object_array_data_offset));
7383 __ j(kNotEqual, &start_loop);
7384 // If `cls` was poisoned above, unpoison it.
7385 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007386 break;
7387 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007388
7389 case TypeCheckKind::kBitstringCheck: {
7390 // /* HeapReference<Class> */ temp = obj->klass_
7391 GenerateReferenceLoadTwoRegisters(instruction,
7392 temp_loc,
7393 obj_loc,
7394 class_offset,
7395 kWithoutReadBarrier);
7396
7397 GenerateBitstringTypeCheckCompare(instruction, temp);
7398 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7399 break;
7400 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007401 }
7402 __ Bind(&done);
7403
Roland Levillain0d5a2812015-11-13 10:07:31 +00007404 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007405}
7406
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007407void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007408 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7409 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007410 InvokeRuntimeCallingConvention calling_convention;
7411 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7412}
7413
7414void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007415 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7416 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007417 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007418 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007419 if (instruction->IsEnter()) {
7420 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7421 } else {
7422 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7423 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007424}
7425
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007426void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7427void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7428void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7429
7430void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7431 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007432 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007433 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7434 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007435 locations->SetInAt(0, Location::RequiresRegister());
7436 locations->SetInAt(1, Location::Any());
7437 locations->SetOut(Location::SameAsFirstInput());
7438}
7439
7440void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7441 HandleBitwiseOperation(instruction);
7442}
7443
7444void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7445 HandleBitwiseOperation(instruction);
7446}
7447
7448void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7449 HandleBitwiseOperation(instruction);
7450}
7451
7452void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7453 LocationSummary* locations = instruction->GetLocations();
7454 Location first = locations->InAt(0);
7455 Location second = locations->InAt(1);
7456 DCHECK(first.Equals(locations->Out()));
7457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007458 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007459 if (second.IsRegister()) {
7460 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007461 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007462 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007463 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007464 } else {
7465 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007466 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007467 }
7468 } else if (second.IsConstant()) {
7469 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007470 __ andl(first.AsRegister<Register>(),
7471 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007472 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007473 __ orl(first.AsRegister<Register>(),
7474 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007475 } else {
7476 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007477 __ xorl(first.AsRegister<Register>(),
7478 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007479 }
7480 } else {
7481 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007482 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007483 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007484 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007485 } else {
7486 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007487 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007488 }
7489 }
7490 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007491 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007492 if (second.IsRegisterPair()) {
7493 if (instruction->IsAnd()) {
7494 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7495 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7496 } else if (instruction->IsOr()) {
7497 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7498 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7499 } else {
7500 DCHECK(instruction->IsXor());
7501 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7502 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7503 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007504 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007505 if (instruction->IsAnd()) {
7506 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7507 __ andl(first.AsRegisterPairHigh<Register>(),
7508 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7509 } else if (instruction->IsOr()) {
7510 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7511 __ orl(first.AsRegisterPairHigh<Register>(),
7512 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7513 } else {
7514 DCHECK(instruction->IsXor());
7515 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7516 __ xorl(first.AsRegisterPairHigh<Register>(),
7517 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7518 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007519 } else {
7520 DCHECK(second.IsConstant()) << second;
7521 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007522 int32_t low_value = Low32Bits(value);
7523 int32_t high_value = High32Bits(value);
7524 Immediate low(low_value);
7525 Immediate high(high_value);
7526 Register first_low = first.AsRegisterPairLow<Register>();
7527 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007528 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007529 if (low_value == 0) {
7530 __ xorl(first_low, first_low);
7531 } else if (low_value != -1) {
7532 __ andl(first_low, low);
7533 }
7534 if (high_value == 0) {
7535 __ xorl(first_high, first_high);
7536 } else if (high_value != -1) {
7537 __ andl(first_high, high);
7538 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007539 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007540 if (low_value != 0) {
7541 __ orl(first_low, low);
7542 }
7543 if (high_value != 0) {
7544 __ orl(first_high, high);
7545 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007546 } else {
7547 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007548 if (low_value != 0) {
7549 __ xorl(first_low, low);
7550 }
7551 if (high_value != 0) {
7552 __ xorl(first_high, high);
7553 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007554 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007555 }
7556 }
7557}
7558
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007559void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7560 HInstruction* instruction,
7561 Location out,
7562 uint32_t offset,
7563 Location maybe_temp,
7564 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007565 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007566 if (read_barrier_option == kWithReadBarrier) {
7567 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007568 if (kUseBakerReadBarrier) {
7569 // Load with fast path based Baker's read barrier.
7570 // /* HeapReference<Object> */ out = *(out + offset)
7571 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007572 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007573 } else {
7574 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007575 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007576 // in the following move operation, as we will need it for the
7577 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007578 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007579 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007580 // /* HeapReference<Object> */ out = *(out + offset)
7581 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007582 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007583 }
7584 } else {
7585 // Plain load with no read barrier.
7586 // /* HeapReference<Object> */ out = *(out + offset)
7587 __ movl(out_reg, Address(out_reg, offset));
7588 __ MaybeUnpoisonHeapReference(out_reg);
7589 }
7590}
7591
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007592void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7593 HInstruction* instruction,
7594 Location out,
7595 Location obj,
7596 uint32_t offset,
7597 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007598 Register out_reg = out.AsRegister<Register>();
7599 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007600 if (read_barrier_option == kWithReadBarrier) {
7601 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007602 if (kUseBakerReadBarrier) {
7603 // Load with fast path based Baker's read barrier.
7604 // /* HeapReference<Object> */ out = *(obj + offset)
7605 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007606 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007607 } else {
7608 // Load with slow path based read barrier.
7609 // /* HeapReference<Object> */ out = *(obj + offset)
7610 __ movl(out_reg, Address(obj_reg, offset));
7611 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7612 }
7613 } else {
7614 // Plain load with no read barrier.
7615 // /* HeapReference<Object> */ out = *(obj + offset)
7616 __ movl(out_reg, Address(obj_reg, offset));
7617 __ MaybeUnpoisonHeapReference(out_reg);
7618 }
7619}
7620
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007621void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7622 HInstruction* instruction,
7623 Location root,
7624 const Address& address,
7625 Label* fixup_label,
7626 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007627 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007628 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007629 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007630 if (kUseBakerReadBarrier) {
7631 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7632 // Baker's read barrier are used:
7633 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007634 // root = obj.field;
7635 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7636 // if (temp != null) {
7637 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007638 // }
7639
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007640 // /* GcRoot<mirror::Object> */ root = *address
7641 __ movl(root_reg, address);
7642 if (fixup_label != nullptr) {
7643 __ Bind(fixup_label);
7644 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007645 static_assert(
7646 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7647 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7648 "have different sizes.");
7649 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7650 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7651 "have different sizes.");
7652
Vladimir Marko953437b2016-08-24 08:30:46 +00007653 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007654 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007655 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007656 codegen_->AddSlowPath(slow_path);
7657
Roland Levillaind966ce72017-02-09 16:20:14 +00007658 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7659 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007660 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007661 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7662 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007663 __ j(kNotEqual, slow_path->GetEntryLabel());
7664 __ Bind(slow_path->GetExitLabel());
7665 } else {
7666 // GC root loaded through a slow path for read barriers other
7667 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007668 // /* GcRoot<mirror::Object>* */ root = address
7669 __ leal(root_reg, address);
7670 if (fixup_label != nullptr) {
7671 __ Bind(fixup_label);
7672 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007673 // /* mirror::Object* */ root = root->Read()
7674 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7675 }
7676 } else {
7677 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007678 // /* GcRoot<mirror::Object> */ root = *address
7679 __ movl(root_reg, address);
7680 if (fixup_label != nullptr) {
7681 __ Bind(fixup_label);
7682 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007683 // Note that GC roots are not affected by heap poisoning, thus we
7684 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007685 }
7686}
7687
7688void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7689 Location ref,
7690 Register obj,
7691 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007692 bool needs_null_check) {
7693 DCHECK(kEmitCompilerReadBarrier);
7694 DCHECK(kUseBakerReadBarrier);
7695
7696 // /* HeapReference<Object> */ ref = *(obj + offset)
7697 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007698 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007699}
7700
7701void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7702 Location ref,
7703 Register obj,
7704 uint32_t data_offset,
7705 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007706 bool needs_null_check) {
7707 DCHECK(kEmitCompilerReadBarrier);
7708 DCHECK(kUseBakerReadBarrier);
7709
Roland Levillain3d312422016-06-23 13:53:42 +01007710 static_assert(
7711 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7712 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007713 // /* HeapReference<Object> */ ref =
7714 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007715 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007716 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007717}
7718
7719void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7720 Location ref,
7721 Register obj,
7722 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007723 bool needs_null_check,
7724 bool always_update_field,
7725 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007726 DCHECK(kEmitCompilerReadBarrier);
7727 DCHECK(kUseBakerReadBarrier);
7728
7729 // In slow path based read barriers, the read barrier call is
7730 // inserted after the original load. However, in fast path based
7731 // Baker's read barriers, we need to perform the load of
7732 // mirror::Object::monitor_ *before* the original reference load.
7733 // This load-load ordering is required by the read barrier.
7734 // The fast path/slow path (for Baker's algorithm) should look like:
7735 //
7736 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7737 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7738 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007739 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007740 // if (is_gray) {
7741 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7742 // }
7743 //
7744 // Note: the original implementation in ReadBarrier::Barrier is
7745 // slightly more complex as:
7746 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007747 // the high-bits of rb_state, which are expected to be all zeroes
7748 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7749 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007750 // - it performs additional checks that we do not do here for
7751 // performance reasons.
7752
7753 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007754 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7755
Vladimir Marko953437b2016-08-24 08:30:46 +00007756 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007757 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7758 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007759 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7760 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7761 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7762
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007763 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007764 // ref = ReadBarrier::Mark(ref);
7765 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7766 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007767 if (needs_null_check) {
7768 MaybeRecordImplicitNullCheck(instruction);
7769 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007770
7771 // Load fence to prevent load-load reordering.
7772 // Note that this is a no-op, thanks to the x86 memory model.
7773 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7774
7775 // The actual reference load.
7776 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007777 __ movl(ref_reg, src); // Flags are unaffected.
7778
7779 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7780 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007781 SlowPathCode* slow_path;
7782 if (always_update_field) {
7783 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007784 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007785 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7786 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007787 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007788 instruction, ref, /* unpoison_ref_before_marking */ true);
7789 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007790 AddSlowPath(slow_path);
7791
7792 // We have done the "if" of the gray bit check above, now branch based on the flags.
7793 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007794
7795 // Object* ref = ref_addr->AsMirrorPtr()
7796 __ MaybeUnpoisonHeapReference(ref_reg);
7797
Roland Levillain7c1559a2015-12-15 10:55:36 +00007798 __ Bind(slow_path->GetExitLabel());
7799}
7800
7801void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7802 Location out,
7803 Location ref,
7804 Location obj,
7805 uint32_t offset,
7806 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007807 DCHECK(kEmitCompilerReadBarrier);
7808
Roland Levillain7c1559a2015-12-15 10:55:36 +00007809 // Insert a slow path based read barrier *after* the reference load.
7810 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007811 // If heap poisoning is enabled, the unpoisoning of the loaded
7812 // reference will be carried out by the runtime within the slow
7813 // path.
7814 //
7815 // Note that `ref` currently does not get unpoisoned (when heap
7816 // poisoning is enabled), which is alright as the `ref` argument is
7817 // not used by the artReadBarrierSlow entry point.
7818 //
7819 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007820 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007821 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7822 AddSlowPath(slow_path);
7823
Roland Levillain0d5a2812015-11-13 10:07:31 +00007824 __ jmp(slow_path->GetEntryLabel());
7825 __ Bind(slow_path->GetExitLabel());
7826}
7827
Roland Levillain7c1559a2015-12-15 10:55:36 +00007828void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7829 Location out,
7830 Location ref,
7831 Location obj,
7832 uint32_t offset,
7833 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007834 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007835 // Baker's read barriers shall be handled by the fast path
7836 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7837 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007838 // If heap poisoning is enabled, unpoisoning will be taken care of
7839 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007840 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007841 } else if (kPoisonHeapReferences) {
7842 __ UnpoisonHeapReference(out.AsRegister<Register>());
7843 }
7844}
7845
Roland Levillain7c1559a2015-12-15 10:55:36 +00007846void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7847 Location out,
7848 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007849 DCHECK(kEmitCompilerReadBarrier);
7850
Roland Levillain7c1559a2015-12-15 10:55:36 +00007851 // Insert a slow path based read barrier *after* the GC root load.
7852 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007853 // Note that GC roots are not affected by heap poisoning, so we do
7854 // not need to do anything special for this here.
7855 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007856 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007857 AddSlowPath(slow_path);
7858
Roland Levillain0d5a2812015-11-13 10:07:31 +00007859 __ jmp(slow_path->GetEntryLabel());
7860 __ Bind(slow_path->GetExitLabel());
7861}
7862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007863void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007864 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007865 LOG(FATAL) << "Unreachable";
7866}
7867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007868void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007869 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007870 LOG(FATAL) << "Unreachable";
7871}
7872
Mark Mendellfe57faa2015-09-18 09:26:15 -04007873// Simple implementation of packed switch - generate cascaded compare/jumps.
7874void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7875 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007876 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007877 locations->SetInAt(0, Location::RequiresRegister());
7878}
7879
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007880void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7881 int32_t lower_bound,
7882 uint32_t num_entries,
7883 HBasicBlock* switch_block,
7884 HBasicBlock* default_block) {
7885 // Figure out the correct compare values and jump conditions.
7886 // Handle the first compare/branch as a special case because it might
7887 // jump to the default case.
7888 DCHECK_GT(num_entries, 2u);
7889 Condition first_condition;
7890 uint32_t index;
7891 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7892 if (lower_bound != 0) {
7893 first_condition = kLess;
7894 __ cmpl(value_reg, Immediate(lower_bound));
7895 __ j(first_condition, codegen_->GetLabelOf(default_block));
7896 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007897
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007898 index = 1;
7899 } else {
7900 // Handle all the compare/jumps below.
7901 first_condition = kBelow;
7902 index = 0;
7903 }
7904
7905 // Handle the rest of the compare/jumps.
7906 for (; index + 1 < num_entries; index += 2) {
7907 int32_t compare_to_value = lower_bound + index + 1;
7908 __ cmpl(value_reg, Immediate(compare_to_value));
7909 // Jump to successors[index] if value < case_value[index].
7910 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7911 // Jump to successors[index + 1] if value == case_value[index + 1].
7912 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7913 }
7914
7915 if (index != num_entries) {
7916 // There are an odd number of entries. Handle the last one.
7917 DCHECK_EQ(index + 1, num_entries);
7918 __ cmpl(value_reg, Immediate(lower_bound + index));
7919 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007920 }
7921
7922 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007923 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7924 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007925 }
7926}
7927
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007928void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7929 int32_t lower_bound = switch_instr->GetStartValue();
7930 uint32_t num_entries = switch_instr->GetNumEntries();
7931 LocationSummary* locations = switch_instr->GetLocations();
7932 Register value_reg = locations->InAt(0).AsRegister<Register>();
7933
7934 GenPackedSwitchWithCompares(value_reg,
7935 lower_bound,
7936 num_entries,
7937 switch_instr->GetBlock(),
7938 switch_instr->GetDefaultBlock());
7939}
7940
Mark Mendell805b3b52015-09-18 14:10:29 -04007941void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7942 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007943 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007944 locations->SetInAt(0, Location::RequiresRegister());
7945
7946 // Constant area pointer.
7947 locations->SetInAt(1, Location::RequiresRegister());
7948
7949 // And the temporary we need.
7950 locations->AddTemp(Location::RequiresRegister());
7951}
7952
7953void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7954 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007955 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007956 LocationSummary* locations = switch_instr->GetLocations();
7957 Register value_reg = locations->InAt(0).AsRegister<Register>();
7958 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7959
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007960 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7961 GenPackedSwitchWithCompares(value_reg,
7962 lower_bound,
7963 num_entries,
7964 switch_instr->GetBlock(),
7965 default_block);
7966 return;
7967 }
7968
Mark Mendell805b3b52015-09-18 14:10:29 -04007969 // Optimizing has a jump area.
7970 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7971 Register constant_area = locations->InAt(1).AsRegister<Register>();
7972
7973 // Remove the bias, if needed.
7974 if (lower_bound != 0) {
7975 __ leal(temp_reg, Address(value_reg, -lower_bound));
7976 value_reg = temp_reg;
7977 }
7978
7979 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007980 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007981 __ cmpl(value_reg, Immediate(num_entries - 1));
7982 __ j(kAbove, codegen_->GetLabelOf(default_block));
7983
7984 // We are in the range of the table.
7985 // Load (target-constant_area) from the jump table, indexing by the value.
7986 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7987
7988 // Compute the actual target address by adding in constant_area.
7989 __ addl(temp_reg, constant_area);
7990
7991 // And jump.
7992 __ jmp(temp_reg);
7993}
7994
Mark Mendell0616ae02015-04-17 12:49:27 -04007995void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7996 HX86ComputeBaseMethodAddress* insn) {
7997 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007998 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007999 locations->SetOut(Location::RequiresRegister());
8000}
8001
8002void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8003 HX86ComputeBaseMethodAddress* insn) {
8004 LocationSummary* locations = insn->GetLocations();
8005 Register reg = locations->Out().AsRegister<Register>();
8006
8007 // Generate call to next instruction.
8008 Label next_instruction;
8009 __ call(&next_instruction);
8010 __ Bind(&next_instruction);
8011
8012 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008013 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008014
8015 // Grab the return address off the stack.
8016 __ popl(reg);
8017}
8018
8019void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8020 HX86LoadFromConstantTable* insn) {
8021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008022 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008023
8024 locations->SetInAt(0, Location::RequiresRegister());
8025 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8026
8027 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008028 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008029 return;
8030 }
8031
8032 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008033 case DataType::Type::kFloat32:
8034 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008035 locations->SetOut(Location::RequiresFpuRegister());
8036 break;
8037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008038 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008039 locations->SetOut(Location::RequiresRegister());
8040 break;
8041
8042 default:
8043 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8044 }
8045}
8046
8047void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008048 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008049 return;
8050 }
8051
8052 LocationSummary* locations = insn->GetLocations();
8053 Location out = locations->Out();
8054 Register const_area = locations->InAt(0).AsRegister<Register>();
8055 HConstant *value = insn->GetConstant();
8056
8057 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008058 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008059 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008060 codegen_->LiteralFloatAddress(
8061 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008062 break;
8063
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008064 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008065 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008066 codegen_->LiteralDoubleAddress(
8067 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008068 break;
8069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008070 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008071 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008072 codegen_->LiteralInt32Address(
8073 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008074 break;
8075
8076 default:
8077 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8078 }
8079}
8080
Mark Mendell0616ae02015-04-17 12:49:27 -04008081/**
8082 * Class to handle late fixup of offsets into constant area.
8083 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008084class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008085 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008086 RIPFixup(CodeGeneratorX86& codegen,
8087 HX86ComputeBaseMethodAddress* base_method_address,
8088 size_t offset)
8089 : codegen_(&codegen),
8090 base_method_address_(base_method_address),
8091 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008092
8093 protected:
8094 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8095
8096 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008097 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008098
8099 private:
8100 void Process(const MemoryRegion& region, int pos) OVERRIDE {
8101 // Patch the correct offset for the instruction. The place to patch is the
8102 // last 4 bytes of the instruction.
8103 // The value to patch is the distance from the offset in the constant area
8104 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008105 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008106 int32_t relative_position =
8107 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008108
8109 // Patch in the right value.
8110 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8111 }
8112
Mark Mendell0616ae02015-04-17 12:49:27 -04008113 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008114 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008115};
8116
Mark Mendell805b3b52015-09-18 14:10:29 -04008117/**
8118 * Class to handle late fixup of offsets to a jump table that will be created in the
8119 * constant area.
8120 */
8121class JumpTableRIPFixup : public RIPFixup {
8122 public:
8123 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008124 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8125 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008126
8127 void CreateJumpTable() {
8128 X86Assembler* assembler = codegen_->GetAssembler();
8129
8130 // Ensure that the reference to the jump table has the correct offset.
8131 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8132 SetOffset(offset_in_constant_table);
8133
8134 // The label values in the jump table are computed relative to the
8135 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008136 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008137
8138 // Populate the jump table with the correct values for the jump table.
8139 int32_t num_entries = switch_instr_->GetNumEntries();
8140 HBasicBlock* block = switch_instr_->GetBlock();
8141 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8142 // The value that we want is the target offset - the position of the table.
8143 for (int32_t i = 0; i < num_entries; i++) {
8144 HBasicBlock* b = successors[i];
8145 Label* l = codegen_->GetLabelOf(b);
8146 DCHECK(l->IsBound());
8147 int32_t offset_to_block = l->Position() - relative_offset;
8148 assembler->AppendInt32(offset_to_block);
8149 }
8150 }
8151
8152 private:
8153 const HX86PackedSwitch* switch_instr_;
8154};
8155
8156void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8157 // Generate the constant area if needed.
8158 X86Assembler* assembler = GetAssembler();
8159 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8160 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8161 // byte values.
8162 assembler->Align(4, 0);
8163 constant_area_start_ = assembler->CodeSize();
8164
8165 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008166 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008167 jump_table->CreateJumpTable();
8168 }
8169
8170 // And now add the constant area to the generated code.
8171 assembler->AddConstantArea();
8172 }
8173
8174 // And finish up.
8175 CodeGenerator::Finalize(allocator);
8176}
8177
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008178Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8179 HX86ComputeBaseMethodAddress* method_base,
8180 Register reg) {
8181 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008182 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008183 return Address(reg, kDummy32BitOffset, fixup);
8184}
8185
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008186Address CodeGeneratorX86::LiteralFloatAddress(float v,
8187 HX86ComputeBaseMethodAddress* method_base,
8188 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008189 AssemblerFixup* fixup =
8190 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008191 return Address(reg, kDummy32BitOffset, fixup);
8192}
8193
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008194Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8195 HX86ComputeBaseMethodAddress* method_base,
8196 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008197 AssemblerFixup* fixup =
8198 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008199 return Address(reg, kDummy32BitOffset, fixup);
8200}
8201
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008202Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8203 HX86ComputeBaseMethodAddress* method_base,
8204 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008205 AssemblerFixup* fixup =
8206 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008207 return Address(reg, kDummy32BitOffset, fixup);
8208}
8209
Aart Bika19616e2016-02-01 18:57:58 -08008210void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8211 if (value == 0) {
8212 __ xorl(dest, dest);
8213 } else {
8214 __ movl(dest, Immediate(value));
8215 }
8216}
8217
8218void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8219 if (value == 0) {
8220 __ testl(dest, dest);
8221 } else {
8222 __ cmpl(dest, Immediate(value));
8223 }
8224}
8225
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008226void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8227 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008228 GenerateIntCompare(lhs_reg, rhs);
8229}
8230
8231void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008232 if (rhs.IsConstant()) {
8233 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008234 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008235 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008236 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008237 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008238 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008239 }
8240}
8241
8242Address CodeGeneratorX86::ArrayAddress(Register obj,
8243 Location index,
8244 ScaleFactor scale,
8245 uint32_t data_offset) {
8246 return index.IsConstant() ?
8247 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8248 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8249}
8250
Mark Mendell805b3b52015-09-18 14:10:29 -04008251Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8252 Register reg,
8253 Register value) {
8254 // Create a fixup to be used to create and address the jump table.
8255 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008256 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008257
8258 // We have to populate the jump tables.
8259 fixups_to_jump_tables_.push_back(table_fixup);
8260
8261 // We want a scaled address, as we are extracting the correct offset from the table.
8262 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8263}
8264
Andreas Gampe85b62f22015-09-09 13:15:38 -07008265// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008266void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008267 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008268 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008269 return;
8270 }
8271
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008272 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008273
8274 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8275 if (target.Equals(return_loc)) {
8276 return;
8277 }
8278
8279 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8280 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008281 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008282 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008283 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8284 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008285 GetMoveResolver()->EmitNativeCode(&parallel_move);
8286 } else {
8287 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008288 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008289 parallel_move.AddMove(return_loc, target, type, nullptr);
8290 GetMoveResolver()->EmitNativeCode(&parallel_move);
8291 }
8292}
8293
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008294void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8295 const uint8_t* roots_data,
8296 const PatchInfo<Label>& info,
8297 uint64_t index_in_table) const {
8298 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8299 uintptr_t address =
8300 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8301 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
8302 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8303 dchecked_integral_cast<uint32_t>(address);
8304}
8305
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008306void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8307 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008308 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008309 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008310 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008311 }
8312
8313 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008314 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008315 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008316 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008317 }
8318}
8319
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008320void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8321 ATTRIBUTE_UNUSED) {
8322 LOG(FATAL) << "Unreachable";
8323}
8324
8325void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8326 ATTRIBUTE_UNUSED) {
8327 LOG(FATAL) << "Unreachable";
8328}
8329
Roland Levillain4d027112015-07-01 15:41:14 +01008330#undef __
8331
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008332} // namespace x86
8333} // namespace art