blob: 4a279d8de173e4f954f79c4bc986ab8cd716d7db [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"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010065 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010066 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010087 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000089 }
90
Alexandre Rames8158f282015-08-07 10:26:17 +010091 bool IsFatal() const OVERRIDE { return true; }
92
Alexandre Rames9931f312015-06-19 14:47:01 +010093 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
94
Calin Juravled0d48522014-11-04 16:40:20 +000095 private:
Calin Juravled0d48522014-11-04 16:40:20 +000096 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
102 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(reg_);
108 } else {
109 __ movl(reg_, Immediate(0));
110 }
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ jmp(GetExitLabel());
112 }
113
Alexandre Rames9931f312015-06-19 14:47:01 +0100114 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
115
Calin Juravled0d48522014-11-04 16:40:20 +0000116 private:
117 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 bool is_div_;
119 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000120};
121
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000124 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000130 // We're moving two locations to locations that could overlap, so we need a parallel
131 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000132 if (instruction_->CanThrowIntoCatchBlock()) {
133 // Live registers will be restored in the catch block if caught.
134 SaveLiveRegisters(codegen, instruction_->GetLocations());
135 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400136
137 // Are we using an array length from memory?
138 HInstruction* array_length = instruction_->InputAt(1);
139 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400141 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
142 // Load the array length into our temporary.
143 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
144 Location array_loc = array_length->GetLocations()->InAt(0);
145 Address array_len(array_loc.AsRegister<Register>(), len_offset);
146 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
147 // Check for conflicts with index.
148 if (length_loc.Equals(locations->InAt(0))) {
149 // We know we aren't using parameter 2.
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
151 }
152 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700153 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100154 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100160 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100162 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
163 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100164 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
165 ? kQuickThrowStringBounds
166 : kQuickThrowArrayBounds;
167 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000169 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 }
171
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 bool IsFatal() const OVERRIDE { return true; }
173
Alexandre Rames9931f312015-06-19 14:47:01 +0100174 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
178};
179
Andreas Gampe85b62f22015-09-09 13:15:38 -0700180class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000183 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700186 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100187 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700189 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100190 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000191 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700192 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100193 if (successor_ == nullptr) {
194 __ jmp(GetReturnLabel());
195 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100196 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000198 }
199
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 Label* GetReturnLabel() {
201 DCHECK(successor_ == nullptr);
202 return &return_label_;
203 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100205 HBasicBlock* GetSuccessor() const {
206 return successor_;
207 }
208
Alexandre Rames9931f312015-06-19 14:47:01 +0100209 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
210
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100212 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213 Label return_label_;
214
215 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
216};
217
Vladimir Markoaad75c62016-10-03 08:46:48 +0000218class LoadStringSlowPathX86 : public SlowPathCode {
219 public:
220 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
221
222 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
223 LocationSummary* locations = instruction_->GetLocations();
224 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
225
226 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
227 __ Bind(GetEntryLabel());
228 SaveLiveRegisters(codegen, locations);
229
230 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000231 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
232 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000233 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
234 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
235 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
236 RestoreLiveRegisters(codegen, locations);
237
238 // Store the resolved String to the BSS entry.
239 Register method_address = locations->InAt(0).AsRegister<Register>();
240 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
241 locations->Out().AsRegister<Register>());
242 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
243 __ Bind(fixup_label);
244
245 __ jmp(GetExitLabel());
246 }
247
248 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
252};
253
Andreas Gampe85b62f22015-09-09 13:15:38 -0700254class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 public:
256 LoadClassSlowPathX86(HLoadClass* cls,
257 HInstruction* at,
258 uint32_t dex_pc,
259 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000260 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
262 }
263
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000264 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000265 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
267 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269
270 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000271 dex::TypeIndex type_index = cls_->GetTypeIndex();
272 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100273 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
274 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000275 instruction_,
276 dex_pc_,
277 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000278 if (do_clinit_) {
279 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
280 } else {
281 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
282 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283
284 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 Location out = locations->Out();
286 if (out.IsValid()) {
287 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
288 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000291 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
292 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
293 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
294 DCHECK(out.IsValid());
295 Register method_address = locations->InAt(0).AsRegister<Register>();
296 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
297 locations->Out().AsRegister<Register>());
298 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
299 __ Bind(fixup_label);
300 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000301 __ jmp(GetExitLabel());
302 }
303
Alexandre Rames9931f312015-06-19 14:47:01 +0100304 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
305
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000306 private:
307 // The class this slow path will load.
308 HLoadClass* const cls_;
309
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310 // The dex PC of `at_`.
311 const uint32_t dex_pc_;
312
313 // Whether to initialize the class.
314 const bool do_clinit_;
315
316 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
317};
318
Andreas Gampe85b62f22015-09-09 13:15:38 -0700319class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000321 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000322 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000324 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 DCHECK(instruction_->IsCheckCast()
327 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
329 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
330 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 if (!is_fatal_) {
333 SaveLiveRegisters(codegen, locations);
334 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335
336 // We're moving two locations to locations that could overlap, so we need a parallel
337 // move resolver.
338 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800339 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800340 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
341 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800342 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
344 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100346 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100347 instruction_,
348 instruction_->GetDexPc(),
349 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 } else {
352 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800353 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
357 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 }
359
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 if (!is_fatal_) {
361 if (instruction_->IsInstanceOf()) {
362 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
363 }
364 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 __ jmp(GetExitLabel());
367 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100372
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375
376 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
377};
378
Andreas Gampe85b62f22015-09-09 13:15:38 -0700379class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 public:
Aart Bik42249c32016-01-07 15:33:50 -0800381 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000382 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383
384 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100385 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100387 LocationSummary* locations = instruction_->GetLocations();
388 SaveLiveRegisters(codegen, locations);
389 InvokeRuntimeCallingConvention calling_convention;
390 x86_codegen->Load32BitValue(
391 calling_convention.GetRegisterAt(0),
392 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100393 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100394 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
401};
402
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100403class ArraySetSlowPathX86 : public SlowPathCode {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConvention calling_convention;
413 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
414 parallel_move.AddMove(
415 locations->InAt(0),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
417 Primitive::kPrimNot,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(1),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
422 Primitive::kPrimInt,
423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(2),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
427 Primitive::kPrimNot,
428 nullptr);
429 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
430
431 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100432 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000433 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 RestoreLiveRegisters(codegen, locations);
435 __ jmp(GetExitLabel());
436 }
437
438 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
439
440 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
442};
443
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100444// Slow path marking an object reference `ref` during a read
445// barrier. The field `obj.field` in the object `obj` holding this
446// reference does not get updated by this slow path after marking (see
447// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
448//
449// This means that after the execution of this slow path, `ref` will
450// always be up-to-date, but `obj.field` may not; i.e., after the
451// flip, `ref` will be a to-space reference, but `obj.field` will
452// probably still be a from-space reference (unless it gets updated by
453// another thread, or if another thread installed another object
454// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000455class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
456 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100457 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
458 Location ref,
459 bool unpoison_ref_before_marking)
460 : SlowPathCode(instruction),
461 ref_(ref),
462 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000463 DCHECK(kEmitCompilerReadBarrier);
464 }
465
466 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
467
468 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
469 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100472 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473 DCHECK(instruction_->IsInstanceFieldGet() ||
474 instruction_->IsStaticFieldGet() ||
475 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100476 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 instruction_->IsLoadClass() ||
478 instruction_->IsLoadString() ||
479 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100480 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100481 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
482 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000483 << "Unexpected instruction in read barrier marking slow path: "
484 << instruction_->DebugName();
485
486 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000488 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000490 }
Roland Levillain4359e612016-07-20 11:32:19 +0100491 // No need to save live registers; it's taken care of by the
492 // entrypoint. Also, there is no need to update the stack mask,
493 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000494 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 DCHECK_NE(ref_reg, ESP);
496 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100497 // "Compact" slow path, saving two moves.
498 //
499 // Instead of using the standard runtime calling convention (input
500 // and output in EAX):
501 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100503 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100505 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // of a dedicated entrypoint:
508 //
509 // rX <- ReadBarrierMarkRegX(rX)
510 //
511 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100513 // This runtime call does not require a stack map.
514 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000515 __ jmp(GetExitLabel());
516 }
517
518 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100519 // The location (register) of the marked object reference.
520 const Location ref_;
521 // Should the reference in `ref_` be unpoisoned prior to marking it?
522 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000523
524 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
525};
526
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100527// Slow path marking an object reference `ref` during a read barrier,
528// and if needed, atomically updating the field `obj.field` in the
529// object `obj` holding this reference after marking (contrary to
530// ReadBarrierMarkSlowPathX86 above, which never tries to update
531// `obj.field`).
532//
533// This means that after the execution of this slow path, both `ref`
534// and `obj.field` will be up-to-date; i.e., after the flip, both will
535// hold the same to-space reference (unless another thread installed
536// another object reference (different from `ref`) in `obj.field`).
537class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
538 public:
539 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
540 Location ref,
541 Register obj,
542 const Address& field_addr,
543 bool unpoison_ref_before_marking,
544 Register temp)
545 : SlowPathCode(instruction),
546 ref_(ref),
547 obj_(obj),
548 field_addr_(field_addr),
549 unpoison_ref_before_marking_(unpoison_ref_before_marking),
550 temp_(temp) {
551 DCHECK(kEmitCompilerReadBarrier);
552 }
553
554 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
555
556 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
557 LocationSummary* locations = instruction_->GetLocations();
558 Register ref_reg = ref_.AsRegister<Register>();
559 DCHECK(locations->CanCall());
560 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
561 // This slow path is only used by the UnsafeCASObject intrinsic.
562 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
563 << "Unexpected instruction in read barrier marking and field updating slow path: "
564 << instruction_->DebugName();
565 DCHECK(instruction_->GetLocations()->Intrinsified());
566 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
567
568 __ Bind(GetEntryLabel());
569 if (unpoison_ref_before_marking_) {
570 // Object* ref = ref_addr->AsMirrorPtr()
571 __ MaybeUnpoisonHeapReference(ref_reg);
572 }
573
574 // Save the old (unpoisoned) reference.
575 __ movl(temp_, ref_reg);
576
577 // No need to save live registers; it's taken care of by the
578 // entrypoint. Also, there is no need to update the stack mask,
579 // as this runtime call will not trigger a garbage collection.
580 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
581 DCHECK_NE(ref_reg, ESP);
582 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
583 // "Compact" slow path, saving two moves.
584 //
585 // Instead of using the standard runtime calling convention (input
586 // and output in EAX):
587 //
588 // EAX <- ref
589 // EAX <- ReadBarrierMark(EAX)
590 // ref <- EAX
591 //
592 // we just use rX (the register containing `ref`) as input and output
593 // of a dedicated entrypoint:
594 //
595 // rX <- ReadBarrierMarkRegX(rX)
596 //
597 int32_t entry_point_offset =
598 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
599 // This runtime call does not require a stack map.
600 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
601
602 // If the new reference is different from the old reference,
603 // update the field in the holder (`*field_addr`).
604 //
605 // Note that this field could also hold a different object, if
606 // another thread had concurrently changed it. In that case, the
607 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
608 // operation below would abort the CAS, leaving the field as-is.
609 NearLabel done;
610 __ cmpl(temp_, ref_reg);
611 __ j(kEqual, &done);
612
613 // Update the the holder's field atomically. This may fail if
614 // mutator updates before us, but it's OK. This is achieved
615 // using a strong compare-and-set (CAS) operation with relaxed
616 // memory synchronization ordering, where the expected value is
617 // the old reference and the desired value is the new reference.
618 // This operation is implemented with a 32-bit LOCK CMPXLCHG
619 // instruction, which requires the expected value (the old
620 // reference) to be in EAX. Save EAX beforehand, and move the
621 // expected value (stored in `temp_`) into EAX.
622 __ pushl(EAX);
623 __ movl(EAX, temp_);
624
625 // Convenience aliases.
626 Register base = obj_;
627 Register expected = EAX;
628 Register value = ref_reg;
629
630 bool base_equals_value = (base == value);
631 if (kPoisonHeapReferences) {
632 if (base_equals_value) {
633 // If `base` and `value` are the same register location, move
634 // `value` to a temporary register. This way, poisoning
635 // `value` won't invalidate `base`.
636 value = temp_;
637 __ movl(value, base);
638 }
639
640 // Check that the register allocator did not assign the location
641 // of `expected` (EAX) to `value` nor to `base`, so that heap
642 // poisoning (when enabled) works as intended below.
643 // - If `value` were equal to `expected`, both references would
644 // be poisoned twice, meaning they would not be poisoned at
645 // all, as heap poisoning uses address negation.
646 // - If `base` were equal to `expected`, poisoning `expected`
647 // would invalidate `base`.
648 DCHECK_NE(value, expected);
649 DCHECK_NE(base, expected);
650
651 __ PoisonHeapReference(expected);
652 __ PoisonHeapReference(value);
653 }
654
655 __ LockCmpxchgl(field_addr_, value);
656
657 // If heap poisoning is enabled, we need to unpoison the values
658 // that were poisoned earlier.
659 if (kPoisonHeapReferences) {
660 if (base_equals_value) {
661 // `value` has been moved to a temporary register, no need
662 // to unpoison it.
663 } else {
664 __ UnpoisonHeapReference(value);
665 }
666 // No need to unpoison `expected` (EAX), as it is be overwritten below.
667 }
668
669 // Restore EAX.
670 __ popl(EAX);
671
672 __ Bind(&done);
673 __ jmp(GetExitLabel());
674 }
675
676 private:
677 // The location (register) of the marked object reference.
678 const Location ref_;
679 // The register containing the object holding the marked object reference field.
680 const Register obj_;
681 // The address of the marked reference field. The base of this address must be `obj_`.
682 const Address field_addr_;
683
684 // Should the reference in `ref_` be unpoisoned prior to marking it?
685 const bool unpoison_ref_before_marking_;
686
687 const Register temp_;
688
689 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
690};
691
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692// Slow path generating a read barrier for a heap reference.
693class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
694 public:
695 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
696 Location out,
697 Location ref,
698 Location obj,
699 uint32_t offset,
700 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000701 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000702 out_(out),
703 ref_(ref),
704 obj_(obj),
705 offset_(offset),
706 index_(index) {
707 DCHECK(kEmitCompilerReadBarrier);
708 // If `obj` is equal to `out` or `ref`, it means the initial object
709 // has been overwritten by (or after) the heap object reference load
710 // to be instrumented, e.g.:
711 //
712 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000713 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000714 //
715 // In that case, we have lost the information about the original
716 // object, and the emitted read barrier cannot work properly.
717 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
718 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
719 }
720
721 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
722 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
723 LocationSummary* locations = instruction_->GetLocations();
724 Register reg_out = out_.AsRegister<Register>();
725 DCHECK(locations->CanCall());
726 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100727 DCHECK(instruction_->IsInstanceFieldGet() ||
728 instruction_->IsStaticFieldGet() ||
729 instruction_->IsArrayGet() ||
730 instruction_->IsInstanceOf() ||
731 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700732 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000733 << "Unexpected instruction in read barrier for heap reference slow path: "
734 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000735
736 __ Bind(GetEntryLabel());
737 SaveLiveRegisters(codegen, locations);
738
739 // We may have to change the index's value, but as `index_` is a
740 // constant member (like other "inputs" of this slow path),
741 // introduce a copy of it, `index`.
742 Location index = index_;
743 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100744 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000745 if (instruction_->IsArrayGet()) {
746 // Compute the actual memory offset and store it in `index`.
747 Register index_reg = index_.AsRegister<Register>();
748 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
749 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
750 // We are about to change the value of `index_reg` (see the
751 // calls to art::x86::X86Assembler::shll and
752 // art::x86::X86Assembler::AddImmediate below), but it has
753 // not been saved by the previous call to
754 // art::SlowPathCode::SaveLiveRegisters, as it is a
755 // callee-save register --
756 // art::SlowPathCode::SaveLiveRegisters does not consider
757 // callee-save registers, as it has been designed with the
758 // assumption that callee-save registers are supposed to be
759 // handled by the called function. So, as a callee-save
760 // register, `index_reg` _would_ eventually be saved onto
761 // the stack, but it would be too late: we would have
762 // changed its value earlier. Therefore, we manually save
763 // it here into another freely available register,
764 // `free_reg`, chosen of course among the caller-save
765 // registers (as a callee-save `free_reg` register would
766 // exhibit the same problem).
767 //
768 // Note we could have requested a temporary register from
769 // the register allocator instead; but we prefer not to, as
770 // this is a slow path, and we know we can find a
771 // caller-save register that is available.
772 Register free_reg = FindAvailableCallerSaveRegister(codegen);
773 __ movl(free_reg, index_reg);
774 index_reg = free_reg;
775 index = Location::RegisterLocation(index_reg);
776 } else {
777 // The initial register stored in `index_` has already been
778 // saved in the call to art::SlowPathCode::SaveLiveRegisters
779 // (as it is not a callee-save register), so we can freely
780 // use it.
781 }
782 // Shifting the index value contained in `index_reg` by the scale
783 // factor (2) cannot overflow in practice, as the runtime is
784 // unable to allocate object arrays with a size larger than
785 // 2^26 - 1 (that is, 2^28 - 4 bytes).
786 __ shll(index_reg, Immediate(TIMES_4));
787 static_assert(
788 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
789 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
790 __ AddImmediate(index_reg, Immediate(offset_));
791 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100792 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
793 // intrinsics, `index_` is not shifted by a scale factor of 2
794 // (as in the case of ArrayGet), as it is actually an offset
795 // to an object field within an object.
796 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 DCHECK(instruction_->GetLocations()->Intrinsified());
798 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
799 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
800 << instruction_->AsInvoke()->GetIntrinsic();
801 DCHECK_EQ(offset_, 0U);
802 DCHECK(index_.IsRegisterPair());
803 // UnsafeGet's offset location is a register pair, the low
804 // part contains the correct offset.
805 index = index_.ToLow();
806 }
807 }
808
809 // We're moving two or three locations to locations that could
810 // overlap, so we need a parallel move resolver.
811 InvokeRuntimeCallingConvention calling_convention;
812 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
813 parallel_move.AddMove(ref_,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
815 Primitive::kPrimNot,
816 nullptr);
817 parallel_move.AddMove(obj_,
818 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
819 Primitive::kPrimNot,
820 nullptr);
821 if (index.IsValid()) {
822 parallel_move.AddMove(index,
823 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
824 Primitive::kPrimInt,
825 nullptr);
826 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
827 } else {
828 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
829 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
830 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100831 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000832 CheckEntrypointTypes<
833 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
834 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
835
836 RestoreLiveRegisters(codegen, locations);
837 __ jmp(GetExitLabel());
838 }
839
840 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
841
842 private:
843 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
844 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
845 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
846 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
847 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
848 return static_cast<Register>(i);
849 }
850 }
851 // We shall never fail to find a free caller-save register, as
852 // there are more than two core caller-save registers on x86
853 // (meaning it is possible to find one which is different from
854 // `ref` and `obj`).
855 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
856 LOG(FATAL) << "Could not find a free caller-save register";
857 UNREACHABLE();
858 }
859
Roland Levillain0d5a2812015-11-13 10:07:31 +0000860 const Location out_;
861 const Location ref_;
862 const Location obj_;
863 const uint32_t offset_;
864 // An additional location containing an index to an array.
865 // Only used for HArrayGet and the UnsafeGetObject &
866 // UnsafeGetObjectVolatile intrinsics.
867 const Location index_;
868
869 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
870};
871
872// Slow path generating a read barrier for a GC root.
873class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
874 public:
875 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000876 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000877 DCHECK(kEmitCompilerReadBarrier);
878 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879
880 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
881 LocationSummary* locations = instruction_->GetLocations();
882 Register reg_out = out_.AsRegister<Register>();
883 DCHECK(locations->CanCall());
884 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000885 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
886 << "Unexpected instruction in read barrier for GC root slow path: "
887 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888
889 __ Bind(GetEntryLabel());
890 SaveLiveRegisters(codegen, locations);
891
892 InvokeRuntimeCallingConvention calling_convention;
893 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
894 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100895 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896 instruction_,
897 instruction_->GetDexPc(),
898 this);
899 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
900 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
901
902 RestoreLiveRegisters(codegen, locations);
903 __ jmp(GetExitLabel());
904 }
905
906 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
907
908 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000909 const Location out_;
910 const Location root_;
911
912 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
913};
914
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100915#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100916// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
917#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100918
Aart Bike9f37602015-10-09 11:15:55 -0700919inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700920 switch (cond) {
921 case kCondEQ: return kEqual;
922 case kCondNE: return kNotEqual;
923 case kCondLT: return kLess;
924 case kCondLE: return kLessEqual;
925 case kCondGT: return kGreater;
926 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700927 case kCondB: return kBelow;
928 case kCondBE: return kBelowEqual;
929 case kCondA: return kAbove;
930 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700931 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100932 LOG(FATAL) << "Unreachable";
933 UNREACHABLE();
934}
935
Aart Bike9f37602015-10-09 11:15:55 -0700936// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100937inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
938 switch (cond) {
939 case kCondEQ: return kEqual;
940 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700941 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 case kCondLT: return kBelow;
943 case kCondLE: return kBelowEqual;
944 case kCondGT: return kAbove;
945 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700946 // Unsigned remain unchanged.
947 case kCondB: return kBelow;
948 case kCondBE: return kBelowEqual;
949 case kCondA: return kAbove;
950 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 }
952 LOG(FATAL) << "Unreachable";
953 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700954}
955
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100956void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100958}
959
960void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100961 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100962}
963
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100964size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
965 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
966 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100967}
968
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100969size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
970 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
971 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100972}
973
Mark Mendell7c8d0092015-01-26 11:21:33 -0500974size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700975 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700976 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700977 } else {
978 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
979 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500980 return GetFloatingPointSpillSlotSize();
981}
982
983size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700984 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700985 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700986 } else {
987 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
988 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500989 return GetFloatingPointSpillSlotSize();
990}
991
Calin Juravle175dc732015-08-25 15:42:32 +0100992void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
993 HInstruction* instruction,
994 uint32_t dex_pc,
995 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100996 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100997 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
998 if (EntrypointRequiresStackMap(entrypoint)) {
999 RecordPcInfo(instruction, dex_pc, slow_path);
1000 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001001}
1002
Roland Levillaindec8f632016-07-22 17:10:06 +01001003void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1004 HInstruction* instruction,
1005 SlowPathCode* slow_path) {
1006 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001007 GenerateInvokeRuntime(entry_point_offset);
1008}
1009
1010void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001011 __ fs()->call(Address::Absolute(entry_point_offset));
1012}
1013
Mark Mendellfb8d2792015-03-31 22:16:59 -04001014CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001015 const X86InstructionSetFeatures& isa_features,
1016 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),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001031 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001032 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001033 isa_features_(isa_features),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001034 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001035 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001036 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1037 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001038 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001039 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001040 constant_area_start_(-1),
1041 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001042 method_address_offset_(std::less<uint32_t>(),
1043 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 // Use a fake return address register to mimic Quick.
1045 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001046}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001047
David Brazdil58282f42016-01-14 12:45:10 +00001048void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001050 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051}
1052
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001053InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001054 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055 assembler_(codegen->GetAssembler()),
1056 codegen_(codegen) {}
1057
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001059 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060}
1061
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001062void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001064 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001065 bool skip_overflow_check =
1066 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001067 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001068
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001070 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001072 }
1073
Mark Mendell5f874182015-03-04 15:42:45 -05001074 if (HasEmptyFrame()) {
1075 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001076 }
Mark Mendell5f874182015-03-04 15:42:45 -05001077
1078 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1079 Register reg = kCoreCalleeSaves[i];
1080 if (allocated_registers_.ContainsCoreRegister(reg)) {
1081 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001082 __ cfi().AdjustCFAOffset(kX86WordSize);
1083 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001084 }
1085 }
1086
Mingyao Yang063fc772016-08-02 11:02:54 -07001087 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1088 // Initialize should_deoptimize flag to 0.
1089 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1090 }
1091
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092 int adjust = GetFrameSize() - FrameEntrySpillSize();
1093 __ subl(ESP, Immediate(adjust));
1094 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001095 // Save the current method if we need it. Note that we do not
1096 // do this in HCurrentMethod, as the instruction might have been removed
1097 // in the SSA graph.
1098 if (RequiresCurrentMethod()) {
1099 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1100 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001101}
1102
1103void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001104 __ cfi().RememberState();
1105 if (!HasEmptyFrame()) {
1106 int adjust = GetFrameSize() - FrameEntrySpillSize();
1107 __ addl(ESP, Immediate(adjust));
1108 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001109
David Srbeckyc34dc932015-04-12 09:27:43 +01001110 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1111 Register reg = kCoreCalleeSaves[i];
1112 if (allocated_registers_.ContainsCoreRegister(reg)) {
1113 __ popl(reg);
1114 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1115 __ cfi().Restore(DWARFReg(reg));
1116 }
Mark Mendell5f874182015-03-04 15:42:45 -05001117 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001118 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001119 __ ret();
1120 __ cfi().RestoreState();
1121 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001122}
1123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001124void CodeGeneratorX86::Bind(HBasicBlock* block) {
1125 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001126}
1127
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001128Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1129 switch (type) {
1130 case Primitive::kPrimBoolean:
1131 case Primitive::kPrimByte:
1132 case Primitive::kPrimChar:
1133 case Primitive::kPrimShort:
1134 case Primitive::kPrimInt:
1135 case Primitive::kPrimNot:
1136 return Location::RegisterLocation(EAX);
1137
1138 case Primitive::kPrimLong:
1139 return Location::RegisterPairLocation(EAX, EDX);
1140
1141 case Primitive::kPrimVoid:
1142 return Location::NoLocation();
1143
1144 case Primitive::kPrimDouble:
1145 case Primitive::kPrimFloat:
1146 return Location::FpuRegisterLocation(XMM0);
1147 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001148
1149 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150}
1151
1152Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1153 return Location::RegisterLocation(kMethodRegisterArgument);
1154}
1155
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001156Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001157 switch (type) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 case Primitive::kPrimChar:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimNot: {
1164 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001165 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001166 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001169 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001170 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001171 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174 uint32_t index = gp_index_;
1175 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1179 calling_convention.GetRegisterPairAt(index));
1180 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001182 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1183 }
1184 }
1185
1186 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001187 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001188 stack_index_++;
1189 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1190 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1191 } else {
1192 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1193 }
1194 }
1195
1196 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001197 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001198 stack_index_ += 2;
1199 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1200 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1201 } else {
1202 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001203 }
1204 }
1205
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001206 case Primitive::kPrimVoid:
1207 LOG(FATAL) << "Unexpected parameter type " << type;
1208 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001209 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001210 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001212
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213void CodeGeneratorX86::Move32(Location destination, Location source) {
1214 if (source.Equals(destination)) {
1215 return;
1216 }
1217 if (destination.IsRegister()) {
1218 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 } else {
1223 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001226 } else if (destination.IsFpuRegister()) {
1227 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 } else {
1232 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001236 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001241 } else if (source.IsConstant()) {
1242 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001243 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001244 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 } else {
1246 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001247 __ pushl(Address(ESP, source.GetStackIndex()));
1248 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 }
1250 }
1251}
1252
1253void CodeGeneratorX86::Move64(Location destination, Location source) {
1254 if (source.Equals(destination)) {
1255 return;
1256 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001257 if (destination.IsRegisterPair()) {
1258 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001259 EmitParallelMoves(
1260 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1261 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001262 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001263 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001264 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1265 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001266 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1268 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1269 __ psrlq(src_reg, Immediate(32));
1270 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001272 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001274 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1275 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001276 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1277 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001278 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001279 if (source.IsFpuRegister()) {
1280 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1281 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001282 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 } else if (source.IsRegisterPair()) {
1284 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1285 // Create stack space for 2 elements.
1286 __ subl(ESP, Immediate(2 * elem_size));
1287 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1288 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1289 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1290 // And remove the temporary stack space we allocated.
1291 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 } else {
1293 LOG(FATAL) << "Unimplemented";
1294 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001296 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001298 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001300 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001302 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001303 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001304 } else if (source.IsConstant()) {
1305 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001306 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1307 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001308 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001309 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1310 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001312 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001313 EmitParallelMoves(
1314 Location::StackSlot(source.GetStackIndex()),
1315 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001316 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001317 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001318 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1319 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001320 }
1321 }
1322}
1323
Calin Juravle175dc732015-08-25 15:42:32 +01001324void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1325 DCHECK(location.IsRegister());
1326 __ movl(location.AsRegister<Register>(), Immediate(value));
1327}
1328
Calin Juravlee460d1d2015-09-29 04:52:17 +01001329void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001330 HParallelMove move(GetGraph()->GetArena());
1331 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1332 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1333 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001335 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001336 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001337 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338}
1339
1340void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1341 if (location.IsRegister()) {
1342 locations->AddTemp(location);
1343 } else if (location.IsRegisterPair()) {
1344 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1345 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1346 } else {
1347 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1348 }
1349}
1350
David Brazdilfc6a86a2015-06-26 10:33:45 +00001351void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001352 DCHECK(!successor->IsExitBlock());
1353
1354 HBasicBlock* block = got->GetBlock();
1355 HInstruction* previous = got->GetPrevious();
1356
1357 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001358 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001359 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1360 return;
1361 }
1362
1363 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1364 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1365 }
1366 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001367 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001368 }
1369}
1370
David Brazdilfc6a86a2015-06-26 10:33:45 +00001371void LocationsBuilderX86::VisitGoto(HGoto* got) {
1372 got->SetLocations(nullptr);
1373}
1374
1375void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1376 HandleGoto(got, got->GetSuccessor());
1377}
1378
1379void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1380 try_boundary->SetLocations(nullptr);
1381}
1382
1383void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1384 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1385 if (!successor->IsExitBlock()) {
1386 HandleGoto(try_boundary, successor);
1387 }
1388}
1389
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001390void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001391 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001392}
1393
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001394void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001395}
1396
Mark Mendell152408f2015-12-31 12:28:50 -05001397template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001398void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001399 LabelType* true_label,
1400 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001401 if (cond->IsFPConditionTrueIfNaN()) {
1402 __ j(kUnordered, true_label);
1403 } else if (cond->IsFPConditionFalseIfNaN()) {
1404 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001405 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001406 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001407}
1408
Mark Mendell152408f2015-12-31 12:28:50 -05001409template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001410void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001411 LabelType* true_label,
1412 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001413 LocationSummary* locations = cond->GetLocations();
1414 Location left = locations->InAt(0);
1415 Location right = locations->InAt(1);
1416 IfCondition if_cond = cond->GetCondition();
1417
Mark Mendellc4701932015-04-10 13:18:51 -04001418 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001419 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001420 IfCondition true_high_cond = if_cond;
1421 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001422 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001423
1424 // Set the conditions for the test, remembering that == needs to be
1425 // decided using the low words.
1426 switch (if_cond) {
1427 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001428 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001429 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001430 break;
1431 case kCondLT:
1432 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001433 break;
1434 case kCondLE:
1435 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001436 break;
1437 case kCondGT:
1438 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001439 break;
1440 case kCondGE:
1441 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001442 break;
Aart Bike9f37602015-10-09 11:15:55 -07001443 case kCondB:
1444 false_high_cond = kCondA;
1445 break;
1446 case kCondBE:
1447 true_high_cond = kCondB;
1448 break;
1449 case kCondA:
1450 false_high_cond = kCondB;
1451 break;
1452 case kCondAE:
1453 true_high_cond = kCondA;
1454 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001455 }
1456
1457 if (right.IsConstant()) {
1458 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001459 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001460 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001461
Aart Bika19616e2016-02-01 18:57:58 -08001462 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001463 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001464 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001465 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001466 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001467 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001468 __ j(X86Condition(true_high_cond), true_label);
1469 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001470 }
1471 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001472 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001473 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001474 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001475 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001476
1477 __ cmpl(left_high, right_high);
1478 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001479 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001480 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001481 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(true_high_cond), true_label);
1484 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001485 }
1486 // Must be equal high, so compare the lows.
1487 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001488 } else {
1489 DCHECK(right.IsDoubleStackSlot());
1490 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1491 if (if_cond == kCondNE) {
1492 __ j(X86Condition(true_high_cond), true_label);
1493 } else if (if_cond == kCondEQ) {
1494 __ j(X86Condition(false_high_cond), false_label);
1495 } else {
1496 __ j(X86Condition(true_high_cond), true_label);
1497 __ j(X86Condition(false_high_cond), false_label);
1498 }
1499 // Must be equal high, so compare the lows.
1500 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001501 }
1502 // The last comparison might be unsigned.
1503 __ j(final_condition, true_label);
1504}
1505
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001506void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1507 Location rhs,
1508 HInstruction* insn,
1509 bool is_double) {
1510 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1511 if (is_double) {
1512 if (rhs.IsFpuRegister()) {
1513 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1514 } else if (const_area != nullptr) {
1515 DCHECK(const_area->IsEmittedAtUseSite());
1516 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1517 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001518 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1519 const_area->GetBaseMethodAddress(),
1520 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001521 } else {
1522 DCHECK(rhs.IsDoubleStackSlot());
1523 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1524 }
1525 } else {
1526 if (rhs.IsFpuRegister()) {
1527 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1528 } else if (const_area != nullptr) {
1529 DCHECK(const_area->IsEmittedAtUseSite());
1530 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1531 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001532 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1533 const_area->GetBaseMethodAddress(),
1534 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001535 } else {
1536 DCHECK(rhs.IsStackSlot());
1537 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1538 }
1539 }
1540}
1541
Mark Mendell152408f2015-12-31 12:28:50 -05001542template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001543void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001544 LabelType* true_target_in,
1545 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001546 // Generated branching requires both targets to be explicit. If either of the
1547 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001548 LabelType fallthrough_target;
1549 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1550 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001551
Mark Mendellc4701932015-04-10 13:18:51 -04001552 LocationSummary* locations = condition->GetLocations();
1553 Location left = locations->InAt(0);
1554 Location right = locations->InAt(1);
1555
Mark Mendellc4701932015-04-10 13:18:51 -04001556 Primitive::Type type = condition->InputAt(0)->GetType();
1557 switch (type) {
1558 case Primitive::kPrimLong:
1559 GenerateLongComparesAndJumps(condition, true_target, false_target);
1560 break;
1561 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001562 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001563 GenerateFPJumps(condition, true_target, false_target);
1564 break;
1565 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001566 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001567 GenerateFPJumps(condition, true_target, false_target);
1568 break;
1569 default:
1570 LOG(FATAL) << "Unexpected compare type " << type;
1571 }
1572
David Brazdil0debae72015-11-12 18:37:00 +00001573 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001574 __ jmp(false_target);
1575 }
David Brazdil0debae72015-11-12 18:37:00 +00001576
1577 if (fallthrough_target.IsLinked()) {
1578 __ Bind(&fallthrough_target);
1579 }
Mark Mendellc4701932015-04-10 13:18:51 -04001580}
1581
David Brazdil0debae72015-11-12 18:37:00 +00001582static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1583 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1584 // are set only strictly before `branch`. We can't use the eflags on long/FP
1585 // conditions if they are materialized due to the complex branching.
1586 return cond->IsCondition() &&
1587 cond->GetNext() == branch &&
1588 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1589 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1590}
1591
Mark Mendell152408f2015-12-31 12:28:50 -05001592template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001593void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001594 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001595 LabelType* true_target,
1596 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001597 HInstruction* cond = instruction->InputAt(condition_input_index);
1598
1599 if (true_target == nullptr && false_target == nullptr) {
1600 // Nothing to do. The code always falls through.
1601 return;
1602 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001603 // Constant condition, statically compared against "true" (integer value 1).
1604 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001605 if (true_target != nullptr) {
1606 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001607 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001608 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001609 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001610 if (false_target != nullptr) {
1611 __ jmp(false_target);
1612 }
1613 }
1614 return;
1615 }
1616
1617 // The following code generates these patterns:
1618 // (1) true_target == nullptr && false_target != nullptr
1619 // - opposite condition true => branch to false_target
1620 // (2) true_target != nullptr && false_target == nullptr
1621 // - condition true => branch to true_target
1622 // (3) true_target != nullptr && false_target != nullptr
1623 // - condition true => branch to true_target
1624 // - branch to false_target
1625 if (IsBooleanValueOrMaterializedCondition(cond)) {
1626 if (AreEflagsSetFrom(cond, instruction)) {
1627 if (true_target == nullptr) {
1628 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1629 } else {
1630 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1631 }
1632 } else {
1633 // Materialized condition, compare against 0.
1634 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1635 if (lhs.IsRegister()) {
1636 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1637 } else {
1638 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1639 }
1640 if (true_target == nullptr) {
1641 __ j(kEqual, false_target);
1642 } else {
1643 __ j(kNotEqual, true_target);
1644 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001645 }
1646 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001647 // Condition has not been materialized, use its inputs as the comparison and
1648 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001649 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001650
1651 // If this is a long or FP comparison that has been folded into
1652 // the HCondition, generate the comparison directly.
1653 Primitive::Type type = condition->InputAt(0)->GetType();
1654 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1655 GenerateCompareTestAndBranch(condition, true_target, false_target);
1656 return;
1657 }
1658
1659 Location lhs = condition->GetLocations()->InAt(0);
1660 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001661 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001662 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001663 if (true_target == nullptr) {
1664 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1665 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001666 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001667 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001668 }
David Brazdil0debae72015-11-12 18:37:00 +00001669
1670 // If neither branch falls through (case 3), the conditional branch to `true_target`
1671 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1672 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001673 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001674 }
1675}
1676
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001677void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1679 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001680 locations->SetInAt(0, Location::Any());
1681 }
1682}
1683
1684void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001685 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1686 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1687 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1688 nullptr : codegen_->GetLabelOf(true_successor);
1689 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1690 nullptr : codegen_->GetLabelOf(false_successor);
1691 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001692}
1693
1694void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1695 LocationSummary* locations = new (GetGraph()->GetArena())
1696 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001697 InvokeRuntimeCallingConvention calling_convention;
1698 RegisterSet caller_saves = RegisterSet::Empty();
1699 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1700 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001701 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001702 locations->SetInAt(0, Location::Any());
1703 }
1704}
1705
1706void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001707 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001708 GenerateTestAndBranch<Label>(deoptimize,
1709 /* condition_input_index */ 0,
1710 slow_path->GetEntryLabel(),
1711 /* false_target */ nullptr);
1712}
1713
Mingyao Yang063fc772016-08-02 11:02:54 -07001714void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1715 LocationSummary* locations = new (GetGraph()->GetArena())
1716 LocationSummary(flag, LocationSummary::kNoCall);
1717 locations->SetOut(Location::RequiresRegister());
1718}
1719
1720void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1721 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1722 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1723}
1724
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001725static bool SelectCanUseCMOV(HSelect* select) {
1726 // There are no conditional move instructions for XMMs.
1727 if (Primitive::IsFloatingPointType(select->GetType())) {
1728 return false;
1729 }
1730
1731 // A FP condition doesn't generate the single CC that we need.
1732 // In 32 bit mode, a long condition doesn't generate a single CC either.
1733 HInstruction* condition = select->GetCondition();
1734 if (condition->IsCondition()) {
1735 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1736 if (compare_type == Primitive::kPrimLong ||
1737 Primitive::IsFloatingPointType(compare_type)) {
1738 return false;
1739 }
1740 }
1741
1742 // We can generate a CMOV for this Select.
1743 return true;
1744}
1745
David Brazdil74eb1b22015-12-14 11:44:01 +00001746void LocationsBuilderX86::VisitSelect(HSelect* select) {
1747 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001748 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001749 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001750 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001751 } else {
1752 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001753 if (SelectCanUseCMOV(select)) {
1754 if (select->InputAt(1)->IsConstant()) {
1755 // Cmov can't handle a constant value.
1756 locations->SetInAt(1, Location::RequiresRegister());
1757 } else {
1758 locations->SetInAt(1, Location::Any());
1759 }
1760 } else {
1761 locations->SetInAt(1, Location::Any());
1762 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001763 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001764 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1765 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001766 }
1767 locations->SetOut(Location::SameAsFirstInput());
1768}
1769
1770void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1771 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772 DCHECK(locations->InAt(0).Equals(locations->Out()));
1773 if (SelectCanUseCMOV(select)) {
1774 // If both the condition and the source types are integer, we can generate
1775 // a CMOV to implement Select.
1776
1777 HInstruction* select_condition = select->GetCondition();
1778 Condition cond = kNotEqual;
1779
1780 // Figure out how to test the 'condition'.
1781 if (select_condition->IsCondition()) {
1782 HCondition* condition = select_condition->AsCondition();
1783 if (!condition->IsEmittedAtUseSite()) {
1784 // This was a previously materialized condition.
1785 // Can we use the existing condition code?
1786 if (AreEflagsSetFrom(condition, select)) {
1787 // Materialization was the previous instruction. Condition codes are right.
1788 cond = X86Condition(condition->GetCondition());
1789 } else {
1790 // No, we have to recreate the condition code.
1791 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1792 __ testl(cond_reg, cond_reg);
1793 }
1794 } else {
1795 // We can't handle FP or long here.
1796 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1797 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1798 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001799 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001800 cond = X86Condition(condition->GetCondition());
1801 }
1802 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001803 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001804 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1805 __ testl(cond_reg, cond_reg);
1806 }
1807
1808 // If the condition is true, overwrite the output, which already contains false.
1809 Location false_loc = locations->InAt(0);
1810 Location true_loc = locations->InAt(1);
1811 if (select->GetType() == Primitive::kPrimLong) {
1812 // 64 bit conditional move.
1813 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1814 Register false_low = false_loc.AsRegisterPairLow<Register>();
1815 if (true_loc.IsRegisterPair()) {
1816 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1817 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1818 } else {
1819 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1820 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1821 }
1822 } else {
1823 // 32 bit conditional move.
1824 Register false_reg = false_loc.AsRegister<Register>();
1825 if (true_loc.IsRegister()) {
1826 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1827 } else {
1828 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1829 }
1830 }
1831 } else {
1832 NearLabel false_target;
1833 GenerateTestAndBranch<NearLabel>(
1834 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1835 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1836 __ Bind(&false_target);
1837 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001838}
1839
David Srbecky0cf44932015-12-09 14:09:59 +00001840void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1841 new (GetGraph()->GetArena()) LocationSummary(info);
1842}
1843
David Srbeckyd28f4a02016-03-14 17:14:24 +00001844void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1845 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001846}
1847
1848void CodeGeneratorX86::GenerateNop() {
1849 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001850}
1851
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001853 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001854 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001855 // Handle the long/FP comparisons made in instruction simplification.
1856 switch (cond->InputAt(0)->GetType()) {
1857 case Primitive::kPrimLong: {
1858 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001859 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001860 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001861 locations->SetOut(Location::RequiresRegister());
1862 }
1863 break;
1864 }
1865 case Primitive::kPrimFloat:
1866 case Primitive::kPrimDouble: {
1867 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001868 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1869 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1870 } else if (cond->InputAt(1)->IsConstant()) {
1871 locations->SetInAt(1, Location::RequiresFpuRegister());
1872 } else {
1873 locations->SetInAt(1, Location::Any());
1874 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001875 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001876 locations->SetOut(Location::RequiresRegister());
1877 }
1878 break;
1879 }
1880 default:
1881 locations->SetInAt(0, Location::RequiresRegister());
1882 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 // We need a byte register.
1885 locations->SetOut(Location::RegisterLocation(ECX));
1886 }
1887 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001888 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001889}
1890
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001891void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001892 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001893 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001894 }
Mark Mendellc4701932015-04-10 13:18:51 -04001895
1896 LocationSummary* locations = cond->GetLocations();
1897 Location lhs = locations->InAt(0);
1898 Location rhs = locations->InAt(1);
1899 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001900 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001901
1902 switch (cond->InputAt(0)->GetType()) {
1903 default: {
1904 // Integer case.
1905
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001906 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001907 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001908 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001909 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001910 return;
1911 }
1912 case Primitive::kPrimLong:
1913 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1914 break;
1915 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001916 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001917 GenerateFPJumps(cond, &true_label, &false_label);
1918 break;
1919 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001920 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001921 GenerateFPJumps(cond, &true_label, &false_label);
1922 break;
1923 }
1924
1925 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001926 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001927
Roland Levillain4fa13f62015-07-06 18:11:54 +01001928 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001929 __ Bind(&false_label);
1930 __ xorl(reg, reg);
1931 __ jmp(&done_label);
1932
Roland Levillain4fa13f62015-07-06 18:11:54 +01001933 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001934 __ Bind(&true_label);
1935 __ movl(reg, Immediate(1));
1936 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001937}
1938
1939void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001940 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001941}
1942
1943void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001945}
1946
1947void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001985}
1986
Aart Bike9f37602015-10-09 11:15:55 -07001987void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001989}
1990
1991void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001993}
1994
1995void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001997}
1998
1999void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002001}
2002
2003void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002019void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002022 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023}
2024
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002025void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002026 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027}
2028
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002029void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2032 locations->SetOut(Location::ConstantLocation(constant));
2033}
2034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002035void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002036 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002037}
2038
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002039void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002042 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043}
2044
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002045void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046 // Will be generated at use site.
2047}
2048
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002049void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2050 LocationSummary* locations =
2051 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2052 locations->SetOut(Location::ConstantLocation(constant));
2053}
2054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002055void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056 // Will be generated at use site.
2057}
2058
2059void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2060 LocationSummary* locations =
2061 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2062 locations->SetOut(Location::ConstantLocation(constant));
2063}
2064
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002065void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002066 // Will be generated at use site.
2067}
2068
Igor Murashkind01745e2017-04-05 16:40:31 -07002069void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2070 constructor_fence->SetLocations(nullptr);
2071}
2072
2073void InstructionCodeGeneratorX86::VisitConstructorFence(
2074 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2075 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2076}
2077
Calin Juravle27df7582015-04-17 19:12:31 +01002078void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2079 memory_barrier->SetLocations(nullptr);
2080}
2081
2082void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002083 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002084}
2085
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002086void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002087 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002088}
2089
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002090void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002091 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002092}
2093
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002095 LocationSummary* locations =
2096 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002097 switch (ret->InputAt(0)->GetType()) {
2098 case Primitive::kPrimBoolean:
2099 case Primitive::kPrimByte:
2100 case Primitive::kPrimChar:
2101 case Primitive::kPrimShort:
2102 case Primitive::kPrimInt:
2103 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002105 break;
2106
2107 case Primitive::kPrimLong:
2108 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002109 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 break;
2111
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002112 case Primitive::kPrimFloat:
2113 case Primitive::kPrimDouble:
2114 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002116 break;
2117
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002118 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121}
2122
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002123void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002124 if (kIsDebugBuild) {
2125 switch (ret->InputAt(0)->GetType()) {
2126 case Primitive::kPrimBoolean:
2127 case Primitive::kPrimByte:
2128 case Primitive::kPrimChar:
2129 case Primitive::kPrimShort:
2130 case Primitive::kPrimInt:
2131 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133 break;
2134
2135 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002136 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2137 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 break;
2139
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 case Primitive::kPrimFloat:
2141 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 break;
2144
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002145 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002146 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 }
2148 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002149 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002150}
2151
Calin Juravle175dc732015-08-25 15:42:32 +01002152void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2153 // The trampoline uses the same calling convention as dex calling conventions,
2154 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2155 // the method_idx.
2156 HandleInvoke(invoke);
2157}
2158
2159void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2160 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2161}
2162
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002163void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002164 // Explicit clinit checks triggered by static invokes must have been pruned by
2165 // art::PrepareForRegisterAllocation.
2166 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002167
Mark Mendellfb8d2792015-03-31 22:16:59 -04002168 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002169 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002170 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002171 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002172 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002173 return;
2174 }
2175
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002176 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002177
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002178 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2179 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002180 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002181 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002182}
2183
Mark Mendell09ed1a32015-03-25 08:30:06 -04002184static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2185 if (invoke->GetLocations()->Intrinsified()) {
2186 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2187 intrinsic.Dispatch(invoke);
2188 return true;
2189 }
2190 return false;
2191}
2192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002193void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002194 // Explicit clinit checks triggered by static invokes must have been pruned by
2195 // art::PrepareForRegisterAllocation.
2196 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002197
Mark Mendell09ed1a32015-03-25 08:30:06 -04002198 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2199 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002200 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002201
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002202 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002203 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002204 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002205 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002206}
2207
2208void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002209 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2210 if (intrinsic.TryDispatch(invoke)) {
2211 return;
2212 }
2213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002214 HandleInvoke(invoke);
2215}
2216
2217void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002218 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002219 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002220}
2221
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002222void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002223 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2224 return;
2225 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002226
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002227 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002228 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002229 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002230}
2231
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002232void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002233 // This call to HandleInvoke allocates a temporary (core) register
2234 // which is also used to transfer the hidden argument from FP to
2235 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 HandleInvoke(invoke);
2237 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002238 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239}
2240
2241void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2242 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002243 LocationSummary* locations = invoke->GetLocations();
2244 Register temp = locations->GetTemp(0).AsRegister<Register>();
2245 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 Location receiver = locations->InAt(0);
2247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2248
Roland Levillain0d5a2812015-11-13 10:07:31 +00002249 // Set the hidden argument. This is safe to do this here, as XMM7
2250 // won't be modified thereafter, before the `call` instruction.
2251 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002253 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255 if (receiver.IsStackSlot()) {
2256 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 __ movl(temp, Address(temp, class_offset));
2259 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002260 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 }
Roland Levillain4d027112015-07-01 15:41:14 +01002263 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002264 // Instead of simply (possibly) unpoisoning `temp` here, we should
2265 // emit a read barrier for the previous class reference load.
2266 // However this is not required in practice, as this is an
2267 // intermediate/temporary reference and because the current
2268 // concurrent copying collector keeps the from-space memory
2269 // intact/accessible until the end of the marking phase (the
2270 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002271 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002272 // temp = temp->GetAddressOfIMT()
2273 __ movl(temp,
2274 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002276 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002277 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002278 __ movl(temp, Address(temp, method_offset));
2279 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002280 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002281 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002282
2283 DCHECK(!codegen_->IsLeafMethod());
2284 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2285}
2286
Orion Hodsonac141392017-01-13 11:53:47 +00002287void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2288 HandleInvoke(invoke);
2289}
2290
2291void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2292 codegen_->GenerateInvokePolymorphicCall(invoke);
2293}
2294
Roland Levillain88cb1752014-10-20 16:36:47 +01002295void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2296 LocationSummary* locations =
2297 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2298 switch (neg->GetResultType()) {
2299 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002300 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetOut(Location::SameAsFirstInput());
2303 break;
2304
Roland Levillain88cb1752014-10-20 16:36:47 +01002305 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002306 locations->SetInAt(0, Location::RequiresFpuRegister());
2307 locations->SetOut(Location::SameAsFirstInput());
2308 locations->AddTemp(Location::RequiresRegister());
2309 locations->AddTemp(Location::RequiresFpuRegister());
2310 break;
2311
Roland Levillain88cb1752014-10-20 16:36:47 +01002312 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002314 locations->SetOut(Location::SameAsFirstInput());
2315 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002316 break;
2317
2318 default:
2319 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2320 }
2321}
2322
2323void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2324 LocationSummary* locations = neg->GetLocations();
2325 Location out = locations->Out();
2326 Location in = locations->InAt(0);
2327 switch (neg->GetResultType()) {
2328 case Primitive::kPrimInt:
2329 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002330 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002332 break;
2333
2334 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002335 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002336 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002337 __ negl(out.AsRegisterPairLow<Register>());
2338 // Negation is similar to subtraction from zero. The least
2339 // significant byte triggers a borrow when it is different from
2340 // zero; to take it into account, add 1 to the most significant
2341 // byte if the carry flag (CF) is set to 1 after the first NEGL
2342 // operation.
2343 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2344 __ negl(out.AsRegisterPairHigh<Register>());
2345 break;
2346
Roland Levillain5368c212014-11-27 15:03:41 +00002347 case Primitive::kPrimFloat: {
2348 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 Register constant = locations->GetTemp(0).AsRegister<Register>();
2350 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002351 // Implement float negation with an exclusive or with value
2352 // 0x80000000 (mask for bit 31, representing the sign of a
2353 // single-precision floating-point number).
2354 __ movl(constant, Immediate(INT32_C(0x80000000)));
2355 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002357 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002358 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002359
Roland Levillain5368c212014-11-27 15:03:41 +00002360 case Primitive::kPrimDouble: {
2361 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002363 // Implement double negation with an exclusive or with value
2364 // 0x8000000000000000 (mask for bit 63, representing the sign of
2365 // a double-precision floating-point number).
2366 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002368 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002369 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002370
2371 default:
2372 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2373 }
2374}
2375
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002376void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2377 LocationSummary* locations =
2378 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2379 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2380 locations->SetInAt(0, Location::RequiresFpuRegister());
2381 locations->SetInAt(1, Location::RequiresRegister());
2382 locations->SetOut(Location::SameAsFirstInput());
2383 locations->AddTemp(Location::RequiresFpuRegister());
2384}
2385
2386void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2387 LocationSummary* locations = neg->GetLocations();
2388 Location out = locations->Out();
2389 DCHECK(locations->InAt(0).Equals(out));
2390
2391 Register constant_area = locations->InAt(1).AsRegister<Register>();
2392 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2393 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002394 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2395 neg->GetBaseMethodAddress(),
2396 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002397 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2398 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002399 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2400 neg->GetBaseMethodAddress(),
2401 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002402 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2403 }
2404}
2405
Roland Levillaindff1f282014-11-05 14:15:05 +00002406void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002407 Primitive::Type result_type = conversion->GetResultType();
2408 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002409 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002410
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002411 // The float-to-long and double-to-long type conversions rely on a
2412 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002413 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002414 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2415 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002416 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002417 : LocationSummary::kNoCall;
2418 LocationSummary* locations =
2419 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2420
David Brazdilb2bd1c52015-03-25 11:17:37 +00002421 // The Java language does not allow treating boolean as an integral type but
2422 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002423
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002425 case Primitive::kPrimByte:
2426 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002427 case Primitive::kPrimLong: {
2428 // Type conversion from long to byte is a result of code transformations.
2429 HInstruction* input = conversion->InputAt(0);
2430 Location input_location = input->IsConstant()
2431 ? Location::ConstantLocation(input->AsConstant())
2432 : Location::RegisterPairLocation(EAX, EDX);
2433 locations->SetInAt(0, input_location);
2434 // Make the output overlap to please the register allocator. This greatly simplifies
2435 // the validation of the linear scan implementation
2436 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2437 break;
2438 }
David Brazdil46e2a392015-03-16 17:31:52 +00002439 case Primitive::kPrimBoolean:
2440 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002441 case Primitive::kPrimShort:
2442 case Primitive::kPrimInt:
2443 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002444 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002445 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2446 // Make the output overlap to please the register allocator. This greatly simplifies
2447 // the validation of the linear scan implementation
2448 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002449 break;
2450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillain01a8d712014-11-14 16:27:39 +00002457 case Primitive::kPrimShort:
2458 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002459 case Primitive::kPrimLong:
2460 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002461 case Primitive::kPrimBoolean:
2462 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002463 case Primitive::kPrimByte:
2464 case Primitive::kPrimInt:
2465 case Primitive::kPrimChar:
2466 // Processing a Dex `int-to-short' instruction.
2467 locations->SetInAt(0, Location::Any());
2468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Roland Levillain946e1432014-11-11 17:35:19 +00002477 case Primitive::kPrimInt:
2478 switch (input_type) {
2479 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002480 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002481 locations->SetInAt(0, Location::Any());
2482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2483 break;
2484
2485 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002486 // Processing a Dex `float-to-int' instruction.
2487 locations->SetInAt(0, Location::RequiresFpuRegister());
2488 locations->SetOut(Location::RequiresRegister());
2489 locations->AddTemp(Location::RequiresFpuRegister());
2490 break;
2491
Roland Levillain946e1432014-11-11 17:35:19 +00002492 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002493 // Processing a Dex `double-to-int' instruction.
2494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetOut(Location::RequiresRegister());
2496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Roland Levillaindff1f282014-11-05 14:15:05 +00002505 case Primitive::kPrimLong:
2506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002507 case Primitive::kPrimBoolean:
2508 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002509 case Primitive::kPrimByte:
2510 case Primitive::kPrimShort:
2511 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002513 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 locations->SetInAt(0, Location::RegisterLocation(EAX));
2515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2516 break;
2517
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002518 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002519 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002520 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002521 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002522 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2523 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2524
Vladimir Marko949c91f2015-01-27 10:48:44 +00002525 // The runtime helper puts the result in EAX, EDX.
2526 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002527 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002528 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002529
2530 default:
2531 LOG(FATAL) << "Unexpected type conversion from " << input_type
2532 << " to " << result_type;
2533 }
2534 break;
2535
Roland Levillain981e4542014-11-14 11:47:14 +00002536 case Primitive::kPrimChar:
2537 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002538 case Primitive::kPrimLong:
2539 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002545 // Processing a Dex `int-to-char' instruction.
2546 locations->SetInAt(0, Location::Any());
2547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554 break;
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002557 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002558 case Primitive::kPrimBoolean:
2559 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002560 case Primitive::kPrimByte:
2561 case Primitive::kPrimShort:
2562 case Primitive::kPrimInt:
2563 case Primitive::kPrimChar:
2564 // Processing a Dex `int-to-float' instruction.
2565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetOut(Location::RequiresFpuRegister());
2567 break;
2568
2569 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002570 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002571 locations->SetInAt(0, Location::Any());
2572 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002573 break;
2574
Roland Levillaincff13742014-11-17 14:32:17 +00002575 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002576 // Processing a Dex `double-to-float' instruction.
2577 locations->SetInAt(0, Location::RequiresFpuRegister());
2578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected type conversion from " << input_type
2583 << " to " << result_type;
2584 };
2585 break;
2586
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002588 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002589 case Primitive::kPrimBoolean:
2590 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002591 case Primitive::kPrimByte:
2592 case Primitive::kPrimShort:
2593 case Primitive::kPrimInt:
2594 case Primitive::kPrimChar:
2595 // Processing a Dex `int-to-double' instruction.
2596 locations->SetInAt(0, Location::RequiresRegister());
2597 locations->SetOut(Location::RequiresFpuRegister());
2598 break;
2599
2600 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002601 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002602 locations->SetInAt(0, Location::Any());
2603 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002604 break;
2605
Roland Levillaincff13742014-11-17 14:32:17 +00002606 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002607 // Processing a Dex `float-to-double' instruction.
2608 locations->SetInAt(0, Location::RequiresFpuRegister());
2609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002610 break;
2611
2612 default:
2613 LOG(FATAL) << "Unexpected type conversion from " << input_type
2614 << " to " << result_type;
2615 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected type conversion from " << input_type
2620 << " to " << result_type;
2621 }
2622}
2623
2624void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2625 LocationSummary* locations = conversion->GetLocations();
2626 Location out = locations->Out();
2627 Location in = locations->InAt(0);
2628 Primitive::Type result_type = conversion->GetResultType();
2629 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002630 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002632 case Primitive::kPrimByte:
2633 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002634 case Primitive::kPrimLong:
2635 // Type conversion from long to byte is a result of code transformations.
2636 if (in.IsRegisterPair()) {
2637 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2638 } else {
2639 DCHECK(in.GetConstant()->IsLongConstant());
2640 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2641 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2642 }
2643 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002644 case Primitive::kPrimBoolean:
2645 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002646 case Primitive::kPrimShort:
2647 case Primitive::kPrimInt:
2648 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002649 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002650 if (in.IsRegister()) {
2651 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002652 } else {
2653 DCHECK(in.GetConstant()->IsIntConstant());
2654 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2655 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2656 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663 break;
2664
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 case Primitive::kPrimShort:
2666 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002667 case Primitive::kPrimLong:
2668 // Type conversion from long to short is a result of code transformations.
2669 if (in.IsRegisterPair()) {
2670 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2671 } else if (in.IsDoubleStackSlot()) {
2672 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2673 } else {
2674 DCHECK(in.GetConstant()->IsLongConstant());
2675 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2676 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2677 }
2678 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
2684 // Processing a Dex `int-to-short' instruction.
2685 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002687 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002689 } else {
2690 DCHECK(in.GetConstant()->IsIntConstant());
2691 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002693 }
2694 break;
2695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 }
2700 break;
2701
Roland Levillain946e1432014-11-11 17:35:19 +00002702 case Primitive::kPrimInt:
2703 switch (input_type) {
2704 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002705 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002706 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002708 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002710 } else {
2711 DCHECK(in.IsConstant());
2712 DCHECK(in.GetConstant()->IsLongConstant());
2713 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002714 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002715 }
2716 break;
2717
Roland Levillain3f8f9362014-12-02 17:45:01 +00002718 case Primitive::kPrimFloat: {
2719 // Processing a Dex `float-to-int' instruction.
2720 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2721 Register output = out.AsRegister<Register>();
2722 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002723 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002724
2725 __ movl(output, Immediate(kPrimIntMax));
2726 // temp = int-to-float(output)
2727 __ cvtsi2ss(temp, output);
2728 // if input >= temp goto done
2729 __ comiss(input, temp);
2730 __ j(kAboveEqual, &done);
2731 // if input == NaN goto nan
2732 __ j(kUnordered, &nan);
2733 // output = float-to-int-truncate(input)
2734 __ cvttss2si(output, input);
2735 __ jmp(&done);
2736 __ Bind(&nan);
2737 // output = 0
2738 __ xorl(output, output);
2739 __ Bind(&done);
2740 break;
2741 }
2742
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002743 case Primitive::kPrimDouble: {
2744 // Processing a Dex `double-to-int' instruction.
2745 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2746 Register output = out.AsRegister<Register>();
2747 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002748 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002749
2750 __ movl(output, Immediate(kPrimIntMax));
2751 // temp = int-to-double(output)
2752 __ cvtsi2sd(temp, output);
2753 // if input >= temp goto done
2754 __ comisd(input, temp);
2755 __ j(kAboveEqual, &done);
2756 // if input == NaN goto nan
2757 __ j(kUnordered, &nan);
2758 // output = double-to-int-truncate(input)
2759 __ cvttsd2si(output, input);
2760 __ jmp(&done);
2761 __ Bind(&nan);
2762 // output = 0
2763 __ xorl(output, output);
2764 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002765 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002766 }
Roland Levillain946e1432014-11-11 17:35:19 +00002767
2768 default:
2769 LOG(FATAL) << "Unexpected type conversion from " << input_type
2770 << " to " << result_type;
2771 }
2772 break;
2773
Roland Levillaindff1f282014-11-05 14:15:05 +00002774 case Primitive::kPrimLong:
2775 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002776 case Primitive::kPrimBoolean:
2777 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002778 case Primitive::kPrimByte:
2779 case Primitive::kPrimShort:
2780 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002781 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002782 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002783 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2784 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002786 __ cdq();
2787 break;
2788
2789 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002790 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002791 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002792 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002793 break;
2794
Roland Levillaindff1f282014-11-05 14:15:05 +00002795 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002796 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002797 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002798 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002799 break;
2800
2801 default:
2802 LOG(FATAL) << "Unexpected type conversion from " << input_type
2803 << " to " << result_type;
2804 }
2805 break;
2806
Roland Levillain981e4542014-11-14 11:47:14 +00002807 case Primitive::kPrimChar:
2808 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002809 case Primitive::kPrimLong:
2810 // Type conversion from long to short is a result of code transformations.
2811 if (in.IsRegisterPair()) {
2812 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2813 } else if (in.IsDoubleStackSlot()) {
2814 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2815 } else {
2816 DCHECK(in.GetConstant()->IsLongConstant());
2817 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2818 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2819 }
2820 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002821 case Primitive::kPrimBoolean:
2822 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002823 case Primitive::kPrimByte:
2824 case Primitive::kPrimShort:
2825 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002826 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2827 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002829 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002831 } else {
2832 DCHECK(in.GetConstant()->IsIntConstant());
2833 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002835 }
2836 break;
2837
2838 default:
2839 LOG(FATAL) << "Unexpected type conversion from " << input_type
2840 << " to " << result_type;
2841 }
2842 break;
2843
Roland Levillaindff1f282014-11-05 14:15:05 +00002844 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002845 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002846 case Primitive::kPrimBoolean:
2847 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002848 case Primitive::kPrimByte:
2849 case Primitive::kPrimShort:
2850 case Primitive::kPrimInt:
2851 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002852 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002854 break;
2855
Roland Levillain6d0e4832014-11-27 18:31:21 +00002856 case Primitive::kPrimLong: {
2857 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002858 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002859
Roland Levillain232ade02015-04-20 15:14:36 +01002860 // Create stack space for the call to
2861 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2862 // TODO: enhance register allocator to ask for stack temporaries.
2863 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2864 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2865 __ subl(ESP, Immediate(adjustment));
2866 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002867
Roland Levillain232ade02015-04-20 15:14:36 +01002868 // Load the value to the FP stack, using temporaries if needed.
2869 PushOntoFPStack(in, 0, adjustment, false, true);
2870
2871 if (out.IsStackSlot()) {
2872 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2873 } else {
2874 __ fstps(Address(ESP, 0));
2875 Location stack_temp = Location::StackSlot(0);
2876 codegen_->Move32(out, stack_temp);
2877 }
2878
2879 // Remove the temporary stack space we allocated.
2880 if (adjustment != 0) {
2881 __ addl(ESP, Immediate(adjustment));
2882 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002883 break;
2884 }
2885
Roland Levillaincff13742014-11-17 14:32:17 +00002886 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002887 // Processing a Dex `double-to-float' instruction.
2888 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002889 break;
2890
2891 default:
2892 LOG(FATAL) << "Unexpected type conversion from " << input_type
2893 << " to " << result_type;
2894 };
2895 break;
2896
Roland Levillaindff1f282014-11-05 14:15:05 +00002897 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002898 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002899 case Primitive::kPrimBoolean:
2900 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002901 case Primitive::kPrimByte:
2902 case Primitive::kPrimShort:
2903 case Primitive::kPrimInt:
2904 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002905 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002906 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002907 break;
2908
Roland Levillain647b9ed2014-11-27 12:06:00 +00002909 case Primitive::kPrimLong: {
2910 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002911 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002912
Roland Levillain232ade02015-04-20 15:14:36 +01002913 // Create stack space for the call to
2914 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2915 // TODO: enhance register allocator to ask for stack temporaries.
2916 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2917 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2918 __ subl(ESP, Immediate(adjustment));
2919 }
2920
2921 // Load the value to the FP stack, using temporaries if needed.
2922 PushOntoFPStack(in, 0, adjustment, false, true);
2923
2924 if (out.IsDoubleStackSlot()) {
2925 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2926 } else {
2927 __ fstpl(Address(ESP, 0));
2928 Location stack_temp = Location::DoubleStackSlot(0);
2929 codegen_->Move64(out, stack_temp);
2930 }
2931
2932 // Remove the temporary stack space we allocated.
2933 if (adjustment != 0) {
2934 __ addl(ESP, Immediate(adjustment));
2935 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002936 break;
2937 }
2938
Roland Levillaincff13742014-11-17 14:32:17 +00002939 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002940 // Processing a Dex `float-to-double' instruction.
2941 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002942 break;
2943
2944 default:
2945 LOG(FATAL) << "Unexpected type conversion from " << input_type
2946 << " to " << result_type;
2947 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002948 break;
2949
2950 default:
2951 LOG(FATAL) << "Unexpected type conversion from " << input_type
2952 << " to " << result_type;
2953 }
2954}
2955
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 LocationSummary* locations =
2958 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002960 case Primitive::kPrimInt: {
2961 locations->SetInAt(0, Location::RequiresRegister());
2962 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2964 break;
2965 }
2966
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002967 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002968 locations->SetInAt(0, Location::RequiresRegister());
2969 locations->SetInAt(1, Location::Any());
2970 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002971 break;
2972 }
2973
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 case Primitive::kPrimFloat:
2975 case Primitive::kPrimDouble: {
2976 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002977 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2978 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002979 } else if (add->InputAt(1)->IsConstant()) {
2980 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002981 } else {
2982 locations->SetInAt(1, Location::Any());
2983 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002984 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002985 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002988 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2990 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002991 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992}
2993
2994void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2995 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002996 Location first = locations->InAt(0);
2997 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002998 Location out = locations->Out();
2999
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003000 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003001 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003002 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003003 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3004 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003005 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003007 } else {
3008 __ leal(out.AsRegister<Register>(), Address(
3009 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3010 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003011 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003012 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3013 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3014 __ addl(out.AsRegister<Register>(), Immediate(value));
3015 } else {
3016 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3017 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003018 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003019 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003021 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003022 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 }
3024
3025 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003026 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003027 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3028 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003029 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003030 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3031 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003032 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003033 } else {
3034 DCHECK(second.IsConstant()) << second;
3035 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3036 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3037 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003038 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039 break;
3040 }
3041
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003042 case Primitive::kPrimFloat: {
3043 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3046 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003047 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003048 __ addss(first.AsFpuRegister<XmmRegister>(),
3049 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003050 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3051 const_area->GetBaseMethodAddress(),
3052 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 } else {
3054 DCHECK(second.IsStackSlot());
3055 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003056 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003057 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 }
3059
3060 case Primitive::kPrimDouble: {
3061 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003063 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3064 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003065 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003066 __ addsd(first.AsFpuRegister<XmmRegister>(),
3067 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003068 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3069 const_area->GetBaseMethodAddress(),
3070 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003071 } else {
3072 DCHECK(second.IsDoubleStackSlot());
3073 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003074 }
3075 break;
3076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003077
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003079 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003080 }
3081}
3082
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003083void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003084 LocationSummary* locations =
3085 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003086 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003087 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003088 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003089 locations->SetInAt(0, Location::RequiresRegister());
3090 locations->SetInAt(1, Location::Any());
3091 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003092 break;
3093 }
Calin Juravle11351682014-10-23 15:38:15 +01003094 case Primitive::kPrimFloat:
3095 case Primitive::kPrimDouble: {
3096 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003097 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3098 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003099 } else if (sub->InputAt(1)->IsConstant()) {
3100 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003101 } else {
3102 locations->SetInAt(1, Location::Any());
3103 }
Calin Juravle11351682014-10-23 15:38:15 +01003104 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003105 break;
Calin Juravle11351682014-10-23 15:38:15 +01003106 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003107
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003108 default:
Calin Juravle11351682014-10-23 15:38:15 +01003109 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111}
3112
3113void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3114 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003115 Location first = locations->InAt(0);
3116 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003117 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003118 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003120 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003122 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003123 __ subl(first.AsRegister<Register>(),
3124 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003126 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003127 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003128 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003129 }
3130
3131 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003132 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003133 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3134 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003135 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003136 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003137 __ sbbl(first.AsRegisterPairHigh<Register>(),
3138 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003139 } else {
3140 DCHECK(second.IsConstant()) << second;
3141 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3142 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3143 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003144 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003145 break;
3146 }
3147
Calin Juravle11351682014-10-23 15:38:15 +01003148 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003149 if (second.IsFpuRegister()) {
3150 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3151 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3152 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003153 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003154 __ subss(first.AsFpuRegister<XmmRegister>(),
3155 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003156 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3157 const_area->GetBaseMethodAddress(),
3158 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003159 } else {
3160 DCHECK(second.IsStackSlot());
3161 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3162 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003163 break;
Calin Juravle11351682014-10-23 15:38:15 +01003164 }
3165
3166 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003167 if (second.IsFpuRegister()) {
3168 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3169 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3170 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003171 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003172 __ subsd(first.AsFpuRegister<XmmRegister>(),
3173 codegen_->LiteralDoubleAddress(
3174 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003175 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003176 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3177 } else {
3178 DCHECK(second.IsDoubleStackSlot());
3179 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3180 }
Calin Juravle11351682014-10-23 15:38:15 +01003181 break;
3182 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003184 default:
Calin Juravle11351682014-10-23 15:38:15 +01003185 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003186 }
3187}
3188
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189void LocationsBuilderX86::VisitMul(HMul* mul) {
3190 LocationSummary* locations =
3191 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3192 switch (mul->GetResultType()) {
3193 case Primitive::kPrimInt:
3194 locations->SetInAt(0, Location::RequiresRegister());
3195 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003196 if (mul->InputAt(1)->IsIntConstant()) {
3197 // Can use 3 operand multiply.
3198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3199 } else {
3200 locations->SetOut(Location::SameAsFirstInput());
3201 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 break;
3203 case Primitive::kPrimLong: {
3204 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003205 locations->SetInAt(1, Location::Any());
3206 locations->SetOut(Location::SameAsFirstInput());
3207 // Needed for imul on 32bits with 64bits output.
3208 locations->AddTemp(Location::RegisterLocation(EAX));
3209 locations->AddTemp(Location::RegisterLocation(EDX));
3210 break;
3211 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003212 case Primitive::kPrimFloat:
3213 case Primitive::kPrimDouble: {
3214 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003215 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3216 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003217 } else if (mul->InputAt(1)->IsConstant()) {
3218 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003219 } else {
3220 locations->SetInAt(1, Location::Any());
3221 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003222 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003224 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225
3226 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003227 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003228 }
3229}
3230
3231void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3232 LocationSummary* locations = mul->GetLocations();
3233 Location first = locations->InAt(0);
3234 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003235 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236
3237 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003238 case Primitive::kPrimInt:
3239 // The constant may have ended up in a register, so test explicitly to avoid
3240 // problems where the output may not be the same as the first operand.
3241 if (mul->InputAt(1)->IsIntConstant()) {
3242 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3243 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3244 } else if (second.IsRegister()) {
3245 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003246 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003247 } else {
3248 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003249 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251 }
3252 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253
3254 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003255 Register in1_hi = first.AsRegisterPairHigh<Register>();
3256 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003257 Register eax = locations->GetTemp(0).AsRegister<Register>();
3258 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003259
3260 DCHECK_EQ(EAX, eax);
3261 DCHECK_EQ(EDX, edx);
3262
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003263 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003264 // output: in1
3265 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3266 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3267 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003268 if (second.IsConstant()) {
3269 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003270
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003271 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3272 int32_t low_value = Low32Bits(value);
3273 int32_t high_value = High32Bits(value);
3274 Immediate low(low_value);
3275 Immediate high(high_value);
3276
3277 __ movl(eax, high);
3278 // eax <- in1.lo * in2.hi
3279 __ imull(eax, in1_lo);
3280 // in1.hi <- in1.hi * in2.lo
3281 __ imull(in1_hi, low);
3282 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3283 __ addl(in1_hi, eax);
3284 // move in2_lo to eax to prepare for double precision
3285 __ movl(eax, low);
3286 // edx:eax <- in1.lo * in2.lo
3287 __ mull(in1_lo);
3288 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3289 __ addl(in1_hi, edx);
3290 // in1.lo <- (in1.lo * in2.lo)[31:0];
3291 __ movl(in1_lo, eax);
3292 } else if (second.IsRegisterPair()) {
3293 Register in2_hi = second.AsRegisterPairHigh<Register>();
3294 Register in2_lo = second.AsRegisterPairLow<Register>();
3295
3296 __ movl(eax, in2_hi);
3297 // eax <- in1.lo * in2.hi
3298 __ imull(eax, in1_lo);
3299 // in1.hi <- in1.hi * in2.lo
3300 __ imull(in1_hi, in2_lo);
3301 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3302 __ addl(in1_hi, eax);
3303 // move in1_lo to eax to prepare for double precision
3304 __ movl(eax, in1_lo);
3305 // edx:eax <- in1.lo * in2.lo
3306 __ mull(in2_lo);
3307 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3308 __ addl(in1_hi, edx);
3309 // in1.lo <- (in1.lo * in2.lo)[31:0];
3310 __ movl(in1_lo, eax);
3311 } else {
3312 DCHECK(second.IsDoubleStackSlot()) << second;
3313 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3314 Address in2_lo(ESP, second.GetStackIndex());
3315
3316 __ movl(eax, in2_hi);
3317 // eax <- in1.lo * in2.hi
3318 __ imull(eax, in1_lo);
3319 // in1.hi <- in1.hi * in2.lo
3320 __ imull(in1_hi, in2_lo);
3321 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3322 __ addl(in1_hi, eax);
3323 // move in1_lo to eax to prepare for double precision
3324 __ movl(eax, in1_lo);
3325 // edx:eax <- in1.lo * in2.lo
3326 __ mull(in2_lo);
3327 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3328 __ addl(in1_hi, edx);
3329 // in1.lo <- (in1.lo * in2.lo)[31:0];
3330 __ movl(in1_lo, eax);
3331 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003332
3333 break;
3334 }
3335
Calin Juravleb5bfa962014-10-21 18:02:24 +01003336 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003337 DCHECK(first.Equals(locations->Out()));
3338 if (second.IsFpuRegister()) {
3339 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3340 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3341 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003342 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003343 __ mulss(first.AsFpuRegister<XmmRegister>(),
3344 codegen_->LiteralFloatAddress(
3345 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003346 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003347 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3348 } else {
3349 DCHECK(second.IsStackSlot());
3350 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3351 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003353 }
3354
3355 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003356 DCHECK(first.Equals(locations->Out()));
3357 if (second.IsFpuRegister()) {
3358 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3359 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3360 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003361 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003362 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3363 codegen_->LiteralDoubleAddress(
3364 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003365 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003366 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3367 } else {
3368 DCHECK(second.IsDoubleStackSlot());
3369 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3370 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003371 break;
3372 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003373
3374 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003375 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003376 }
3377}
3378
Roland Levillain232ade02015-04-20 15:14:36 +01003379void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3380 uint32_t temp_offset,
3381 uint32_t stack_adjustment,
3382 bool is_fp,
3383 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003385 DCHECK(!is_wide);
3386 if (is_fp) {
3387 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3388 } else {
3389 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003392 DCHECK(is_wide);
3393 if (is_fp) {
3394 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3395 } else {
3396 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3397 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003398 } else {
3399 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003400 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003401 Location stack_temp = Location::StackSlot(temp_offset);
3402 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003403 if (is_fp) {
3404 __ flds(Address(ESP, temp_offset));
3405 } else {
3406 __ filds(Address(ESP, temp_offset));
3407 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003408 } else {
3409 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3410 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003411 if (is_fp) {
3412 __ fldl(Address(ESP, temp_offset));
3413 } else {
3414 __ fildl(Address(ESP, temp_offset));
3415 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003416 }
3417 }
3418}
3419
3420void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3421 Primitive::Type type = rem->GetResultType();
3422 bool is_float = type == Primitive::kPrimFloat;
3423 size_t elem_size = Primitive::ComponentSize(type);
3424 LocationSummary* locations = rem->GetLocations();
3425 Location first = locations->InAt(0);
3426 Location second = locations->InAt(1);
3427 Location out = locations->Out();
3428
3429 // Create stack space for 2 elements.
3430 // TODO: enhance register allocator to ask for stack temporaries.
3431 __ subl(ESP, Immediate(2 * elem_size));
3432
3433 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003434 const bool is_wide = !is_float;
3435 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3436 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003437
3438 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003439 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003440 __ Bind(&retry);
3441 __ fprem();
3442
3443 // Move FP status to AX.
3444 __ fstsw();
3445
3446 // And see if the argument reduction is complete. This is signaled by the
3447 // C2 FPU flag bit set to 0.
3448 __ andl(EAX, Immediate(kC2ConditionMask));
3449 __ j(kNotEqual, &retry);
3450
3451 // We have settled on the final value. Retrieve it into an XMM register.
3452 // Store FP top of stack to real stack.
3453 if (is_float) {
3454 __ fsts(Address(ESP, 0));
3455 } else {
3456 __ fstl(Address(ESP, 0));
3457 }
3458
3459 // Pop the 2 items from the FP stack.
3460 __ fucompp();
3461
3462 // Load the value from the stack into an XMM register.
3463 DCHECK(out.IsFpuRegister()) << out;
3464 if (is_float) {
3465 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3466 } else {
3467 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3468 }
3469
3470 // And remove the temporary stack space we allocated.
3471 __ addl(ESP, Immediate(2 * elem_size));
3472}
3473
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3476 DCHECK(instruction->IsDiv() || instruction->IsRem());
3477
3478 LocationSummary* locations = instruction->GetLocations();
3479 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003480 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481
3482 Register out_register = locations->Out().AsRegister<Register>();
3483 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003484 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003485
3486 DCHECK(imm == 1 || imm == -1);
3487
3488 if (instruction->IsRem()) {
3489 __ xorl(out_register, out_register);
3490 } else {
3491 __ movl(out_register, input_register);
3492 if (imm == -1) {
3493 __ negl(out_register);
3494 }
3495 }
3496}
3497
3498
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003499void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 LocationSummary* locations = instruction->GetLocations();
3501
3502 Register out_register = locations->Out().AsRegister<Register>();
3503 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003504 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003505 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3506 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 Register num = locations->GetTemp(0).AsRegister<Register>();
3509
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003510 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511 __ testl(input_register, input_register);
3512 __ cmovl(kGreaterEqual, num, input_register);
3513 int shift = CTZ(imm);
3514 __ sarl(num, Immediate(shift));
3515
3516 if (imm < 0) {
3517 __ negl(num);
3518 }
3519
3520 __ movl(out_register, num);
3521}
3522
3523void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3524 DCHECK(instruction->IsDiv() || instruction->IsRem());
3525
3526 LocationSummary* locations = instruction->GetLocations();
3527 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3528
3529 Register eax = locations->InAt(0).AsRegister<Register>();
3530 Register out = locations->Out().AsRegister<Register>();
3531 Register num;
3532 Register edx;
3533
3534 if (instruction->IsDiv()) {
3535 edx = locations->GetTemp(0).AsRegister<Register>();
3536 num = locations->GetTemp(1).AsRegister<Register>();
3537 } else {
3538 edx = locations->Out().AsRegister<Register>();
3539 num = locations->GetTemp(0).AsRegister<Register>();
3540 }
3541
3542 DCHECK_EQ(EAX, eax);
3543 DCHECK_EQ(EDX, edx);
3544 if (instruction->IsDiv()) {
3545 DCHECK_EQ(EAX, out);
3546 } else {
3547 DCHECK_EQ(EDX, out);
3548 }
3549
3550 int64_t magic;
3551 int shift;
3552 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3553
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 // Save the numerator.
3555 __ movl(num, eax);
3556
3557 // EAX = magic
3558 __ movl(eax, Immediate(magic));
3559
3560 // EDX:EAX = magic * numerator
3561 __ imull(num);
3562
3563 if (imm > 0 && magic < 0) {
3564 // EDX += num
3565 __ addl(edx, num);
3566 } else if (imm < 0 && magic > 0) {
3567 __ subl(edx, num);
3568 }
3569
3570 // Shift if needed.
3571 if (shift != 0) {
3572 __ sarl(edx, Immediate(shift));
3573 }
3574
3575 // EDX += 1 if EDX < 0
3576 __ movl(eax, edx);
3577 __ shrl(edx, Immediate(31));
3578 __ addl(edx, eax);
3579
3580 if (instruction->IsRem()) {
3581 __ movl(eax, num);
3582 __ imull(edx, Immediate(imm));
3583 __ subl(eax, edx);
3584 __ movl(edx, eax);
3585 } else {
3586 __ movl(eax, edx);
3587 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588}
3589
Calin Juravlebacfec32014-11-14 15:54:36 +00003590void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3591 DCHECK(instruction->IsDiv() || instruction->IsRem());
3592
3593 LocationSummary* locations = instruction->GetLocations();
3594 Location out = locations->Out();
3595 Location first = locations->InAt(0);
3596 Location second = locations->InAt(1);
3597 bool is_div = instruction->IsDiv();
3598
3599 switch (instruction->GetResultType()) {
3600 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 DCHECK_EQ(EAX, first.AsRegister<Register>());
3602 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003603
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003604 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003605 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606
3607 if (imm == 0) {
3608 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3609 } else if (imm == 1 || imm == -1) {
3610 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003611 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003612 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 } else {
3614 DCHECK(imm <= -2 || imm >= 2);
3615 GenerateDivRemWithAnyConstant(instruction);
3616 }
3617 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003618 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3619 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003621
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 Register second_reg = second.AsRegister<Register>();
3623 // 0x80000000/-1 triggers an arithmetic exception!
3624 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3625 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003626
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003627 __ cmpl(second_reg, Immediate(-1));
3628 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003629
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003630 // edx:eax <- sign-extended of eax
3631 __ cdq();
3632 // eax = quotient, edx = remainder
3633 __ idivl(second_reg);
3634 __ Bind(slow_path->GetExitLabel());
3635 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003636 break;
3637 }
3638
3639 case Primitive::kPrimLong: {
3640 InvokeRuntimeCallingConvention calling_convention;
3641 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3642 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3643 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3645 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3646 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3647
3648 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003649 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003650 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003651 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003652 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003653 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003654 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 break;
3656 }
3657
3658 default:
3659 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3660 }
3661}
3662
Calin Juravle7c4954d2014-10-28 16:57:40 +00003663void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003664 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003665 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003666 : LocationSummary::kNoCall;
3667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3668
Calin Juravle7c4954d2014-10-28 16:57:40 +00003669 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003670 case Primitive::kPrimInt: {
3671 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003672 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003673 locations->SetOut(Location::SameAsFirstInput());
3674 // Intel uses edx:eax as the dividend.
3675 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003676 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3677 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3678 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003679 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003680 locations->AddTemp(Location::RequiresRegister());
3681 }
Calin Juravled0d48522014-11-04 16:40:20 +00003682 break;
3683 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003684 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003685 InvokeRuntimeCallingConvention calling_convention;
3686 locations->SetInAt(0, Location::RegisterPairLocation(
3687 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3688 locations->SetInAt(1, Location::RegisterPairLocation(
3689 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3690 // Runtime helper puts the result in EAX, EDX.
3691 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003692 break;
3693 }
3694 case Primitive::kPrimFloat:
3695 case Primitive::kPrimDouble: {
3696 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003697 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3698 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003699 } else if (div->InputAt(1)->IsConstant()) {
3700 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003701 } else {
3702 locations->SetInAt(1, Location::Any());
3703 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003704 locations->SetOut(Location::SameAsFirstInput());
3705 break;
3706 }
3707
3708 default:
3709 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3710 }
3711}
3712
3713void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3714 LocationSummary* locations = div->GetLocations();
3715 Location first = locations->InAt(0);
3716 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003717
3718 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003719 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003720 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003721 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003722 break;
3723 }
3724
3725 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003726 if (second.IsFpuRegister()) {
3727 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3728 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3729 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003730 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003731 __ divss(first.AsFpuRegister<XmmRegister>(),
3732 codegen_->LiteralFloatAddress(
3733 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003734 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003735 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3736 } else {
3737 DCHECK(second.IsStackSlot());
3738 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3739 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003740 break;
3741 }
3742
3743 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003744 if (second.IsFpuRegister()) {
3745 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3746 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3747 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003748 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003749 __ divsd(first.AsFpuRegister<XmmRegister>(),
3750 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003751 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3752 const_area->GetBaseMethodAddress(),
3753 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003754 } else {
3755 DCHECK(second.IsDoubleStackSlot());
3756 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3757 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003758 break;
3759 }
3760
3761 default:
3762 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3763 }
3764}
3765
Calin Juravlebacfec32014-11-14 15:54:36 +00003766void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003767 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003768
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003769 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003770 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003771 : LocationSummary::kNoCall;
3772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003773
Calin Juravled2ec87d2014-12-08 14:24:46 +00003774 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 case Primitive::kPrimInt: {
3776 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003777 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3780 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3781 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003782 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003783 locations->AddTemp(Location::RequiresRegister());
3784 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003785 break;
3786 }
3787 case Primitive::kPrimLong: {
3788 InvokeRuntimeCallingConvention calling_convention;
3789 locations->SetInAt(0, Location::RegisterPairLocation(
3790 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3791 locations->SetInAt(1, Location::RegisterPairLocation(
3792 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3793 // Runtime helper puts the result in EAX, EDX.
3794 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3795 break;
3796 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003797 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003798 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799 locations->SetInAt(0, Location::Any());
3800 locations->SetInAt(1, Location::Any());
3801 locations->SetOut(Location::RequiresFpuRegister());
3802 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003803 break;
3804 }
3805
3806 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003807 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003808 }
3809}
3810
3811void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3812 Primitive::Type type = rem->GetResultType();
3813 switch (type) {
3814 case Primitive::kPrimInt:
3815 case Primitive::kPrimLong: {
3816 GenerateDivRemIntegral(rem);
3817 break;
3818 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003819 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003820 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003821 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003822 break;
3823 }
3824 default:
3825 LOG(FATAL) << "Unexpected rem type " << type;
3826 }
3827}
3828
Calin Juravled0d48522014-11-04 16:40:20 +00003829void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003830 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003831 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003832 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003833 case Primitive::kPrimByte:
3834 case Primitive::kPrimChar:
3835 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 case Primitive::kPrimInt: {
3837 locations->SetInAt(0, Location::Any());
3838 break;
3839 }
3840 case Primitive::kPrimLong: {
3841 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3842 if (!instruction->IsConstant()) {
3843 locations->AddTemp(Location::RequiresRegister());
3844 }
3845 break;
3846 }
3847 default:
3848 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3849 }
Calin Juravled0d48522014-11-04 16:40:20 +00003850}
3851
3852void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003853 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003854 codegen_->AddSlowPath(slow_path);
3855
3856 LocationSummary* locations = instruction->GetLocations();
3857 Location value = locations->InAt(0);
3858
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003859 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003860 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003861 case Primitive::kPrimByte:
3862 case Primitive::kPrimChar:
3863 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003864 case Primitive::kPrimInt: {
3865 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003867 __ j(kEqual, slow_path->GetEntryLabel());
3868 } else if (value.IsStackSlot()) {
3869 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3870 __ j(kEqual, slow_path->GetEntryLabel());
3871 } else {
3872 DCHECK(value.IsConstant()) << value;
3873 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003874 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003875 }
3876 }
3877 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003878 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003879 case Primitive::kPrimLong: {
3880 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003882 __ movl(temp, value.AsRegisterPairLow<Register>());
3883 __ orl(temp, value.AsRegisterPairHigh<Register>());
3884 __ j(kEqual, slow_path->GetEntryLabel());
3885 } else {
3886 DCHECK(value.IsConstant()) << value;
3887 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3888 __ jmp(slow_path->GetEntryLabel());
3889 }
3890 }
3891 break;
3892 }
3893 default:
3894 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003895 }
Calin Juravled0d48522014-11-04 16:40:20 +00003896}
3897
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3899 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3900
3901 LocationSummary* locations =
3902 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3903
3904 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003905 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003907 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003909 // The shift count needs to be in CL or a constant.
3910 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911 locations->SetOut(Location::SameAsFirstInput());
3912 break;
3913 }
3914 default:
3915 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3916 }
3917}
3918
3919void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3920 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3921
3922 LocationSummary* locations = op->GetLocations();
3923 Location first = locations->InAt(0);
3924 Location second = locations->InAt(1);
3925 DCHECK(first.Equals(locations->Out()));
3926
3927 switch (op->GetResultType()) {
3928 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003929 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003930 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003931 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003933 DCHECK_EQ(ECX, second_reg);
3934 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003935 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003937 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940 }
3941 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003942 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003943 if (shift == 0) {
3944 return;
3945 }
3946 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003947 if (op->IsShl()) {
3948 __ shll(first_reg, imm);
3949 } else if (op->IsShr()) {
3950 __ sarl(first_reg, imm);
3951 } else {
3952 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003953 }
3954 }
3955 break;
3956 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003957 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003958 if (second.IsRegister()) {
3959 Register second_reg = second.AsRegister<Register>();
3960 DCHECK_EQ(ECX, second_reg);
3961 if (op->IsShl()) {
3962 GenerateShlLong(first, second_reg);
3963 } else if (op->IsShr()) {
3964 GenerateShrLong(first, second_reg);
3965 } else {
3966 GenerateUShrLong(first, second_reg);
3967 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003968 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003969 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003970 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003971 // Nothing to do if the shift is 0, as the input is already the output.
3972 if (shift != 0) {
3973 if (op->IsShl()) {
3974 GenerateShlLong(first, shift);
3975 } else if (op->IsShr()) {
3976 GenerateShrLong(first, shift);
3977 } else {
3978 GenerateUShrLong(first, shift);
3979 }
3980 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003981 }
3982 break;
3983 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003984 default:
3985 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3986 }
3987}
3988
Mark P Mendell73945692015-04-29 14:56:17 +00003989void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3990 Register low = loc.AsRegisterPairLow<Register>();
3991 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003992 if (shift == 1) {
3993 // This is just an addition.
3994 __ addl(low, low);
3995 __ adcl(high, high);
3996 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003997 // Shift by 32 is easy. High gets low, and low gets 0.
3998 codegen_->EmitParallelMoves(
3999 loc.ToLow(),
4000 loc.ToHigh(),
4001 Primitive::kPrimInt,
4002 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4003 loc.ToLow(),
4004 Primitive::kPrimInt);
4005 } else if (shift > 32) {
4006 // Low part becomes 0. High part is low part << (shift-32).
4007 __ movl(high, low);
4008 __ shll(high, Immediate(shift - 32));
4009 __ xorl(low, low);
4010 } else {
4011 // Between 1 and 31.
4012 __ shld(high, low, Immediate(shift));
4013 __ shll(low, Immediate(shift));
4014 }
4015}
4016
Calin Juravle9aec02f2014-11-18 23:06:35 +00004017void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004018 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4020 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4021 __ testl(shifter, Immediate(32));
4022 __ j(kEqual, &done);
4023 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4024 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4025 __ Bind(&done);
4026}
4027
Mark P Mendell73945692015-04-29 14:56:17 +00004028void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4029 Register low = loc.AsRegisterPairLow<Register>();
4030 Register high = loc.AsRegisterPairHigh<Register>();
4031 if (shift == 32) {
4032 // Need to copy the sign.
4033 DCHECK_NE(low, high);
4034 __ movl(low, high);
4035 __ sarl(high, Immediate(31));
4036 } else if (shift > 32) {
4037 DCHECK_NE(low, high);
4038 // High part becomes sign. Low part is shifted by shift - 32.
4039 __ movl(low, high);
4040 __ sarl(high, Immediate(31));
4041 __ sarl(low, Immediate(shift - 32));
4042 } else {
4043 // Between 1 and 31.
4044 __ shrd(low, high, Immediate(shift));
4045 __ sarl(high, Immediate(shift));
4046 }
4047}
4048
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004050 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004051 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4052 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4053 __ testl(shifter, Immediate(32));
4054 __ j(kEqual, &done);
4055 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4056 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4057 __ Bind(&done);
4058}
4059
Mark P Mendell73945692015-04-29 14:56:17 +00004060void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4061 Register low = loc.AsRegisterPairLow<Register>();
4062 Register high = loc.AsRegisterPairHigh<Register>();
4063 if (shift == 32) {
4064 // Shift by 32 is easy. Low gets high, and high gets 0.
4065 codegen_->EmitParallelMoves(
4066 loc.ToHigh(),
4067 loc.ToLow(),
4068 Primitive::kPrimInt,
4069 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4070 loc.ToHigh(),
4071 Primitive::kPrimInt);
4072 } else if (shift > 32) {
4073 // Low part is high >> (shift - 32). High part becomes 0.
4074 __ movl(low, high);
4075 __ shrl(low, Immediate(shift - 32));
4076 __ xorl(high, high);
4077 } else {
4078 // Between 1 and 31.
4079 __ shrd(low, high, Immediate(shift));
4080 __ shrl(high, Immediate(shift));
4081 }
4082}
4083
Calin Juravle9aec02f2014-11-18 23:06:35 +00004084void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004085 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004086 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4087 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4088 __ testl(shifter, Immediate(32));
4089 __ j(kEqual, &done);
4090 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4091 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4092 __ Bind(&done);
4093}
4094
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004095void LocationsBuilderX86::VisitRor(HRor* ror) {
4096 LocationSummary* locations =
4097 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4098
4099 switch (ror->GetResultType()) {
4100 case Primitive::kPrimLong:
4101 // Add the temporary needed.
4102 locations->AddTemp(Location::RequiresRegister());
4103 FALLTHROUGH_INTENDED;
4104 case Primitive::kPrimInt:
4105 locations->SetInAt(0, Location::RequiresRegister());
4106 // The shift count needs to be in CL (unless it is a constant).
4107 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4108 locations->SetOut(Location::SameAsFirstInput());
4109 break;
4110 default:
4111 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4112 UNREACHABLE();
4113 }
4114}
4115
4116void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4117 LocationSummary* locations = ror->GetLocations();
4118 Location first = locations->InAt(0);
4119 Location second = locations->InAt(1);
4120
4121 if (ror->GetResultType() == Primitive::kPrimInt) {
4122 Register first_reg = first.AsRegister<Register>();
4123 if (second.IsRegister()) {
4124 Register second_reg = second.AsRegister<Register>();
4125 __ rorl(first_reg, second_reg);
4126 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004127 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004128 __ rorl(first_reg, imm);
4129 }
4130 return;
4131 }
4132
4133 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4134 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4135 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4136 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4137 if (second.IsRegister()) {
4138 Register second_reg = second.AsRegister<Register>();
4139 DCHECK_EQ(second_reg, ECX);
4140 __ movl(temp_reg, first_reg_hi);
4141 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4142 __ shrd(first_reg_lo, temp_reg, second_reg);
4143 __ movl(temp_reg, first_reg_hi);
4144 __ testl(second_reg, Immediate(32));
4145 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4146 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4147 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004148 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004149 if (shift_amt == 0) {
4150 // Already fine.
4151 return;
4152 }
4153 if (shift_amt == 32) {
4154 // Just swap.
4155 __ movl(temp_reg, first_reg_lo);
4156 __ movl(first_reg_lo, first_reg_hi);
4157 __ movl(first_reg_hi, temp_reg);
4158 return;
4159 }
4160
4161 Immediate imm(shift_amt);
4162 // Save the constents of the low value.
4163 __ movl(temp_reg, first_reg_lo);
4164
4165 // Shift right into low, feeding bits from high.
4166 __ shrd(first_reg_lo, first_reg_hi, imm);
4167
4168 // Shift right into high, feeding bits from the original low.
4169 __ shrd(first_reg_hi, temp_reg, imm);
4170
4171 // Swap if needed.
4172 if (shift_amt > 32) {
4173 __ movl(temp_reg, first_reg_lo);
4174 __ movl(first_reg_lo, first_reg_hi);
4175 __ movl(first_reg_hi, temp_reg);
4176 }
4177 }
4178}
4179
Calin Juravle9aec02f2014-11-18 23:06:35 +00004180void LocationsBuilderX86::VisitShl(HShl* shl) {
4181 HandleShift(shl);
4182}
4183
4184void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4185 HandleShift(shl);
4186}
4187
4188void LocationsBuilderX86::VisitShr(HShr* shr) {
4189 HandleShift(shr);
4190}
4191
4192void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4193 HandleShift(shr);
4194}
4195
4196void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4197 HandleShift(ushr);
4198}
4199
4200void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4201 HandleShift(ushr);
4202}
4203
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004204void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004205 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004207 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004208 if (instruction->IsStringAlloc()) {
4209 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4210 } else {
4211 InvokeRuntimeCallingConvention calling_convention;
4212 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004213 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004214}
4215
4216void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004217 // Note: if heap poisoning is enabled, the entry point takes cares
4218 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004219 if (instruction->IsStringAlloc()) {
4220 // String is allocated through StringFactory. Call NewEmptyString entry point.
4221 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004222 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004223 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4224 __ call(Address(temp, code_offset.Int32Value()));
4225 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4226 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004227 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004228 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004229 DCHECK(!codegen_->IsLeafMethod());
4230 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004231}
4232
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004233void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4234 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004235 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004236 locations->SetOut(Location::RegisterLocation(EAX));
4237 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004238 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4239 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004240}
4241
4242void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004243 // Note: if heap poisoning is enabled, the entry point takes cares
4244 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004245 QuickEntrypointEnum entrypoint =
4246 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4247 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004248 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004249 DCHECK(!codegen_->IsLeafMethod());
4250}
4251
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004252void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004253 LocationSummary* locations =
4254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004255 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4256 if (location.IsStackSlot()) {
4257 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4258 } else if (location.IsDoubleStackSlot()) {
4259 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004260 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004261 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004262}
4263
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004264void InstructionCodeGeneratorX86::VisitParameterValue(
4265 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4266}
4267
4268void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4269 LocationSummary* locations =
4270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4271 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4272}
4273
4274void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004275}
4276
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004277void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4278 LocationSummary* locations =
4279 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4280 locations->SetInAt(0, Location::RequiresRegister());
4281 locations->SetOut(Location::RequiresRegister());
4282}
4283
4284void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4285 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004286 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004287 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004288 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004289 __ movl(locations->Out().AsRegister<Register>(),
4290 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004291 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004292 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004293 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004294 __ movl(locations->Out().AsRegister<Register>(),
4295 Address(locations->InAt(0).AsRegister<Register>(),
4296 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4297 // temp = temp->GetImtEntryAt(method_offset);
4298 __ movl(locations->Out().AsRegister<Register>(),
4299 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004300 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004301}
4302
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004303void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004304 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004305 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004306 locations->SetInAt(0, Location::RequiresRegister());
4307 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004308}
4309
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004310void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4311 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004312 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004313 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004314 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004315 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004316 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004317 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004318 break;
4319
4320 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004321 __ notl(out.AsRegisterPairLow<Register>());
4322 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004323 break;
4324
4325 default:
4326 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4327 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004328}
4329
David Brazdil66d126e2015-04-03 16:02:44 +01004330void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4331 LocationSummary* locations =
4332 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4333 locations->SetInAt(0, Location::RequiresRegister());
4334 locations->SetOut(Location::SameAsFirstInput());
4335}
4336
4337void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004338 LocationSummary* locations = bool_not->GetLocations();
4339 Location in = locations->InAt(0);
4340 Location out = locations->Out();
4341 DCHECK(in.Equals(out));
4342 __ xorl(out.AsRegister<Register>(), Immediate(1));
4343}
4344
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004345void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004346 LocationSummary* locations =
4347 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004348 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004349 case Primitive::kPrimBoolean:
4350 case Primitive::kPrimByte:
4351 case Primitive::kPrimShort:
4352 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004353 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004354 case Primitive::kPrimLong: {
4355 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004356 locations->SetInAt(1, Location::Any());
4357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4358 break;
4359 }
4360 case Primitive::kPrimFloat:
4361 case Primitive::kPrimDouble: {
4362 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004363 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4364 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4365 } else if (compare->InputAt(1)->IsConstant()) {
4366 locations->SetInAt(1, Location::RequiresFpuRegister());
4367 } else {
4368 locations->SetInAt(1, Location::Any());
4369 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004370 locations->SetOut(Location::RequiresRegister());
4371 break;
4372 }
4373 default:
4374 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4375 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004376}
4377
4378void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004379 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004380 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004381 Location left = locations->InAt(0);
4382 Location right = locations->InAt(1);
4383
Mark Mendell0c9497d2015-08-21 09:30:05 -04004384 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004385 Condition less_cond = kLess;
4386
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004387 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004388 case Primitive::kPrimBoolean:
4389 case Primitive::kPrimByte:
4390 case Primitive::kPrimShort:
4391 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004392 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004393 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004394 break;
4395 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004396 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 Register left_low = left.AsRegisterPairLow<Register>();
4398 Register left_high = left.AsRegisterPairHigh<Register>();
4399 int32_t val_low = 0;
4400 int32_t val_high = 0;
4401 bool right_is_const = false;
4402
4403 if (right.IsConstant()) {
4404 DCHECK(right.GetConstant()->IsLongConstant());
4405 right_is_const = true;
4406 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4407 val_low = Low32Bits(val);
4408 val_high = High32Bits(val);
4409 }
4410
Calin Juravleddb7df22014-11-25 20:56:51 +00004411 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004412 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004413 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004414 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004415 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004416 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004417 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004419 __ j(kLess, &less); // Signed compare.
4420 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004421 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004423 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004425 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004426 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004427 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428 }
Aart Bika19616e2016-02-01 18:57:58 -08004429 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004430 break;
4431 }
4432 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004433 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004434 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004435 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004436 break;
4437 }
4438 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004439 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004440 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004441 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004442 break;
4443 }
4444 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004445 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004446 }
Aart Bika19616e2016-02-01 18:57:58 -08004447
Calin Juravleddb7df22014-11-25 20:56:51 +00004448 __ movl(out, Immediate(0));
4449 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004450 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004451
4452 __ Bind(&greater);
4453 __ movl(out, Immediate(1));
4454 __ jmp(&done);
4455
4456 __ Bind(&less);
4457 __ movl(out, Immediate(-1));
4458
4459 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004460}
4461
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004462void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004463 LocationSummary* locations =
4464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004465 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004466 locations->SetInAt(i, Location::Any());
4467 }
4468 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004469}
4470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004471void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004472 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004473}
4474
Roland Levillain7c1559a2015-12-15 10:55:36 +00004475void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004476 /*
4477 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4478 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4479 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4480 */
4481 switch (kind) {
4482 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004483 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004484 break;
4485 }
4486 case MemBarrierKind::kAnyStore:
4487 case MemBarrierKind::kLoadAny:
4488 case MemBarrierKind::kStoreStore: {
4489 // nop
4490 break;
4491 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004492 case MemBarrierKind::kNTStoreStore:
4493 // Non-Temporal Store/Store needs an explicit fence.
4494 MemoryFence(/* non-temporal */ true);
4495 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004496 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004497}
4498
Vladimir Markodc151b22015-10-15 18:02:30 +01004499HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4500 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004501 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004502 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004503}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004505Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4506 Register temp) {
4507 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004508 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004509 if (!invoke->GetLocations()->Intrinsified()) {
4510 return location.AsRegister<Register>();
4511 }
4512 // For intrinsics we allow any location, so it may be on the stack.
4513 if (!location.IsRegister()) {
4514 __ movl(temp, Address(ESP, location.GetStackIndex()));
4515 return temp;
4516 }
4517 // For register locations, check if the register was saved. If so, get it from the stack.
4518 // Note: There is a chance that the register was saved but not overwritten, so we could
4519 // save one load. However, since this is just an intrinsic slow path we prefer this
4520 // simple and more robust approach rather that trying to determine if that's the case.
4521 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004522 if (slow_path != nullptr) {
4523 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4524 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4525 __ movl(temp, Address(ESP, stack_offset));
4526 return temp;
4527 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004528 }
4529 return location.AsRegister<Register>();
4530}
4531
Serguei Katkov288c7a82016-05-16 11:53:15 +06004532Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4533 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004534 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4535 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004536 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004537 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004538 uint32_t offset =
4539 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4540 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004541 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004544 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004545 break;
4546 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4547 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4548 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004549 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4550 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4551 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004552 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004553 // Bind a new fixup label at the end of the "movl" insn.
4554 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004555 __ Bind(NewPcRelativeDexCacheArrayPatch(
4556 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4557 invoke->GetDexFileForPcRelativeDexCache(),
4558 offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004559 break;
4560 }
Vladimir Marko58155012015-08-19 12:49:41 +00004561 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004562 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004563 Register method_reg;
4564 Register reg = temp.AsRegister<Register>();
4565 if (current_method.IsRegister()) {
4566 method_reg = current_method.AsRegister<Register>();
4567 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004568 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004569 DCHECK(!current_method.IsValid());
4570 method_reg = reg;
4571 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4572 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004573 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004574 __ movl(reg, Address(method_reg,
4575 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004576 // temp = temp[index_in_cache];
4577 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4578 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004579 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4580 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004581 }
Vladimir Marko58155012015-08-19 12:49:41 +00004582 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004583 return callee_method;
4584}
4585
4586void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4587 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004588
4589 switch (invoke->GetCodePtrLocation()) {
4590 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4591 __ call(GetFrameEntryLabel());
4592 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004593 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4594 // (callee_method + offset_of_quick_compiled_code)()
4595 __ call(Address(callee_method.AsRegister<Register>(),
4596 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004597 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004598 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004599 }
4600
4601 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004602}
4603
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004604void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4605 Register temp = temp_in.AsRegister<Register>();
4606 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4607 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004608
4609 // Use the calling convention instead of the location of the receiver, as
4610 // intrinsics may have put the receiver in a different register. In the intrinsics
4611 // slow path, the arguments have been moved to the right place, so here we are
4612 // guaranteed that the receiver is the first register of the calling convention.
4613 InvokeDexCallingConvention calling_convention;
4614 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004615 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004616 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004617 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004618 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004619 // Instead of simply (possibly) unpoisoning `temp` here, we should
4620 // emit a read barrier for the previous class reference load.
4621 // However this is not required in practice, as this is an
4622 // intermediate/temporary reference and because the current
4623 // concurrent copying collector keeps the from-space memory
4624 // intact/accessible until the end of the marking phase (the
4625 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004626 __ MaybeUnpoisonHeapReference(temp);
4627 // temp = temp->GetMethodAt(method_offset);
4628 __ movl(temp, Address(temp, method_offset));
4629 // call temp->GetEntryPoint();
4630 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004631 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004632}
4633
Vladimir Markoaad75c62016-10-03 08:46:48 +00004634void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4635 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko00916b92017-05-17 17:45:45 +01004636 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004637 string_patches_.emplace_back(address,
4638 load_string->GetDexFile(),
4639 load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004640 __ Bind(&string_patches_.back().label);
4641}
4642
Vladimir Marko1998cd02017-01-13 13:02:58 +00004643void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004644 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004645 boot_image_type_patches_.emplace_back(address,
4646 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004647 load_class->GetTypeIndex().index_);
4648 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004649}
4650
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004651Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004652 HX86ComputeBaseMethodAddress* address =
4653 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4654 type_bss_entry_patches_.emplace_back(
4655 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004656 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004657}
4658
Vladimir Markoaad75c62016-10-03 08:46:48 +00004659Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4660 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004661 HX86ComputeBaseMethodAddress* address =
4662 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4663 string_patches_.emplace_back(
4664 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004665 return &string_patches_.back().label;
4666}
4667
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004668Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(
4669 HX86ComputeBaseMethodAddress* method_address,
4670 const DexFile& dex_file,
4671 uint32_t element_offset) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004672 // Add the patch entry and bind its label at the end of the instruction.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004673 pc_relative_dex_cache_patches_.emplace_back(method_address, dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004674 return &pc_relative_dex_cache_patches_.back().label;
4675}
4676
Vladimir Markoaad75c62016-10-03 08:46:48 +00004677// The label points to the end of the "movl" or another instruction but the literal offset
4678// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4679constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4680
4681template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4682inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004683 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004684 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004685 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004686 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004687 linker_patches->push_back(Factory(
4688 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004689 }
4690}
4691
Vladimir Marko58155012015-08-19 12:49:41 +00004692void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4693 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004694 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004695 pc_relative_dex_cache_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004696 string_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004697 boot_image_type_patches_.size() +
4698 type_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004699 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004700 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4701 linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01004702 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004703 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4704 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004705 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004706 } else {
Vladimir Marko764d4542017-05-16 10:31:41 +01004707 DCHECK(boot_image_type_patches_.empty());
4708 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004709 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004710 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4711 linker_patches);
4712 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004713}
4714
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004715void CodeGeneratorX86::MarkGCCard(Register temp,
4716 Register card,
4717 Register object,
4718 Register value,
4719 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004720 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004721 if (value_can_be_null) {
4722 __ testl(value, value);
4723 __ j(kEqual, &is_null);
4724 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004725 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004726 __ movl(temp, object);
4727 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004728 __ movb(Address(temp, card, TIMES_1, 0),
4729 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004730 if (value_can_be_null) {
4731 __ Bind(&is_null);
4732 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733}
4734
Calin Juravle52c48962014-12-16 17:02:57 +00004735void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4736 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004737
4738 bool object_field_get_with_read_barrier =
4739 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004740 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741 new (GetGraph()->GetArena()) LocationSummary(instruction,
4742 kEmitCompilerReadBarrier ?
4743 LocationSummary::kCallOnSlowPath :
4744 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004745 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004746 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004747 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004748 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004749
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004750 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4751 locations->SetOut(Location::RequiresFpuRegister());
4752 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004753 // The output overlaps in case of long: we don't want the low move
4754 // to overwrite the object's location. Likewise, in the case of
4755 // an object field get with read barriers enabled, we do not want
4756 // the move to overwrite the object's location, as we need it to emit
4757 // the read barrier.
4758 locations->SetOut(
4759 Location::RequiresRegister(),
4760 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4761 Location::kOutputOverlap :
4762 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004763 }
Calin Juravle52c48962014-12-16 17:02:57 +00004764
4765 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4766 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004767 // So we use an XMM register as a temp to achieve atomicity (first
4768 // load the temp into the XMM and then copy the XMM into the
4769 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004770 locations->AddTemp(Location::RequiresFpuRegister());
4771 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772}
4773
Calin Juravle52c48962014-12-16 17:02:57 +00004774void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4775 const FieldInfo& field_info) {
4776 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004777
Calin Juravle52c48962014-12-16 17:02:57 +00004778 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004779 Location base_loc = locations->InAt(0);
4780 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004781 Location out = locations->Out();
4782 bool is_volatile = field_info.IsVolatile();
4783 Primitive::Type field_type = field_info.GetFieldType();
4784 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4785
4786 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004787 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004788 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
4790 }
4791
4792 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004793 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 break;
4795 }
4796
4797 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004798 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004799 break;
4800 }
4801
4802 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004803 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004804 break;
4805 }
4806
4807 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004808 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004809 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004810
4811 case Primitive::kPrimNot: {
4812 // /* HeapReference<Object> */ out = *(base + offset)
4813 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004814 // Note that a potential implicit null check is handled in this
4815 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4816 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004817 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004818 if (is_volatile) {
4819 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4820 }
4821 } else {
4822 __ movl(out.AsRegister<Register>(), Address(base, offset));
4823 codegen_->MaybeRecordImplicitNullCheck(instruction);
4824 if (is_volatile) {
4825 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4826 }
4827 // If read barriers are enabled, emit read barriers other than
4828 // Baker's using a slow path (and also unpoison the loaded
4829 // reference, if heap poisoning is enabled).
4830 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4831 }
4832 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004833 }
4834
4835 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004836 if (is_volatile) {
4837 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4838 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004839 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004840 __ movd(out.AsRegisterPairLow<Register>(), temp);
4841 __ psrlq(temp, Immediate(32));
4842 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4843 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004844 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004845 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004846 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004847 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4848 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004849 break;
4850 }
4851
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004852 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004853 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004854 break;
4855 }
4856
4857 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004858 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004859 break;
4860 }
4861
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004862 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004863 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004864 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004865 }
Calin Juravle52c48962014-12-16 17:02:57 +00004866
Roland Levillain7c1559a2015-12-15 10:55:36 +00004867 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4868 // Potential implicit null checks, in the case of reference or
4869 // long fields, are handled in the previous switch statement.
4870 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004871 codegen_->MaybeRecordImplicitNullCheck(instruction);
4872 }
4873
Calin Juravle52c48962014-12-16 17:02:57 +00004874 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004875 if (field_type == Primitive::kPrimNot) {
4876 // Memory barriers, in the case of references, are also handled
4877 // in the previous switch statement.
4878 } else {
4879 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4880 }
Roland Levillain4d027112015-07-01 15:41:14 +01004881 }
Calin Juravle52c48962014-12-16 17:02:57 +00004882}
4883
4884void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4885 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4886
4887 LocationSummary* locations =
4888 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4889 locations->SetInAt(0, Location::RequiresRegister());
4890 bool is_volatile = field_info.IsVolatile();
4891 Primitive::Type field_type = field_info.GetFieldType();
4892 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4893 || (field_type == Primitive::kPrimByte);
4894
4895 // The register allocator does not support multiple
4896 // inputs that die at entry with one in a specific register.
4897 if (is_byte_type) {
4898 // Ensure the value is in a byte register.
4899 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004900 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004901 if (is_volatile && field_type == Primitive::kPrimDouble) {
4902 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4903 locations->SetInAt(1, Location::RequiresFpuRegister());
4904 } else {
4905 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4906 }
4907 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4908 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004909 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004910
Calin Juravle52c48962014-12-16 17:02:57 +00004911 // 64bits value can be atomically written to an address with movsd and an XMM register.
4912 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4913 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4914 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4915 // isolated cases when we need this it isn't worth adding the extra complexity.
4916 locations->AddTemp(Location::RequiresFpuRegister());
4917 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004918 } else {
4919 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4920
4921 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4922 // Temporary registers for the write barrier.
4923 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4924 // Ensure the card is in a byte register.
4925 locations->AddTemp(Location::RegisterLocation(ECX));
4926 }
Calin Juravle52c48962014-12-16 17:02:57 +00004927 }
4928}
4929
4930void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004931 const FieldInfo& field_info,
4932 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004933 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4934
4935 LocationSummary* locations = instruction->GetLocations();
4936 Register base = locations->InAt(0).AsRegister<Register>();
4937 Location value = locations->InAt(1);
4938 bool is_volatile = field_info.IsVolatile();
4939 Primitive::Type field_type = field_info.GetFieldType();
4940 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004941 bool needs_write_barrier =
4942 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004943
4944 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004945 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004946 }
4947
Mark Mendell81489372015-11-04 11:30:41 -05004948 bool maybe_record_implicit_null_check_done = false;
4949
Calin Juravle52c48962014-12-16 17:02:57 +00004950 switch (field_type) {
4951 case Primitive::kPrimBoolean:
4952 case Primitive::kPrimByte: {
4953 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4954 break;
4955 }
4956
4957 case Primitive::kPrimShort:
4958 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004959 if (value.IsConstant()) {
4960 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4961 __ movw(Address(base, offset), Immediate(v));
4962 } else {
4963 __ movw(Address(base, offset), value.AsRegister<Register>());
4964 }
Calin Juravle52c48962014-12-16 17:02:57 +00004965 break;
4966 }
4967
4968 case Primitive::kPrimInt:
4969 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004970 if (kPoisonHeapReferences && needs_write_barrier) {
4971 // Note that in the case where `value` is a null reference,
4972 // we do not enter this block, as the reference does not
4973 // need poisoning.
4974 DCHECK_EQ(field_type, Primitive::kPrimNot);
4975 Register temp = locations->GetTemp(0).AsRegister<Register>();
4976 __ movl(temp, value.AsRegister<Register>());
4977 __ PoisonHeapReference(temp);
4978 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004979 } else if (value.IsConstant()) {
4980 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4981 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004982 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004983 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004984 __ movl(Address(base, offset), value.AsRegister<Register>());
4985 }
Calin Juravle52c48962014-12-16 17:02:57 +00004986 break;
4987 }
4988
4989 case Primitive::kPrimLong: {
4990 if (is_volatile) {
4991 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4992 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4993 __ movd(temp1, value.AsRegisterPairLow<Register>());
4994 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4995 __ punpckldq(temp1, temp2);
4996 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004997 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004998 } else if (value.IsConstant()) {
4999 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5000 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5001 codegen_->MaybeRecordImplicitNullCheck(instruction);
5002 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005003 } else {
5004 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005005 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005006 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5007 }
Mark Mendell81489372015-11-04 11:30:41 -05005008 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005009 break;
5010 }
5011
5012 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005013 if (value.IsConstant()) {
5014 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5015 __ movl(Address(base, offset), Immediate(v));
5016 } else {
5017 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5018 }
Calin Juravle52c48962014-12-16 17:02:57 +00005019 break;
5020 }
5021
5022 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005023 if (value.IsConstant()) {
5024 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5025 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5026 codegen_->MaybeRecordImplicitNullCheck(instruction);
5027 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5028 maybe_record_implicit_null_check_done = true;
5029 } else {
5030 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5031 }
Calin Juravle52c48962014-12-16 17:02:57 +00005032 break;
5033 }
5034
5035 case Primitive::kPrimVoid:
5036 LOG(FATAL) << "Unreachable type " << field_type;
5037 UNREACHABLE();
5038 }
5039
Mark Mendell81489372015-11-04 11:30:41 -05005040 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005041 codegen_->MaybeRecordImplicitNullCheck(instruction);
5042 }
5043
Roland Levillain4d027112015-07-01 15:41:14 +01005044 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 Register temp = locations->GetTemp(0).AsRegister<Register>();
5046 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005047 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005048 }
5049
Calin Juravle52c48962014-12-16 17:02:57 +00005050 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005051 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005052 }
5053}
5054
5055void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5056 HandleFieldGet(instruction, instruction->GetFieldInfo());
5057}
5058
5059void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5060 HandleFieldGet(instruction, instruction->GetFieldInfo());
5061}
5062
5063void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5064 HandleFieldSet(instruction, instruction->GetFieldInfo());
5065}
5066
5067void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005068 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005069}
5070
5071void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5072 HandleFieldSet(instruction, instruction->GetFieldInfo());
5073}
5074
5075void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005076 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005077}
5078
5079void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5080 HandleFieldGet(instruction, instruction->GetFieldInfo());
5081}
5082
5083void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5084 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085}
5086
Calin Juravlee460d1d2015-09-29 04:52:17 +01005087void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5088 HUnresolvedInstanceFieldGet* instruction) {
5089 FieldAccessCallingConventionX86 calling_convention;
5090 codegen_->CreateUnresolvedFieldLocationSummary(
5091 instruction, instruction->GetFieldType(), calling_convention);
5092}
5093
5094void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5095 HUnresolvedInstanceFieldGet* instruction) {
5096 FieldAccessCallingConventionX86 calling_convention;
5097 codegen_->GenerateUnresolvedFieldAccess(instruction,
5098 instruction->GetFieldType(),
5099 instruction->GetFieldIndex(),
5100 instruction->GetDexPc(),
5101 calling_convention);
5102}
5103
5104void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5105 HUnresolvedInstanceFieldSet* instruction) {
5106 FieldAccessCallingConventionX86 calling_convention;
5107 codegen_->CreateUnresolvedFieldLocationSummary(
5108 instruction, instruction->GetFieldType(), calling_convention);
5109}
5110
5111void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5112 HUnresolvedInstanceFieldSet* instruction) {
5113 FieldAccessCallingConventionX86 calling_convention;
5114 codegen_->GenerateUnresolvedFieldAccess(instruction,
5115 instruction->GetFieldType(),
5116 instruction->GetFieldIndex(),
5117 instruction->GetDexPc(),
5118 calling_convention);
5119}
5120
5121void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5122 HUnresolvedStaticFieldGet* instruction) {
5123 FieldAccessCallingConventionX86 calling_convention;
5124 codegen_->CreateUnresolvedFieldLocationSummary(
5125 instruction, instruction->GetFieldType(), calling_convention);
5126}
5127
5128void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5129 HUnresolvedStaticFieldGet* instruction) {
5130 FieldAccessCallingConventionX86 calling_convention;
5131 codegen_->GenerateUnresolvedFieldAccess(instruction,
5132 instruction->GetFieldType(),
5133 instruction->GetFieldIndex(),
5134 instruction->GetDexPc(),
5135 calling_convention);
5136}
5137
5138void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5139 HUnresolvedStaticFieldSet* instruction) {
5140 FieldAccessCallingConventionX86 calling_convention;
5141 codegen_->CreateUnresolvedFieldLocationSummary(
5142 instruction, instruction->GetFieldType(), calling_convention);
5143}
5144
5145void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5146 HUnresolvedStaticFieldSet* instruction) {
5147 FieldAccessCallingConventionX86 calling_convention;
5148 codegen_->GenerateUnresolvedFieldAccess(instruction,
5149 instruction->GetFieldType(),
5150 instruction->GetFieldIndex(),
5151 instruction->GetDexPc(),
5152 calling_convention);
5153}
5154
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005155void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005156 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5157 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5158 ? Location::RequiresRegister()
5159 : Location::Any();
5160 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161}
5162
Calin Juravle2ae48182016-03-16 14:05:09 +00005163void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5164 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005165 return;
5166 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005167 LocationSummary* locations = instruction->GetLocations();
5168 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005169
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005170 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005171 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005172}
5173
Calin Juravle2ae48182016-03-16 14:05:09 +00005174void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005175 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005176 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005177
5178 LocationSummary* locations = instruction->GetLocations();
5179 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005180
5181 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005182 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005183 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005185 } else {
5186 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005187 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005188 __ jmp(slow_path->GetEntryLabel());
5189 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 }
5191 __ j(kEqual, slow_path->GetEntryLabel());
5192}
5193
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005194void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005195 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005196}
5197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005199 bool object_array_get_with_read_barrier =
5200 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005201 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005202 new (GetGraph()->GetArena()) LocationSummary(instruction,
5203 object_array_get_with_read_barrier ?
5204 LocationSummary::kCallOnSlowPath :
5205 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005206 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005207 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005208 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005209 locations->SetInAt(0, Location::RequiresRegister());
5210 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005211 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5212 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5213 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214 // The output overlaps in case of long: we don't want the low move
5215 // to overwrite the array's location. Likewise, in the case of an
5216 // object array get with read barriers enabled, we do not want the
5217 // move to overwrite the array's location, as we need it to emit
5218 // the read barrier.
5219 locations->SetOut(
5220 Location::RequiresRegister(),
5221 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5222 Location::kOutputOverlap :
5223 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225}
5226
5227void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5228 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005229 Location obj_loc = locations->InAt(0);
5230 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005232 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005233 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234
Calin Juravle77520bc2015-01-12 18:45:46 +00005235 Primitive::Type type = instruction->GetType();
5236 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005238 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005239 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240 break;
5241 }
5242
5243 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005244 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005245 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 break;
5247 }
5248
5249 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005250 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005251 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 break;
5253 }
5254
5255 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005256 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005257 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5258 // Branch cases into compressed and uncompressed for each index's type.
5259 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5260 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005261 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005262 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005263 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5264 "Expecting 0=compressed, 1=uncompressed");
5265 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005266 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5267 __ jmp(&done);
5268 __ Bind(&not_compressed);
5269 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5270 __ Bind(&done);
5271 } else {
5272 // Common case for charAt of array of char or when string compression's
5273 // feature is turned off.
5274 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5275 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 break;
5277 }
5278
Roland Levillain7c1559a2015-12-15 10:55:36 +00005279 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005281 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 break;
5283 }
5284
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 case Primitive::kPrimNot: {
5286 static_assert(
5287 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5288 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289 // /* HeapReference<Object> */ out =
5290 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5291 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005292 // Note that a potential implicit null check is handled in this
5293 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5294 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005295 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005296 } else {
5297 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005298 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5299 codegen_->MaybeRecordImplicitNullCheck(instruction);
5300 // If read barriers are enabled, emit read barriers other than
5301 // Baker's using a slow path (and also unpoison the loaded
5302 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005303 if (index.IsConstant()) {
5304 uint32_t offset =
5305 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005306 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5307 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 codegen_->MaybeGenerateReadBarrierSlow(
5309 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5310 }
5311 }
5312 break;
5313 }
5314
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005316 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005317 __ movl(out_loc.AsRegisterPairLow<Register>(),
5318 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5319 codegen_->MaybeRecordImplicitNullCheck(instruction);
5320 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5321 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 break;
5323 }
5324
Mark Mendell7c8d0092015-01-26 11:21:33 -05005325 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005326 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005327 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005328 break;
5329 }
5330
5331 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005332 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005333 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005334 break;
5335 }
5336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005338 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005339 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005341
Roland Levillain7c1559a2015-12-15 10:55:36 +00005342 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5343 // Potential implicit null checks, in the case of reference or
5344 // long arrays, are handled in the previous switch statement.
5345 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005346 codegen_->MaybeRecordImplicitNullCheck(instruction);
5347 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348}
5349
5350void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005351 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005352
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005353 bool needs_write_barrier =
5354 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005355 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005356
Nicolas Geoffray39468442014-09-02 15:17:15 +01005357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5358 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005359 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 LocationSummary::kCallOnSlowPath :
5361 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005362
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005363 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5364 || (value_type == Primitive::kPrimByte);
5365 // We need the inputs to be different than the output in case of long operation.
5366 // In case of a byte operation, the register allocator does not support multiple
5367 // inputs that die at entry with one in a specific register.
5368 locations->SetInAt(0, Location::RequiresRegister());
5369 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5370 if (is_byte_type) {
5371 // Ensure the value is in a byte register.
5372 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5373 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005374 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005376 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5377 }
5378 if (needs_write_barrier) {
5379 // Temporary registers for the write barrier.
5380 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5381 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005382 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384}
5385
5386void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5387 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005388 Location array_loc = locations->InAt(0);
5389 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005391 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005392 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005393 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5394 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5395 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005396 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005397 bool needs_write_barrier =
5398 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399
5400 switch (value_type) {
5401 case Primitive::kPrimBoolean:
5402 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005403 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005404 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005405 if (value.IsRegister()) {
5406 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005410 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 break;
5412 }
5413
5414 case Primitive::kPrimShort:
5415 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005417 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418 if (value.IsRegister()) {
5419 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005423 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 break;
5425 }
5426
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005427 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005429 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005430
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 if (!value.IsRegister()) {
5432 // Just setting null.
5433 DCHECK(instruction->InputAt(2)->IsNullConstant());
5434 DCHECK(value.IsConstant()) << value;
5435 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005436 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005437 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005438 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441
5442 DCHECK(needs_write_barrier);
5443 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005444 // We cannot use a NearLabel for `done`, as its range may be too
5445 // short when Baker read barriers are enabled.
5446 Label done;
5447 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005448 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005449 Location temp_loc = locations->GetTemp(0);
5450 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005451 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005452 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5453 codegen_->AddSlowPath(slow_path);
5454 if (instruction->GetValueCanBeNull()) {
5455 __ testl(register_value, register_value);
5456 __ j(kNotEqual, &not_null);
5457 __ movl(address, Immediate(0));
5458 codegen_->MaybeRecordImplicitNullCheck(instruction);
5459 __ jmp(&done);
5460 __ Bind(&not_null);
5461 }
5462
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005463 // Note that when Baker read barriers are enabled, the type
5464 // checks are performed without read barriers. This is fine,
5465 // even in the case where a class object is in the from-space
5466 // after the flip, as a comparison involving such a type would
5467 // not produce a false positive; it may of course produce a
5468 // false negative, in which case we would take the ArraySet
5469 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005470
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005471 // /* HeapReference<Class> */ temp = array->klass_
5472 __ movl(temp, Address(array, class_offset));
5473 codegen_->MaybeRecordImplicitNullCheck(instruction);
5474 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005475
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005476 // /* HeapReference<Class> */ temp = temp->component_type_
5477 __ movl(temp, Address(temp, component_offset));
5478 // If heap poisoning is enabled, no need to unpoison `temp`
5479 // nor the object reference in `register_value->klass`, as
5480 // we are comparing two poisoned references.
5481 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005482
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005483 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5484 __ j(kEqual, &do_put);
5485 // If heap poisoning is enabled, the `temp` reference has
5486 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005487 __ MaybeUnpoisonHeapReference(temp);
5488
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005489 // If heap poisoning is enabled, no need to unpoison the
5490 // heap reference loaded below, as it is only used for a
5491 // comparison with null.
5492 __ cmpl(Address(temp, super_offset), Immediate(0));
5493 __ j(kNotEqual, slow_path->GetEntryLabel());
5494 __ Bind(&do_put);
5495 } else {
5496 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005497 }
5498 }
5499
5500 if (kPoisonHeapReferences) {
5501 __ movl(temp, register_value);
5502 __ PoisonHeapReference(temp);
5503 __ movl(address, temp);
5504 } else {
5505 __ movl(address, register_value);
5506 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005507 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 codegen_->MaybeRecordImplicitNullCheck(instruction);
5509 }
5510
5511 Register card = locations->GetTemp(1).AsRegister<Register>();
5512 codegen_->MarkGCCard(
5513 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5514 __ Bind(&done);
5515
5516 if (slow_path != nullptr) {
5517 __ Bind(slow_path->GetExitLabel());
5518 }
5519
5520 break;
5521 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005522
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005523 case Primitive::kPrimInt: {
5524 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005525 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005526 if (value.IsRegister()) {
5527 __ movl(address, value.AsRegister<Register>());
5528 } else {
5529 DCHECK(value.IsConstant()) << value;
5530 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5531 __ movl(address, Immediate(v));
5532 }
5533 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005534 break;
5535 }
5536
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005537 case Primitive::kPrimLong: {
5538 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005539 if (value.IsRegisterPair()) {
5540 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5541 value.AsRegisterPairLow<Register>());
5542 codegen_->MaybeRecordImplicitNullCheck(instruction);
5543 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5544 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005546 DCHECK(value.IsConstant());
5547 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5548 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5549 Immediate(Low32Bits(val)));
5550 codegen_->MaybeRecordImplicitNullCheck(instruction);
5551 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5552 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553 }
5554 break;
5555 }
5556
Mark Mendell7c8d0092015-01-26 11:21:33 -05005557 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005558 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005559 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005560 if (value.IsFpuRegister()) {
5561 __ movss(address, value.AsFpuRegister<XmmRegister>());
5562 } else {
5563 DCHECK(value.IsConstant());
5564 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5565 __ movl(address, Immediate(v));
5566 }
5567 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005568 break;
5569 }
5570
5571 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005572 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005573 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005574 if (value.IsFpuRegister()) {
5575 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5576 } else {
5577 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005578 Address address_hi =
5579 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005580 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5581 __ movl(address, Immediate(Low32Bits(v)));
5582 codegen_->MaybeRecordImplicitNullCheck(instruction);
5583 __ movl(address_hi, Immediate(High32Bits(v)));
5584 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005585 break;
5586 }
5587
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005588 case Primitive::kPrimVoid:
5589 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005590 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 }
5592}
5593
5594void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005596 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005597 if (!instruction->IsEmittedAtUseSite()) {
5598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005600}
5601
5602void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005603 if (instruction->IsEmittedAtUseSite()) {
5604 return;
5605 }
5606
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005607 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005608 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005609 Register obj = locations->InAt(0).AsRegister<Register>();
5610 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005612 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005613 // Mask out most significant bit in case the array is String's array of char.
5614 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005615 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617}
5618
5619void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005620 RegisterSet caller_saves = RegisterSet::Empty();
5621 InvokeRuntimeCallingConvention calling_convention;
5622 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5623 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5624 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005625 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005626 HInstruction* length = instruction->InputAt(1);
5627 if (!length->IsEmittedAtUseSite()) {
5628 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5629 }
jessicahandojo4877b792016-09-08 19:49:13 -07005630 // Need register to see array's length.
5631 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5632 locations->AddTemp(Location::RequiresRegister());
5633 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634}
5635
5636void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005637 const bool is_string_compressed_char_at =
5638 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005639 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005640 Location index_loc = locations->InAt(0);
5641 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005642 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005643 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005644
Mark Mendell99dbd682015-04-22 16:18:52 -04005645 if (length_loc.IsConstant()) {
5646 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5647 if (index_loc.IsConstant()) {
5648 // BCE will remove the bounds check if we are guarenteed to pass.
5649 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5650 if (index < 0 || index >= length) {
5651 codegen_->AddSlowPath(slow_path);
5652 __ jmp(slow_path->GetEntryLabel());
5653 } else {
5654 // Some optimization after BCE may have generated this, and we should not
5655 // generate a bounds check if it is a valid range.
5656 }
5657 return;
5658 }
5659
5660 // We have to reverse the jump condition because the length is the constant.
5661 Register index_reg = index_loc.AsRegister<Register>();
5662 __ cmpl(index_reg, Immediate(length));
5663 codegen_->AddSlowPath(slow_path);
5664 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005665 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005666 HInstruction* array_length = instruction->InputAt(1);
5667 if (array_length->IsEmittedAtUseSite()) {
5668 // Address the length field in the array.
5669 DCHECK(array_length->IsArrayLength());
5670 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5671 Location array_loc = array_length->GetLocations()->InAt(0);
5672 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005673 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005674 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5675 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005676 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5677 __ movl(length_reg, array_len);
5678 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005679 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005680 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005681 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005682 // Checking bounds for general case:
5683 // Array of char or string's array with feature compression off.
5684 if (index_loc.IsConstant()) {
5685 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5686 __ cmpl(array_len, Immediate(value));
5687 } else {
5688 __ cmpl(array_len, index_loc.AsRegister<Register>());
5689 }
5690 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005691 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005692 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005693 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005694 }
5695 codegen_->AddSlowPath(slow_path);
5696 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005697 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005698}
5699
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005700void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005701 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005702}
5703
5704void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005705 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5706}
5707
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005708void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005709 LocationSummary* locations =
5710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005711 // In suspend check slow path, usually there are no caller-save registers at all.
5712 // If SIMD instructions are present, however, we force spilling all live SIMD
5713 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005714 locations->SetCustomSlowPathCallerSaves(
5715 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005716}
5717
5718void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005719 HBasicBlock* block = instruction->GetBlock();
5720 if (block->GetLoopInformation() != nullptr) {
5721 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5722 // The back edge will generate the suspend check.
5723 return;
5724 }
5725 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5726 // The goto will generate the suspend check.
5727 return;
5728 }
5729 GenerateSuspendCheck(instruction, nullptr);
5730}
5731
5732void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5733 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005734 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005735 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5736 if (slow_path == nullptr) {
5737 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5738 instruction->SetSlowPath(slow_path);
5739 codegen_->AddSlowPath(slow_path);
5740 if (successor != nullptr) {
5741 DCHECK(successor->IsLoopHeader());
5742 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5743 }
5744 } else {
5745 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5746 }
5747
Andreas Gampe542451c2016-07-26 09:02:02 -07005748 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005749 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005750 if (successor == nullptr) {
5751 __ j(kNotEqual, slow_path->GetEntryLabel());
5752 __ Bind(slow_path->GetReturnLabel());
5753 } else {
5754 __ j(kEqual, codegen_->GetLabelOf(successor));
5755 __ jmp(slow_path->GetEntryLabel());
5756 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005757}
5758
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5760 return codegen_->GetAssembler();
5761}
5762
Mark Mendell7c8d0092015-01-26 11:21:33 -05005763void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005764 ScratchRegisterScope ensure_scratch(
5765 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5766 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5767 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5768 __ movl(temp_reg, Address(ESP, src + stack_offset));
5769 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005770}
5771
5772void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005773 ScratchRegisterScope ensure_scratch(
5774 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5775 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5776 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5777 __ movl(temp_reg, Address(ESP, src + stack_offset));
5778 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5779 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5780 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005781}
5782
5783void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005784 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005785 Location source = move->GetSource();
5786 Location destination = move->GetDestination();
5787
5788 if (source.IsRegister()) {
5789 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005790 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005791 } else if (destination.IsFpuRegister()) {
5792 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005793 } else {
5794 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005795 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005797 } else if (source.IsRegisterPair()) {
5798 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5799 // Create stack space for 2 elements.
5800 __ subl(ESP, Immediate(2 * elem_size));
5801 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5802 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5803 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5804 // And remove the temporary stack space we allocated.
5805 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005807 if (destination.IsRegister()) {
5808 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5809 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005811 } else if (destination.IsRegisterPair()) {
5812 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5813 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5814 __ psrlq(src_reg, Immediate(32));
5815 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 } else if (destination.IsStackSlot()) {
5817 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005818 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005820 } else {
5821 DCHECK(destination.IsSIMDStackSlot());
5822 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005823 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005824 } else if (source.IsStackSlot()) {
5825 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005826 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005827 } else if (destination.IsFpuRegister()) {
5828 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005829 } else {
5830 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005831 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5832 }
5833 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005834 if (destination.IsRegisterPair()) {
5835 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5836 __ movl(destination.AsRegisterPairHigh<Register>(),
5837 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5838 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5840 } else {
5841 DCHECK(destination.IsDoubleStackSlot()) << destination;
5842 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005843 }
Aart Bik5576f372017-03-23 16:17:37 -07005844 } else if (source.IsSIMDStackSlot()) {
5845 DCHECK(destination.IsFpuRegister());
5846 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005847 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005848 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005849 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005850 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005851 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005852 if (value == 0) {
5853 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5854 } else {
5855 __ movl(destination.AsRegister<Register>(), Immediate(value));
5856 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005857 } else {
5858 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005859 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005860 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005861 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005862 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005863 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005864 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005865 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005866 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5867 if (value == 0) {
5868 // Easy handling of 0.0.
5869 __ xorps(dest, dest);
5870 } else {
5871 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005872 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5873 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5874 __ movl(temp, Immediate(value));
5875 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005876 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005877 } else {
5878 DCHECK(destination.IsStackSlot()) << destination;
5879 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5880 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005881 } else if (constant->IsLongConstant()) {
5882 int64_t value = constant->AsLongConstant()->GetValue();
5883 int32_t low_value = Low32Bits(value);
5884 int32_t high_value = High32Bits(value);
5885 Immediate low(low_value);
5886 Immediate high(high_value);
5887 if (destination.IsDoubleStackSlot()) {
5888 __ movl(Address(ESP, destination.GetStackIndex()), low);
5889 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5890 } else {
5891 __ movl(destination.AsRegisterPairLow<Register>(), low);
5892 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5893 }
5894 } else {
5895 DCHECK(constant->IsDoubleConstant());
5896 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005897 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005898 int32_t low_value = Low32Bits(value);
5899 int32_t high_value = High32Bits(value);
5900 Immediate low(low_value);
5901 Immediate high(high_value);
5902 if (destination.IsFpuRegister()) {
5903 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5904 if (value == 0) {
5905 // Easy handling of 0.0.
5906 __ xorpd(dest, dest);
5907 } else {
5908 __ pushl(high);
5909 __ pushl(low);
5910 __ movsd(dest, Address(ESP, 0));
5911 __ addl(ESP, Immediate(8));
5912 }
5913 } else {
5914 DCHECK(destination.IsDoubleStackSlot()) << destination;
5915 __ movl(Address(ESP, destination.GetStackIndex()), low);
5916 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5917 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005918 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005919 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005920 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005921 }
5922}
5923
Mark Mendella5c19ce2015-04-01 12:51:05 -04005924void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005925 Register suggested_scratch = reg == EAX ? EBX : EAX;
5926 ScratchRegisterScope ensure_scratch(
5927 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5928
5929 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5930 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5931 __ movl(Address(ESP, mem + stack_offset), reg);
5932 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005933}
5934
Mark Mendell7c8d0092015-01-26 11:21:33 -05005935void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005936 ScratchRegisterScope ensure_scratch(
5937 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5938
5939 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5940 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5941 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5942 __ movss(Address(ESP, mem + stack_offset), reg);
5943 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005944}
5945
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005947 ScratchRegisterScope ensure_scratch1(
5948 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005949
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005950 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5951 ScratchRegisterScope ensure_scratch2(
5952 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005953
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005954 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5955 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5956 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5957 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5958 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5959 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005960}
5961
5962void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005963 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005964 Location source = move->GetSource();
5965 Location destination = move->GetDestination();
5966
5967 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005968 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5969 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5970 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5971 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5972 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005973 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005974 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005975 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005976 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005977 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5978 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005979 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5980 // Use XOR Swap algorithm to avoid a temporary.
5981 DCHECK_NE(source.reg(), destination.reg());
5982 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5983 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5984 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5985 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5986 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5987 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5988 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005989 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5990 // Take advantage of the 16 bytes in the XMM register.
5991 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5992 Address stack(ESP, destination.GetStackIndex());
5993 // Load the double into the high doubleword.
5994 __ movhpd(reg, stack);
5995
5996 // Store the low double into the destination.
5997 __ movsd(stack, reg);
5998
5999 // Move the high double to the low double.
6000 __ psrldq(reg, Immediate(8));
6001 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6002 // Take advantage of the 16 bytes in the XMM register.
6003 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6004 Address stack(ESP, source.GetStackIndex());
6005 // Load the double into the high doubleword.
6006 __ movhpd(reg, stack);
6007
6008 // Store the low double into the destination.
6009 __ movsd(stack, reg);
6010
6011 // Move the high double to the low double.
6012 __ psrldq(reg, Immediate(8));
6013 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6014 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6015 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006016 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006017 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006018 }
6019}
6020
6021void ParallelMoveResolverX86::SpillScratch(int reg) {
6022 __ pushl(static_cast<Register>(reg));
6023}
6024
6025void ParallelMoveResolverX86::RestoreScratch(int reg) {
6026 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006027}
6028
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006029HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6030 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006032 case HLoadClass::LoadKind::kInvalid:
6033 LOG(FATAL) << "UNREACHABLE";
6034 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006035 case HLoadClass::LoadKind::kReferrersClass:
6036 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006038 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006039 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006041 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 DCHECK(Runtime::Current()->UseJitCompilation());
6043 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006044 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 case HLoadClass::LoadKind::kDexCacheViaMethod:
6046 break;
6047 }
6048 return desired_class_load_kind;
6049}
6050
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006051void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006052 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6053 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006055 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 cls,
6057 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006058 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006059 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 return;
6061 }
Vladimir Marko41559982017-01-06 14:04:23 +00006062 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006064 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6065 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 ? LocationSummary::kCallOnSlowPath
6067 : LocationSummary::kNoCall;
6068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006069 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006070 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006071 }
6072
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006073 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006074 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6075 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006076 locations->SetInAt(0, Location::RequiresRegister());
6077 }
6078 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006079 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6080 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6081 // Rely on the type resolution and/or initialization to save everything.
6082 RegisterSet caller_saves = RegisterSet::Empty();
6083 InvokeRuntimeCallingConvention calling_convention;
6084 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6085 locations->SetCustomSlowPathCallerSaves(caller_saves);
6086 } else {
6087 // For non-Baker read barrier we have a temp-clobbering call.
6088 }
6089 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006090}
6091
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006092Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6093 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006094 Handle<mirror::Class> handle) {
6095 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6096 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006097 // Add a patch entry and return the label.
6098 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6099 PatchInfo<Label>* info = &jit_class_patches_.back();
6100 return &info->label;
6101}
6102
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006103// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6104// move.
6105void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006106 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6107 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6108 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006109 return;
6110 }
Vladimir Marko41559982017-01-06 14:04:23 +00006111 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006112
Vladimir Marko41559982017-01-06 14:04:23 +00006113 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114 Location out_loc = locations->Out();
6115 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006116
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006117 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006118 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6119 ? kWithoutReadBarrier
6120 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006121 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 case HLoadClass::LoadKind::kReferrersClass: {
6123 DCHECK(!cls->CanCallRuntime());
6124 DCHECK(!cls->MustGenerateClinitCheck());
6125 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6126 Register current_method = locations->InAt(0).AsRegister<Register>();
6127 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006128 cls,
6129 out_loc,
6130 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006131 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006132 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133 break;
6134 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006136 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006137 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 Register method_address = locations->InAt(0).AsRegister<Register>();
6139 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006140 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 break;
6142 }
6143 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006144 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006145 uint32_t address = dchecked_integral_cast<uint32_t>(
6146 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6147 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 break;
6150 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006151 case HLoadClass::LoadKind::kBssEntry: {
6152 Register method_address = locations->InAt(0).AsRegister<Register>();
6153 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6154 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6155 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6156 generate_null_check = true;
6157 break;
6158 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006159 case HLoadClass::LoadKind::kJitTableAddress: {
6160 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6161 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006162 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006164 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 break;
6166 }
Vladimir Marko41559982017-01-06 14:04:23 +00006167 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006168 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006169 LOG(FATAL) << "UNREACHABLE";
6170 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006172
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6174 DCHECK(cls->CanCallRuntime());
6175 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6176 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6177 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006178
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 if (generate_null_check) {
6180 __ testl(out, out);
6181 __ j(kEqual, slow_path->GetEntryLabel());
6182 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006183
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006184 if (cls->MustGenerateClinitCheck()) {
6185 GenerateClassInitializationCheck(slow_path, out);
6186 } else {
6187 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006188 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006189 }
6190}
6191
6192void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6193 LocationSummary* locations =
6194 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6195 locations->SetInAt(0, Location::RequiresRegister());
6196 if (check->HasUses()) {
6197 locations->SetOut(Location::SameAsFirstInput());
6198 }
6199}
6200
6201void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006202 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006203 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006204 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006206 GenerateClassInitializationCheck(slow_path,
6207 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006208}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006209
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006210void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006211 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6213 Immediate(mirror::Class::kStatusInitialized));
6214 __ j(kLess, slow_path->GetEntryLabel());
6215 __ Bind(slow_path->GetExitLabel());
6216 // No need for memory fence, thanks to the X86 memory model.
6217}
6218
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6220 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006222 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006223 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006224 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006226 case HLoadString::LoadKind::kJitTableAddress:
6227 DCHECK(Runtime::Current()->UseJitCompilation());
6228 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006229 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006230 case HLoadString::LoadKind::kDexCacheViaMethod:
6231 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006232 }
6233 return desired_string_load_kind;
6234}
6235
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006236void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006237 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006238 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006239 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006240 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006241 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 locations->SetInAt(0, Location::RequiresRegister());
6243 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006244 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6245 locations->SetOut(Location::RegisterLocation(EAX));
6246 } else {
6247 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006248 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6249 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006250 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006251 RegisterSet caller_saves = RegisterSet::Empty();
6252 InvokeRuntimeCallingConvention calling_convention;
6253 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6254 locations->SetCustomSlowPathCallerSaves(caller_saves);
6255 } else {
6256 // For non-Baker read barrier we have a temp-clobbering call.
6257 }
6258 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006259 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006260}
6261
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006262Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006263 dex::StringIndex dex_index,
6264 Handle<mirror::String> handle) {
6265 jit_string_roots_.Overwrite(
6266 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006267 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006268 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006269 PatchInfo<Label>* info = &jit_string_patches_.back();
6270 return &info->label;
6271}
6272
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006273// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6274// move.
6275void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006276 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277 Location out_loc = locations->Out();
6278 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006280 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006281 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006282 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006283 Register method_address = locations->InAt(0).AsRegister<Register>();
6284 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006285 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 return; // No dex cache slow path.
6287 }
6288 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006289 uint32_t address = dchecked_integral_cast<uint32_t>(
6290 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6291 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006292 __ movl(out, Immediate(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 return; // No dex cache slow path.
6294 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006295 case HLoadString::LoadKind::kBssEntry: {
6296 Register method_address = locations->InAt(0).AsRegister<Register>();
6297 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6298 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006299 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006300 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006301 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6302 codegen_->AddSlowPath(slow_path);
6303 __ testl(out, out);
6304 __ j(kEqual, slow_path->GetEntryLabel());
6305 __ Bind(slow_path->GetExitLabel());
6306 return;
6307 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006308 case HLoadString::LoadKind::kJitTableAddress: {
6309 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6310 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006311 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006312 // /* GcRoot<mirror::String> */ out = *address
6313 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6314 return;
6315 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006316 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006317 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006318 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006320 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006321 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006322 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006323 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006324 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6325 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006326}
6327
David Brazdilcb1c0552015-08-04 16:22:25 +01006328static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006329 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006330}
6331
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006332void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6333 LocationSummary* locations =
6334 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6335 locations->SetOut(Location::RequiresRegister());
6336}
6337
6338void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006339 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6340}
6341
6342void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6343 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6344}
6345
6346void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6347 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006348}
6349
6350void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6351 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006352 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006353 InvokeRuntimeCallingConvention calling_convention;
6354 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6355}
6356
6357void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006358 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006359 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006360}
6361
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006362// Temp is used for read barrier.
6363static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6364 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006365 !kUseBakerReadBarrier &&
6366 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006367 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006368 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6369 return 1;
6370 }
6371 return 0;
6372}
6373
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006374// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006375// interface pointer, one for loading the current interface.
6376// The other checks have one temp for loading the object's class.
6377static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6378 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6379 return 2;
6380 }
6381 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006382}
6383
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006384void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006385 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006386 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006387 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 case TypeCheckKind::kExactCheck:
6390 case TypeCheckKind::kAbstractClassCheck:
6391 case TypeCheckKind::kClassHierarchyCheck:
6392 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006393 call_kind =
6394 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006395 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 break;
6397 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 case TypeCheckKind::kUnresolvedCheck:
6399 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 call_kind = LocationSummary::kCallOnSlowPath;
6401 break;
6402 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006404 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006405 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006406 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006407 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006408 locations->SetInAt(0, Location::RequiresRegister());
6409 locations->SetInAt(1, Location::Any());
6410 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6411 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006412 // When read barriers are enabled, we need a temporary register for some cases.
6413 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006414}
6415
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006416void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006417 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006418 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419 Location obj_loc = locations->InAt(0);
6420 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006421 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 Location out_loc = locations->Out();
6423 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006424 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6425 DCHECK_LE(num_temps, 1u);
6426 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006427 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6429 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6430 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006431 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006433
6434 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006435 // Avoid null check if we know obj is not null.
6436 if (instruction->MustDoNullCheck()) {
6437 __ testl(obj, obj);
6438 __ j(kEqual, &zero);
6439 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440
Roland Levillain7c1559a2015-12-15 10:55:36 +00006441 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006443 // /* HeapReference<Class> */ out = obj->klass_
6444 GenerateReferenceLoadTwoRegisters(instruction,
6445 out_loc,
6446 obj_loc,
6447 class_offset,
6448 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006449 if (cls.IsRegister()) {
6450 __ cmpl(out, cls.AsRegister<Register>());
6451 } else {
6452 DCHECK(cls.IsStackSlot()) << cls;
6453 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6454 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006455
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 // Classes must be equal for the instanceof to succeed.
6457 __ j(kNotEqual, &zero);
6458 __ movl(out, Immediate(1));
6459 __ jmp(&done);
6460 break;
6461 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006462
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006464 // /* HeapReference<Class> */ out = obj->klass_
6465 GenerateReferenceLoadTwoRegisters(instruction,
6466 out_loc,
6467 obj_loc,
6468 class_offset,
6469 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 // If the class is abstract, we eagerly fetch the super class of the
6471 // object to avoid doing a comparison we know will fail.
6472 NearLabel loop;
6473 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006475 GenerateReferenceLoadOneRegister(instruction,
6476 out_loc,
6477 super_offset,
6478 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 __ testl(out, out);
6481 // If `out` is null, we use it for the result, and jump to `done`.
6482 __ j(kEqual, &done);
6483 if (cls.IsRegister()) {
6484 __ cmpl(out, cls.AsRegister<Register>());
6485 } else {
6486 DCHECK(cls.IsStackSlot()) << cls;
6487 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6488 }
6489 __ j(kNotEqual, &loop);
6490 __ movl(out, Immediate(1));
6491 if (zero.IsLinked()) {
6492 __ jmp(&done);
6493 }
6494 break;
6495 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006498 // /* HeapReference<Class> */ out = obj->klass_
6499 GenerateReferenceLoadTwoRegisters(instruction,
6500 out_loc,
6501 obj_loc,
6502 class_offset,
6503 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 // Walk over the class hierarchy to find a match.
6505 NearLabel loop, success;
6506 __ Bind(&loop);
6507 if (cls.IsRegister()) {
6508 __ cmpl(out, cls.AsRegister<Register>());
6509 } else {
6510 DCHECK(cls.IsStackSlot()) << cls;
6511 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6512 }
6513 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006514 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006515 GenerateReferenceLoadOneRegister(instruction,
6516 out_loc,
6517 super_offset,
6518 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006519 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006520 __ testl(out, out);
6521 __ j(kNotEqual, &loop);
6522 // If `out` is null, we use it for the result, and jump to `done`.
6523 __ jmp(&done);
6524 __ Bind(&success);
6525 __ movl(out, Immediate(1));
6526 if (zero.IsLinked()) {
6527 __ jmp(&done);
6528 }
6529 break;
6530 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006533 // /* HeapReference<Class> */ out = obj->klass_
6534 GenerateReferenceLoadTwoRegisters(instruction,
6535 out_loc,
6536 obj_loc,
6537 class_offset,
6538 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006539 // Do an exact check.
6540 NearLabel exact_check;
6541 if (cls.IsRegister()) {
6542 __ cmpl(out, cls.AsRegister<Register>());
6543 } else {
6544 DCHECK(cls.IsStackSlot()) << cls;
6545 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6546 }
6547 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006549 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006550 GenerateReferenceLoadOneRegister(instruction,
6551 out_loc,
6552 component_offset,
6553 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006554 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006555 __ testl(out, out);
6556 // If `out` is null, we use it for the result, and jump to `done`.
6557 __ j(kEqual, &done);
6558 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6559 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006560 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 __ movl(out, Immediate(1));
6562 __ jmp(&done);
6563 break;
6564 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006565
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006567 // No read barrier since the slow path will retry upon failure.
6568 // /* HeapReference<Class> */ out = obj->klass_
6569 GenerateReferenceLoadTwoRegisters(instruction,
6570 out_loc,
6571 obj_loc,
6572 class_offset,
6573 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006574 if (cls.IsRegister()) {
6575 __ cmpl(out, cls.AsRegister<Register>());
6576 } else {
6577 DCHECK(cls.IsStackSlot()) << cls;
6578 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6579 }
6580 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6582 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006583 codegen_->AddSlowPath(slow_path);
6584 __ j(kNotEqual, slow_path->GetEntryLabel());
6585 __ movl(out, Immediate(1));
6586 if (zero.IsLinked()) {
6587 __ jmp(&done);
6588 }
6589 break;
6590 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591
Calin Juravle98893e12015-10-02 21:05:03 +01006592 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006593 case TypeCheckKind::kInterfaceCheck: {
6594 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006595 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006596 // cases.
6597 //
6598 // We cannot directly call the InstanceofNonTrivial runtime
6599 // entry point without resorting to a type checking slow path
6600 // here (i.e. by calling InvokeRuntime directly), as it would
6601 // require to assign fixed registers for the inputs of this
6602 // HInstanceOf instruction (following the runtime calling
6603 // convention), which might be cluttered by the potential first
6604 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006605 //
6606 // TODO: Introduce a new runtime entry point taking the object
6607 // to test (instead of its class) as argument, and let it deal
6608 // with the read barrier issues. This will let us refactor this
6609 // case of the `switch` code as it was previously (with a direct
6610 // call to the runtime not using a type checking slow path).
6611 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612 DCHECK(locations->OnlyCallsOnSlowPath());
6613 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6614 /* is_fatal */ false);
6615 codegen_->AddSlowPath(slow_path);
6616 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006617 if (zero.IsLinked()) {
6618 __ jmp(&done);
6619 }
6620 break;
6621 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006622 }
6623
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006624 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006625 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006626 __ xorl(out, out);
6627 }
6628
6629 if (done.IsLinked()) {
6630 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006631 }
6632
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006633 if (slow_path != nullptr) {
6634 __ Bind(slow_path->GetExitLabel());
6635 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006636}
6637
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006638static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6639 switch (type_check_kind) {
6640 case TypeCheckKind::kExactCheck:
6641 case TypeCheckKind::kAbstractClassCheck:
6642 case TypeCheckKind::kClassHierarchyCheck:
6643 case TypeCheckKind::kArrayObjectCheck:
6644 return !throws_into_catch && !kEmitCompilerReadBarrier;
6645 case TypeCheckKind::kInterfaceCheck:
6646 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6647 case TypeCheckKind::kArrayCheck:
6648 case TypeCheckKind::kUnresolvedCheck:
6649 return false;
6650 }
6651 LOG(FATAL) << "Unreachable";
6652 UNREACHABLE();
6653}
6654
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006655void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006657 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006658 LocationSummary::CallKind call_kind =
6659 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6660 ? LocationSummary::kNoCall
6661 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6663 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006664 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6665 // Require a register for the interface check since there is a loop that compares the class to
6666 // a memory address.
6667 locations->SetInAt(1, Location::RequiresRegister());
6668 } else {
6669 locations->SetInAt(1, Location::Any());
6670 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006671 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6672 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006673 // When read barriers are enabled, we need an additional temporary register for some cases.
6674 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6675}
6676
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006677void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006678 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006679 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 Location obj_loc = locations->InAt(0);
6681 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006682 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 Location temp_loc = locations->GetTemp(0);
6684 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006685 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6686 DCHECK_GE(num_temps, 1u);
6687 DCHECK_LE(num_temps, 2u);
6688 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6689 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6690 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6691 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6692 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6693 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6694 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6695 const uint32_t object_array_data_offset =
6696 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006697
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006698 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6699 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6700 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006702 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6703
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 SlowPathCode* type_check_slow_path =
6705 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6706 is_type_check_slow_path_fatal);
6707 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006708
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006710 // Avoid null check if we know obj is not null.
6711 if (instruction->MustDoNullCheck()) {
6712 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006713 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006714 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006715
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006717 case TypeCheckKind::kExactCheck:
6718 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006719 // /* HeapReference<Class> */ temp = obj->klass_
6720 GenerateReferenceLoadTwoRegisters(instruction,
6721 temp_loc,
6722 obj_loc,
6723 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006724 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006725
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 if (cls.IsRegister()) {
6727 __ cmpl(temp, cls.AsRegister<Register>());
6728 } else {
6729 DCHECK(cls.IsStackSlot()) << cls;
6730 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6731 }
6732 // Jump to slow path for throwing the exception or doing a
6733 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006734 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006735 break;
6736 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006739 // /* HeapReference<Class> */ temp = obj->klass_
6740 GenerateReferenceLoadTwoRegisters(instruction,
6741 temp_loc,
6742 obj_loc,
6743 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006744 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 // If the class is abstract, we eagerly fetch the super class of the
6747 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006748 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006751 GenerateReferenceLoadOneRegister(instruction,
6752 temp_loc,
6753 super_offset,
6754 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006755 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006756
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006757 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6758 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006759 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006760 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006762 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006763 if (cls.IsRegister()) {
6764 __ cmpl(temp, cls.AsRegister<Register>());
6765 } else {
6766 DCHECK(cls.IsStackSlot()) << cls;
6767 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6768 }
6769 __ j(kNotEqual, &loop);
6770 break;
6771 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006773 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006774 // /* HeapReference<Class> */ temp = obj->klass_
6775 GenerateReferenceLoadTwoRegisters(instruction,
6776 temp_loc,
6777 obj_loc,
6778 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006779 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006780
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006781 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006782 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006783 __ Bind(&loop);
6784 if (cls.IsRegister()) {
6785 __ cmpl(temp, cls.AsRegister<Register>());
6786 } else {
6787 DCHECK(cls.IsStackSlot()) << cls;
6788 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6789 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006790 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006791
Roland Levillain0d5a2812015-11-13 10:07:31 +00006792 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006793 GenerateReferenceLoadOneRegister(instruction,
6794 temp_loc,
6795 super_offset,
6796 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006797 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006798
6799 // If the class reference currently in `temp` is not null, jump
6800 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006801 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006802 __ j(kNotZero, &loop);
6803 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006805 break;
6806 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006807
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006808 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006809 // /* HeapReference<Class> */ temp = obj->klass_
6810 GenerateReferenceLoadTwoRegisters(instruction,
6811 temp_loc,
6812 obj_loc,
6813 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006814 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006815
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006816 // Do an exact check.
6817 if (cls.IsRegister()) {
6818 __ cmpl(temp, cls.AsRegister<Register>());
6819 } else {
6820 DCHECK(cls.IsStackSlot()) << cls;
6821 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6822 }
6823 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006824
6825 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006826 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006827 GenerateReferenceLoadOneRegister(instruction,
6828 temp_loc,
6829 component_offset,
6830 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006831 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006832
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006833 // If the component type is null (i.e. the object not an array), jump to the slow path to
6834 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006835 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006836 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006838 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006839 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006840 break;
6841 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006842
Calin Juravle98893e12015-10-02 21:05:03 +01006843 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006844 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006845 // We cannot directly call the CheckCast runtime entry point
6846 // without resorting to a type checking slow path here (i.e. by
6847 // calling InvokeRuntime directly), as it would require to
6848 // assign fixed registers for the inputs of this HInstanceOf
6849 // instruction (following the runtime calling convention), which
6850 // might be cluttered by the potential first read barrier
6851 // emission at the beginning of this method.
6852 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006853 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006854
6855 case TypeCheckKind::kInterfaceCheck: {
6856 // Fast path for the interface check. Since we compare with a memory location in the inner
6857 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6858 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006859 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006860 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006861 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6862 // doing this.
6863 // /* HeapReference<Class> */ temp = obj->klass_
6864 GenerateReferenceLoadTwoRegisters(instruction,
6865 temp_loc,
6866 obj_loc,
6867 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006868 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006869
6870 // /* HeapReference<Class> */ temp = temp->iftable_
6871 GenerateReferenceLoadTwoRegisters(instruction,
6872 temp_loc,
6873 temp_loc,
6874 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006875 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006876 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006877 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006878 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006879 NearLabel start_loop;
6880 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006881 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006882 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006883 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6884 // Go to next interface if the classes do not match.
6885 __ cmpl(cls.AsRegister<Register>(),
6886 CodeGeneratorX86::ArrayAddress(temp,
6887 maybe_temp2_loc,
6888 TIMES_4,
6889 object_array_data_offset));
6890 __ j(kNotEqual, &start_loop);
6891 } else {
6892 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006893 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006894 break;
6895 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006896 }
6897 __ Bind(&done);
6898
Roland Levillain0d5a2812015-11-13 10:07:31 +00006899 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006900}
6901
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006902void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6903 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006905 InvokeRuntimeCallingConvention calling_convention;
6906 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6907}
6908
6909void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006910 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6911 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006912 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006913 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006914 if (instruction->IsEnter()) {
6915 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6916 } else {
6917 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6918 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006919}
6920
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006921void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6922void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6923void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6924
6925void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6926 LocationSummary* locations =
6927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6928 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6929 || instruction->GetResultType() == Primitive::kPrimLong);
6930 locations->SetInAt(0, Location::RequiresRegister());
6931 locations->SetInAt(1, Location::Any());
6932 locations->SetOut(Location::SameAsFirstInput());
6933}
6934
6935void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6936 HandleBitwiseOperation(instruction);
6937}
6938
6939void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6940 HandleBitwiseOperation(instruction);
6941}
6942
6943void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6944 HandleBitwiseOperation(instruction);
6945}
6946
6947void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6948 LocationSummary* locations = instruction->GetLocations();
6949 Location first = locations->InAt(0);
6950 Location second = locations->InAt(1);
6951 DCHECK(first.Equals(locations->Out()));
6952
6953 if (instruction->GetResultType() == Primitive::kPrimInt) {
6954 if (second.IsRegister()) {
6955 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006956 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006957 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006958 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006959 } else {
6960 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006961 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006962 }
6963 } else if (second.IsConstant()) {
6964 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006965 __ andl(first.AsRegister<Register>(),
6966 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006967 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006968 __ orl(first.AsRegister<Register>(),
6969 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006970 } else {
6971 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006972 __ xorl(first.AsRegister<Register>(),
6973 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 }
6975 } else {
6976 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006977 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006978 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006979 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 } else {
6981 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006982 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006983 }
6984 }
6985 } else {
6986 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6987 if (second.IsRegisterPair()) {
6988 if (instruction->IsAnd()) {
6989 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6990 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6991 } else if (instruction->IsOr()) {
6992 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6993 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6994 } else {
6995 DCHECK(instruction->IsXor());
6996 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6997 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6998 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006999 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007000 if (instruction->IsAnd()) {
7001 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7002 __ andl(first.AsRegisterPairHigh<Register>(),
7003 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7004 } else if (instruction->IsOr()) {
7005 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7006 __ orl(first.AsRegisterPairHigh<Register>(),
7007 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7008 } else {
7009 DCHECK(instruction->IsXor());
7010 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7011 __ xorl(first.AsRegisterPairHigh<Register>(),
7012 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7013 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007014 } else {
7015 DCHECK(second.IsConstant()) << second;
7016 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007017 int32_t low_value = Low32Bits(value);
7018 int32_t high_value = High32Bits(value);
7019 Immediate low(low_value);
7020 Immediate high(high_value);
7021 Register first_low = first.AsRegisterPairLow<Register>();
7022 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007023 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007024 if (low_value == 0) {
7025 __ xorl(first_low, first_low);
7026 } else if (low_value != -1) {
7027 __ andl(first_low, low);
7028 }
7029 if (high_value == 0) {
7030 __ xorl(first_high, first_high);
7031 } else if (high_value != -1) {
7032 __ andl(first_high, high);
7033 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007034 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007035 if (low_value != 0) {
7036 __ orl(first_low, low);
7037 }
7038 if (high_value != 0) {
7039 __ orl(first_high, high);
7040 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007041 } else {
7042 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007043 if (low_value != 0) {
7044 __ xorl(first_low, low);
7045 }
7046 if (high_value != 0) {
7047 __ xorl(first_high, high);
7048 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007049 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007050 }
7051 }
7052}
7053
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007054void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7055 HInstruction* instruction,
7056 Location out,
7057 uint32_t offset,
7058 Location maybe_temp,
7059 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007060 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007061 if (read_barrier_option == kWithReadBarrier) {
7062 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007063 if (kUseBakerReadBarrier) {
7064 // Load with fast path based Baker's read barrier.
7065 // /* HeapReference<Object> */ out = *(out + offset)
7066 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007067 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007068 } else {
7069 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007070 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007071 // in the following move operation, as we will need it for the
7072 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007073 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007074 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007075 // /* HeapReference<Object> */ out = *(out + offset)
7076 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007077 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 }
7079 } else {
7080 // Plain load with no read barrier.
7081 // /* HeapReference<Object> */ out = *(out + offset)
7082 __ movl(out_reg, Address(out_reg, offset));
7083 __ MaybeUnpoisonHeapReference(out_reg);
7084 }
7085}
7086
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007087void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7088 HInstruction* instruction,
7089 Location out,
7090 Location obj,
7091 uint32_t offset,
7092 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 Register out_reg = out.AsRegister<Register>();
7094 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007095 if (read_barrier_option == kWithReadBarrier) {
7096 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007097 if (kUseBakerReadBarrier) {
7098 // Load with fast path based Baker's read barrier.
7099 // /* HeapReference<Object> */ out = *(obj + offset)
7100 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007101 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102 } else {
7103 // Load with slow path based read barrier.
7104 // /* HeapReference<Object> */ out = *(obj + offset)
7105 __ movl(out_reg, Address(obj_reg, offset));
7106 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7107 }
7108 } else {
7109 // Plain load with no read barrier.
7110 // /* HeapReference<Object> */ out = *(obj + offset)
7111 __ movl(out_reg, Address(obj_reg, offset));
7112 __ MaybeUnpoisonHeapReference(out_reg);
7113 }
7114}
7115
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007116void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7117 HInstruction* instruction,
7118 Location root,
7119 const Address& address,
7120 Label* fixup_label,
7121 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007122 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007123 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007124 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007125 if (kUseBakerReadBarrier) {
7126 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7127 // Baker's read barrier are used:
7128 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007129 // root = obj.field;
7130 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7131 // if (temp != null) {
7132 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007133 // }
7134
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007135 // /* GcRoot<mirror::Object> */ root = *address
7136 __ movl(root_reg, address);
7137 if (fixup_label != nullptr) {
7138 __ Bind(fixup_label);
7139 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 static_assert(
7141 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7142 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7143 "have different sizes.");
7144 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7145 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7146 "have different sizes.");
7147
Vladimir Marko953437b2016-08-24 08:30:46 +00007148 // Slow path marking the GC root `root`.
7149 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007150 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151 codegen_->AddSlowPath(slow_path);
7152
Roland Levillaind966ce72017-02-09 16:20:14 +00007153 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7154 const int32_t entry_point_offset =
7155 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
7156 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7157 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007158 __ j(kNotEqual, slow_path->GetEntryLabel());
7159 __ Bind(slow_path->GetExitLabel());
7160 } else {
7161 // GC root loaded through a slow path for read barriers other
7162 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007163 // /* GcRoot<mirror::Object>* */ root = address
7164 __ leal(root_reg, address);
7165 if (fixup_label != nullptr) {
7166 __ Bind(fixup_label);
7167 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007168 // /* mirror::Object* */ root = root->Read()
7169 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7170 }
7171 } else {
7172 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007173 // /* GcRoot<mirror::Object> */ root = *address
7174 __ movl(root_reg, address);
7175 if (fixup_label != nullptr) {
7176 __ Bind(fixup_label);
7177 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007178 // Note that GC roots are not affected by heap poisoning, thus we
7179 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180 }
7181}
7182
7183void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7184 Location ref,
7185 Register obj,
7186 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007187 bool needs_null_check) {
7188 DCHECK(kEmitCompilerReadBarrier);
7189 DCHECK(kUseBakerReadBarrier);
7190
7191 // /* HeapReference<Object> */ ref = *(obj + offset)
7192 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007193 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007194}
7195
7196void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7197 Location ref,
7198 Register obj,
7199 uint32_t data_offset,
7200 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007201 bool needs_null_check) {
7202 DCHECK(kEmitCompilerReadBarrier);
7203 DCHECK(kUseBakerReadBarrier);
7204
Roland Levillain3d312422016-06-23 13:53:42 +01007205 static_assert(
7206 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7207 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007208 // /* HeapReference<Object> */ ref =
7209 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007210 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007211 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007212}
7213
7214void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7215 Location ref,
7216 Register obj,
7217 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007218 bool needs_null_check,
7219 bool always_update_field,
7220 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007221 DCHECK(kEmitCompilerReadBarrier);
7222 DCHECK(kUseBakerReadBarrier);
7223
7224 // In slow path based read barriers, the read barrier call is
7225 // inserted after the original load. However, in fast path based
7226 // Baker's read barriers, we need to perform the load of
7227 // mirror::Object::monitor_ *before* the original reference load.
7228 // This load-load ordering is required by the read barrier.
7229 // The fast path/slow path (for Baker's algorithm) should look like:
7230 //
7231 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7232 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7233 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007234 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007235 // if (is_gray) {
7236 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7237 // }
7238 //
7239 // Note: the original implementation in ReadBarrier::Barrier is
7240 // slightly more complex as:
7241 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007242 // the high-bits of rb_state, which are expected to be all zeroes
7243 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7244 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007245 // - it performs additional checks that we do not do here for
7246 // performance reasons.
7247
7248 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007249 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7250
Vladimir Marko953437b2016-08-24 08:30:46 +00007251 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007252 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7253 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007254 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7255 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7256 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7257
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007258 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007259 // ref = ReadBarrier::Mark(ref);
7260 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7261 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007262 if (needs_null_check) {
7263 MaybeRecordImplicitNullCheck(instruction);
7264 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007265
7266 // Load fence to prevent load-load reordering.
7267 // Note that this is a no-op, thanks to the x86 memory model.
7268 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7269
7270 // The actual reference load.
7271 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007272 __ movl(ref_reg, src); // Flags are unaffected.
7273
7274 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7275 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007276 SlowPathCode* slow_path;
7277 if (always_update_field) {
7278 DCHECK(temp != nullptr);
7279 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7280 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7281 } else {
7282 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7283 instruction, ref, /* unpoison_ref_before_marking */ true);
7284 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007285 AddSlowPath(slow_path);
7286
7287 // We have done the "if" of the gray bit check above, now branch based on the flags.
7288 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007289
7290 // Object* ref = ref_addr->AsMirrorPtr()
7291 __ MaybeUnpoisonHeapReference(ref_reg);
7292
Roland Levillain7c1559a2015-12-15 10:55:36 +00007293 __ Bind(slow_path->GetExitLabel());
7294}
7295
7296void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7297 Location out,
7298 Location ref,
7299 Location obj,
7300 uint32_t offset,
7301 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007302 DCHECK(kEmitCompilerReadBarrier);
7303
Roland Levillain7c1559a2015-12-15 10:55:36 +00007304 // Insert a slow path based read barrier *after* the reference load.
7305 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007306 // If heap poisoning is enabled, the unpoisoning of the loaded
7307 // reference will be carried out by the runtime within the slow
7308 // path.
7309 //
7310 // Note that `ref` currently does not get unpoisoned (when heap
7311 // poisoning is enabled), which is alright as the `ref` argument is
7312 // not used by the artReadBarrierSlow entry point.
7313 //
7314 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7315 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7316 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7317 AddSlowPath(slow_path);
7318
Roland Levillain0d5a2812015-11-13 10:07:31 +00007319 __ jmp(slow_path->GetEntryLabel());
7320 __ Bind(slow_path->GetExitLabel());
7321}
7322
Roland Levillain7c1559a2015-12-15 10:55:36 +00007323void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7324 Location out,
7325 Location ref,
7326 Location obj,
7327 uint32_t offset,
7328 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007330 // Baker's read barriers shall be handled by the fast path
7331 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7332 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007333 // If heap poisoning is enabled, unpoisoning will be taken care of
7334 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007335 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007336 } else if (kPoisonHeapReferences) {
7337 __ UnpoisonHeapReference(out.AsRegister<Register>());
7338 }
7339}
7340
Roland Levillain7c1559a2015-12-15 10:55:36 +00007341void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7342 Location out,
7343 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344 DCHECK(kEmitCompilerReadBarrier);
7345
Roland Levillain7c1559a2015-12-15 10:55:36 +00007346 // Insert a slow path based read barrier *after* the GC root load.
7347 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348 // Note that GC roots are not affected by heap poisoning, so we do
7349 // not need to do anything special for this here.
7350 SlowPathCode* slow_path =
7351 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7352 AddSlowPath(slow_path);
7353
Roland Levillain0d5a2812015-11-13 10:07:31 +00007354 __ jmp(slow_path->GetEntryLabel());
7355 __ Bind(slow_path->GetExitLabel());
7356}
7357
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007358void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007359 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007360 LOG(FATAL) << "Unreachable";
7361}
7362
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007363void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007364 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007365 LOG(FATAL) << "Unreachable";
7366}
7367
Mark Mendellfe57faa2015-09-18 09:26:15 -04007368// Simple implementation of packed switch - generate cascaded compare/jumps.
7369void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7370 LocationSummary* locations =
7371 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7372 locations->SetInAt(0, Location::RequiresRegister());
7373}
7374
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007375void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7376 int32_t lower_bound,
7377 uint32_t num_entries,
7378 HBasicBlock* switch_block,
7379 HBasicBlock* default_block) {
7380 // Figure out the correct compare values and jump conditions.
7381 // Handle the first compare/branch as a special case because it might
7382 // jump to the default case.
7383 DCHECK_GT(num_entries, 2u);
7384 Condition first_condition;
7385 uint32_t index;
7386 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7387 if (lower_bound != 0) {
7388 first_condition = kLess;
7389 __ cmpl(value_reg, Immediate(lower_bound));
7390 __ j(first_condition, codegen_->GetLabelOf(default_block));
7391 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007392
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007393 index = 1;
7394 } else {
7395 // Handle all the compare/jumps below.
7396 first_condition = kBelow;
7397 index = 0;
7398 }
7399
7400 // Handle the rest of the compare/jumps.
7401 for (; index + 1 < num_entries; index += 2) {
7402 int32_t compare_to_value = lower_bound + index + 1;
7403 __ cmpl(value_reg, Immediate(compare_to_value));
7404 // Jump to successors[index] if value < case_value[index].
7405 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7406 // Jump to successors[index + 1] if value == case_value[index + 1].
7407 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7408 }
7409
7410 if (index != num_entries) {
7411 // There are an odd number of entries. Handle the last one.
7412 DCHECK_EQ(index + 1, num_entries);
7413 __ cmpl(value_reg, Immediate(lower_bound + index));
7414 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007415 }
7416
7417 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007418 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7419 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007420 }
7421}
7422
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007423void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7424 int32_t lower_bound = switch_instr->GetStartValue();
7425 uint32_t num_entries = switch_instr->GetNumEntries();
7426 LocationSummary* locations = switch_instr->GetLocations();
7427 Register value_reg = locations->InAt(0).AsRegister<Register>();
7428
7429 GenPackedSwitchWithCompares(value_reg,
7430 lower_bound,
7431 num_entries,
7432 switch_instr->GetBlock(),
7433 switch_instr->GetDefaultBlock());
7434}
7435
Mark Mendell805b3b52015-09-18 14:10:29 -04007436void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7437 LocationSummary* locations =
7438 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7439 locations->SetInAt(0, Location::RequiresRegister());
7440
7441 // Constant area pointer.
7442 locations->SetInAt(1, Location::RequiresRegister());
7443
7444 // And the temporary we need.
7445 locations->AddTemp(Location::RequiresRegister());
7446}
7447
7448void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7449 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007450 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007451 LocationSummary* locations = switch_instr->GetLocations();
7452 Register value_reg = locations->InAt(0).AsRegister<Register>();
7453 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7454
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007455 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7456 GenPackedSwitchWithCompares(value_reg,
7457 lower_bound,
7458 num_entries,
7459 switch_instr->GetBlock(),
7460 default_block);
7461 return;
7462 }
7463
Mark Mendell805b3b52015-09-18 14:10:29 -04007464 // Optimizing has a jump area.
7465 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7466 Register constant_area = locations->InAt(1).AsRegister<Register>();
7467
7468 // Remove the bias, if needed.
7469 if (lower_bound != 0) {
7470 __ leal(temp_reg, Address(value_reg, -lower_bound));
7471 value_reg = temp_reg;
7472 }
7473
7474 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007475 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007476 __ cmpl(value_reg, Immediate(num_entries - 1));
7477 __ j(kAbove, codegen_->GetLabelOf(default_block));
7478
7479 // We are in the range of the table.
7480 // Load (target-constant_area) from the jump table, indexing by the value.
7481 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7482
7483 // Compute the actual target address by adding in constant_area.
7484 __ addl(temp_reg, constant_area);
7485
7486 // And jump.
7487 __ jmp(temp_reg);
7488}
7489
Mark Mendell0616ae02015-04-17 12:49:27 -04007490void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7491 HX86ComputeBaseMethodAddress* insn) {
7492 LocationSummary* locations =
7493 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7494 locations->SetOut(Location::RequiresRegister());
7495}
7496
7497void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7498 HX86ComputeBaseMethodAddress* insn) {
7499 LocationSummary* locations = insn->GetLocations();
7500 Register reg = locations->Out().AsRegister<Register>();
7501
7502 // Generate call to next instruction.
7503 Label next_instruction;
7504 __ call(&next_instruction);
7505 __ Bind(&next_instruction);
7506
7507 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007508 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007509
7510 // Grab the return address off the stack.
7511 __ popl(reg);
7512}
7513
7514void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7515 HX86LoadFromConstantTable* insn) {
7516 LocationSummary* locations =
7517 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7518
7519 locations->SetInAt(0, Location::RequiresRegister());
7520 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7521
7522 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007523 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007524 return;
7525 }
7526
7527 switch (insn->GetType()) {
7528 case Primitive::kPrimFloat:
7529 case Primitive::kPrimDouble:
7530 locations->SetOut(Location::RequiresFpuRegister());
7531 break;
7532
7533 case Primitive::kPrimInt:
7534 locations->SetOut(Location::RequiresRegister());
7535 break;
7536
7537 default:
7538 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7539 }
7540}
7541
7542void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007543 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007544 return;
7545 }
7546
7547 LocationSummary* locations = insn->GetLocations();
7548 Location out = locations->Out();
7549 Register const_area = locations->InAt(0).AsRegister<Register>();
7550 HConstant *value = insn->GetConstant();
7551
7552 switch (insn->GetType()) {
7553 case Primitive::kPrimFloat:
7554 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007555 codegen_->LiteralFloatAddress(
7556 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007557 break;
7558
7559 case Primitive::kPrimDouble:
7560 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007561 codegen_->LiteralDoubleAddress(
7562 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007563 break;
7564
7565 case Primitive::kPrimInt:
7566 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007567 codegen_->LiteralInt32Address(
7568 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007569 break;
7570
7571 default:
7572 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7573 }
7574}
7575
Mark Mendell0616ae02015-04-17 12:49:27 -04007576/**
7577 * Class to handle late fixup of offsets into constant area.
7578 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007579class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007580 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007581 RIPFixup(CodeGeneratorX86& codegen,
7582 HX86ComputeBaseMethodAddress* base_method_address,
7583 size_t offset)
7584 : codegen_(&codegen),
7585 base_method_address_(base_method_address),
7586 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007587
7588 protected:
7589 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7590
7591 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007592 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007593
7594 private:
7595 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7596 // Patch the correct offset for the instruction. The place to patch is the
7597 // last 4 bytes of the instruction.
7598 // The value to patch is the distance from the offset in the constant area
7599 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007600 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007601 int32_t relative_position =
7602 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007603
7604 // Patch in the right value.
7605 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7606 }
7607
Mark Mendell0616ae02015-04-17 12:49:27 -04007608 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007609 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007610};
7611
Mark Mendell805b3b52015-09-18 14:10:29 -04007612/**
7613 * Class to handle late fixup of offsets to a jump table that will be created in the
7614 * constant area.
7615 */
7616class JumpTableRIPFixup : public RIPFixup {
7617 public:
7618 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007619 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7620 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007621
7622 void CreateJumpTable() {
7623 X86Assembler* assembler = codegen_->GetAssembler();
7624
7625 // Ensure that the reference to the jump table has the correct offset.
7626 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7627 SetOffset(offset_in_constant_table);
7628
7629 // The label values in the jump table are computed relative to the
7630 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007631 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007632
7633 // Populate the jump table with the correct values for the jump table.
7634 int32_t num_entries = switch_instr_->GetNumEntries();
7635 HBasicBlock* block = switch_instr_->GetBlock();
7636 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7637 // The value that we want is the target offset - the position of the table.
7638 for (int32_t i = 0; i < num_entries; i++) {
7639 HBasicBlock* b = successors[i];
7640 Label* l = codegen_->GetLabelOf(b);
7641 DCHECK(l->IsBound());
7642 int32_t offset_to_block = l->Position() - relative_offset;
7643 assembler->AppendInt32(offset_to_block);
7644 }
7645 }
7646
7647 private:
7648 const HX86PackedSwitch* switch_instr_;
7649};
7650
7651void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7652 // Generate the constant area if needed.
7653 X86Assembler* assembler = GetAssembler();
7654 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7655 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7656 // byte values.
7657 assembler->Align(4, 0);
7658 constant_area_start_ = assembler->CodeSize();
7659
7660 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007661 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007662 jump_table->CreateJumpTable();
7663 }
7664
7665 // And now add the constant area to the generated code.
7666 assembler->AddConstantArea();
7667 }
7668
7669 // And finish up.
7670 CodeGenerator::Finalize(allocator);
7671}
7672
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007673Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7674 HX86ComputeBaseMethodAddress* method_base,
7675 Register reg) {
7676 AssemblerFixup* fixup =
7677 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007678 return Address(reg, kDummy32BitOffset, fixup);
7679}
7680
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007681Address CodeGeneratorX86::LiteralFloatAddress(float v,
7682 HX86ComputeBaseMethodAddress* method_base,
7683 Register reg) {
7684 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007685 return Address(reg, kDummy32BitOffset, fixup);
7686}
7687
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007688Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7689 HX86ComputeBaseMethodAddress* method_base,
7690 Register reg) {
7691 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007692 return Address(reg, kDummy32BitOffset, fixup);
7693}
7694
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007695Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7696 HX86ComputeBaseMethodAddress* method_base,
7697 Register reg) {
7698 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007699 return Address(reg, kDummy32BitOffset, fixup);
7700}
7701
Aart Bika19616e2016-02-01 18:57:58 -08007702void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7703 if (value == 0) {
7704 __ xorl(dest, dest);
7705 } else {
7706 __ movl(dest, Immediate(value));
7707 }
7708}
7709
7710void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7711 if (value == 0) {
7712 __ testl(dest, dest);
7713 } else {
7714 __ cmpl(dest, Immediate(value));
7715 }
7716}
7717
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007718void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7719 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007720 GenerateIntCompare(lhs_reg, rhs);
7721}
7722
7723void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007724 if (rhs.IsConstant()) {
7725 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007726 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007727 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007728 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007729 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007730 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007731 }
7732}
7733
7734Address CodeGeneratorX86::ArrayAddress(Register obj,
7735 Location index,
7736 ScaleFactor scale,
7737 uint32_t data_offset) {
7738 return index.IsConstant() ?
7739 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7740 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7741}
7742
Mark Mendell805b3b52015-09-18 14:10:29 -04007743Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7744 Register reg,
7745 Register value) {
7746 // Create a fixup to be used to create and address the jump table.
7747 JumpTableRIPFixup* table_fixup =
7748 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7749
7750 // We have to populate the jump tables.
7751 fixups_to_jump_tables_.push_back(table_fixup);
7752
7753 // We want a scaled address, as we are extracting the correct offset from the table.
7754 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7755}
7756
Andreas Gampe85b62f22015-09-09 13:15:38 -07007757// TODO: target as memory.
7758void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7759 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007760 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007761 return;
7762 }
7763
7764 DCHECK_NE(type, Primitive::kPrimVoid);
7765
7766 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7767 if (target.Equals(return_loc)) {
7768 return;
7769 }
7770
7771 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7772 // with the else branch.
7773 if (type == Primitive::kPrimLong) {
7774 HParallelMove parallel_move(GetGraph()->GetArena());
7775 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7776 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7777 GetMoveResolver()->EmitNativeCode(&parallel_move);
7778 } else {
7779 // Let the parallel move resolver take care of all of this.
7780 HParallelMove parallel_move(GetGraph()->GetArena());
7781 parallel_move.AddMove(return_loc, target, type, nullptr);
7782 GetMoveResolver()->EmitNativeCode(&parallel_move);
7783 }
7784}
7785
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007786void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7787 const uint8_t* roots_data,
7788 const PatchInfo<Label>& info,
7789 uint64_t index_in_table) const {
7790 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7791 uintptr_t address =
7792 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7793 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7794 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7795 dchecked_integral_cast<uint32_t>(address);
7796}
7797
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007798void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7799 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007800 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007801 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007802 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007803 uint64_t index_in_table = it->second;
7804 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007805 }
7806
7807 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007808 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007809 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7810 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007811 uint64_t index_in_table = it->second;
7812 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007813 }
7814}
7815
Roland Levillain4d027112015-07-01 15:41:14 +01007816#undef __
7817
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007818} // namespace x86
7819} // namespace art