blob: f3ec11254838a5f7a71db0aba2e49a4503c4ebb7 [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 Marko65979462017-05-19 17:25:12 +01001035 boot_image_method_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)),
Vladimir Marko65979462017-05-19 17:25:12 +01001038 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001039 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001040 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001041 constant_area_start_(-1),
1042 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001043 method_address_offset_(std::less<uint32_t>(),
1044 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001045 // Use a fake return address register to mimic Quick.
1046 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001047}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048
David Brazdil58282f42016-01-14 12:45:10 +00001049void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001050 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001051 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052}
1053
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001054InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001055 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001056 assembler_(codegen->GetAssembler()),
1057 codegen_(codegen) {}
1058
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001059static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001060 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061}
1062
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001063void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001064 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001065 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001066 bool skip_overflow_check =
1067 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001068 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001069
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001071 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001072 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001073 }
1074
Mark Mendell5f874182015-03-04 15:42:45 -05001075 if (HasEmptyFrame()) {
1076 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001077 }
Mark Mendell5f874182015-03-04 15:42:45 -05001078
1079 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1080 Register reg = kCoreCalleeSaves[i];
1081 if (allocated_registers_.ContainsCoreRegister(reg)) {
1082 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083 __ cfi().AdjustCFAOffset(kX86WordSize);
1084 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001085 }
1086 }
1087
Mingyao Yang063fc772016-08-02 11:02:54 -07001088 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1089 // Initialize should_deoptimize flag to 0.
1090 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1091 }
1092
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001093 int adjust = GetFrameSize() - FrameEntrySpillSize();
1094 __ subl(ESP, Immediate(adjust));
1095 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001096 // Save the current method if we need it. Note that we do not
1097 // do this in HCurrentMethod, as the instruction might have been removed
1098 // in the SSA graph.
1099 if (RequiresCurrentMethod()) {
1100 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1101 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001102}
1103
1104void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001105 __ cfi().RememberState();
1106 if (!HasEmptyFrame()) {
1107 int adjust = GetFrameSize() - FrameEntrySpillSize();
1108 __ addl(ESP, Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001110
David Srbeckyc34dc932015-04-12 09:27:43 +01001111 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1112 Register reg = kCoreCalleeSaves[i];
1113 if (allocated_registers_.ContainsCoreRegister(reg)) {
1114 __ popl(reg);
1115 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1116 __ cfi().Restore(DWARFReg(reg));
1117 }
Mark Mendell5f874182015-03-04 15:42:45 -05001118 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001119 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001120 __ ret();
1121 __ cfi().RestoreState();
1122 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001123}
1124
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001125void CodeGeneratorX86::Bind(HBasicBlock* block) {
1126 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001129Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1130 switch (type) {
1131 case Primitive::kPrimBoolean:
1132 case Primitive::kPrimByte:
1133 case Primitive::kPrimChar:
1134 case Primitive::kPrimShort:
1135 case Primitive::kPrimInt:
1136 case Primitive::kPrimNot:
1137 return Location::RegisterLocation(EAX);
1138
1139 case Primitive::kPrimLong:
1140 return Location::RegisterPairLocation(EAX, EDX);
1141
1142 case Primitive::kPrimVoid:
1143 return Location::NoLocation();
1144
1145 case Primitive::kPrimDouble:
1146 case Primitive::kPrimFloat:
1147 return Location::FpuRegisterLocation(XMM0);
1148 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001149
1150 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001151}
1152
1153Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1154 return Location::RegisterLocation(kMethodRegisterArgument);
1155}
1156
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001157Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001158 switch (type) {
1159 case Primitive::kPrimBoolean:
1160 case Primitive::kPrimByte:
1161 case Primitive::kPrimChar:
1162 case Primitive::kPrimShort:
1163 case Primitive::kPrimInt:
1164 case Primitive::kPrimNot: {
1165 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001166 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001167 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001169 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001170 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001171 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001172 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001174 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 uint32_t index = gp_index_;
1176 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1180 calling_convention.GetRegisterPairAt(index));
1181 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001183 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1184 }
1185 }
1186
1187 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001188 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001189 stack_index_++;
1190 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1191 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1192 } else {
1193 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1194 }
1195 }
1196
1197 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001198 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001199 stack_index_ += 2;
1200 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1201 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1202 } else {
1203 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001204 }
1205 }
1206
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001207 case Primitive::kPrimVoid:
1208 LOG(FATAL) << "Unexpected parameter type " << type;
1209 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001210 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001211 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001212}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001213
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214void CodeGeneratorX86::Move32(Location destination, Location source) {
1215 if (source.Equals(destination)) {
1216 return;
1217 }
1218 if (destination.IsRegister()) {
1219 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001220 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 } else {
1224 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001225 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else if (destination.IsFpuRegister()) {
1228 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 } else {
1233 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001237 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001242 } else if (source.IsConstant()) {
1243 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001244 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001245 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 } else {
1247 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001248 __ pushl(Address(ESP, source.GetStackIndex()));
1249 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 }
1251 }
1252}
1253
1254void CodeGeneratorX86::Move64(Location destination, Location source) {
1255 if (source.Equals(destination)) {
1256 return;
1257 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001258 if (destination.IsRegisterPair()) {
1259 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001260 EmitParallelMoves(
1261 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1262 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001263 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001264 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001265 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1266 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001267 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1269 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1270 __ psrlq(src_reg, Immediate(32));
1271 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1276 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1278 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001280 if (source.IsFpuRegister()) {
1281 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1282 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001284 } else if (source.IsRegisterPair()) {
1285 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1286 // Create stack space for 2 elements.
1287 __ subl(ESP, Immediate(2 * elem_size));
1288 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1289 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1290 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1291 // And remove the temporary stack space we allocated.
1292 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001293 } else {
1294 LOG(FATAL) << "Unimplemented";
1295 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001297 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001298 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001299 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001300 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001301 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001304 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001305 } else if (source.IsConstant()) {
1306 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001307 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1308 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001309 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001310 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1311 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001313 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001314 EmitParallelMoves(
1315 Location::StackSlot(source.GetStackIndex()),
1316 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001317 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001318 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001319 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1320 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 }
1322 }
1323}
1324
Calin Juravle175dc732015-08-25 15:42:32 +01001325void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1326 DCHECK(location.IsRegister());
1327 __ movl(location.AsRegister<Register>(), Immediate(value));
1328}
1329
Calin Juravlee460d1d2015-09-29 04:52:17 +01001330void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001331 HParallelMove move(GetGraph()->GetArena());
1332 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1333 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1334 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001335 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001336 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001338 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001339}
1340
1341void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1342 if (location.IsRegister()) {
1343 locations->AddTemp(location);
1344 } else if (location.IsRegisterPair()) {
1345 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1346 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1347 } else {
1348 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1349 }
1350}
1351
David Brazdilfc6a86a2015-06-26 10:33:45 +00001352void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001353 DCHECK(!successor->IsExitBlock());
1354
1355 HBasicBlock* block = got->GetBlock();
1356 HInstruction* previous = got->GetPrevious();
1357
1358 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001359 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001360 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1361 return;
1362 }
1363
1364 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1365 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1366 }
1367 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001368 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369 }
1370}
1371
David Brazdilfc6a86a2015-06-26 10:33:45 +00001372void LocationsBuilderX86::VisitGoto(HGoto* got) {
1373 got->SetLocations(nullptr);
1374}
1375
1376void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1377 HandleGoto(got, got->GetSuccessor());
1378}
1379
1380void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1381 try_boundary->SetLocations(nullptr);
1382}
1383
1384void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1385 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1386 if (!successor->IsExitBlock()) {
1387 HandleGoto(try_boundary, successor);
1388 }
1389}
1390
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001392 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393}
1394
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001395void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001396}
1397
Mark Mendell152408f2015-12-31 12:28:50 -05001398template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001399void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001400 LabelType* true_label,
1401 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 if (cond->IsFPConditionTrueIfNaN()) {
1403 __ j(kUnordered, true_label);
1404 } else if (cond->IsFPConditionFalseIfNaN()) {
1405 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001406 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001408}
1409
Mark Mendell152408f2015-12-31 12:28:50 -05001410template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001411void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001412 LabelType* true_label,
1413 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001414 LocationSummary* locations = cond->GetLocations();
1415 Location left = locations->InAt(0);
1416 Location right = locations->InAt(1);
1417 IfCondition if_cond = cond->GetCondition();
1418
Mark Mendellc4701932015-04-10 13:18:51 -04001419 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001421 IfCondition true_high_cond = if_cond;
1422 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001423 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001424
1425 // Set the conditions for the test, remembering that == needs to be
1426 // decided using the low words.
1427 switch (if_cond) {
1428 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001429 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001430 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001431 break;
1432 case kCondLT:
1433 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001434 break;
1435 case kCondLE:
1436 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001437 break;
1438 case kCondGT:
1439 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001440 break;
1441 case kCondGE:
1442 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001443 break;
Aart Bike9f37602015-10-09 11:15:55 -07001444 case kCondB:
1445 false_high_cond = kCondA;
1446 break;
1447 case kCondBE:
1448 true_high_cond = kCondB;
1449 break;
1450 case kCondA:
1451 false_high_cond = kCondB;
1452 break;
1453 case kCondAE:
1454 true_high_cond = kCondA;
1455 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001456 }
1457
1458 if (right.IsConstant()) {
1459 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001460 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001461 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001462
Aart Bika19616e2016-02-01 18:57:58 -08001463 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001464 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001465 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001466 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001467 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001468 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001469 __ j(X86Condition(true_high_cond), true_label);
1470 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 }
1472 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001473 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001474 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001475 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001476 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001477
1478 __ cmpl(left_high, right_high);
1479 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001480 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001481 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001482 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001483 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001484 __ j(X86Condition(true_high_cond), true_label);
1485 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 }
1487 // Must be equal high, so compare the lows.
1488 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001489 } else {
1490 DCHECK(right.IsDoubleStackSlot());
1491 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1492 if (if_cond == kCondNE) {
1493 __ j(X86Condition(true_high_cond), true_label);
1494 } else if (if_cond == kCondEQ) {
1495 __ j(X86Condition(false_high_cond), false_label);
1496 } else {
1497 __ j(X86Condition(true_high_cond), true_label);
1498 __ j(X86Condition(false_high_cond), false_label);
1499 }
1500 // Must be equal high, so compare the lows.
1501 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001502 }
1503 // The last comparison might be unsigned.
1504 __ j(final_condition, true_label);
1505}
1506
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001507void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1508 Location rhs,
1509 HInstruction* insn,
1510 bool is_double) {
1511 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1512 if (is_double) {
1513 if (rhs.IsFpuRegister()) {
1514 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1515 } else if (const_area != nullptr) {
1516 DCHECK(const_area->IsEmittedAtUseSite());
1517 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1518 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001519 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1520 const_area->GetBaseMethodAddress(),
1521 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001522 } else {
1523 DCHECK(rhs.IsDoubleStackSlot());
1524 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1525 }
1526 } else {
1527 if (rhs.IsFpuRegister()) {
1528 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1529 } else if (const_area != nullptr) {
1530 DCHECK(const_area->IsEmittedAtUseSite());
1531 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1532 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001533 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1534 const_area->GetBaseMethodAddress(),
1535 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001536 } else {
1537 DCHECK(rhs.IsStackSlot());
1538 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1539 }
1540 }
1541}
1542
Mark Mendell152408f2015-12-31 12:28:50 -05001543template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001544void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001545 LabelType* true_target_in,
1546 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001547 // Generated branching requires both targets to be explicit. If either of the
1548 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001549 LabelType fallthrough_target;
1550 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1551 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001552
Mark Mendellc4701932015-04-10 13:18:51 -04001553 LocationSummary* locations = condition->GetLocations();
1554 Location left = locations->InAt(0);
1555 Location right = locations->InAt(1);
1556
Mark Mendellc4701932015-04-10 13:18:51 -04001557 Primitive::Type type = condition->InputAt(0)->GetType();
1558 switch (type) {
1559 case Primitive::kPrimLong:
1560 GenerateLongComparesAndJumps(condition, true_target, false_target);
1561 break;
1562 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001563 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001564 GenerateFPJumps(condition, true_target, false_target);
1565 break;
1566 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001567 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001568 GenerateFPJumps(condition, true_target, false_target);
1569 break;
1570 default:
1571 LOG(FATAL) << "Unexpected compare type " << type;
1572 }
1573
David Brazdil0debae72015-11-12 18:37:00 +00001574 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001575 __ jmp(false_target);
1576 }
David Brazdil0debae72015-11-12 18:37:00 +00001577
1578 if (fallthrough_target.IsLinked()) {
1579 __ Bind(&fallthrough_target);
1580 }
Mark Mendellc4701932015-04-10 13:18:51 -04001581}
1582
David Brazdil0debae72015-11-12 18:37:00 +00001583static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1584 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1585 // are set only strictly before `branch`. We can't use the eflags on long/FP
1586 // conditions if they are materialized due to the complex branching.
1587 return cond->IsCondition() &&
1588 cond->GetNext() == branch &&
1589 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1590 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1591}
1592
Mark Mendell152408f2015-12-31 12:28:50 -05001593template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001594void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001595 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001596 LabelType* true_target,
1597 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001598 HInstruction* cond = instruction->InputAt(condition_input_index);
1599
1600 if (true_target == nullptr && false_target == nullptr) {
1601 // Nothing to do. The code always falls through.
1602 return;
1603 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001604 // Constant condition, statically compared against "true" (integer value 1).
1605 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001606 if (true_target != nullptr) {
1607 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001608 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001609 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001610 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001611 if (false_target != nullptr) {
1612 __ jmp(false_target);
1613 }
1614 }
1615 return;
1616 }
1617
1618 // The following code generates these patterns:
1619 // (1) true_target == nullptr && false_target != nullptr
1620 // - opposite condition true => branch to false_target
1621 // (2) true_target != nullptr && false_target == nullptr
1622 // - condition true => branch to true_target
1623 // (3) true_target != nullptr && false_target != nullptr
1624 // - condition true => branch to true_target
1625 // - branch to false_target
1626 if (IsBooleanValueOrMaterializedCondition(cond)) {
1627 if (AreEflagsSetFrom(cond, instruction)) {
1628 if (true_target == nullptr) {
1629 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1630 } else {
1631 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1632 }
1633 } else {
1634 // Materialized condition, compare against 0.
1635 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1636 if (lhs.IsRegister()) {
1637 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1638 } else {
1639 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1640 }
1641 if (true_target == nullptr) {
1642 __ j(kEqual, false_target);
1643 } else {
1644 __ j(kNotEqual, true_target);
1645 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001646 }
1647 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001648 // Condition has not been materialized, use its inputs as the comparison and
1649 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001650 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001651
1652 // If this is a long or FP comparison that has been folded into
1653 // the HCondition, generate the comparison directly.
1654 Primitive::Type type = condition->InputAt(0)->GetType();
1655 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1656 GenerateCompareTestAndBranch(condition, true_target, false_target);
1657 return;
1658 }
1659
1660 Location lhs = condition->GetLocations()->InAt(0);
1661 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001663 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001664 if (true_target == nullptr) {
1665 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1666 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001667 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001668 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001669 }
David Brazdil0debae72015-11-12 18:37:00 +00001670
1671 // If neither branch falls through (case 3), the conditional branch to `true_target`
1672 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1673 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001675 }
1676}
1677
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001678void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1680 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001681 locations->SetInAt(0, Location::Any());
1682 }
1683}
1684
1685void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001686 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1687 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1688 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1689 nullptr : codegen_->GetLabelOf(true_successor);
1690 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1691 nullptr : codegen_->GetLabelOf(false_successor);
1692 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693}
1694
1695void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1696 LocationSummary* locations = new (GetGraph()->GetArena())
1697 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001698 InvokeRuntimeCallingConvention calling_convention;
1699 RegisterSet caller_saves = RegisterSet::Empty();
1700 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1701 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001702 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001703 locations->SetInAt(0, Location::Any());
1704 }
1705}
1706
1707void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001708 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001709 GenerateTestAndBranch<Label>(deoptimize,
1710 /* condition_input_index */ 0,
1711 slow_path->GetEntryLabel(),
1712 /* false_target */ nullptr);
1713}
1714
Mingyao Yang063fc772016-08-02 11:02:54 -07001715void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1716 LocationSummary* locations = new (GetGraph()->GetArena())
1717 LocationSummary(flag, LocationSummary::kNoCall);
1718 locations->SetOut(Location::RequiresRegister());
1719}
1720
1721void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1722 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1723 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1724}
1725
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001726static bool SelectCanUseCMOV(HSelect* select) {
1727 // There are no conditional move instructions for XMMs.
1728 if (Primitive::IsFloatingPointType(select->GetType())) {
1729 return false;
1730 }
1731
1732 // A FP condition doesn't generate the single CC that we need.
1733 // In 32 bit mode, a long condition doesn't generate a single CC either.
1734 HInstruction* condition = select->GetCondition();
1735 if (condition->IsCondition()) {
1736 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1737 if (compare_type == Primitive::kPrimLong ||
1738 Primitive::IsFloatingPointType(compare_type)) {
1739 return false;
1740 }
1741 }
1742
1743 // We can generate a CMOV for this Select.
1744 return true;
1745}
1746
David Brazdil74eb1b22015-12-14 11:44:01 +00001747void LocationsBuilderX86::VisitSelect(HSelect* select) {
1748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001749 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001750 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001751 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001752 } else {
1753 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001754 if (SelectCanUseCMOV(select)) {
1755 if (select->InputAt(1)->IsConstant()) {
1756 // Cmov can't handle a constant value.
1757 locations->SetInAt(1, Location::RequiresRegister());
1758 } else {
1759 locations->SetInAt(1, Location::Any());
1760 }
1761 } else {
1762 locations->SetInAt(1, Location::Any());
1763 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001764 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001765 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1766 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001767 }
1768 locations->SetOut(Location::SameAsFirstInput());
1769}
1770
1771void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1772 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001773 DCHECK(locations->InAt(0).Equals(locations->Out()));
1774 if (SelectCanUseCMOV(select)) {
1775 // If both the condition and the source types are integer, we can generate
1776 // a CMOV to implement Select.
1777
1778 HInstruction* select_condition = select->GetCondition();
1779 Condition cond = kNotEqual;
1780
1781 // Figure out how to test the 'condition'.
1782 if (select_condition->IsCondition()) {
1783 HCondition* condition = select_condition->AsCondition();
1784 if (!condition->IsEmittedAtUseSite()) {
1785 // This was a previously materialized condition.
1786 // Can we use the existing condition code?
1787 if (AreEflagsSetFrom(condition, select)) {
1788 // Materialization was the previous instruction. Condition codes are right.
1789 cond = X86Condition(condition->GetCondition());
1790 } else {
1791 // No, we have to recreate the condition code.
1792 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1793 __ testl(cond_reg, cond_reg);
1794 }
1795 } else {
1796 // We can't handle FP or long here.
1797 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1798 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1799 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001800 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001801 cond = X86Condition(condition->GetCondition());
1802 }
1803 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001804 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001805 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1806 __ testl(cond_reg, cond_reg);
1807 }
1808
1809 // If the condition is true, overwrite the output, which already contains false.
1810 Location false_loc = locations->InAt(0);
1811 Location true_loc = locations->InAt(1);
1812 if (select->GetType() == Primitive::kPrimLong) {
1813 // 64 bit conditional move.
1814 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1815 Register false_low = false_loc.AsRegisterPairLow<Register>();
1816 if (true_loc.IsRegisterPair()) {
1817 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1818 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1819 } else {
1820 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1821 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1822 }
1823 } else {
1824 // 32 bit conditional move.
1825 Register false_reg = false_loc.AsRegister<Register>();
1826 if (true_loc.IsRegister()) {
1827 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1828 } else {
1829 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1830 }
1831 }
1832 } else {
1833 NearLabel false_target;
1834 GenerateTestAndBranch<NearLabel>(
1835 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1836 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1837 __ Bind(&false_target);
1838 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001839}
1840
David Srbecky0cf44932015-12-09 14:09:59 +00001841void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1842 new (GetGraph()->GetArena()) LocationSummary(info);
1843}
1844
David Srbeckyd28f4a02016-03-14 17:14:24 +00001845void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1846 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001847}
1848
1849void CodeGeneratorX86::GenerateNop() {
1850 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001851}
1852
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001854 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001855 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001856 // Handle the long/FP comparisons made in instruction simplification.
1857 switch (cond->InputAt(0)->GetType()) {
1858 case Primitive::kPrimLong: {
1859 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001860 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001861 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001862 locations->SetOut(Location::RequiresRegister());
1863 }
1864 break;
1865 }
1866 case Primitive::kPrimFloat:
1867 case Primitive::kPrimDouble: {
1868 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001869 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1870 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1871 } else if (cond->InputAt(1)->IsConstant()) {
1872 locations->SetInAt(1, Location::RequiresFpuRegister());
1873 } else {
1874 locations->SetInAt(1, Location::Any());
1875 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001876 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001877 locations->SetOut(Location::RequiresRegister());
1878 }
1879 break;
1880 }
1881 default:
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001884 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001885 // We need a byte register.
1886 locations->SetOut(Location::RegisterLocation(ECX));
1887 }
1888 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001890}
1891
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001893 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001894 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001895 }
Mark Mendellc4701932015-04-10 13:18:51 -04001896
1897 LocationSummary* locations = cond->GetLocations();
1898 Location lhs = locations->InAt(0);
1899 Location rhs = locations->InAt(1);
1900 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001901 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001902
1903 switch (cond->InputAt(0)->GetType()) {
1904 default: {
1905 // Integer case.
1906
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001907 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001908 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001909 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001910 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001911 return;
1912 }
1913 case Primitive::kPrimLong:
1914 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1915 break;
1916 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001917 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001918 GenerateFPJumps(cond, &true_label, &false_label);
1919 break;
1920 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001921 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001922 GenerateFPJumps(cond, &true_label, &false_label);
1923 break;
1924 }
1925
1926 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001927 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001928
Roland Levillain4fa13f62015-07-06 18:11:54 +01001929 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001930 __ Bind(&false_label);
1931 __ xorl(reg, reg);
1932 __ jmp(&done_label);
1933
Roland Levillain4fa13f62015-07-06 18:11:54 +01001934 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001935 __ Bind(&true_label);
1936 __ movl(reg, Immediate(1));
1937 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001946}
1947
1948void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001954}
1955
1956void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001962}
1963
1964void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001970}
1971
1972void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001974}
1975
1976void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001978}
1979
1980void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001982}
1983
1984void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001986}
1987
Aart Bike9f37602015-10-09 11:15:55 -07001988void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001990}
1991
1992void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001994}
1995
1996void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001998}
1999
2000void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002002}
2003
2004void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002006}
2007
2008void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002010}
2011
2012void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002014}
2015
2016void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002018}
2019
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002020void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002027 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028}
2029
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002030void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2033 locations->SetOut(Location::ConstantLocation(constant));
2034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002037 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002038}
2039
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002043 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044}
2045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 // Will be generated at use site.
2048}
2049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002050void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2051 LocationSummary* locations =
2052 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2053 locations->SetOut(Location::ConstantLocation(constant));
2054}
2055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002056void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 // Will be generated at use site.
2058}
2059
2060void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2063 locations->SetOut(Location::ConstantLocation(constant));
2064}
2065
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002066void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067 // Will be generated at use site.
2068}
2069
Igor Murashkind01745e2017-04-05 16:40:31 -07002070void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2071 constructor_fence->SetLocations(nullptr);
2072}
2073
2074void InstructionCodeGeneratorX86::VisitConstructorFence(
2075 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2076 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2077}
2078
Calin Juravle27df7582015-04-17 19:12:31 +01002079void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2080 memory_barrier->SetLocations(nullptr);
2081}
2082
2083void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002084 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002085}
2086
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002087void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002088 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002089}
2090
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002091void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002092 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002093}
2094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002095void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002098 switch (ret->InputAt(0)->GetType()) {
2099 case Primitive::kPrimBoolean:
2100 case Primitive::kPrimByte:
2101 case Primitive::kPrimChar:
2102 case Primitive::kPrimShort:
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002106 break;
2107
2108 case Primitive::kPrimLong:
2109 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002110 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002111 break;
2112
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002113 case Primitive::kPrimFloat:
2114 case Primitive::kPrimDouble:
2115 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002116 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002117 break;
2118
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002120 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122}
2123
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002124void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 if (kIsDebugBuild) {
2126 switch (ret->InputAt(0)->GetType()) {
2127 case Primitive::kPrimBoolean:
2128 case Primitive::kPrimByte:
2129 case Primitive::kPrimChar:
2130 case Primitive::kPrimShort:
2131 case Primitive::kPrimInt:
2132 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134 break;
2135
2136 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2138 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002139 break;
2140
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002141 case Primitive::kPrimFloat:
2142 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002144 break;
2145
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002146 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002147 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 }
2149 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002150 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151}
2152
Calin Juravle175dc732015-08-25 15:42:32 +01002153void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2154 // The trampoline uses the same calling convention as dex calling conventions,
2155 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2156 // the method_idx.
2157 HandleInvoke(invoke);
2158}
2159
2160void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2161 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2162}
2163
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002164void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002165 // Explicit clinit checks triggered by static invokes must have been pruned by
2166 // art::PrepareForRegisterAllocation.
2167 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002168
Mark Mendellfb8d2792015-03-31 22:16:59 -04002169 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002170 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002171 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002172 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002173 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002174 return;
2175 }
2176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002177 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002178
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002179 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002180 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002181 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002182 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183}
2184
Mark Mendell09ed1a32015-03-25 08:30:06 -04002185static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2186 if (invoke->GetLocations()->Intrinsified()) {
2187 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2188 intrinsic.Dispatch(invoke);
2189 return true;
2190 }
2191 return false;
2192}
2193
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002194void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002195 // Explicit clinit checks triggered by static invokes must have been pruned by
2196 // art::PrepareForRegisterAllocation.
2197 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002198
Mark Mendell09ed1a32015-03-25 08:30:06 -04002199 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2200 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002201 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002202
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002203 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002204 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002205 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002206 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002207}
2208
2209void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002210 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2211 if (intrinsic.TryDispatch(invoke)) {
2212 return;
2213 }
2214
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002215 HandleInvoke(invoke);
2216}
2217
2218void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002219 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002220 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002221}
2222
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002223void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002224 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2225 return;
2226 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002227
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002228 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002229 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002230 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002231}
2232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002234 // This call to HandleInvoke allocates a temporary (core) register
2235 // which is also used to transfer the hidden argument from FP to
2236 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 HandleInvoke(invoke);
2238 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002239 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240}
2241
2242void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2243 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 LocationSummary* locations = invoke->GetLocations();
2245 Register temp = locations->GetTemp(0).AsRegister<Register>();
2246 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002247 Location receiver = locations->InAt(0);
2248 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2249
Roland Levillain0d5a2812015-11-13 10:07:31 +00002250 // Set the hidden argument. This is safe to do this here, as XMM7
2251 // won't be modified thereafter, before the `call` instruction.
2252 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002254 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 if (receiver.IsStackSlot()) {
2257 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259 __ movl(temp, Address(temp, class_offset));
2260 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002261 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002263 }
Roland Levillain4d027112015-07-01 15:41:14 +01002264 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // Instead of simply (possibly) unpoisoning `temp` here, we should
2266 // emit a read barrier for the previous class reference load.
2267 // However this is not required in practice, as this is an
2268 // intermediate/temporary reference and because the current
2269 // concurrent copying collector keeps the from-space memory
2270 // intact/accessible until the end of the marking phase (the
2271 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002272 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002273 // temp = temp->GetAddressOfIMT()
2274 __ movl(temp,
2275 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002277 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002278 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279 __ movl(temp, Address(temp, method_offset));
2280 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002281 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002282 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283
2284 DCHECK(!codegen_->IsLeafMethod());
2285 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2286}
2287
Orion Hodsonac141392017-01-13 11:53:47 +00002288void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2289 HandleInvoke(invoke);
2290}
2291
2292void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2293 codegen_->GenerateInvokePolymorphicCall(invoke);
2294}
2295
Roland Levillain88cb1752014-10-20 16:36:47 +01002296void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2297 LocationSummary* locations =
2298 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2299 switch (neg->GetResultType()) {
2300 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002301 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetOut(Location::SameAsFirstInput());
2304 break;
2305
Roland Levillain88cb1752014-10-20 16:36:47 +01002306 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002307 locations->SetInAt(0, Location::RequiresFpuRegister());
2308 locations->SetOut(Location::SameAsFirstInput());
2309 locations->AddTemp(Location::RequiresRegister());
2310 locations->AddTemp(Location::RequiresFpuRegister());
2311 break;
2312
Roland Levillain88cb1752014-10-20 16:36:47 +01002313 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002314 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002315 locations->SetOut(Location::SameAsFirstInput());
2316 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 break;
2318
2319 default:
2320 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2321 }
2322}
2323
2324void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2325 LocationSummary* locations = neg->GetLocations();
2326 Location out = locations->Out();
2327 Location in = locations->InAt(0);
2328 switch (neg->GetResultType()) {
2329 case Primitive::kPrimInt:
2330 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002331 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002333 break;
2334
2335 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002336 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002337 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002338 __ negl(out.AsRegisterPairLow<Register>());
2339 // Negation is similar to subtraction from zero. The least
2340 // significant byte triggers a borrow when it is different from
2341 // zero; to take it into account, add 1 to the most significant
2342 // byte if the carry flag (CF) is set to 1 after the first NEGL
2343 // operation.
2344 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2345 __ negl(out.AsRegisterPairHigh<Register>());
2346 break;
2347
Roland Levillain5368c212014-11-27 15:03:41 +00002348 case Primitive::kPrimFloat: {
2349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 Register constant = locations->GetTemp(0).AsRegister<Register>();
2351 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002352 // Implement float negation with an exclusive or with value
2353 // 0x80000000 (mask for bit 31, representing the sign of a
2354 // single-precision floating-point number).
2355 __ movl(constant, Immediate(INT32_C(0x80000000)));
2356 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002358 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002359 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360
Roland Levillain5368c212014-11-27 15:03:41 +00002361 case Primitive::kPrimDouble: {
2362 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002364 // Implement double negation with an exclusive or with value
2365 // 0x8000000000000000 (mask for bit 63, representing the sign of
2366 // a double-precision floating-point number).
2367 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002370 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002371
2372 default:
2373 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2374 }
2375}
2376
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002377void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2378 LocationSummary* locations =
2379 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2380 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2381 locations->SetInAt(0, Location::RequiresFpuRegister());
2382 locations->SetInAt(1, Location::RequiresRegister());
2383 locations->SetOut(Location::SameAsFirstInput());
2384 locations->AddTemp(Location::RequiresFpuRegister());
2385}
2386
2387void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2388 LocationSummary* locations = neg->GetLocations();
2389 Location out = locations->Out();
2390 DCHECK(locations->InAt(0).Equals(out));
2391
2392 Register constant_area = locations->InAt(1).AsRegister<Register>();
2393 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2394 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002395 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2396 neg->GetBaseMethodAddress(),
2397 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002398 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2399 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002400 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2401 neg->GetBaseMethodAddress(),
2402 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002403 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2404 }
2405}
2406
Roland Levillaindff1f282014-11-05 14:15:05 +00002407void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002408 Primitive::Type result_type = conversion->GetResultType();
2409 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002410 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002411
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002412 // The float-to-long and double-to-long type conversions rely on a
2413 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002414 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002415 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2416 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002417 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002418 : LocationSummary::kNoCall;
2419 LocationSummary* locations =
2420 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2421
David Brazdilb2bd1c52015-03-25 11:17:37 +00002422 // The Java language does not allow treating boolean as an integral type but
2423 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002426 case Primitive::kPrimByte:
2427 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002428 case Primitive::kPrimLong: {
2429 // Type conversion from long to byte is a result of code transformations.
2430 HInstruction* input = conversion->InputAt(0);
2431 Location input_location = input->IsConstant()
2432 ? Location::ConstantLocation(input->AsConstant())
2433 : Location::RegisterPairLocation(EAX, EDX);
2434 locations->SetInAt(0, input_location);
2435 // Make the output overlap to please the register allocator. This greatly simplifies
2436 // the validation of the linear scan implementation
2437 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2438 break;
2439 }
David Brazdil46e2a392015-03-16 17:31:52 +00002440 case Primitive::kPrimBoolean:
2441 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002442 case Primitive::kPrimShort:
2443 case Primitive::kPrimInt:
2444 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002445 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002446 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2447 // Make the output overlap to please the register allocator. This greatly simplifies
2448 // the validation of the linear scan implementation
2449 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002450 break;
2451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 }
2456 break;
2457
Roland Levillain01a8d712014-11-14 16:27:39 +00002458 case Primitive::kPrimShort:
2459 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002460 case Primitive::kPrimLong:
2461 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002462 case Primitive::kPrimBoolean:
2463 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002464 case Primitive::kPrimByte:
2465 case Primitive::kPrimInt:
2466 case Primitive::kPrimChar:
2467 // Processing a Dex `int-to-short' instruction.
2468 locations->SetInAt(0, Location::Any());
2469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2470 break;
2471
2472 default:
2473 LOG(FATAL) << "Unexpected type conversion from " << input_type
2474 << " to " << result_type;
2475 }
2476 break;
2477
Roland Levillain946e1432014-11-11 17:35:19 +00002478 case Primitive::kPrimInt:
2479 switch (input_type) {
2480 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002481 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002482 locations->SetInAt(0, Location::Any());
2483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2484 break;
2485
2486 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002487 // Processing a Dex `float-to-int' instruction.
2488 locations->SetInAt(0, Location::RequiresFpuRegister());
2489 locations->SetOut(Location::RequiresRegister());
2490 locations->AddTemp(Location::RequiresFpuRegister());
2491 break;
2492
Roland Levillain946e1432014-11-11 17:35:19 +00002493 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002494 // Processing a Dex `double-to-int' instruction.
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
2496 locations->SetOut(Location::RequiresRegister());
2497 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002498 break;
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimLong:
2507 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002508 case Primitive::kPrimBoolean:
2509 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002510 case Primitive::kPrimByte:
2511 case Primitive::kPrimShort:
2512 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002513 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002514 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002515 locations->SetInAt(0, Location::RegisterLocation(EAX));
2516 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2517 break;
2518
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002519 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002520 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002521 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002522 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002523 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2524 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2525
Vladimir Marko949c91f2015-01-27 10:48:44 +00002526 // The runtime helper puts the result in EAX, EDX.
2527 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002528 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002529 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002530
2531 default:
2532 LOG(FATAL) << "Unexpected type conversion from " << input_type
2533 << " to " << result_type;
2534 }
2535 break;
2536
Roland Levillain981e4542014-11-14 11:47:14 +00002537 case Primitive::kPrimChar:
2538 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002539 case Primitive::kPrimLong:
2540 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002541 case Primitive::kPrimBoolean:
2542 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002543 case Primitive::kPrimByte:
2544 case Primitive::kPrimShort:
2545 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002546 // Processing a Dex `int-to-char' instruction.
2547 locations->SetInAt(0, Location::Any());
2548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2549 break;
2550
2551 default:
2552 LOG(FATAL) << "Unexpected type conversion from " << input_type
2553 << " to " << result_type;
2554 }
2555 break;
2556
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002558 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002559 case Primitive::kPrimBoolean:
2560 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002561 case Primitive::kPrimByte:
2562 case Primitive::kPrimShort:
2563 case Primitive::kPrimInt:
2564 case Primitive::kPrimChar:
2565 // Processing a Dex `int-to-float' instruction.
2566 locations->SetInAt(0, Location::RequiresRegister());
2567 locations->SetOut(Location::RequiresFpuRegister());
2568 break;
2569
2570 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002571 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002572 locations->SetInAt(0, Location::Any());
2573 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002574 break;
2575
Roland Levillaincff13742014-11-17 14:32:17 +00002576 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002577 // Processing a Dex `double-to-float' instruction.
2578 locations->SetInAt(0, Location::RequiresFpuRegister());
2579 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002580 break;
2581
2582 default:
2583 LOG(FATAL) << "Unexpected type conversion from " << input_type
2584 << " to " << result_type;
2585 };
2586 break;
2587
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002589 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002590 case Primitive::kPrimBoolean:
2591 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002592 case Primitive::kPrimByte:
2593 case Primitive::kPrimShort:
2594 case Primitive::kPrimInt:
2595 case Primitive::kPrimChar:
2596 // Processing a Dex `int-to-double' instruction.
2597 locations->SetInAt(0, Location::RequiresRegister());
2598 locations->SetOut(Location::RequiresFpuRegister());
2599 break;
2600
2601 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002602 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002603 locations->SetInAt(0, Location::Any());
2604 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002605 break;
2606
Roland Levillaincff13742014-11-17 14:32:17 +00002607 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002608 // Processing a Dex `float-to-double' instruction.
2609 locations->SetInAt(0, Location::RequiresFpuRegister());
2610 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002611 break;
2612
2613 default:
2614 LOG(FATAL) << "Unexpected type conversion from " << input_type
2615 << " to " << result_type;
2616 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002617 break;
2618
2619 default:
2620 LOG(FATAL) << "Unexpected type conversion from " << input_type
2621 << " to " << result_type;
2622 }
2623}
2624
2625void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2626 LocationSummary* locations = conversion->GetLocations();
2627 Location out = locations->Out();
2628 Location in = locations->InAt(0);
2629 Primitive::Type result_type = conversion->GetResultType();
2630 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002631 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002632 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002633 case Primitive::kPrimByte:
2634 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002635 case Primitive::kPrimLong:
2636 // Type conversion from long to byte is a result of code transformations.
2637 if (in.IsRegisterPair()) {
2638 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2639 } else {
2640 DCHECK(in.GetConstant()->IsLongConstant());
2641 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2643 }
2644 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002645 case Primitive::kPrimBoolean:
2646 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002647 case Primitive::kPrimShort:
2648 case Primitive::kPrimInt:
2649 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002650 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002651 if (in.IsRegister()) {
2652 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002653 } else {
2654 DCHECK(in.GetConstant()->IsIntConstant());
2655 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2656 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2657 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002658 break;
2659
2660 default:
2661 LOG(FATAL) << "Unexpected type conversion from " << input_type
2662 << " to " << result_type;
2663 }
2664 break;
2665
Roland Levillain01a8d712014-11-14 16:27:39 +00002666 case Primitive::kPrimShort:
2667 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002668 case Primitive::kPrimLong:
2669 // Type conversion from long to short is a result of code transformations.
2670 if (in.IsRegisterPair()) {
2671 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2672 } else if (in.IsDoubleStackSlot()) {
2673 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2674 } else {
2675 DCHECK(in.GetConstant()->IsLongConstant());
2676 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2677 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2678 }
2679 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002680 case Primitive::kPrimBoolean:
2681 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002682 case Primitive::kPrimByte:
2683 case Primitive::kPrimInt:
2684 case Primitive::kPrimChar:
2685 // Processing a Dex `int-to-short' instruction.
2686 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002689 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002690 } else {
2691 DCHECK(in.GetConstant()->IsIntConstant());
2692 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002694 }
2695 break;
2696
2697 default:
2698 LOG(FATAL) << "Unexpected type conversion from " << input_type
2699 << " to " << result_type;
2700 }
2701 break;
2702
Roland Levillain946e1432014-11-11 17:35:19 +00002703 case Primitive::kPrimInt:
2704 switch (input_type) {
2705 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002706 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002707 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002709 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002710 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002711 } else {
2712 DCHECK(in.IsConstant());
2713 DCHECK(in.GetConstant()->IsLongConstant());
2714 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002716 }
2717 break;
2718
Roland Levillain3f8f9362014-12-02 17:45:01 +00002719 case Primitive::kPrimFloat: {
2720 // Processing a Dex `float-to-int' instruction.
2721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2722 Register output = out.AsRegister<Register>();
2723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002724 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002725
2726 __ movl(output, Immediate(kPrimIntMax));
2727 // temp = int-to-float(output)
2728 __ cvtsi2ss(temp, output);
2729 // if input >= temp goto done
2730 __ comiss(input, temp);
2731 __ j(kAboveEqual, &done);
2732 // if input == NaN goto nan
2733 __ j(kUnordered, &nan);
2734 // output = float-to-int-truncate(input)
2735 __ cvttss2si(output, input);
2736 __ jmp(&done);
2737 __ Bind(&nan);
2738 // output = 0
2739 __ xorl(output, output);
2740 __ Bind(&done);
2741 break;
2742 }
2743
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002744 case Primitive::kPrimDouble: {
2745 // Processing a Dex `double-to-int' instruction.
2746 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2747 Register output = out.AsRegister<Register>();
2748 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002749 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002750
2751 __ movl(output, Immediate(kPrimIntMax));
2752 // temp = int-to-double(output)
2753 __ cvtsi2sd(temp, output);
2754 // if input >= temp goto done
2755 __ comisd(input, temp);
2756 __ j(kAboveEqual, &done);
2757 // if input == NaN goto nan
2758 __ j(kUnordered, &nan);
2759 // output = double-to-int-truncate(input)
2760 __ cvttsd2si(output, input);
2761 __ jmp(&done);
2762 __ Bind(&nan);
2763 // output = 0
2764 __ xorl(output, output);
2765 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002766 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002767 }
Roland Levillain946e1432014-11-11 17:35:19 +00002768
2769 default:
2770 LOG(FATAL) << "Unexpected type conversion from " << input_type
2771 << " to " << result_type;
2772 }
2773 break;
2774
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 case Primitive::kPrimLong:
2776 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002777 case Primitive::kPrimBoolean:
2778 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002779 case Primitive::kPrimByte:
2780 case Primitive::kPrimShort:
2781 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002782 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002783 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2785 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002786 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002787 __ cdq();
2788 break;
2789
2790 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002791 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002792 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002793 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002794 break;
2795
Roland Levillaindff1f282014-11-05 14:15:05 +00002796 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002797 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002798 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002799 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 break;
2801
2802 default:
2803 LOG(FATAL) << "Unexpected type conversion from " << input_type
2804 << " to " << result_type;
2805 }
2806 break;
2807
Roland Levillain981e4542014-11-14 11:47:14 +00002808 case Primitive::kPrimChar:
2809 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002810 case Primitive::kPrimLong:
2811 // Type conversion from long to short is a result of code transformations.
2812 if (in.IsRegisterPair()) {
2813 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2814 } else if (in.IsDoubleStackSlot()) {
2815 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2816 } else {
2817 DCHECK(in.GetConstant()->IsLongConstant());
2818 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2819 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2820 }
2821 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002822 case Primitive::kPrimBoolean:
2823 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002824 case Primitive::kPrimByte:
2825 case Primitive::kPrimShort:
2826 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002827 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2828 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002829 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002830 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002832 } else {
2833 DCHECK(in.GetConstant()->IsIntConstant());
2834 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002835 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002836 }
2837 break;
2838
2839 default:
2840 LOG(FATAL) << "Unexpected type conversion from " << input_type
2841 << " to " << result_type;
2842 }
2843 break;
2844
Roland Levillaindff1f282014-11-05 14:15:05 +00002845 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002846 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002847 case Primitive::kPrimBoolean:
2848 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002849 case Primitive::kPrimByte:
2850 case Primitive::kPrimShort:
2851 case Primitive::kPrimInt:
2852 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002853 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002855 break;
2856
Roland Levillain6d0e4832014-11-27 18:31:21 +00002857 case Primitive::kPrimLong: {
2858 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002859 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002860
Roland Levillain232ade02015-04-20 15:14:36 +01002861 // Create stack space for the call to
2862 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2863 // TODO: enhance register allocator to ask for stack temporaries.
2864 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2865 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2866 __ subl(ESP, Immediate(adjustment));
2867 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002868
Roland Levillain232ade02015-04-20 15:14:36 +01002869 // Load the value to the FP stack, using temporaries if needed.
2870 PushOntoFPStack(in, 0, adjustment, false, true);
2871
2872 if (out.IsStackSlot()) {
2873 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2874 } else {
2875 __ fstps(Address(ESP, 0));
2876 Location stack_temp = Location::StackSlot(0);
2877 codegen_->Move32(out, stack_temp);
2878 }
2879
2880 // Remove the temporary stack space we allocated.
2881 if (adjustment != 0) {
2882 __ addl(ESP, Immediate(adjustment));
2883 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002884 break;
2885 }
2886
Roland Levillaincff13742014-11-17 14:32:17 +00002887 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002888 // Processing a Dex `double-to-float' instruction.
2889 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002890 break;
2891
2892 default:
2893 LOG(FATAL) << "Unexpected type conversion from " << input_type
2894 << " to " << result_type;
2895 };
2896 break;
2897
Roland Levillaindff1f282014-11-05 14:15:05 +00002898 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002899 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002900 case Primitive::kPrimBoolean:
2901 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002902 case Primitive::kPrimByte:
2903 case Primitive::kPrimShort:
2904 case Primitive::kPrimInt:
2905 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002906 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002908 break;
2909
Roland Levillain647b9ed2014-11-27 12:06:00 +00002910 case Primitive::kPrimLong: {
2911 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002912 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002913
Roland Levillain232ade02015-04-20 15:14:36 +01002914 // Create stack space for the call to
2915 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2916 // TODO: enhance register allocator to ask for stack temporaries.
2917 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2918 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2919 __ subl(ESP, Immediate(adjustment));
2920 }
2921
2922 // Load the value to the FP stack, using temporaries if needed.
2923 PushOntoFPStack(in, 0, adjustment, false, true);
2924
2925 if (out.IsDoubleStackSlot()) {
2926 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2927 } else {
2928 __ fstpl(Address(ESP, 0));
2929 Location stack_temp = Location::DoubleStackSlot(0);
2930 codegen_->Move64(out, stack_temp);
2931 }
2932
2933 // Remove the temporary stack space we allocated.
2934 if (adjustment != 0) {
2935 __ addl(ESP, Immediate(adjustment));
2936 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002937 break;
2938 }
2939
Roland Levillaincff13742014-11-17 14:32:17 +00002940 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002941 // Processing a Dex `float-to-double' instruction.
2942 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002943 break;
2944
2945 default:
2946 LOG(FATAL) << "Unexpected type conversion from " << input_type
2947 << " to " << result_type;
2948 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002949 break;
2950
2951 default:
2952 LOG(FATAL) << "Unexpected type conversion from " << input_type
2953 << " to " << result_type;
2954 }
2955}
2956
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002958 LocationSummary* locations =
2959 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002961 case Primitive::kPrimInt: {
2962 locations->SetInAt(0, Location::RequiresRegister());
2963 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2965 break;
2966 }
2967
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002968 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002969 locations->SetInAt(0, Location::RequiresRegister());
2970 locations->SetInAt(1, Location::Any());
2971 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972 break;
2973 }
2974
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002975 case Primitive::kPrimFloat:
2976 case Primitive::kPrimDouble: {
2977 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002978 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2979 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002980 } else if (add->InputAt(1)->IsConstant()) {
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002982 } else {
2983 locations->SetInAt(1, Location::Any());
2984 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002985 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002986 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002988
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002989 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2991 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993}
2994
2995void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2996 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002997 Location first = locations->InAt(0);
2998 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002999 Location out = locations->Out();
3000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003002 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003004 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3005 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003006 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3007 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003008 } else {
3009 __ leal(out.AsRegister<Register>(), Address(
3010 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3011 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003013 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3014 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3015 __ addl(out.AsRegister<Register>(), Immediate(value));
3016 } else {
3017 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3018 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003019 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003020 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003022 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003023 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024 }
3025
3026 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003027 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003028 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3029 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003030 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003031 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3032 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003033 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003034 } else {
3035 DCHECK(second.IsConstant()) << second;
3036 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3037 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3038 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003039 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003040 break;
3041 }
3042
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003043 case Primitive::kPrimFloat: {
3044 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003046 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3047 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003048 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003049 __ addss(first.AsFpuRegister<XmmRegister>(),
3050 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003051 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3052 const_area->GetBaseMethodAddress(),
3053 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003054 } else {
3055 DCHECK(second.IsStackSlot());
3056 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003057 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003058 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003059 }
3060
3061 case Primitive::kPrimDouble: {
3062 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003064 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3065 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003066 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003067 __ addsd(first.AsFpuRegister<XmmRegister>(),
3068 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003069 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3070 const_area->GetBaseMethodAddress(),
3071 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003072 } else {
3073 DCHECK(second.IsDoubleStackSlot());
3074 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003075 }
3076 break;
3077 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003078
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003079 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003080 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003081 }
3082}
3083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003084void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003088 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003089 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003090 locations->SetInAt(0, Location::RequiresRegister());
3091 locations->SetInAt(1, Location::Any());
3092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093 break;
3094 }
Calin Juravle11351682014-10-23 15:38:15 +01003095 case Primitive::kPrimFloat:
3096 case Primitive::kPrimDouble: {
3097 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003098 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3099 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003100 } else if (sub->InputAt(1)->IsConstant()) {
3101 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003102 } else {
3103 locations->SetInAt(1, Location::Any());
3104 }
Calin Juravle11351682014-10-23 15:38:15 +01003105 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003106 break;
Calin Juravle11351682014-10-23 15:38:15 +01003107 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003108
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003109 default:
Calin Juravle11351682014-10-23 15:38:15 +01003110 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003112}
3113
3114void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3115 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003116 Location first = locations->InAt(0);
3117 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003118 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003119 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003120 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003121 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003122 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003123 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003124 __ subl(first.AsRegister<Register>(),
3125 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003126 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003127 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003128 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003129 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003130 }
3131
3132 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003133 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003134 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3135 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003136 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003137 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003138 __ sbbl(first.AsRegisterPairHigh<Register>(),
3139 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003140 } else {
3141 DCHECK(second.IsConstant()) << second;
3142 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3143 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3144 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003145 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003146 break;
3147 }
3148
Calin Juravle11351682014-10-23 15:38:15 +01003149 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003150 if (second.IsFpuRegister()) {
3151 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3152 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3153 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003154 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003155 __ subss(first.AsFpuRegister<XmmRegister>(),
3156 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003157 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3158 const_area->GetBaseMethodAddress(),
3159 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003160 } else {
3161 DCHECK(second.IsStackSlot());
3162 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3163 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003164 break;
Calin Juravle11351682014-10-23 15:38:15 +01003165 }
3166
3167 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003168 if (second.IsFpuRegister()) {
3169 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3170 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3171 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003172 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003173 __ subsd(first.AsFpuRegister<XmmRegister>(),
3174 codegen_->LiteralDoubleAddress(
3175 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003176 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003177 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3178 } else {
3179 DCHECK(second.IsDoubleStackSlot());
3180 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3181 }
Calin Juravle11351682014-10-23 15:38:15 +01003182 break;
3183 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003184
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003185 default:
Calin Juravle11351682014-10-23 15:38:15 +01003186 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003187 }
3188}
3189
Calin Juravle34bacdf2014-10-07 20:23:36 +01003190void LocationsBuilderX86::VisitMul(HMul* mul) {
3191 LocationSummary* locations =
3192 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3193 switch (mul->GetResultType()) {
3194 case Primitive::kPrimInt:
3195 locations->SetInAt(0, Location::RequiresRegister());
3196 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003197 if (mul->InputAt(1)->IsIntConstant()) {
3198 // Can use 3 operand multiply.
3199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3200 } else {
3201 locations->SetOut(Location::SameAsFirstInput());
3202 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203 break;
3204 case Primitive::kPrimLong: {
3205 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206 locations->SetInAt(1, Location::Any());
3207 locations->SetOut(Location::SameAsFirstInput());
3208 // Needed for imul on 32bits with 64bits output.
3209 locations->AddTemp(Location::RegisterLocation(EAX));
3210 locations->AddTemp(Location::RegisterLocation(EDX));
3211 break;
3212 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003213 case Primitive::kPrimFloat:
3214 case Primitive::kPrimDouble: {
3215 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003216 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3217 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003218 } else if (mul->InputAt(1)->IsConstant()) {
3219 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003220 } else {
3221 locations->SetInAt(1, Location::Any());
3222 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003223 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003225 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226
3227 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003228 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003229 }
3230}
3231
3232void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3233 LocationSummary* locations = mul->GetLocations();
3234 Location first = locations->InAt(0);
3235 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003236 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237
3238 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003239 case Primitive::kPrimInt:
3240 // The constant may have ended up in a register, so test explicitly to avoid
3241 // problems where the output may not be the same as the first operand.
3242 if (mul->InputAt(1)->IsIntConstant()) {
3243 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3244 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3245 } else if (second.IsRegister()) {
3246 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003248 } else {
3249 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003250 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252 }
3253 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003254
3255 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003256 Register in1_hi = first.AsRegisterPairHigh<Register>();
3257 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 Register eax = locations->GetTemp(0).AsRegister<Register>();
3259 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003260
3261 DCHECK_EQ(EAX, eax);
3262 DCHECK_EQ(EDX, edx);
3263
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003264 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003265 // output: in1
3266 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3267 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3268 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003269 if (second.IsConstant()) {
3270 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003272 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3273 int32_t low_value = Low32Bits(value);
3274 int32_t high_value = High32Bits(value);
3275 Immediate low(low_value);
3276 Immediate high(high_value);
3277
3278 __ movl(eax, high);
3279 // eax <- in1.lo * in2.hi
3280 __ imull(eax, in1_lo);
3281 // in1.hi <- in1.hi * in2.lo
3282 __ imull(in1_hi, low);
3283 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3284 __ addl(in1_hi, eax);
3285 // move in2_lo to eax to prepare for double precision
3286 __ movl(eax, low);
3287 // edx:eax <- in1.lo * in2.lo
3288 __ mull(in1_lo);
3289 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3290 __ addl(in1_hi, edx);
3291 // in1.lo <- (in1.lo * in2.lo)[31:0];
3292 __ movl(in1_lo, eax);
3293 } else if (second.IsRegisterPair()) {
3294 Register in2_hi = second.AsRegisterPairHigh<Register>();
3295 Register in2_lo = second.AsRegisterPairLow<Register>();
3296
3297 __ movl(eax, in2_hi);
3298 // eax <- in1.lo * in2.hi
3299 __ imull(eax, in1_lo);
3300 // in1.hi <- in1.hi * in2.lo
3301 __ imull(in1_hi, in2_lo);
3302 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3303 __ addl(in1_hi, eax);
3304 // move in1_lo to eax to prepare for double precision
3305 __ movl(eax, in1_lo);
3306 // edx:eax <- in1.lo * in2.lo
3307 __ mull(in2_lo);
3308 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3309 __ addl(in1_hi, edx);
3310 // in1.lo <- (in1.lo * in2.lo)[31:0];
3311 __ movl(in1_lo, eax);
3312 } else {
3313 DCHECK(second.IsDoubleStackSlot()) << second;
3314 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3315 Address in2_lo(ESP, second.GetStackIndex());
3316
3317 __ movl(eax, in2_hi);
3318 // eax <- in1.lo * in2.hi
3319 __ imull(eax, in1_lo);
3320 // in1.hi <- in1.hi * in2.lo
3321 __ imull(in1_hi, in2_lo);
3322 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3323 __ addl(in1_hi, eax);
3324 // move in1_lo to eax to prepare for double precision
3325 __ movl(eax, in1_lo);
3326 // edx:eax <- in1.lo * in2.lo
3327 __ mull(in2_lo);
3328 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3329 __ addl(in1_hi, edx);
3330 // in1.lo <- (in1.lo * in2.lo)[31:0];
3331 __ movl(in1_lo, eax);
3332 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003333
3334 break;
3335 }
3336
Calin Juravleb5bfa962014-10-21 18:02:24 +01003337 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 DCHECK(first.Equals(locations->Out()));
3339 if (second.IsFpuRegister()) {
3340 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3341 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3342 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003343 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003344 __ mulss(first.AsFpuRegister<XmmRegister>(),
3345 codegen_->LiteralFloatAddress(
3346 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003347 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003348 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3349 } else {
3350 DCHECK(second.IsStackSlot());
3351 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3352 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003353 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003354 }
3355
3356 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003357 DCHECK(first.Equals(locations->Out()));
3358 if (second.IsFpuRegister()) {
3359 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3360 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3361 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003362 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003363 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3364 codegen_->LiteralDoubleAddress(
3365 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003366 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003367 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3368 } else {
3369 DCHECK(second.IsDoubleStackSlot());
3370 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3371 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003372 break;
3373 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374
3375 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003376 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003377 }
3378}
3379
Roland Levillain232ade02015-04-20 15:14:36 +01003380void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3381 uint32_t temp_offset,
3382 uint32_t stack_adjustment,
3383 bool is_fp,
3384 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003385 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003386 DCHECK(!is_wide);
3387 if (is_fp) {
3388 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3389 } else {
3390 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3391 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003392 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003393 DCHECK(is_wide);
3394 if (is_fp) {
3395 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3396 } else {
3397 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3398 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003399 } else {
3400 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003401 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003402 Location stack_temp = Location::StackSlot(temp_offset);
3403 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003404 if (is_fp) {
3405 __ flds(Address(ESP, temp_offset));
3406 } else {
3407 __ filds(Address(ESP, temp_offset));
3408 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003409 } else {
3410 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3411 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003412 if (is_fp) {
3413 __ fldl(Address(ESP, temp_offset));
3414 } else {
3415 __ fildl(Address(ESP, temp_offset));
3416 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003417 }
3418 }
3419}
3420
3421void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3422 Primitive::Type type = rem->GetResultType();
3423 bool is_float = type == Primitive::kPrimFloat;
3424 size_t elem_size = Primitive::ComponentSize(type);
3425 LocationSummary* locations = rem->GetLocations();
3426 Location first = locations->InAt(0);
3427 Location second = locations->InAt(1);
3428 Location out = locations->Out();
3429
3430 // Create stack space for 2 elements.
3431 // TODO: enhance register allocator to ask for stack temporaries.
3432 __ subl(ESP, Immediate(2 * elem_size));
3433
3434 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003435 const bool is_wide = !is_float;
3436 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3437 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003438
3439 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003440 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003441 __ Bind(&retry);
3442 __ fprem();
3443
3444 // Move FP status to AX.
3445 __ fstsw();
3446
3447 // And see if the argument reduction is complete. This is signaled by the
3448 // C2 FPU flag bit set to 0.
3449 __ andl(EAX, Immediate(kC2ConditionMask));
3450 __ j(kNotEqual, &retry);
3451
3452 // We have settled on the final value. Retrieve it into an XMM register.
3453 // Store FP top of stack to real stack.
3454 if (is_float) {
3455 __ fsts(Address(ESP, 0));
3456 } else {
3457 __ fstl(Address(ESP, 0));
3458 }
3459
3460 // Pop the 2 items from the FP stack.
3461 __ fucompp();
3462
3463 // Load the value from the stack into an XMM register.
3464 DCHECK(out.IsFpuRegister()) << out;
3465 if (is_float) {
3466 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3467 } else {
3468 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3469 }
3470
3471 // And remove the temporary stack space we allocated.
3472 __ addl(ESP, Immediate(2 * elem_size));
3473}
3474
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475
3476void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3477 DCHECK(instruction->IsDiv() || instruction->IsRem());
3478
3479 LocationSummary* locations = instruction->GetLocations();
3480 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003481 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482
3483 Register out_register = locations->Out().AsRegister<Register>();
3484 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003485 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486
3487 DCHECK(imm == 1 || imm == -1);
3488
3489 if (instruction->IsRem()) {
3490 __ xorl(out_register, out_register);
3491 } else {
3492 __ movl(out_register, input_register);
3493 if (imm == -1) {
3494 __ negl(out_register);
3495 }
3496 }
3497}
3498
3499
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003500void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 LocationSummary* locations = instruction->GetLocations();
3502
3503 Register out_register = locations->Out().AsRegister<Register>();
3504 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003505 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003506 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3507 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 Register num = locations->GetTemp(0).AsRegister<Register>();
3510
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003511 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 __ testl(input_register, input_register);
3513 __ cmovl(kGreaterEqual, num, input_register);
3514 int shift = CTZ(imm);
3515 __ sarl(num, Immediate(shift));
3516
3517 if (imm < 0) {
3518 __ negl(num);
3519 }
3520
3521 __ movl(out_register, num);
3522}
3523
3524void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3525 DCHECK(instruction->IsDiv() || instruction->IsRem());
3526
3527 LocationSummary* locations = instruction->GetLocations();
3528 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3529
3530 Register eax = locations->InAt(0).AsRegister<Register>();
3531 Register out = locations->Out().AsRegister<Register>();
3532 Register num;
3533 Register edx;
3534
3535 if (instruction->IsDiv()) {
3536 edx = locations->GetTemp(0).AsRegister<Register>();
3537 num = locations->GetTemp(1).AsRegister<Register>();
3538 } else {
3539 edx = locations->Out().AsRegister<Register>();
3540 num = locations->GetTemp(0).AsRegister<Register>();
3541 }
3542
3543 DCHECK_EQ(EAX, eax);
3544 DCHECK_EQ(EDX, edx);
3545 if (instruction->IsDiv()) {
3546 DCHECK_EQ(EAX, out);
3547 } else {
3548 DCHECK_EQ(EDX, out);
3549 }
3550
3551 int64_t magic;
3552 int shift;
3553 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3554
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 // Save the numerator.
3556 __ movl(num, eax);
3557
3558 // EAX = magic
3559 __ movl(eax, Immediate(magic));
3560
3561 // EDX:EAX = magic * numerator
3562 __ imull(num);
3563
3564 if (imm > 0 && magic < 0) {
3565 // EDX += num
3566 __ addl(edx, num);
3567 } else if (imm < 0 && magic > 0) {
3568 __ subl(edx, num);
3569 }
3570
3571 // Shift if needed.
3572 if (shift != 0) {
3573 __ sarl(edx, Immediate(shift));
3574 }
3575
3576 // EDX += 1 if EDX < 0
3577 __ movl(eax, edx);
3578 __ shrl(edx, Immediate(31));
3579 __ addl(edx, eax);
3580
3581 if (instruction->IsRem()) {
3582 __ movl(eax, num);
3583 __ imull(edx, Immediate(imm));
3584 __ subl(eax, edx);
3585 __ movl(edx, eax);
3586 } else {
3587 __ movl(eax, edx);
3588 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589}
3590
Calin Juravlebacfec32014-11-14 15:54:36 +00003591void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3592 DCHECK(instruction->IsDiv() || instruction->IsRem());
3593
3594 LocationSummary* locations = instruction->GetLocations();
3595 Location out = locations->Out();
3596 Location first = locations->InAt(0);
3597 Location second = locations->InAt(1);
3598 bool is_div = instruction->IsDiv();
3599
3600 switch (instruction->GetResultType()) {
3601 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 DCHECK_EQ(EAX, first.AsRegister<Register>());
3603 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003604
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003605 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003606 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607
3608 if (imm == 0) {
3609 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3610 } else if (imm == 1 || imm == -1) {
3611 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003612 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003613 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 } else {
3615 DCHECK(imm <= -2 || imm >= 2);
3616 GenerateDivRemWithAnyConstant(instruction);
3617 }
3618 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003619 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3620 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003621 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003622
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 Register second_reg = second.AsRegister<Register>();
3624 // 0x80000000/-1 triggers an arithmetic exception!
3625 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3626 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003627
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 __ cmpl(second_reg, Immediate(-1));
3629 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003630
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003631 // edx:eax <- sign-extended of eax
3632 __ cdq();
3633 // eax = quotient, edx = remainder
3634 __ idivl(second_reg);
3635 __ Bind(slow_path->GetExitLabel());
3636 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003637 break;
3638 }
3639
3640 case Primitive::kPrimLong: {
3641 InvokeRuntimeCallingConvention calling_convention;
3642 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3643 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3645 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3646 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3647 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3648
3649 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003650 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003651 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003653 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003654 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 break;
3657 }
3658
3659 default:
3660 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3661 }
3662}
3663
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003665 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003666 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003667 : LocationSummary::kNoCall;
3668 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3669
Calin Juravle7c4954d2014-10-28 16:57:40 +00003670 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003671 case Primitive::kPrimInt: {
3672 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003673 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003674 locations->SetOut(Location::SameAsFirstInput());
3675 // Intel uses edx:eax as the dividend.
3676 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003677 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3678 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3679 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003680 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003681 locations->AddTemp(Location::RequiresRegister());
3682 }
Calin Juravled0d48522014-11-04 16:40:20 +00003683 break;
3684 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003685 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003686 InvokeRuntimeCallingConvention calling_convention;
3687 locations->SetInAt(0, Location::RegisterPairLocation(
3688 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3689 locations->SetInAt(1, Location::RegisterPairLocation(
3690 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3691 // Runtime helper puts the result in EAX, EDX.
3692 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003693 break;
3694 }
3695 case Primitive::kPrimFloat:
3696 case Primitive::kPrimDouble: {
3697 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003698 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3699 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003700 } else if (div->InputAt(1)->IsConstant()) {
3701 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003702 } else {
3703 locations->SetInAt(1, Location::Any());
3704 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003705 locations->SetOut(Location::SameAsFirstInput());
3706 break;
3707 }
3708
3709 default:
3710 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3711 }
3712}
3713
3714void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3715 LocationSummary* locations = div->GetLocations();
3716 Location first = locations->InAt(0);
3717 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003718
3719 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003720 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003721 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003722 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003723 break;
3724 }
3725
3726 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003727 if (second.IsFpuRegister()) {
3728 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3729 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3730 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003731 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003732 __ divss(first.AsFpuRegister<XmmRegister>(),
3733 codegen_->LiteralFloatAddress(
3734 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003735 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003736 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3737 } else {
3738 DCHECK(second.IsStackSlot());
3739 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3740 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003741 break;
3742 }
3743
3744 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003745 if (second.IsFpuRegister()) {
3746 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3747 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3748 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003749 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003750 __ divsd(first.AsFpuRegister<XmmRegister>(),
3751 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003752 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3753 const_area->GetBaseMethodAddress(),
3754 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003755 } else {
3756 DCHECK(second.IsDoubleStackSlot());
3757 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3758 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003759 break;
3760 }
3761
3762 default:
3763 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3764 }
3765}
3766
Calin Juravlebacfec32014-11-14 15:54:36 +00003767void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003768 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003769
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003770 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003771 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003772 : LocationSummary::kNoCall;
3773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003774
Calin Juravled2ec87d2014-12-08 14:24:46 +00003775 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003776 case Primitive::kPrimInt: {
3777 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003778 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003780 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3781 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3782 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003783 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003784 locations->AddTemp(Location::RequiresRegister());
3785 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003786 break;
3787 }
3788 case Primitive::kPrimLong: {
3789 InvokeRuntimeCallingConvention calling_convention;
3790 locations->SetInAt(0, Location::RegisterPairLocation(
3791 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3792 locations->SetInAt(1, Location::RegisterPairLocation(
3793 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3794 // Runtime helper puts the result in EAX, EDX.
3795 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3796 break;
3797 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003798 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003799 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003800 locations->SetInAt(0, Location::Any());
3801 locations->SetInAt(1, Location::Any());
3802 locations->SetOut(Location::RequiresFpuRegister());
3803 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003804 break;
3805 }
3806
3807 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003808 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 }
3810}
3811
3812void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3813 Primitive::Type type = rem->GetResultType();
3814 switch (type) {
3815 case Primitive::kPrimInt:
3816 case Primitive::kPrimLong: {
3817 GenerateDivRemIntegral(rem);
3818 break;
3819 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003820 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003821 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003822 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003823 break;
3824 }
3825 default:
3826 LOG(FATAL) << "Unexpected rem type " << type;
3827 }
3828}
3829
Calin Juravled0d48522014-11-04 16:40:20 +00003830void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003831 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003832 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003833 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003834 case Primitive::kPrimByte:
3835 case Primitive::kPrimChar:
3836 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003837 case Primitive::kPrimInt: {
3838 locations->SetInAt(0, Location::Any());
3839 break;
3840 }
3841 case Primitive::kPrimLong: {
3842 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3843 if (!instruction->IsConstant()) {
3844 locations->AddTemp(Location::RequiresRegister());
3845 }
3846 break;
3847 }
3848 default:
3849 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3850 }
Calin Juravled0d48522014-11-04 16:40:20 +00003851}
3852
3853void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003854 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003855 codegen_->AddSlowPath(slow_path);
3856
3857 LocationSummary* locations = instruction->GetLocations();
3858 Location value = locations->InAt(0);
3859
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003860 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003861 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003862 case Primitive::kPrimByte:
3863 case Primitive::kPrimChar:
3864 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003865 case Primitive::kPrimInt: {
3866 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003868 __ j(kEqual, slow_path->GetEntryLabel());
3869 } else if (value.IsStackSlot()) {
3870 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3871 __ j(kEqual, slow_path->GetEntryLabel());
3872 } else {
3873 DCHECK(value.IsConstant()) << value;
3874 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003875 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003876 }
3877 }
3878 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003879 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003880 case Primitive::kPrimLong: {
3881 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003882 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003883 __ movl(temp, value.AsRegisterPairLow<Register>());
3884 __ orl(temp, value.AsRegisterPairHigh<Register>());
3885 __ j(kEqual, slow_path->GetEntryLabel());
3886 } else {
3887 DCHECK(value.IsConstant()) << value;
3888 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3889 __ jmp(slow_path->GetEntryLabel());
3890 }
3891 }
3892 break;
3893 }
3894 default:
3895 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003896 }
Calin Juravled0d48522014-11-04 16:40:20 +00003897}
3898
Calin Juravle9aec02f2014-11-18 23:06:35 +00003899void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3900 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3901
3902 LocationSummary* locations =
3903 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3904
3905 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003906 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003908 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003910 // The shift count needs to be in CL or a constant.
3911 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003912 locations->SetOut(Location::SameAsFirstInput());
3913 break;
3914 }
3915 default:
3916 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3917 }
3918}
3919
3920void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3921 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3922
3923 LocationSummary* locations = op->GetLocations();
3924 Location first = locations->InAt(0);
3925 Location second = locations->InAt(1);
3926 DCHECK(first.Equals(locations->Out()));
3927
3928 switch (op->GetResultType()) {
3929 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003930 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003931 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003934 DCHECK_EQ(ECX, second_reg);
3935 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003936 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003937 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003938 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003939 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003940 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941 }
3942 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003943 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003944 if (shift == 0) {
3945 return;
3946 }
3947 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003948 if (op->IsShl()) {
3949 __ shll(first_reg, imm);
3950 } else if (op->IsShr()) {
3951 __ sarl(first_reg, imm);
3952 } else {
3953 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954 }
3955 }
3956 break;
3957 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003958 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003959 if (second.IsRegister()) {
3960 Register second_reg = second.AsRegister<Register>();
3961 DCHECK_EQ(ECX, second_reg);
3962 if (op->IsShl()) {
3963 GenerateShlLong(first, second_reg);
3964 } else if (op->IsShr()) {
3965 GenerateShrLong(first, second_reg);
3966 } else {
3967 GenerateUShrLong(first, second_reg);
3968 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003969 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003970 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003971 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003972 // Nothing to do if the shift is 0, as the input is already the output.
3973 if (shift != 0) {
3974 if (op->IsShl()) {
3975 GenerateShlLong(first, shift);
3976 } else if (op->IsShr()) {
3977 GenerateShrLong(first, shift);
3978 } else {
3979 GenerateUShrLong(first, shift);
3980 }
3981 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003982 }
3983 break;
3984 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003985 default:
3986 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3987 }
3988}
3989
Mark P Mendell73945692015-04-29 14:56:17 +00003990void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3991 Register low = loc.AsRegisterPairLow<Register>();
3992 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003993 if (shift == 1) {
3994 // This is just an addition.
3995 __ addl(low, low);
3996 __ adcl(high, high);
3997 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003998 // Shift by 32 is easy. High gets low, and low gets 0.
3999 codegen_->EmitParallelMoves(
4000 loc.ToLow(),
4001 loc.ToHigh(),
4002 Primitive::kPrimInt,
4003 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4004 loc.ToLow(),
4005 Primitive::kPrimInt);
4006 } else if (shift > 32) {
4007 // Low part becomes 0. High part is low part << (shift-32).
4008 __ movl(high, low);
4009 __ shll(high, Immediate(shift - 32));
4010 __ xorl(low, low);
4011 } else {
4012 // Between 1 and 31.
4013 __ shld(high, low, Immediate(shift));
4014 __ shll(low, Immediate(shift));
4015 }
4016}
4017
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004019 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004020 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4021 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4022 __ testl(shifter, Immediate(32));
4023 __ j(kEqual, &done);
4024 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4025 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4026 __ Bind(&done);
4027}
4028
Mark P Mendell73945692015-04-29 14:56:17 +00004029void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4030 Register low = loc.AsRegisterPairLow<Register>();
4031 Register high = loc.AsRegisterPairHigh<Register>();
4032 if (shift == 32) {
4033 // Need to copy the sign.
4034 DCHECK_NE(low, high);
4035 __ movl(low, high);
4036 __ sarl(high, Immediate(31));
4037 } else if (shift > 32) {
4038 DCHECK_NE(low, high);
4039 // High part becomes sign. Low part is shifted by shift - 32.
4040 __ movl(low, high);
4041 __ sarl(high, Immediate(31));
4042 __ sarl(low, Immediate(shift - 32));
4043 } else {
4044 // Between 1 and 31.
4045 __ shrd(low, high, Immediate(shift));
4046 __ sarl(high, Immediate(shift));
4047 }
4048}
4049
Calin Juravle9aec02f2014-11-18 23:06:35 +00004050void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004051 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004052 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4053 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4054 __ testl(shifter, Immediate(32));
4055 __ j(kEqual, &done);
4056 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4057 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4058 __ Bind(&done);
4059}
4060
Mark P Mendell73945692015-04-29 14:56:17 +00004061void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4062 Register low = loc.AsRegisterPairLow<Register>();
4063 Register high = loc.AsRegisterPairHigh<Register>();
4064 if (shift == 32) {
4065 // Shift by 32 is easy. Low gets high, and high gets 0.
4066 codegen_->EmitParallelMoves(
4067 loc.ToHigh(),
4068 loc.ToLow(),
4069 Primitive::kPrimInt,
4070 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4071 loc.ToHigh(),
4072 Primitive::kPrimInt);
4073 } else if (shift > 32) {
4074 // Low part is high >> (shift - 32). High part becomes 0.
4075 __ movl(low, high);
4076 __ shrl(low, Immediate(shift - 32));
4077 __ xorl(high, high);
4078 } else {
4079 // Between 1 and 31.
4080 __ shrd(low, high, Immediate(shift));
4081 __ shrl(high, Immediate(shift));
4082 }
4083}
4084
Calin Juravle9aec02f2014-11-18 23:06:35 +00004085void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004086 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004087 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4088 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4089 __ testl(shifter, Immediate(32));
4090 __ j(kEqual, &done);
4091 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4092 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4093 __ Bind(&done);
4094}
4095
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004096void LocationsBuilderX86::VisitRor(HRor* ror) {
4097 LocationSummary* locations =
4098 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4099
4100 switch (ror->GetResultType()) {
4101 case Primitive::kPrimLong:
4102 // Add the temporary needed.
4103 locations->AddTemp(Location::RequiresRegister());
4104 FALLTHROUGH_INTENDED;
4105 case Primitive::kPrimInt:
4106 locations->SetInAt(0, Location::RequiresRegister());
4107 // The shift count needs to be in CL (unless it is a constant).
4108 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4109 locations->SetOut(Location::SameAsFirstInput());
4110 break;
4111 default:
4112 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4113 UNREACHABLE();
4114 }
4115}
4116
4117void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4118 LocationSummary* locations = ror->GetLocations();
4119 Location first = locations->InAt(0);
4120 Location second = locations->InAt(1);
4121
4122 if (ror->GetResultType() == Primitive::kPrimInt) {
4123 Register first_reg = first.AsRegister<Register>();
4124 if (second.IsRegister()) {
4125 Register second_reg = second.AsRegister<Register>();
4126 __ rorl(first_reg, second_reg);
4127 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004128 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004129 __ rorl(first_reg, imm);
4130 }
4131 return;
4132 }
4133
4134 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4135 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4136 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4137 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4138 if (second.IsRegister()) {
4139 Register second_reg = second.AsRegister<Register>();
4140 DCHECK_EQ(second_reg, ECX);
4141 __ movl(temp_reg, first_reg_hi);
4142 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4143 __ shrd(first_reg_lo, temp_reg, second_reg);
4144 __ movl(temp_reg, first_reg_hi);
4145 __ testl(second_reg, Immediate(32));
4146 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4147 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4148 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004149 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004150 if (shift_amt == 0) {
4151 // Already fine.
4152 return;
4153 }
4154 if (shift_amt == 32) {
4155 // Just swap.
4156 __ movl(temp_reg, first_reg_lo);
4157 __ movl(first_reg_lo, first_reg_hi);
4158 __ movl(first_reg_hi, temp_reg);
4159 return;
4160 }
4161
4162 Immediate imm(shift_amt);
4163 // Save the constents of the low value.
4164 __ movl(temp_reg, first_reg_lo);
4165
4166 // Shift right into low, feeding bits from high.
4167 __ shrd(first_reg_lo, first_reg_hi, imm);
4168
4169 // Shift right into high, feeding bits from the original low.
4170 __ shrd(first_reg_hi, temp_reg, imm);
4171
4172 // Swap if needed.
4173 if (shift_amt > 32) {
4174 __ movl(temp_reg, first_reg_lo);
4175 __ movl(first_reg_lo, first_reg_hi);
4176 __ movl(first_reg_hi, temp_reg);
4177 }
4178 }
4179}
4180
Calin Juravle9aec02f2014-11-18 23:06:35 +00004181void LocationsBuilderX86::VisitShl(HShl* shl) {
4182 HandleShift(shl);
4183}
4184
4185void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4186 HandleShift(shl);
4187}
4188
4189void LocationsBuilderX86::VisitShr(HShr* shr) {
4190 HandleShift(shr);
4191}
4192
4193void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4194 HandleShift(shr);
4195}
4196
4197void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4198 HandleShift(ushr);
4199}
4200
4201void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4202 HandleShift(ushr);
4203}
4204
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004205void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004206 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004208 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004209 if (instruction->IsStringAlloc()) {
4210 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4211 } else {
4212 InvokeRuntimeCallingConvention calling_convention;
4213 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004214 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004215}
4216
4217void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004218 // Note: if heap poisoning is enabled, the entry point takes cares
4219 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004220 if (instruction->IsStringAlloc()) {
4221 // String is allocated through StringFactory. Call NewEmptyString entry point.
4222 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004223 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004224 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4225 __ call(Address(temp, code_offset.Int32Value()));
4226 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4227 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004228 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004229 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004230 DCHECK(!codegen_->IsLeafMethod());
4231 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004232}
4233
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4235 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004236 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237 locations->SetOut(Location::RegisterLocation(EAX));
4238 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4240 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241}
4242
4243void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004244 // Note: if heap poisoning is enabled, the entry point takes cares
4245 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004246 QuickEntrypointEnum entrypoint =
4247 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4248 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004249 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004250 DCHECK(!codegen_->IsLeafMethod());
4251}
4252
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004253void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004254 LocationSummary* locations =
4255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004256 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4257 if (location.IsStackSlot()) {
4258 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4259 } else if (location.IsDoubleStackSlot()) {
4260 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004261 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004262 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004263}
4264
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004265void InstructionCodeGeneratorX86::VisitParameterValue(
4266 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4267}
4268
4269void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4270 LocationSummary* locations =
4271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4272 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4273}
4274
4275void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004276}
4277
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004278void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4279 LocationSummary* locations =
4280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4281 locations->SetInAt(0, Location::RequiresRegister());
4282 locations->SetOut(Location::RequiresRegister());
4283}
4284
4285void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4286 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004287 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004288 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004289 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004290 __ movl(locations->Out().AsRegister<Register>(),
4291 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004292 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004293 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004294 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004295 __ movl(locations->Out().AsRegister<Register>(),
4296 Address(locations->InAt(0).AsRegister<Register>(),
4297 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4298 // temp = temp->GetImtEntryAt(method_offset);
4299 __ movl(locations->Out().AsRegister<Register>(),
4300 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004301 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004302}
4303
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004304void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004305 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004306 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004307 locations->SetInAt(0, Location::RequiresRegister());
4308 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004309}
4310
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004311void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4312 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004313 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004314 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004315 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004316 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004317 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004318 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004319 break;
4320
4321 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004322 __ notl(out.AsRegisterPairLow<Register>());
4323 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004324 break;
4325
4326 default:
4327 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4328 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004329}
4330
David Brazdil66d126e2015-04-03 16:02:44 +01004331void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4332 LocationSummary* locations =
4333 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4334 locations->SetInAt(0, Location::RequiresRegister());
4335 locations->SetOut(Location::SameAsFirstInput());
4336}
4337
4338void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004339 LocationSummary* locations = bool_not->GetLocations();
4340 Location in = locations->InAt(0);
4341 Location out = locations->Out();
4342 DCHECK(in.Equals(out));
4343 __ xorl(out.AsRegister<Register>(), Immediate(1));
4344}
4345
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004346void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004347 LocationSummary* locations =
4348 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004349 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004350 case Primitive::kPrimBoolean:
4351 case Primitive::kPrimByte:
4352 case Primitive::kPrimShort:
4353 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004354 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004355 case Primitive::kPrimLong: {
4356 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004357 locations->SetInAt(1, Location::Any());
4358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4359 break;
4360 }
4361 case Primitive::kPrimFloat:
4362 case Primitive::kPrimDouble: {
4363 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004364 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4365 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4366 } else if (compare->InputAt(1)->IsConstant()) {
4367 locations->SetInAt(1, Location::RequiresFpuRegister());
4368 } else {
4369 locations->SetInAt(1, Location::Any());
4370 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004371 locations->SetOut(Location::RequiresRegister());
4372 break;
4373 }
4374 default:
4375 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4376 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004377}
4378
4379void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004380 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 Location left = locations->InAt(0);
4383 Location right = locations->InAt(1);
4384
Mark Mendell0c9497d2015-08-21 09:30:05 -04004385 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004386 Condition less_cond = kLess;
4387
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004388 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004389 case Primitive::kPrimBoolean:
4390 case Primitive::kPrimByte:
4391 case Primitive::kPrimShort:
4392 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004393 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004394 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004395 break;
4396 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004397 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 Register left_low = left.AsRegisterPairLow<Register>();
4399 Register left_high = left.AsRegisterPairHigh<Register>();
4400 int32_t val_low = 0;
4401 int32_t val_high = 0;
4402 bool right_is_const = false;
4403
4404 if (right.IsConstant()) {
4405 DCHECK(right.GetConstant()->IsLongConstant());
4406 right_is_const = true;
4407 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4408 val_low = Low32Bits(val);
4409 val_high = High32Bits(val);
4410 }
4411
Calin Juravleddb7df22014-11-25 20:56:51 +00004412 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004413 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004414 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004415 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004416 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004417 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004418 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004420 __ j(kLess, &less); // Signed compare.
4421 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004422 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004423 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004424 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004425 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004426 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004427 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004428 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 }
Aart Bika19616e2016-02-01 18:57:58 -08004430 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004431 break;
4432 }
4433 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004434 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004435 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004436 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004437 break;
4438 }
4439 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004440 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004441 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004442 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004443 break;
4444 }
4445 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004446 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004447 }
Aart Bika19616e2016-02-01 18:57:58 -08004448
Calin Juravleddb7df22014-11-25 20:56:51 +00004449 __ movl(out, Immediate(0));
4450 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004451 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004452
4453 __ Bind(&greater);
4454 __ movl(out, Immediate(1));
4455 __ jmp(&done);
4456
4457 __ Bind(&less);
4458 __ movl(out, Immediate(-1));
4459
4460 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004461}
4462
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004463void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004464 LocationSummary* locations =
4465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004466 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004467 locations->SetInAt(i, Location::Any());
4468 }
4469 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004470}
4471
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004472void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004473 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004474}
4475
Roland Levillain7c1559a2015-12-15 10:55:36 +00004476void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004477 /*
4478 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4479 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4480 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4481 */
4482 switch (kind) {
4483 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004484 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004485 break;
4486 }
4487 case MemBarrierKind::kAnyStore:
4488 case MemBarrierKind::kLoadAny:
4489 case MemBarrierKind::kStoreStore: {
4490 // nop
4491 break;
4492 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004493 case MemBarrierKind::kNTStoreStore:
4494 // Non-Temporal Store/Store needs an explicit fence.
4495 MemoryFence(/* non-temporal */ true);
4496 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004497 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004498}
4499
Vladimir Markodc151b22015-10-15 18:02:30 +01004500HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4501 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004502 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004503 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004504}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004506Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4507 Register temp) {
4508 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004509 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004510 if (!invoke->GetLocations()->Intrinsified()) {
4511 return location.AsRegister<Register>();
4512 }
4513 // For intrinsics we allow any location, so it may be on the stack.
4514 if (!location.IsRegister()) {
4515 __ movl(temp, Address(ESP, location.GetStackIndex()));
4516 return temp;
4517 }
4518 // For register locations, check if the register was saved. If so, get it from the stack.
4519 // Note: There is a chance that the register was saved but not overwritten, so we could
4520 // save one load. However, since this is just an intrinsic slow path we prefer this
4521 // simple and more robust approach rather that trying to determine if that's the case.
4522 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004523 if (slow_path != nullptr) {
4524 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4525 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4526 __ movl(temp, Address(ESP, stack_offset));
4527 return temp;
4528 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 }
4530 return location.AsRegister<Register>();
4531}
4532
Serguei Katkov288c7a82016-05-16 11:53:15 +06004533Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4534 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004535 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4536 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004537 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004538 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004539 uint32_t offset =
4540 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4541 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004542 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004543 }
Vladimir Marko58155012015-08-19 12:49:41 +00004544 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004545 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004546 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004547 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4548 DCHECK(GetCompilerOptions().IsBootImage());
4549 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4550 temp.AsRegister<Register>());
4551 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4552 RecordBootMethodPatch(invoke);
4553 break;
4554 }
Vladimir Marko58155012015-08-19 12:49:41 +00004555 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4556 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4557 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004558 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4559 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4560 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004561 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004562 // Bind a new fixup label at the end of the "movl" insn.
4563 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004564 __ Bind(NewPcRelativeDexCacheArrayPatch(
4565 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4566 invoke->GetDexFileForPcRelativeDexCache(),
4567 offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004568 break;
4569 }
Vladimir Marko58155012015-08-19 12:49:41 +00004570 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004571 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004572 Register method_reg;
4573 Register reg = temp.AsRegister<Register>();
4574 if (current_method.IsRegister()) {
4575 method_reg = current_method.AsRegister<Register>();
4576 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004577 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004578 DCHECK(!current_method.IsValid());
4579 method_reg = reg;
4580 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4581 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004582 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004583 __ movl(reg, Address(method_reg,
4584 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004585 // temp = temp[index_in_cache];
4586 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4587 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004588 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4589 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004590 }
Vladimir Marko58155012015-08-19 12:49:41 +00004591 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004592 return callee_method;
4593}
4594
4595void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4596 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004597
4598 switch (invoke->GetCodePtrLocation()) {
4599 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4600 __ call(GetFrameEntryLabel());
4601 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004602 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4603 // (callee_method + offset_of_quick_compiled_code)()
4604 __ call(Address(callee_method.AsRegister<Register>(),
4605 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004606 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004607 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004608 }
4609
4610 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004611}
4612
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004613void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4614 Register temp = temp_in.AsRegister<Register>();
4615 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4616 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004617
4618 // Use the calling convention instead of the location of the receiver, as
4619 // intrinsics may have put the receiver in a different register. In the intrinsics
4620 // slow path, the arguments have been moved to the right place, so here we are
4621 // guaranteed that the receiver is the first register of the calling convention.
4622 InvokeDexCallingConvention calling_convention;
4623 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004624 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004625 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004626 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004627 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004628 // Instead of simply (possibly) unpoisoning `temp` here, we should
4629 // emit a read barrier for the previous class reference load.
4630 // However this is not required in practice, as this is an
4631 // intermediate/temporary reference and because the current
4632 // concurrent copying collector keeps the from-space memory
4633 // intact/accessible until the end of the marking phase (the
4634 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004635 __ MaybeUnpoisonHeapReference(temp);
4636 // temp = temp->GetMethodAt(method_offset);
4637 __ movl(temp, Address(temp, method_offset));
4638 // call temp->GetEntryPoint();
4639 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004640 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004641}
4642
Vladimir Marko65979462017-05-19 17:25:12 +01004643void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4644 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4645 HX86ComputeBaseMethodAddress* address =
4646 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4647 boot_image_method_patches_.emplace_back(address,
4648 *invoke->GetTargetMethod().dex_file,
4649 invoke->GetTargetMethod().dex_method_index);
4650 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004651}
4652
Vladimir Marko1998cd02017-01-13 13:02:58 +00004653void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004654 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004655 boot_image_type_patches_.emplace_back(address,
4656 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004657 load_class->GetTypeIndex().index_);
4658 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004659}
4660
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004661Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004662 HX86ComputeBaseMethodAddress* address =
4663 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4664 type_bss_entry_patches_.emplace_back(
4665 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004666 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004667}
4668
Vladimir Marko65979462017-05-19 17:25:12 +01004669void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4670 DCHECK(GetCompilerOptions().IsBootImage());
4671 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4672 string_patches_.emplace_back(address,
4673 load_string->GetDexFile(),
4674 load_string->GetStringIndex().index_);
4675 __ Bind(&string_patches_.back().label);
4676}
4677
Vladimir Markoaad75c62016-10-03 08:46:48 +00004678Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4679 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004680 HX86ComputeBaseMethodAddress* address =
4681 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4682 string_patches_.emplace_back(
4683 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004684 return &string_patches_.back().label;
4685}
4686
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004687Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(
4688 HX86ComputeBaseMethodAddress* method_address,
4689 const DexFile& dex_file,
4690 uint32_t element_offset) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004691 // Add the patch entry and bind its label at the end of the instruction.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004692 pc_relative_dex_cache_patches_.emplace_back(method_address, dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004693 return &pc_relative_dex_cache_patches_.back().label;
4694}
4695
Vladimir Markoaad75c62016-10-03 08:46:48 +00004696// The label points to the end of the "movl" or another instruction but the literal offset
4697// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4698constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4699
4700template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4701inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004702 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004703 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004704 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004705 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004706 linker_patches->push_back(Factory(
4707 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004708 }
4709}
4710
Vladimir Marko58155012015-08-19 12:49:41 +00004711void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4712 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004713 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004714 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004715 boot_image_method_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004716 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004717 type_bss_entry_patches_.size() +
4718 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004719 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004720 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4721 linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01004722 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01004723 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
4724 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004725 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4726 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004727 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004728 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004729 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01004730 DCHECK(boot_image_type_patches_.empty());
4731 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004732 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004733 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4734 linker_patches);
4735 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004736}
4737
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004738void CodeGeneratorX86::MarkGCCard(Register temp,
4739 Register card,
4740 Register object,
4741 Register value,
4742 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004743 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004744 if (value_can_be_null) {
4745 __ testl(value, value);
4746 __ j(kEqual, &is_null);
4747 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004748 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749 __ movl(temp, object);
4750 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004751 __ movb(Address(temp, card, TIMES_1, 0),
4752 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004753 if (value_can_be_null) {
4754 __ Bind(&is_null);
4755 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004756}
4757
Calin Juravle52c48962014-12-16 17:02:57 +00004758void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4759 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004760
4761 bool object_field_get_with_read_barrier =
4762 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004763 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004764 new (GetGraph()->GetArena()) LocationSummary(instruction,
4765 kEmitCompilerReadBarrier ?
4766 LocationSummary::kCallOnSlowPath :
4767 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004768 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004769 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004770 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004771 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004772
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004773 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4774 locations->SetOut(Location::RequiresFpuRegister());
4775 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004776 // The output overlaps in case of long: we don't want the low move
4777 // to overwrite the object's location. Likewise, in the case of
4778 // an object field get with read barriers enabled, we do not want
4779 // the move to overwrite the object's location, as we need it to emit
4780 // the read barrier.
4781 locations->SetOut(
4782 Location::RequiresRegister(),
4783 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4784 Location::kOutputOverlap :
4785 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004786 }
Calin Juravle52c48962014-12-16 17:02:57 +00004787
4788 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4789 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004790 // So we use an XMM register as a temp to achieve atomicity (first
4791 // load the temp into the XMM and then copy the XMM into the
4792 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004793 locations->AddTemp(Location::RequiresFpuRegister());
4794 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004795}
4796
Calin Juravle52c48962014-12-16 17:02:57 +00004797void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4798 const FieldInfo& field_info) {
4799 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004800
Calin Juravle52c48962014-12-16 17:02:57 +00004801 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004802 Location base_loc = locations->InAt(0);
4803 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004804 Location out = locations->Out();
4805 bool is_volatile = field_info.IsVolatile();
4806 Primitive::Type field_type = field_info.GetFieldType();
4807 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4808
4809 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004810 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004811 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004812 break;
4813 }
4814
4815 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004816 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004817 break;
4818 }
4819
4820 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004821 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004822 break;
4823 }
4824
4825 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004826 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004827 break;
4828 }
4829
4830 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004831 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004833
4834 case Primitive::kPrimNot: {
4835 // /* HeapReference<Object> */ out = *(base + offset)
4836 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004837 // Note that a potential implicit null check is handled in this
4838 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4839 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004840 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004841 if (is_volatile) {
4842 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4843 }
4844 } else {
4845 __ movl(out.AsRegister<Register>(), Address(base, offset));
4846 codegen_->MaybeRecordImplicitNullCheck(instruction);
4847 if (is_volatile) {
4848 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4849 }
4850 // If read barriers are enabled, emit read barriers other than
4851 // Baker's using a slow path (and also unpoison the loaded
4852 // reference, if heap poisoning is enabled).
4853 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4854 }
4855 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004856 }
4857
4858 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004859 if (is_volatile) {
4860 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4861 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004862 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004863 __ movd(out.AsRegisterPairLow<Register>(), temp);
4864 __ psrlq(temp, Immediate(32));
4865 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4866 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004867 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004868 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004870 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4871 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004872 break;
4873 }
4874
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004875 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004876 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004877 break;
4878 }
4879
4880 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004881 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004882 break;
4883 }
4884
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004885 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004886 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004887 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004888 }
Calin Juravle52c48962014-12-16 17:02:57 +00004889
Roland Levillain7c1559a2015-12-15 10:55:36 +00004890 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4891 // Potential implicit null checks, in the case of reference or
4892 // long fields, are handled in the previous switch statement.
4893 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004894 codegen_->MaybeRecordImplicitNullCheck(instruction);
4895 }
4896
Calin Juravle52c48962014-12-16 17:02:57 +00004897 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004898 if (field_type == Primitive::kPrimNot) {
4899 // Memory barriers, in the case of references, are also handled
4900 // in the previous switch statement.
4901 } else {
4902 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4903 }
Roland Levillain4d027112015-07-01 15:41:14 +01004904 }
Calin Juravle52c48962014-12-16 17:02:57 +00004905}
4906
4907void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4908 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4909
4910 LocationSummary* locations =
4911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4912 locations->SetInAt(0, Location::RequiresRegister());
4913 bool is_volatile = field_info.IsVolatile();
4914 Primitive::Type field_type = field_info.GetFieldType();
4915 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4916 || (field_type == Primitive::kPrimByte);
4917
4918 // The register allocator does not support multiple
4919 // inputs that die at entry with one in a specific register.
4920 if (is_byte_type) {
4921 // Ensure the value is in a byte register.
4922 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004923 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004924 if (is_volatile && field_type == Primitive::kPrimDouble) {
4925 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4926 locations->SetInAt(1, Location::RequiresFpuRegister());
4927 } else {
4928 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4929 }
4930 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4931 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004932 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004933
Calin Juravle52c48962014-12-16 17:02:57 +00004934 // 64bits value can be atomically written to an address with movsd and an XMM register.
4935 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4936 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4937 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4938 // isolated cases when we need this it isn't worth adding the extra complexity.
4939 locations->AddTemp(Location::RequiresFpuRegister());
4940 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004941 } else {
4942 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4943
4944 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4945 // Temporary registers for the write barrier.
4946 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4947 // Ensure the card is in a byte register.
4948 locations->AddTemp(Location::RegisterLocation(ECX));
4949 }
Calin Juravle52c48962014-12-16 17:02:57 +00004950 }
4951}
4952
4953void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004954 const FieldInfo& field_info,
4955 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004956 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4957
4958 LocationSummary* locations = instruction->GetLocations();
4959 Register base = locations->InAt(0).AsRegister<Register>();
4960 Location value = locations->InAt(1);
4961 bool is_volatile = field_info.IsVolatile();
4962 Primitive::Type field_type = field_info.GetFieldType();
4963 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004964 bool needs_write_barrier =
4965 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004966
4967 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004968 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004969 }
4970
Mark Mendell81489372015-11-04 11:30:41 -05004971 bool maybe_record_implicit_null_check_done = false;
4972
Calin Juravle52c48962014-12-16 17:02:57 +00004973 switch (field_type) {
4974 case Primitive::kPrimBoolean:
4975 case Primitive::kPrimByte: {
4976 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4977 break;
4978 }
4979
4980 case Primitive::kPrimShort:
4981 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004982 if (value.IsConstant()) {
4983 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4984 __ movw(Address(base, offset), Immediate(v));
4985 } else {
4986 __ movw(Address(base, offset), value.AsRegister<Register>());
4987 }
Calin Juravle52c48962014-12-16 17:02:57 +00004988 break;
4989 }
4990
4991 case Primitive::kPrimInt:
4992 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004993 if (kPoisonHeapReferences && needs_write_barrier) {
4994 // Note that in the case where `value` is a null reference,
4995 // we do not enter this block, as the reference does not
4996 // need poisoning.
4997 DCHECK_EQ(field_type, Primitive::kPrimNot);
4998 Register temp = locations->GetTemp(0).AsRegister<Register>();
4999 __ movl(temp, value.AsRegister<Register>());
5000 __ PoisonHeapReference(temp);
5001 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005002 } else if (value.IsConstant()) {
5003 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5004 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005005 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005006 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005007 __ movl(Address(base, offset), value.AsRegister<Register>());
5008 }
Calin Juravle52c48962014-12-16 17:02:57 +00005009 break;
5010 }
5011
5012 case Primitive::kPrimLong: {
5013 if (is_volatile) {
5014 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5015 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5016 __ movd(temp1, value.AsRegisterPairLow<Register>());
5017 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5018 __ punpckldq(temp1, temp2);
5019 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005020 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005021 } else if (value.IsConstant()) {
5022 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5023 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5024 codegen_->MaybeRecordImplicitNullCheck(instruction);
5025 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005026 } else {
5027 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005028 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005029 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5030 }
Mark Mendell81489372015-11-04 11:30:41 -05005031 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005032 break;
5033 }
5034
5035 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005036 if (value.IsConstant()) {
5037 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5038 __ movl(Address(base, offset), Immediate(v));
5039 } else {
5040 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5041 }
Calin Juravle52c48962014-12-16 17:02:57 +00005042 break;
5043 }
5044
5045 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005046 if (value.IsConstant()) {
5047 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5048 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5049 codegen_->MaybeRecordImplicitNullCheck(instruction);
5050 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5051 maybe_record_implicit_null_check_done = true;
5052 } else {
5053 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5054 }
Calin Juravle52c48962014-12-16 17:02:57 +00005055 break;
5056 }
5057
5058 case Primitive::kPrimVoid:
5059 LOG(FATAL) << "Unreachable type " << field_type;
5060 UNREACHABLE();
5061 }
5062
Mark Mendell81489372015-11-04 11:30:41 -05005063 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005064 codegen_->MaybeRecordImplicitNullCheck(instruction);
5065 }
5066
Roland Levillain4d027112015-07-01 15:41:14 +01005067 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005068 Register temp = locations->GetTemp(0).AsRegister<Register>();
5069 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005070 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005071 }
5072
Calin Juravle52c48962014-12-16 17:02:57 +00005073 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005074 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005075 }
5076}
5077
5078void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5079 HandleFieldGet(instruction, instruction->GetFieldInfo());
5080}
5081
5082void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5083 HandleFieldGet(instruction, instruction->GetFieldInfo());
5084}
5085
5086void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5087 HandleFieldSet(instruction, instruction->GetFieldInfo());
5088}
5089
5090void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005091 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005092}
5093
5094void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5095 HandleFieldSet(instruction, instruction->GetFieldInfo());
5096}
5097
5098void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005099 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005100}
5101
5102void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5103 HandleFieldGet(instruction, instruction->GetFieldInfo());
5104}
5105
5106void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5107 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005108}
5109
Calin Juravlee460d1d2015-09-29 04:52:17 +01005110void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5111 HUnresolvedInstanceFieldGet* instruction) {
5112 FieldAccessCallingConventionX86 calling_convention;
5113 codegen_->CreateUnresolvedFieldLocationSummary(
5114 instruction, instruction->GetFieldType(), calling_convention);
5115}
5116
5117void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5118 HUnresolvedInstanceFieldGet* instruction) {
5119 FieldAccessCallingConventionX86 calling_convention;
5120 codegen_->GenerateUnresolvedFieldAccess(instruction,
5121 instruction->GetFieldType(),
5122 instruction->GetFieldIndex(),
5123 instruction->GetDexPc(),
5124 calling_convention);
5125}
5126
5127void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5128 HUnresolvedInstanceFieldSet* instruction) {
5129 FieldAccessCallingConventionX86 calling_convention;
5130 codegen_->CreateUnresolvedFieldLocationSummary(
5131 instruction, instruction->GetFieldType(), calling_convention);
5132}
5133
5134void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5135 HUnresolvedInstanceFieldSet* instruction) {
5136 FieldAccessCallingConventionX86 calling_convention;
5137 codegen_->GenerateUnresolvedFieldAccess(instruction,
5138 instruction->GetFieldType(),
5139 instruction->GetFieldIndex(),
5140 instruction->GetDexPc(),
5141 calling_convention);
5142}
5143
5144void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5145 HUnresolvedStaticFieldGet* instruction) {
5146 FieldAccessCallingConventionX86 calling_convention;
5147 codegen_->CreateUnresolvedFieldLocationSummary(
5148 instruction, instruction->GetFieldType(), calling_convention);
5149}
5150
5151void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5152 HUnresolvedStaticFieldGet* instruction) {
5153 FieldAccessCallingConventionX86 calling_convention;
5154 codegen_->GenerateUnresolvedFieldAccess(instruction,
5155 instruction->GetFieldType(),
5156 instruction->GetFieldIndex(),
5157 instruction->GetDexPc(),
5158 calling_convention);
5159}
5160
5161void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5162 HUnresolvedStaticFieldSet* instruction) {
5163 FieldAccessCallingConventionX86 calling_convention;
5164 codegen_->CreateUnresolvedFieldLocationSummary(
5165 instruction, instruction->GetFieldType(), calling_convention);
5166}
5167
5168void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5169 HUnresolvedStaticFieldSet* instruction) {
5170 FieldAccessCallingConventionX86 calling_convention;
5171 codegen_->GenerateUnresolvedFieldAccess(instruction,
5172 instruction->GetFieldType(),
5173 instruction->GetFieldIndex(),
5174 instruction->GetDexPc(),
5175 calling_convention);
5176}
5177
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005178void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005179 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5180 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5181 ? Location::RequiresRegister()
5182 : Location::Any();
5183 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184}
5185
Calin Juravle2ae48182016-03-16 14:05:09 +00005186void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5187 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005188 return;
5189 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005190 LocationSummary* locations = instruction->GetLocations();
5191 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005192
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005193 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005194 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005195}
5196
Calin Juravle2ae48182016-03-16 14:05:09 +00005197void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005198 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005199 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005200
5201 LocationSummary* locations = instruction->GetLocations();
5202 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005203
5204 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005205 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005206 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005208 } else {
5209 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005210 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005211 __ jmp(slow_path->GetEntryLabel());
5212 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005213 }
5214 __ j(kEqual, slow_path->GetEntryLabel());
5215}
5216
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005217void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005218 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005219}
5220
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005222 bool object_array_get_with_read_barrier =
5223 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005224 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005225 new (GetGraph()->GetArena()) LocationSummary(instruction,
5226 object_array_get_with_read_barrier ?
5227 LocationSummary::kCallOnSlowPath :
5228 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005229 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005230 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005231 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005232 locations->SetInAt(0, Location::RequiresRegister());
5233 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005234 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5235 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5236 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005237 // The output overlaps in case of long: we don't want the low move
5238 // to overwrite the array's location. Likewise, in the case of an
5239 // object array get with read barriers enabled, we do not want the
5240 // move to overwrite the array's location, as we need it to emit
5241 // the read barrier.
5242 locations->SetOut(
5243 Location::RequiresRegister(),
5244 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5245 Location::kOutputOverlap :
5246 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248}
5249
5250void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5251 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005252 Location obj_loc = locations->InAt(0);
5253 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005255 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005256 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257
Calin Juravle77520bc2015-01-12 18:45:46 +00005258 Primitive::Type type = instruction->GetType();
5259 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005261 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005262 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 break;
5264 }
5265
5266 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005267 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005268 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 break;
5270 }
5271
5272 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005274 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 break;
5276 }
5277
5278 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005279 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005280 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5281 // Branch cases into compressed and uncompressed for each index's type.
5282 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5283 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005284 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005285 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005286 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5287 "Expecting 0=compressed, 1=uncompressed");
5288 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005289 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5290 __ jmp(&done);
5291 __ Bind(&not_compressed);
5292 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5293 __ Bind(&done);
5294 } else {
5295 // Common case for charAt of array of char or when string compression's
5296 // feature is turned off.
5297 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 break;
5300 }
5301
Roland Levillain7c1559a2015-12-15 10:55:36 +00005302 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005303 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005304 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 break;
5306 }
5307
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 case Primitive::kPrimNot: {
5309 static_assert(
5310 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5311 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005312 // /* HeapReference<Object> */ out =
5313 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5314 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005315 // Note that a potential implicit null check is handled in this
5316 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5317 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005318 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005319 } else {
5320 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005321 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5322 codegen_->MaybeRecordImplicitNullCheck(instruction);
5323 // If read barriers are enabled, emit read barriers other than
5324 // Baker's using a slow path (and also unpoison the loaded
5325 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005326 if (index.IsConstant()) {
5327 uint32_t offset =
5328 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005329 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5330 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005331 codegen_->MaybeGenerateReadBarrierSlow(
5332 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5333 }
5334 }
5335 break;
5336 }
5337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005339 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005340 __ movl(out_loc.AsRegisterPairLow<Register>(),
5341 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5342 codegen_->MaybeRecordImplicitNullCheck(instruction);
5343 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5344 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 break;
5346 }
5347
Mark Mendell7c8d0092015-01-26 11:21:33 -05005348 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005349 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005350 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005351 break;
5352 }
5353
5354 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005355 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005356 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005357 break;
5358 }
5359
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005361 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005362 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005364
Roland Levillain7c1559a2015-12-15 10:55:36 +00005365 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5366 // Potential implicit null checks, in the case of reference or
5367 // long arrays, are handled in the previous switch statement.
5368 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005369 codegen_->MaybeRecordImplicitNullCheck(instruction);
5370 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371}
5372
5373void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005374 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005375
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005376 bool needs_write_barrier =
5377 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005378 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005379
Nicolas Geoffray39468442014-09-02 15:17:15 +01005380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5381 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005382 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005383 LocationSummary::kCallOnSlowPath :
5384 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005385
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005386 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5387 || (value_type == Primitive::kPrimByte);
5388 // We need the inputs to be different than the output in case of long operation.
5389 // In case of a byte operation, the register allocator does not support multiple
5390 // inputs that die at entry with one in a specific register.
5391 locations->SetInAt(0, Location::RequiresRegister());
5392 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5393 if (is_byte_type) {
5394 // Ensure the value is in a byte register.
5395 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5396 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005397 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5400 }
5401 if (needs_write_barrier) {
5402 // Temporary registers for the write barrier.
5403 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5404 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005405 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407}
5408
5409void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5410 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005411 Location array_loc = locations->InAt(0);
5412 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005414 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005415 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5417 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5418 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005419 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005420 bool needs_write_barrier =
5421 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422
5423 switch (value_type) {
5424 case Primitive::kPrimBoolean:
5425 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005427 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 if (value.IsRegister()) {
5429 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 break;
5435 }
5436
5437 case Primitive::kPrimShort:
5438 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005440 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441 if (value.IsRegister()) {
5442 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447 break;
5448 }
5449
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005450 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005451 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005452 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005453
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005454 if (!value.IsRegister()) {
5455 // Just setting null.
5456 DCHECK(instruction->InputAt(2)->IsNullConstant());
5457 DCHECK(value.IsConstant()) << value;
5458 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005459 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005460 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005461 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005462 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005464
5465 DCHECK(needs_write_barrier);
5466 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005467 // We cannot use a NearLabel for `done`, as its range may be too
5468 // short when Baker read barriers are enabled.
5469 Label done;
5470 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005471 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005472 Location temp_loc = locations->GetTemp(0);
5473 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005474 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5476 codegen_->AddSlowPath(slow_path);
5477 if (instruction->GetValueCanBeNull()) {
5478 __ testl(register_value, register_value);
5479 __ j(kNotEqual, &not_null);
5480 __ movl(address, Immediate(0));
5481 codegen_->MaybeRecordImplicitNullCheck(instruction);
5482 __ jmp(&done);
5483 __ Bind(&not_null);
5484 }
5485
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005486 // Note that when Baker read barriers are enabled, the type
5487 // checks are performed without read barriers. This is fine,
5488 // even in the case where a class object is in the from-space
5489 // after the flip, as a comparison involving such a type would
5490 // not produce a false positive; it may of course produce a
5491 // false negative, in which case we would take the ArraySet
5492 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005493
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005494 // /* HeapReference<Class> */ temp = array->klass_
5495 __ movl(temp, Address(array, class_offset));
5496 codegen_->MaybeRecordImplicitNullCheck(instruction);
5497 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005498
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005499 // /* HeapReference<Class> */ temp = temp->component_type_
5500 __ movl(temp, Address(temp, component_offset));
5501 // If heap poisoning is enabled, no need to unpoison `temp`
5502 // nor the object reference in `register_value->klass`, as
5503 // we are comparing two poisoned references.
5504 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005505
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005506 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5507 __ j(kEqual, &do_put);
5508 // If heap poisoning is enabled, the `temp` reference has
5509 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005510 __ MaybeUnpoisonHeapReference(temp);
5511
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005512 // If heap poisoning is enabled, no need to unpoison the
5513 // heap reference loaded below, as it is only used for a
5514 // comparison with null.
5515 __ cmpl(Address(temp, super_offset), Immediate(0));
5516 __ j(kNotEqual, slow_path->GetEntryLabel());
5517 __ Bind(&do_put);
5518 } else {
5519 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005520 }
5521 }
5522
5523 if (kPoisonHeapReferences) {
5524 __ movl(temp, register_value);
5525 __ PoisonHeapReference(temp);
5526 __ movl(address, temp);
5527 } else {
5528 __ movl(address, register_value);
5529 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005530 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005531 codegen_->MaybeRecordImplicitNullCheck(instruction);
5532 }
5533
5534 Register card = locations->GetTemp(1).AsRegister<Register>();
5535 codegen_->MarkGCCard(
5536 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5537 __ Bind(&done);
5538
5539 if (slow_path != nullptr) {
5540 __ Bind(slow_path->GetExitLabel());
5541 }
5542
5543 break;
5544 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005545
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005546 case Primitive::kPrimInt: {
5547 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005548 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005549 if (value.IsRegister()) {
5550 __ movl(address, value.AsRegister<Register>());
5551 } else {
5552 DCHECK(value.IsConstant()) << value;
5553 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5554 __ movl(address, Immediate(v));
5555 }
5556 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 break;
5558 }
5559
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005560 case Primitive::kPrimLong: {
5561 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005562 if (value.IsRegisterPair()) {
5563 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5564 value.AsRegisterPairLow<Register>());
5565 codegen_->MaybeRecordImplicitNullCheck(instruction);
5566 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5567 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005569 DCHECK(value.IsConstant());
5570 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5571 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5572 Immediate(Low32Bits(val)));
5573 codegen_->MaybeRecordImplicitNullCheck(instruction);
5574 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5575 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 }
5577 break;
5578 }
5579
Mark Mendell7c8d0092015-01-26 11:21:33 -05005580 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005581 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005582 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005583 if (value.IsFpuRegister()) {
5584 __ movss(address, value.AsFpuRegister<XmmRegister>());
5585 } else {
5586 DCHECK(value.IsConstant());
5587 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5588 __ movl(address, Immediate(v));
5589 }
5590 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005591 break;
5592 }
5593
5594 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005595 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005596 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005597 if (value.IsFpuRegister()) {
5598 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5599 } else {
5600 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005601 Address address_hi =
5602 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005603 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5604 __ movl(address, Immediate(Low32Bits(v)));
5605 codegen_->MaybeRecordImplicitNullCheck(instruction);
5606 __ movl(address_hi, Immediate(High32Bits(v)));
5607 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005608 break;
5609 }
5610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611 case Primitive::kPrimVoid:
5612 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005613 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005614 }
5615}
5616
5617void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005619 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005620 if (!instruction->IsEmittedAtUseSite()) {
5621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5622 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005623}
5624
5625void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005626 if (instruction->IsEmittedAtUseSite()) {
5627 return;
5628 }
5629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005631 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005632 Register obj = locations->InAt(0).AsRegister<Register>();
5633 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005635 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005636 // Mask out most significant bit in case the array is String's array of char.
5637 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005638 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005639 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005640}
5641
5642void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005643 RegisterSet caller_saves = RegisterSet::Empty();
5644 InvokeRuntimeCallingConvention calling_convention;
5645 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5646 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5647 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005648 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005649 HInstruction* length = instruction->InputAt(1);
5650 if (!length->IsEmittedAtUseSite()) {
5651 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5652 }
jessicahandojo4877b792016-09-08 19:49:13 -07005653 // Need register to see array's length.
5654 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5655 locations->AddTemp(Location::RequiresRegister());
5656 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005657}
5658
5659void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005660 const bool is_string_compressed_char_at =
5661 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005662 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005663 Location index_loc = locations->InAt(0);
5664 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005665 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005666 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005667
Mark Mendell99dbd682015-04-22 16:18:52 -04005668 if (length_loc.IsConstant()) {
5669 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5670 if (index_loc.IsConstant()) {
5671 // BCE will remove the bounds check if we are guarenteed to pass.
5672 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5673 if (index < 0 || index >= length) {
5674 codegen_->AddSlowPath(slow_path);
5675 __ jmp(slow_path->GetEntryLabel());
5676 } else {
5677 // Some optimization after BCE may have generated this, and we should not
5678 // generate a bounds check if it is a valid range.
5679 }
5680 return;
5681 }
5682
5683 // We have to reverse the jump condition because the length is the constant.
5684 Register index_reg = index_loc.AsRegister<Register>();
5685 __ cmpl(index_reg, Immediate(length));
5686 codegen_->AddSlowPath(slow_path);
5687 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005688 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005689 HInstruction* array_length = instruction->InputAt(1);
5690 if (array_length->IsEmittedAtUseSite()) {
5691 // Address the length field in the array.
5692 DCHECK(array_length->IsArrayLength());
5693 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5694 Location array_loc = array_length->GetLocations()->InAt(0);
5695 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005696 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005697 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5698 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005699 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5700 __ movl(length_reg, array_len);
5701 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005702 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005703 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005704 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005705 // Checking bounds for general case:
5706 // Array of char or string's array with feature compression off.
5707 if (index_loc.IsConstant()) {
5708 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5709 __ cmpl(array_len, Immediate(value));
5710 } else {
5711 __ cmpl(array_len, index_loc.AsRegister<Register>());
5712 }
5713 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005714 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005715 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005716 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005717 }
5718 codegen_->AddSlowPath(slow_path);
5719 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005720 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005721}
5722
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005723void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005724 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005725}
5726
5727void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005728 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5729}
5730
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005731void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005732 LocationSummary* locations =
5733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005734 // In suspend check slow path, usually there are no caller-save registers at all.
5735 // If SIMD instructions are present, however, we force spilling all live SIMD
5736 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005737 locations->SetCustomSlowPathCallerSaves(
5738 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005739}
5740
5741void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005742 HBasicBlock* block = instruction->GetBlock();
5743 if (block->GetLoopInformation() != nullptr) {
5744 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5745 // The back edge will generate the suspend check.
5746 return;
5747 }
5748 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5749 // The goto will generate the suspend check.
5750 return;
5751 }
5752 GenerateSuspendCheck(instruction, nullptr);
5753}
5754
5755void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5756 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005757 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005758 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5759 if (slow_path == nullptr) {
5760 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5761 instruction->SetSlowPath(slow_path);
5762 codegen_->AddSlowPath(slow_path);
5763 if (successor != nullptr) {
5764 DCHECK(successor->IsLoopHeader());
5765 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5766 }
5767 } else {
5768 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5769 }
5770
Andreas Gampe542451c2016-07-26 09:02:02 -07005771 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005772 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005773 if (successor == nullptr) {
5774 __ j(kNotEqual, slow_path->GetEntryLabel());
5775 __ Bind(slow_path->GetReturnLabel());
5776 } else {
5777 __ j(kEqual, codegen_->GetLabelOf(successor));
5778 __ jmp(slow_path->GetEntryLabel());
5779 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005780}
5781
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005782X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5783 return codegen_->GetAssembler();
5784}
5785
Mark Mendell7c8d0092015-01-26 11:21:33 -05005786void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005787 ScratchRegisterScope ensure_scratch(
5788 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5789 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5790 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5791 __ movl(temp_reg, Address(ESP, src + stack_offset));
5792 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005793}
5794
5795void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005796 ScratchRegisterScope ensure_scratch(
5797 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5798 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5799 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5800 __ movl(temp_reg, Address(ESP, src + stack_offset));
5801 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5802 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5803 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005804}
5805
5806void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005807 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005808 Location source = move->GetSource();
5809 Location destination = move->GetDestination();
5810
5811 if (source.IsRegister()) {
5812 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005813 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005814 } else if (destination.IsFpuRegister()) {
5815 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005816 } else {
5817 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005818 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005819 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005820 } else if (source.IsRegisterPair()) {
5821 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5822 // Create stack space for 2 elements.
5823 __ subl(ESP, Immediate(2 * elem_size));
5824 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5825 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5826 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5827 // And remove the temporary stack space we allocated.
5828 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005830 if (destination.IsRegister()) {
5831 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5832 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005834 } else if (destination.IsRegisterPair()) {
5835 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5836 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5837 __ psrlq(src_reg, Immediate(32));
5838 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839 } else if (destination.IsStackSlot()) {
5840 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005841 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005842 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005843 } else {
5844 DCHECK(destination.IsSIMDStackSlot());
5845 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005847 } else if (source.IsStackSlot()) {
5848 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005849 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 } else if (destination.IsFpuRegister()) {
5851 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005852 } else {
5853 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005854 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5855 }
5856 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005857 if (destination.IsRegisterPair()) {
5858 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5859 __ movl(destination.AsRegisterPairHigh<Register>(),
5860 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5861 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005862 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5863 } else {
5864 DCHECK(destination.IsDoubleStackSlot()) << destination;
5865 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005866 }
Aart Bik5576f372017-03-23 16:17:37 -07005867 } else if (source.IsSIMDStackSlot()) {
5868 DCHECK(destination.IsFpuRegister());
5869 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005870 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005871 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005872 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005873 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005874 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005875 if (value == 0) {
5876 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5877 } else {
5878 __ movl(destination.AsRegister<Register>(), Immediate(value));
5879 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005880 } else {
5881 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005882 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005883 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005884 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005885 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005886 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005887 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005888 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005889 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5890 if (value == 0) {
5891 // Easy handling of 0.0.
5892 __ xorps(dest, dest);
5893 } else {
5894 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005895 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5896 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5897 __ movl(temp, Immediate(value));
5898 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005899 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005900 } else {
5901 DCHECK(destination.IsStackSlot()) << destination;
5902 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5903 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005904 } else if (constant->IsLongConstant()) {
5905 int64_t value = constant->AsLongConstant()->GetValue();
5906 int32_t low_value = Low32Bits(value);
5907 int32_t high_value = High32Bits(value);
5908 Immediate low(low_value);
5909 Immediate high(high_value);
5910 if (destination.IsDoubleStackSlot()) {
5911 __ movl(Address(ESP, destination.GetStackIndex()), low);
5912 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5913 } else {
5914 __ movl(destination.AsRegisterPairLow<Register>(), low);
5915 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5916 }
5917 } else {
5918 DCHECK(constant->IsDoubleConstant());
5919 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005920 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005921 int32_t low_value = Low32Bits(value);
5922 int32_t high_value = High32Bits(value);
5923 Immediate low(low_value);
5924 Immediate high(high_value);
5925 if (destination.IsFpuRegister()) {
5926 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5927 if (value == 0) {
5928 // Easy handling of 0.0.
5929 __ xorpd(dest, dest);
5930 } else {
5931 __ pushl(high);
5932 __ pushl(low);
5933 __ movsd(dest, Address(ESP, 0));
5934 __ addl(ESP, Immediate(8));
5935 }
5936 } else {
5937 DCHECK(destination.IsDoubleStackSlot()) << destination;
5938 __ movl(Address(ESP, destination.GetStackIndex()), low);
5939 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5940 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005941 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005942 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005943 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005944 }
5945}
5946
Mark Mendella5c19ce2015-04-01 12:51:05 -04005947void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005948 Register suggested_scratch = reg == EAX ? EBX : EAX;
5949 ScratchRegisterScope ensure_scratch(
5950 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5951
5952 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5953 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5954 __ movl(Address(ESP, mem + stack_offset), reg);
5955 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005956}
5957
Mark Mendell7c8d0092015-01-26 11:21:33 -05005958void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005959 ScratchRegisterScope ensure_scratch(
5960 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5961
5962 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5963 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5964 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5965 __ movss(Address(ESP, mem + stack_offset), reg);
5966 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005967}
5968
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005969void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005970 ScratchRegisterScope ensure_scratch1(
5971 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005972
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005973 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5974 ScratchRegisterScope ensure_scratch2(
5975 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005976
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005977 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5978 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5979 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5980 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5981 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5982 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005983}
5984
5985void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005986 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005987 Location source = move->GetSource();
5988 Location destination = move->GetDestination();
5989
5990 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005991 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5992 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5993 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5994 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5995 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005996 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005997 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005998 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005999 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006000 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6001 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006002 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6003 // Use XOR Swap algorithm to avoid a temporary.
6004 DCHECK_NE(source.reg(), destination.reg());
6005 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6006 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6007 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6008 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6009 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6010 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6011 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006012 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6013 // Take advantage of the 16 bytes in the XMM register.
6014 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6015 Address stack(ESP, destination.GetStackIndex());
6016 // Load the double into the high doubleword.
6017 __ movhpd(reg, stack);
6018
6019 // Store the low double into the destination.
6020 __ movsd(stack, reg);
6021
6022 // Move the high double to the low double.
6023 __ psrldq(reg, Immediate(8));
6024 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6025 // Take advantage of the 16 bytes in the XMM register.
6026 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6027 Address stack(ESP, source.GetStackIndex());
6028 // Load the double into the high doubleword.
6029 __ movhpd(reg, stack);
6030
6031 // Store the low double into the destination.
6032 __ movsd(stack, reg);
6033
6034 // Move the high double to the low double.
6035 __ psrldq(reg, Immediate(8));
6036 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6037 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6038 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006039 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006040 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006041 }
6042}
6043
6044void ParallelMoveResolverX86::SpillScratch(int reg) {
6045 __ pushl(static_cast<Register>(reg));
6046}
6047
6048void ParallelMoveResolverX86::RestoreScratch(int reg) {
6049 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006050}
6051
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6053 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006055 case HLoadClass::LoadKind::kInvalid:
6056 LOG(FATAL) << "UNREACHABLE";
6057 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006058 case HLoadClass::LoadKind::kReferrersClass:
6059 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006061 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006062 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006064 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 DCHECK(Runtime::Current()->UseJitCompilation());
6066 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006067 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006068 case HLoadClass::LoadKind::kDexCacheViaMethod:
6069 break;
6070 }
6071 return desired_class_load_kind;
6072}
6073
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006075 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6076 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006077 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006078 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006079 cls,
6080 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006081 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006082 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006083 return;
6084 }
Vladimir Marko41559982017-01-06 14:04:23 +00006085 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006086
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006087 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6088 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006089 ? LocationSummary::kCallOnSlowPath
6090 : LocationSummary::kNoCall;
6091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006092 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006093 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006094 }
6095
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006096 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006097 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6098 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099 locations->SetInAt(0, Location::RequiresRegister());
6100 }
6101 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006102 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6103 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6104 // Rely on the type resolution and/or initialization to save everything.
6105 RegisterSet caller_saves = RegisterSet::Empty();
6106 InvokeRuntimeCallingConvention calling_convention;
6107 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6108 locations->SetCustomSlowPathCallerSaves(caller_saves);
6109 } else {
6110 // For non-Baker read barrier we have a temp-clobbering call.
6111 }
6112 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006113}
6114
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006115Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6116 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006117 Handle<mirror::Class> handle) {
6118 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6119 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006120 // Add a patch entry and return the label.
6121 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6122 PatchInfo<Label>* info = &jit_class_patches_.back();
6123 return &info->label;
6124}
6125
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006126// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6127// move.
6128void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006129 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6130 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6131 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006132 return;
6133 }
Vladimir Marko41559982017-01-06 14:04:23 +00006134 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006135
Vladimir Marko41559982017-01-06 14:04:23 +00006136 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 Location out_loc = locations->Out();
6138 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006140 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006141 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6142 ? kWithoutReadBarrier
6143 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006144 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006145 case HLoadClass::LoadKind::kReferrersClass: {
6146 DCHECK(!cls->CanCallRuntime());
6147 DCHECK(!cls->MustGenerateClinitCheck());
6148 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6149 Register current_method = locations->InAt(0).AsRegister<Register>();
6150 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006151 cls,
6152 out_loc,
6153 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006154 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006155 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 break;
6157 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006158 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006159 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006160 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006161 Register method_address = locations->InAt(0).AsRegister<Register>();
6162 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006163 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 break;
6165 }
6166 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006167 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006168 uint32_t address = dchecked_integral_cast<uint32_t>(
6169 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6170 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 break;
6173 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006174 case HLoadClass::LoadKind::kBssEntry: {
6175 Register method_address = locations->InAt(0).AsRegister<Register>();
6176 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6177 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6178 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6179 generate_null_check = true;
6180 break;
6181 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006182 case HLoadClass::LoadKind::kJitTableAddress: {
6183 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6184 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006185 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006186 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006187 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006188 break;
6189 }
Vladimir Marko41559982017-01-06 14:04:23 +00006190 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006191 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006192 LOG(FATAL) << "UNREACHABLE";
6193 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006194 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006195
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006196 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6197 DCHECK(cls->CanCallRuntime());
6198 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6199 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6200 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006201
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006202 if (generate_null_check) {
6203 __ testl(out, out);
6204 __ j(kEqual, slow_path->GetEntryLabel());
6205 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006206
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006207 if (cls->MustGenerateClinitCheck()) {
6208 GenerateClassInitializationCheck(slow_path, out);
6209 } else {
6210 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006211 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212 }
6213}
6214
6215void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6216 LocationSummary* locations =
6217 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6218 locations->SetInAt(0, Location::RequiresRegister());
6219 if (check->HasUses()) {
6220 locations->SetOut(Location::SameAsFirstInput());
6221 }
6222}
6223
6224void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006225 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006226 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006227 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006228 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006229 GenerateClassInitializationCheck(slow_path,
6230 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006231}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006232
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006233void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006234 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006235 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6236 Immediate(mirror::Class::kStatusInitialized));
6237 __ j(kLess, slow_path->GetEntryLabel());
6238 __ Bind(slow_path->GetExitLabel());
6239 // No need for memory fence, thanks to the X86 memory model.
6240}
6241
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6243 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006245 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006246 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006247 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006248 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006249 case HLoadString::LoadKind::kJitTableAddress:
6250 DCHECK(Runtime::Current()->UseJitCompilation());
6251 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006252 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006253 case HLoadString::LoadKind::kDexCacheViaMethod:
6254 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006255 }
6256 return desired_string_load_kind;
6257}
6258
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006259void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006260 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006263 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006264 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006265 locations->SetInAt(0, Location::RequiresRegister());
6266 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006267 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6268 locations->SetOut(Location::RegisterLocation(EAX));
6269 } else {
6270 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006271 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6272 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006273 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006274 RegisterSet caller_saves = RegisterSet::Empty();
6275 InvokeRuntimeCallingConvention calling_convention;
6276 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6277 locations->SetCustomSlowPathCallerSaves(caller_saves);
6278 } else {
6279 // For non-Baker read barrier we have a temp-clobbering call.
6280 }
6281 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006282 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006283}
6284
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006285Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006286 dex::StringIndex dex_index,
6287 Handle<mirror::String> handle) {
6288 jit_string_roots_.Overwrite(
6289 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006290 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006291 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006292 PatchInfo<Label>* info = &jit_string_patches_.back();
6293 return &info->label;
6294}
6295
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006296// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6297// move.
6298void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006299 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300 Location out_loc = locations->Out();
6301 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006303 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006304 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006305 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 Register method_address = locations->InAt(0).AsRegister<Register>();
6307 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006308 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006309 return; // No dex cache slow path.
6310 }
6311 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006312 uint32_t address = dchecked_integral_cast<uint32_t>(
6313 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6314 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006315 __ movl(out, Immediate(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006316 return; // No dex cache slow path.
6317 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006318 case HLoadString::LoadKind::kBssEntry: {
6319 Register method_address = locations->InAt(0).AsRegister<Register>();
6320 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6321 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006322 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006323 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006324 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6325 codegen_->AddSlowPath(slow_path);
6326 __ testl(out, out);
6327 __ j(kEqual, slow_path->GetEntryLabel());
6328 __ Bind(slow_path->GetExitLabel());
6329 return;
6330 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006331 case HLoadString::LoadKind::kJitTableAddress: {
6332 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6333 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006334 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006335 // /* GcRoot<mirror::String> */ out = *address
6336 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6337 return;
6338 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006339 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006340 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006341 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006342
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006343 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006344 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006345 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006346 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006347 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6348 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006349}
6350
David Brazdilcb1c0552015-08-04 16:22:25 +01006351static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006352 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006353}
6354
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006355void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6356 LocationSummary* locations =
6357 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6358 locations->SetOut(Location::RequiresRegister());
6359}
6360
6361void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006362 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6363}
6364
6365void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6366 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6367}
6368
6369void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6370 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006371}
6372
6373void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6374 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006376 InvokeRuntimeCallingConvention calling_convention;
6377 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6378}
6379
6380void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006381 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006382 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006383}
6384
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006385// Temp is used for read barrier.
6386static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6387 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006388 !kUseBakerReadBarrier &&
6389 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006390 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006391 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6392 return 1;
6393 }
6394 return 0;
6395}
6396
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006397// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006398// interface pointer, one for loading the current interface.
6399// The other checks have one temp for loading the object's class.
6400static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6401 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6402 return 2;
6403 }
6404 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006405}
6406
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006407void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006410 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006411 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 case TypeCheckKind::kExactCheck:
6413 case TypeCheckKind::kAbstractClassCheck:
6414 case TypeCheckKind::kClassHierarchyCheck:
6415 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 call_kind =
6417 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006418 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 break;
6420 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 case TypeCheckKind::kUnresolvedCheck:
6422 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 call_kind = LocationSummary::kCallOnSlowPath;
6424 break;
6425 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006428 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006429 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006430 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 locations->SetInAt(0, Location::RequiresRegister());
6432 locations->SetInAt(1, Location::Any());
6433 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6434 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006435 // When read barriers are enabled, we need a temporary register for some cases.
6436 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006437}
6438
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006439void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006440 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006441 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442 Location obj_loc = locations->InAt(0);
6443 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006444 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 Location out_loc = locations->Out();
6446 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006447 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6448 DCHECK_LE(num_temps, 1u);
6449 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006450 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6452 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6453 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006454 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006456
6457 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006458 // Avoid null check if we know obj is not null.
6459 if (instruction->MustDoNullCheck()) {
6460 __ testl(obj, obj);
6461 __ j(kEqual, &zero);
6462 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463
Roland Levillain7c1559a2015-12-15 10:55:36 +00006464 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006466 // /* HeapReference<Class> */ out = obj->klass_
6467 GenerateReferenceLoadTwoRegisters(instruction,
6468 out_loc,
6469 obj_loc,
6470 class_offset,
6471 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 if (cls.IsRegister()) {
6473 __ cmpl(out, cls.AsRegister<Register>());
6474 } else {
6475 DCHECK(cls.IsStackSlot()) << cls;
6476 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6477 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006478
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 // Classes must be equal for the instanceof to succeed.
6480 __ j(kNotEqual, &zero);
6481 __ movl(out, Immediate(1));
6482 __ jmp(&done);
6483 break;
6484 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006486 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006487 // /* HeapReference<Class> */ out = obj->klass_
6488 GenerateReferenceLoadTwoRegisters(instruction,
6489 out_loc,
6490 obj_loc,
6491 class_offset,
6492 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006493 // If the class is abstract, we eagerly fetch the super class of the
6494 // object to avoid doing a comparison we know will fail.
6495 NearLabel loop;
6496 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006498 GenerateReferenceLoadOneRegister(instruction,
6499 out_loc,
6500 super_offset,
6501 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006502 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 __ testl(out, out);
6504 // If `out` is null, we use it for the result, and jump to `done`.
6505 __ j(kEqual, &done);
6506 if (cls.IsRegister()) {
6507 __ cmpl(out, cls.AsRegister<Register>());
6508 } else {
6509 DCHECK(cls.IsStackSlot()) << cls;
6510 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6511 }
6512 __ j(kNotEqual, &loop);
6513 __ movl(out, Immediate(1));
6514 if (zero.IsLinked()) {
6515 __ jmp(&done);
6516 }
6517 break;
6518 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006519
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006520 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006521 // /* HeapReference<Class> */ out = obj->klass_
6522 GenerateReferenceLoadTwoRegisters(instruction,
6523 out_loc,
6524 obj_loc,
6525 class_offset,
6526 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 // Walk over the class hierarchy to find a match.
6528 NearLabel loop, success;
6529 __ Bind(&loop);
6530 if (cls.IsRegister()) {
6531 __ cmpl(out, cls.AsRegister<Register>());
6532 } else {
6533 DCHECK(cls.IsStackSlot()) << cls;
6534 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6535 }
6536 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006538 GenerateReferenceLoadOneRegister(instruction,
6539 out_loc,
6540 super_offset,
6541 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006542 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006543 __ testl(out, out);
6544 __ j(kNotEqual, &loop);
6545 // If `out` is null, we use it for the result, and jump to `done`.
6546 __ jmp(&done);
6547 __ Bind(&success);
6548 __ movl(out, Immediate(1));
6549 if (zero.IsLinked()) {
6550 __ jmp(&done);
6551 }
6552 break;
6553 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006555 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006556 // /* HeapReference<Class> */ out = obj->klass_
6557 GenerateReferenceLoadTwoRegisters(instruction,
6558 out_loc,
6559 obj_loc,
6560 class_offset,
6561 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006562 // Do an exact check.
6563 NearLabel exact_check;
6564 if (cls.IsRegister()) {
6565 __ cmpl(out, cls.AsRegister<Register>());
6566 } else {
6567 DCHECK(cls.IsStackSlot()) << cls;
6568 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6569 }
6570 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006573 GenerateReferenceLoadOneRegister(instruction,
6574 out_loc,
6575 component_offset,
6576 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006577 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 __ testl(out, out);
6579 // If `out` is null, we use it for the result, and jump to `done`.
6580 __ j(kEqual, &done);
6581 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6582 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006583 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 __ movl(out, Immediate(1));
6585 __ jmp(&done);
6586 break;
6587 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006590 // No read barrier since the slow path will retry upon failure.
6591 // /* HeapReference<Class> */ out = obj->klass_
6592 GenerateReferenceLoadTwoRegisters(instruction,
6593 out_loc,
6594 obj_loc,
6595 class_offset,
6596 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006597 if (cls.IsRegister()) {
6598 __ cmpl(out, cls.AsRegister<Register>());
6599 } else {
6600 DCHECK(cls.IsStackSlot()) << cls;
6601 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6602 }
6603 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6605 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006606 codegen_->AddSlowPath(slow_path);
6607 __ j(kNotEqual, slow_path->GetEntryLabel());
6608 __ movl(out, Immediate(1));
6609 if (zero.IsLinked()) {
6610 __ jmp(&done);
6611 }
6612 break;
6613 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006614
Calin Juravle98893e12015-10-02 21:05:03 +01006615 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616 case TypeCheckKind::kInterfaceCheck: {
6617 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006618 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006619 // cases.
6620 //
6621 // We cannot directly call the InstanceofNonTrivial runtime
6622 // entry point without resorting to a type checking slow path
6623 // here (i.e. by calling InvokeRuntime directly), as it would
6624 // require to assign fixed registers for the inputs of this
6625 // HInstanceOf instruction (following the runtime calling
6626 // convention), which might be cluttered by the potential first
6627 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006628 //
6629 // TODO: Introduce a new runtime entry point taking the object
6630 // to test (instead of its class) as argument, and let it deal
6631 // with the read barrier issues. This will let us refactor this
6632 // case of the `switch` code as it was previously (with a direct
6633 // call to the runtime not using a type checking slow path).
6634 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006635 DCHECK(locations->OnlyCallsOnSlowPath());
6636 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6637 /* is_fatal */ false);
6638 codegen_->AddSlowPath(slow_path);
6639 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006640 if (zero.IsLinked()) {
6641 __ jmp(&done);
6642 }
6643 break;
6644 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006645 }
6646
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006647 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006648 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006649 __ xorl(out, out);
6650 }
6651
6652 if (done.IsLinked()) {
6653 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006654 }
6655
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006656 if (slow_path != nullptr) {
6657 __ Bind(slow_path->GetExitLabel());
6658 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006659}
6660
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006661static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6662 switch (type_check_kind) {
6663 case TypeCheckKind::kExactCheck:
6664 case TypeCheckKind::kAbstractClassCheck:
6665 case TypeCheckKind::kClassHierarchyCheck:
6666 case TypeCheckKind::kArrayObjectCheck:
6667 return !throws_into_catch && !kEmitCompilerReadBarrier;
6668 case TypeCheckKind::kInterfaceCheck:
6669 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6670 case TypeCheckKind::kArrayCheck:
6671 case TypeCheckKind::kUnresolvedCheck:
6672 return false;
6673 }
6674 LOG(FATAL) << "Unreachable";
6675 UNREACHABLE();
6676}
6677
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006678void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006679 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006681 LocationSummary::CallKind call_kind =
6682 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6683 ? LocationSummary::kNoCall
6684 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6686 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006687 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6688 // Require a register for the interface check since there is a loop that compares the class to
6689 // a memory address.
6690 locations->SetInAt(1, Location::RequiresRegister());
6691 } else {
6692 locations->SetInAt(1, Location::Any());
6693 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6695 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006696 // When read barriers are enabled, we need an additional temporary register for some cases.
6697 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6698}
6699
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006700void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006701 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006702 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 Location obj_loc = locations->InAt(0);
6704 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006705 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006706 Location temp_loc = locations->GetTemp(0);
6707 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006708 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6709 DCHECK_GE(num_temps, 1u);
6710 DCHECK_LE(num_temps, 2u);
6711 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6712 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6713 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6714 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6715 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6716 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6717 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6718 const uint32_t object_array_data_offset =
6719 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006720
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006721 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6722 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6723 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006724 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006725 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6726
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 SlowPathCode* type_check_slow_path =
6728 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6729 is_type_check_slow_path_fatal);
6730 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006731
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006733 // Avoid null check if we know obj is not null.
6734 if (instruction->MustDoNullCheck()) {
6735 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006736 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006737 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740 case TypeCheckKind::kExactCheck:
6741 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006742 // /* HeapReference<Class> */ temp = obj->klass_
6743 GenerateReferenceLoadTwoRegisters(instruction,
6744 temp_loc,
6745 obj_loc,
6746 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006747 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 if (cls.IsRegister()) {
6750 __ cmpl(temp, cls.AsRegister<Register>());
6751 } else {
6752 DCHECK(cls.IsStackSlot()) << cls;
6753 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6754 }
6755 // Jump to slow path for throwing the exception or doing a
6756 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006758 break;
6759 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006760
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006762 // /* HeapReference<Class> */ temp = obj->klass_
6763 GenerateReferenceLoadTwoRegisters(instruction,
6764 temp_loc,
6765 obj_loc,
6766 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006767 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006768
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006769 // If the class is abstract, we eagerly fetch the super class of the
6770 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006771 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006772 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006773 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006774 GenerateReferenceLoadOneRegister(instruction,
6775 temp_loc,
6776 super_offset,
6777 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006778 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006779
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006780 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6781 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006782 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006783 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006785 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006786 if (cls.IsRegister()) {
6787 __ cmpl(temp, cls.AsRegister<Register>());
6788 } else {
6789 DCHECK(cls.IsStackSlot()) << cls;
6790 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6791 }
6792 __ j(kNotEqual, &loop);
6793 break;
6794 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006795
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006796 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006797 // /* HeapReference<Class> */ temp = obj->klass_
6798 GenerateReferenceLoadTwoRegisters(instruction,
6799 temp_loc,
6800 obj_loc,
6801 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006802 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006803
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006804 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006805 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006806 __ Bind(&loop);
6807 if (cls.IsRegister()) {
6808 __ cmpl(temp, cls.AsRegister<Register>());
6809 } else {
6810 DCHECK(cls.IsStackSlot()) << cls;
6811 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6812 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006813 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006816 GenerateReferenceLoadOneRegister(instruction,
6817 temp_loc,
6818 super_offset,
6819 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006820 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006821
6822 // If the class reference currently in `temp` is not null, jump
6823 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006824 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006825 __ j(kNotZero, &loop);
6826 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006827 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006828 break;
6829 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006830
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006831 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006832 // /* HeapReference<Class> */ temp = obj->klass_
6833 GenerateReferenceLoadTwoRegisters(instruction,
6834 temp_loc,
6835 obj_loc,
6836 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006837 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006838
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006839 // Do an exact check.
6840 if (cls.IsRegister()) {
6841 __ cmpl(temp, cls.AsRegister<Register>());
6842 } else {
6843 DCHECK(cls.IsStackSlot()) << cls;
6844 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6845 }
6846 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006847
6848 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006849 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006850 GenerateReferenceLoadOneRegister(instruction,
6851 temp_loc,
6852 component_offset,
6853 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006854 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006855
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006856 // If the component type is null (i.e. the object not an array), jump to the slow path to
6857 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006858 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006859 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006860
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006861 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006862 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006863 break;
6864 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006865
Calin Juravle98893e12015-10-02 21:05:03 +01006866 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006867 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006868 // We cannot directly call the CheckCast runtime entry point
6869 // without resorting to a type checking slow path here (i.e. by
6870 // calling InvokeRuntime directly), as it would require to
6871 // assign fixed registers for the inputs of this HInstanceOf
6872 // instruction (following the runtime calling convention), which
6873 // might be cluttered by the potential first read barrier
6874 // emission at the beginning of this method.
6875 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006876 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006877
6878 case TypeCheckKind::kInterfaceCheck: {
6879 // Fast path for the interface check. Since we compare with a memory location in the inner
6880 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6881 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006882 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006883 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006884 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6885 // doing this.
6886 // /* HeapReference<Class> */ temp = obj->klass_
6887 GenerateReferenceLoadTwoRegisters(instruction,
6888 temp_loc,
6889 obj_loc,
6890 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006891 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006892
6893 // /* HeapReference<Class> */ temp = temp->iftable_
6894 GenerateReferenceLoadTwoRegisters(instruction,
6895 temp_loc,
6896 temp_loc,
6897 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006898 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006899 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006900 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006901 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006902 NearLabel start_loop;
6903 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006904 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006905 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006906 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6907 // Go to next interface if the classes do not match.
6908 __ cmpl(cls.AsRegister<Register>(),
6909 CodeGeneratorX86::ArrayAddress(temp,
6910 maybe_temp2_loc,
6911 TIMES_4,
6912 object_array_data_offset));
6913 __ j(kNotEqual, &start_loop);
6914 } else {
6915 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006916 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006917 break;
6918 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006919 }
6920 __ Bind(&done);
6921
Roland Levillain0d5a2812015-11-13 10:07:31 +00006922 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006923}
6924
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006925void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6926 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006928 InvokeRuntimeCallingConvention calling_convention;
6929 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6930}
6931
6932void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006933 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6934 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006935 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006936 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006937 if (instruction->IsEnter()) {
6938 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6939 } else {
6940 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6941 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006942}
6943
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006944void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6945void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6946void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6947
6948void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6949 LocationSummary* locations =
6950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6951 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6952 || instruction->GetResultType() == Primitive::kPrimLong);
6953 locations->SetInAt(0, Location::RequiresRegister());
6954 locations->SetInAt(1, Location::Any());
6955 locations->SetOut(Location::SameAsFirstInput());
6956}
6957
6958void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6959 HandleBitwiseOperation(instruction);
6960}
6961
6962void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6963 HandleBitwiseOperation(instruction);
6964}
6965
6966void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6967 HandleBitwiseOperation(instruction);
6968}
6969
6970void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6971 LocationSummary* locations = instruction->GetLocations();
6972 Location first = locations->InAt(0);
6973 Location second = locations->InAt(1);
6974 DCHECK(first.Equals(locations->Out()));
6975
6976 if (instruction->GetResultType() == Primitive::kPrimInt) {
6977 if (second.IsRegister()) {
6978 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006979 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006981 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006982 } else {
6983 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006984 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006985 }
6986 } else if (second.IsConstant()) {
6987 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006988 __ andl(first.AsRegister<Register>(),
6989 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006990 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006991 __ orl(first.AsRegister<Register>(),
6992 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006993 } else {
6994 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006995 __ xorl(first.AsRegister<Register>(),
6996 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006997 }
6998 } else {
6999 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007000 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007001 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007002 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007003 } else {
7004 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007005 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007006 }
7007 }
7008 } else {
7009 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7010 if (second.IsRegisterPair()) {
7011 if (instruction->IsAnd()) {
7012 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7013 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7014 } else if (instruction->IsOr()) {
7015 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7016 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7017 } else {
7018 DCHECK(instruction->IsXor());
7019 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7020 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7021 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007022 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007023 if (instruction->IsAnd()) {
7024 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7025 __ andl(first.AsRegisterPairHigh<Register>(),
7026 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7027 } else if (instruction->IsOr()) {
7028 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7029 __ orl(first.AsRegisterPairHigh<Register>(),
7030 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7031 } else {
7032 DCHECK(instruction->IsXor());
7033 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7034 __ xorl(first.AsRegisterPairHigh<Register>(),
7035 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7036 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007037 } else {
7038 DCHECK(second.IsConstant()) << second;
7039 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007040 int32_t low_value = Low32Bits(value);
7041 int32_t high_value = High32Bits(value);
7042 Immediate low(low_value);
7043 Immediate high(high_value);
7044 Register first_low = first.AsRegisterPairLow<Register>();
7045 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007046 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007047 if (low_value == 0) {
7048 __ xorl(first_low, first_low);
7049 } else if (low_value != -1) {
7050 __ andl(first_low, low);
7051 }
7052 if (high_value == 0) {
7053 __ xorl(first_high, first_high);
7054 } else if (high_value != -1) {
7055 __ andl(first_high, high);
7056 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007057 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007058 if (low_value != 0) {
7059 __ orl(first_low, low);
7060 }
7061 if (high_value != 0) {
7062 __ orl(first_high, high);
7063 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007064 } else {
7065 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007066 if (low_value != 0) {
7067 __ xorl(first_low, low);
7068 }
7069 if (high_value != 0) {
7070 __ xorl(first_high, high);
7071 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007072 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007073 }
7074 }
7075}
7076
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007077void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7078 HInstruction* instruction,
7079 Location out,
7080 uint32_t offset,
7081 Location maybe_temp,
7082 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007083 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007084 if (read_barrier_option == kWithReadBarrier) {
7085 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007086 if (kUseBakerReadBarrier) {
7087 // Load with fast path based Baker's read barrier.
7088 // /* HeapReference<Object> */ out = *(out + offset)
7089 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007090 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007091 } else {
7092 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007093 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007094 // in the following move operation, as we will need it for the
7095 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007096 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007097 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007098 // /* HeapReference<Object> */ out = *(out + offset)
7099 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007100 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007101 }
7102 } else {
7103 // Plain load with no read barrier.
7104 // /* HeapReference<Object> */ out = *(out + offset)
7105 __ movl(out_reg, Address(out_reg, offset));
7106 __ MaybeUnpoisonHeapReference(out_reg);
7107 }
7108}
7109
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007110void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7111 HInstruction* instruction,
7112 Location out,
7113 Location obj,
7114 uint32_t offset,
7115 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007116 Register out_reg = out.AsRegister<Register>();
7117 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007118 if (read_barrier_option == kWithReadBarrier) {
7119 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007120 if (kUseBakerReadBarrier) {
7121 // Load with fast path based Baker's read barrier.
7122 // /* HeapReference<Object> */ out = *(obj + offset)
7123 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007124 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007125 } else {
7126 // Load with slow path based read barrier.
7127 // /* HeapReference<Object> */ out = *(obj + offset)
7128 __ movl(out_reg, Address(obj_reg, offset));
7129 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7130 }
7131 } else {
7132 // Plain load with no read barrier.
7133 // /* HeapReference<Object> */ out = *(obj + offset)
7134 __ movl(out_reg, Address(obj_reg, offset));
7135 __ MaybeUnpoisonHeapReference(out_reg);
7136 }
7137}
7138
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007139void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7140 HInstruction* instruction,
7141 Location root,
7142 const Address& address,
7143 Label* fixup_label,
7144 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007146 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007147 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007148 if (kUseBakerReadBarrier) {
7149 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7150 // Baker's read barrier are used:
7151 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007152 // root = obj.field;
7153 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7154 // if (temp != null) {
7155 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007156 // }
7157
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007158 // /* GcRoot<mirror::Object> */ root = *address
7159 __ movl(root_reg, address);
7160 if (fixup_label != nullptr) {
7161 __ Bind(fixup_label);
7162 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007163 static_assert(
7164 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7165 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7166 "have different sizes.");
7167 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7168 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7169 "have different sizes.");
7170
Vladimir Marko953437b2016-08-24 08:30:46 +00007171 // Slow path marking the GC root `root`.
7172 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007173 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007174 codegen_->AddSlowPath(slow_path);
7175
Roland Levillaind966ce72017-02-09 16:20:14 +00007176 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7177 const int32_t entry_point_offset =
7178 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
7179 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7180 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007181 __ j(kNotEqual, slow_path->GetEntryLabel());
7182 __ Bind(slow_path->GetExitLabel());
7183 } else {
7184 // GC root loaded through a slow path for read barriers other
7185 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007186 // /* GcRoot<mirror::Object>* */ root = address
7187 __ leal(root_reg, address);
7188 if (fixup_label != nullptr) {
7189 __ Bind(fixup_label);
7190 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007191 // /* mirror::Object* */ root = root->Read()
7192 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7193 }
7194 } else {
7195 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007196 // /* GcRoot<mirror::Object> */ root = *address
7197 __ movl(root_reg, address);
7198 if (fixup_label != nullptr) {
7199 __ Bind(fixup_label);
7200 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007201 // Note that GC roots are not affected by heap poisoning, thus we
7202 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007203 }
7204}
7205
7206void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7207 Location ref,
7208 Register obj,
7209 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007210 bool needs_null_check) {
7211 DCHECK(kEmitCompilerReadBarrier);
7212 DCHECK(kUseBakerReadBarrier);
7213
7214 // /* HeapReference<Object> */ ref = *(obj + offset)
7215 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007216 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007217}
7218
7219void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7220 Location ref,
7221 Register obj,
7222 uint32_t data_offset,
7223 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224 bool needs_null_check) {
7225 DCHECK(kEmitCompilerReadBarrier);
7226 DCHECK(kUseBakerReadBarrier);
7227
Roland Levillain3d312422016-06-23 13:53:42 +01007228 static_assert(
7229 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7230 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007231 // /* HeapReference<Object> */ ref =
7232 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007233 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007234 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007235}
7236
7237void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7238 Location ref,
7239 Register obj,
7240 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007241 bool needs_null_check,
7242 bool always_update_field,
7243 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007244 DCHECK(kEmitCompilerReadBarrier);
7245 DCHECK(kUseBakerReadBarrier);
7246
7247 // In slow path based read barriers, the read barrier call is
7248 // inserted after the original load. However, in fast path based
7249 // Baker's read barriers, we need to perform the load of
7250 // mirror::Object::monitor_ *before* the original reference load.
7251 // This load-load ordering is required by the read barrier.
7252 // The fast path/slow path (for Baker's algorithm) should look like:
7253 //
7254 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7255 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7256 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007257 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007258 // if (is_gray) {
7259 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7260 // }
7261 //
7262 // Note: the original implementation in ReadBarrier::Barrier is
7263 // slightly more complex as:
7264 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007265 // the high-bits of rb_state, which are expected to be all zeroes
7266 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7267 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007268 // - it performs additional checks that we do not do here for
7269 // performance reasons.
7270
7271 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007272 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7273
Vladimir Marko953437b2016-08-24 08:30:46 +00007274 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007275 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7276 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007277 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7278 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7279 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7280
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007281 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007282 // ref = ReadBarrier::Mark(ref);
7283 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7284 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007285 if (needs_null_check) {
7286 MaybeRecordImplicitNullCheck(instruction);
7287 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007288
7289 // Load fence to prevent load-load reordering.
7290 // Note that this is a no-op, thanks to the x86 memory model.
7291 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7292
7293 // The actual reference load.
7294 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007295 __ movl(ref_reg, src); // Flags are unaffected.
7296
7297 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7298 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007299 SlowPathCode* slow_path;
7300 if (always_update_field) {
7301 DCHECK(temp != nullptr);
7302 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7303 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7304 } else {
7305 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7306 instruction, ref, /* unpoison_ref_before_marking */ true);
7307 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007308 AddSlowPath(slow_path);
7309
7310 // We have done the "if" of the gray bit check above, now branch based on the flags.
7311 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007312
7313 // Object* ref = ref_addr->AsMirrorPtr()
7314 __ MaybeUnpoisonHeapReference(ref_reg);
7315
Roland Levillain7c1559a2015-12-15 10:55:36 +00007316 __ Bind(slow_path->GetExitLabel());
7317}
7318
7319void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7320 Location out,
7321 Location ref,
7322 Location obj,
7323 uint32_t offset,
7324 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007325 DCHECK(kEmitCompilerReadBarrier);
7326
Roland Levillain7c1559a2015-12-15 10:55:36 +00007327 // Insert a slow path based read barrier *after* the reference load.
7328 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 // If heap poisoning is enabled, the unpoisoning of the loaded
7330 // reference will be carried out by the runtime within the slow
7331 // path.
7332 //
7333 // Note that `ref` currently does not get unpoisoned (when heap
7334 // poisoning is enabled), which is alright as the `ref` argument is
7335 // not used by the artReadBarrierSlow entry point.
7336 //
7337 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7338 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7339 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7340 AddSlowPath(slow_path);
7341
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 __ jmp(slow_path->GetEntryLabel());
7343 __ Bind(slow_path->GetExitLabel());
7344}
7345
Roland Levillain7c1559a2015-12-15 10:55:36 +00007346void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7347 Location out,
7348 Location ref,
7349 Location obj,
7350 uint32_t offset,
7351 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007352 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353 // Baker's read barriers shall be handled by the fast path
7354 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7355 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 // If heap poisoning is enabled, unpoisoning will be taken care of
7357 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007358 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007359 } else if (kPoisonHeapReferences) {
7360 __ UnpoisonHeapReference(out.AsRegister<Register>());
7361 }
7362}
7363
Roland Levillain7c1559a2015-12-15 10:55:36 +00007364void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7365 Location out,
7366 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007367 DCHECK(kEmitCompilerReadBarrier);
7368
Roland Levillain7c1559a2015-12-15 10:55:36 +00007369 // Insert a slow path based read barrier *after* the GC root load.
7370 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007371 // Note that GC roots are not affected by heap poisoning, so we do
7372 // not need to do anything special for this here.
7373 SlowPathCode* slow_path =
7374 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7375 AddSlowPath(slow_path);
7376
Roland Levillain0d5a2812015-11-13 10:07:31 +00007377 __ jmp(slow_path->GetEntryLabel());
7378 __ Bind(slow_path->GetExitLabel());
7379}
7380
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007381void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007382 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007383 LOG(FATAL) << "Unreachable";
7384}
7385
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007386void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007387 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007388 LOG(FATAL) << "Unreachable";
7389}
7390
Mark Mendellfe57faa2015-09-18 09:26:15 -04007391// Simple implementation of packed switch - generate cascaded compare/jumps.
7392void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7393 LocationSummary* locations =
7394 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7395 locations->SetInAt(0, Location::RequiresRegister());
7396}
7397
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007398void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7399 int32_t lower_bound,
7400 uint32_t num_entries,
7401 HBasicBlock* switch_block,
7402 HBasicBlock* default_block) {
7403 // Figure out the correct compare values and jump conditions.
7404 // Handle the first compare/branch as a special case because it might
7405 // jump to the default case.
7406 DCHECK_GT(num_entries, 2u);
7407 Condition first_condition;
7408 uint32_t index;
7409 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7410 if (lower_bound != 0) {
7411 first_condition = kLess;
7412 __ cmpl(value_reg, Immediate(lower_bound));
7413 __ j(first_condition, codegen_->GetLabelOf(default_block));
7414 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007415
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007416 index = 1;
7417 } else {
7418 // Handle all the compare/jumps below.
7419 first_condition = kBelow;
7420 index = 0;
7421 }
7422
7423 // Handle the rest of the compare/jumps.
7424 for (; index + 1 < num_entries; index += 2) {
7425 int32_t compare_to_value = lower_bound + index + 1;
7426 __ cmpl(value_reg, Immediate(compare_to_value));
7427 // Jump to successors[index] if value < case_value[index].
7428 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7429 // Jump to successors[index + 1] if value == case_value[index + 1].
7430 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7431 }
7432
7433 if (index != num_entries) {
7434 // There are an odd number of entries. Handle the last one.
7435 DCHECK_EQ(index + 1, num_entries);
7436 __ cmpl(value_reg, Immediate(lower_bound + index));
7437 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007438 }
7439
7440 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007441 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7442 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007443 }
7444}
7445
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007446void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7447 int32_t lower_bound = switch_instr->GetStartValue();
7448 uint32_t num_entries = switch_instr->GetNumEntries();
7449 LocationSummary* locations = switch_instr->GetLocations();
7450 Register value_reg = locations->InAt(0).AsRegister<Register>();
7451
7452 GenPackedSwitchWithCompares(value_reg,
7453 lower_bound,
7454 num_entries,
7455 switch_instr->GetBlock(),
7456 switch_instr->GetDefaultBlock());
7457}
7458
Mark Mendell805b3b52015-09-18 14:10:29 -04007459void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7460 LocationSummary* locations =
7461 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7462 locations->SetInAt(0, Location::RequiresRegister());
7463
7464 // Constant area pointer.
7465 locations->SetInAt(1, Location::RequiresRegister());
7466
7467 // And the temporary we need.
7468 locations->AddTemp(Location::RequiresRegister());
7469}
7470
7471void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7472 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007473 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007474 LocationSummary* locations = switch_instr->GetLocations();
7475 Register value_reg = locations->InAt(0).AsRegister<Register>();
7476 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7477
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007478 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7479 GenPackedSwitchWithCompares(value_reg,
7480 lower_bound,
7481 num_entries,
7482 switch_instr->GetBlock(),
7483 default_block);
7484 return;
7485 }
7486
Mark Mendell805b3b52015-09-18 14:10:29 -04007487 // Optimizing has a jump area.
7488 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7489 Register constant_area = locations->InAt(1).AsRegister<Register>();
7490
7491 // Remove the bias, if needed.
7492 if (lower_bound != 0) {
7493 __ leal(temp_reg, Address(value_reg, -lower_bound));
7494 value_reg = temp_reg;
7495 }
7496
7497 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007498 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007499 __ cmpl(value_reg, Immediate(num_entries - 1));
7500 __ j(kAbove, codegen_->GetLabelOf(default_block));
7501
7502 // We are in the range of the table.
7503 // Load (target-constant_area) from the jump table, indexing by the value.
7504 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7505
7506 // Compute the actual target address by adding in constant_area.
7507 __ addl(temp_reg, constant_area);
7508
7509 // And jump.
7510 __ jmp(temp_reg);
7511}
7512
Mark Mendell0616ae02015-04-17 12:49:27 -04007513void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7514 HX86ComputeBaseMethodAddress* insn) {
7515 LocationSummary* locations =
7516 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7517 locations->SetOut(Location::RequiresRegister());
7518}
7519
7520void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7521 HX86ComputeBaseMethodAddress* insn) {
7522 LocationSummary* locations = insn->GetLocations();
7523 Register reg = locations->Out().AsRegister<Register>();
7524
7525 // Generate call to next instruction.
7526 Label next_instruction;
7527 __ call(&next_instruction);
7528 __ Bind(&next_instruction);
7529
7530 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007531 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007532
7533 // Grab the return address off the stack.
7534 __ popl(reg);
7535}
7536
7537void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7538 HX86LoadFromConstantTable* insn) {
7539 LocationSummary* locations =
7540 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7541
7542 locations->SetInAt(0, Location::RequiresRegister());
7543 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7544
7545 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007546 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007547 return;
7548 }
7549
7550 switch (insn->GetType()) {
7551 case Primitive::kPrimFloat:
7552 case Primitive::kPrimDouble:
7553 locations->SetOut(Location::RequiresFpuRegister());
7554 break;
7555
7556 case Primitive::kPrimInt:
7557 locations->SetOut(Location::RequiresRegister());
7558 break;
7559
7560 default:
7561 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7562 }
7563}
7564
7565void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007566 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007567 return;
7568 }
7569
7570 LocationSummary* locations = insn->GetLocations();
7571 Location out = locations->Out();
7572 Register const_area = locations->InAt(0).AsRegister<Register>();
7573 HConstant *value = insn->GetConstant();
7574
7575 switch (insn->GetType()) {
7576 case Primitive::kPrimFloat:
7577 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007578 codegen_->LiteralFloatAddress(
7579 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007580 break;
7581
7582 case Primitive::kPrimDouble:
7583 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007584 codegen_->LiteralDoubleAddress(
7585 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007586 break;
7587
7588 case Primitive::kPrimInt:
7589 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007590 codegen_->LiteralInt32Address(
7591 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007592 break;
7593
7594 default:
7595 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7596 }
7597}
7598
Mark Mendell0616ae02015-04-17 12:49:27 -04007599/**
7600 * Class to handle late fixup of offsets into constant area.
7601 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007602class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007603 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007604 RIPFixup(CodeGeneratorX86& codegen,
7605 HX86ComputeBaseMethodAddress* base_method_address,
7606 size_t offset)
7607 : codegen_(&codegen),
7608 base_method_address_(base_method_address),
7609 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007610
7611 protected:
7612 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7613
7614 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007615 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007616
7617 private:
7618 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7619 // Patch the correct offset for the instruction. The place to patch is the
7620 // last 4 bytes of the instruction.
7621 // The value to patch is the distance from the offset in the constant area
7622 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007623 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007624 int32_t relative_position =
7625 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007626
7627 // Patch in the right value.
7628 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7629 }
7630
Mark Mendell0616ae02015-04-17 12:49:27 -04007631 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007632 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007633};
7634
Mark Mendell805b3b52015-09-18 14:10:29 -04007635/**
7636 * Class to handle late fixup of offsets to a jump table that will be created in the
7637 * constant area.
7638 */
7639class JumpTableRIPFixup : public RIPFixup {
7640 public:
7641 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007642 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7643 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007644
7645 void CreateJumpTable() {
7646 X86Assembler* assembler = codegen_->GetAssembler();
7647
7648 // Ensure that the reference to the jump table has the correct offset.
7649 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7650 SetOffset(offset_in_constant_table);
7651
7652 // The label values in the jump table are computed relative to the
7653 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007654 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007655
7656 // Populate the jump table with the correct values for the jump table.
7657 int32_t num_entries = switch_instr_->GetNumEntries();
7658 HBasicBlock* block = switch_instr_->GetBlock();
7659 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7660 // The value that we want is the target offset - the position of the table.
7661 for (int32_t i = 0; i < num_entries; i++) {
7662 HBasicBlock* b = successors[i];
7663 Label* l = codegen_->GetLabelOf(b);
7664 DCHECK(l->IsBound());
7665 int32_t offset_to_block = l->Position() - relative_offset;
7666 assembler->AppendInt32(offset_to_block);
7667 }
7668 }
7669
7670 private:
7671 const HX86PackedSwitch* switch_instr_;
7672};
7673
7674void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7675 // Generate the constant area if needed.
7676 X86Assembler* assembler = GetAssembler();
7677 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7678 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7679 // byte values.
7680 assembler->Align(4, 0);
7681 constant_area_start_ = assembler->CodeSize();
7682
7683 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007684 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007685 jump_table->CreateJumpTable();
7686 }
7687
7688 // And now add the constant area to the generated code.
7689 assembler->AddConstantArea();
7690 }
7691
7692 // And finish up.
7693 CodeGenerator::Finalize(allocator);
7694}
7695
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007696Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7697 HX86ComputeBaseMethodAddress* method_base,
7698 Register reg) {
7699 AssemblerFixup* fixup =
7700 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007701 return Address(reg, kDummy32BitOffset, fixup);
7702}
7703
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007704Address CodeGeneratorX86::LiteralFloatAddress(float v,
7705 HX86ComputeBaseMethodAddress* method_base,
7706 Register reg) {
7707 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007708 return Address(reg, kDummy32BitOffset, fixup);
7709}
7710
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007711Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7712 HX86ComputeBaseMethodAddress* method_base,
7713 Register reg) {
7714 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007715 return Address(reg, kDummy32BitOffset, fixup);
7716}
7717
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007718Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7719 HX86ComputeBaseMethodAddress* method_base,
7720 Register reg) {
7721 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007722 return Address(reg, kDummy32BitOffset, fixup);
7723}
7724
Aart Bika19616e2016-02-01 18:57:58 -08007725void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7726 if (value == 0) {
7727 __ xorl(dest, dest);
7728 } else {
7729 __ movl(dest, Immediate(value));
7730 }
7731}
7732
7733void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7734 if (value == 0) {
7735 __ testl(dest, dest);
7736 } else {
7737 __ cmpl(dest, Immediate(value));
7738 }
7739}
7740
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007741void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7742 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007743 GenerateIntCompare(lhs_reg, rhs);
7744}
7745
7746void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007747 if (rhs.IsConstant()) {
7748 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007749 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007750 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007751 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007752 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007753 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007754 }
7755}
7756
7757Address CodeGeneratorX86::ArrayAddress(Register obj,
7758 Location index,
7759 ScaleFactor scale,
7760 uint32_t data_offset) {
7761 return index.IsConstant() ?
7762 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7763 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7764}
7765
Mark Mendell805b3b52015-09-18 14:10:29 -04007766Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7767 Register reg,
7768 Register value) {
7769 // Create a fixup to be used to create and address the jump table.
7770 JumpTableRIPFixup* table_fixup =
7771 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7772
7773 // We have to populate the jump tables.
7774 fixups_to_jump_tables_.push_back(table_fixup);
7775
7776 // We want a scaled address, as we are extracting the correct offset from the table.
7777 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7778}
7779
Andreas Gampe85b62f22015-09-09 13:15:38 -07007780// TODO: target as memory.
7781void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7782 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007783 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007784 return;
7785 }
7786
7787 DCHECK_NE(type, Primitive::kPrimVoid);
7788
7789 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7790 if (target.Equals(return_loc)) {
7791 return;
7792 }
7793
7794 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7795 // with the else branch.
7796 if (type == Primitive::kPrimLong) {
7797 HParallelMove parallel_move(GetGraph()->GetArena());
7798 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7799 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7800 GetMoveResolver()->EmitNativeCode(&parallel_move);
7801 } else {
7802 // Let the parallel move resolver take care of all of this.
7803 HParallelMove parallel_move(GetGraph()->GetArena());
7804 parallel_move.AddMove(return_loc, target, type, nullptr);
7805 GetMoveResolver()->EmitNativeCode(&parallel_move);
7806 }
7807}
7808
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007809void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7810 const uint8_t* roots_data,
7811 const PatchInfo<Label>& info,
7812 uint64_t index_in_table) const {
7813 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7814 uintptr_t address =
7815 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7816 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7817 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7818 dchecked_integral_cast<uint32_t>(address);
7819}
7820
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007821void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7822 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007823 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007824 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007825 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007826 uint64_t index_in_table = it->second;
7827 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007828 }
7829
7830 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007831 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007832 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7833 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007834 uint64_t index_in_table = it->second;
7835 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007836 }
7837}
7838
Roland Levillain4d027112015-07-01 15:41:14 +01007839#undef __
7840
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007841} // namespace x86
7842} // namespace art