blob: 80776e8b788581e6494391e5cb68a8be6f3c4ee2 [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 Bikb13c65b2017-03-21 20:14:07 -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 Bikb13c65b2017-03-21 20:14:07 -0700192 RestoreLiveRegisters(codegen, locations); // only saves 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());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100387 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000388 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
392
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
395};
396
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100397class ArraySetSlowPathX86 : public SlowPathCode {
398 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000399 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 LocationSummary* locations = instruction_->GetLocations();
403 __ Bind(GetEntryLabel());
404 SaveLiveRegisters(codegen, locations);
405
406 InvokeRuntimeCallingConvention calling_convention;
407 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
408 parallel_move.AddMove(
409 locations->InAt(0),
410 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
411 Primitive::kPrimNot,
412 nullptr);
413 parallel_move.AddMove(
414 locations->InAt(1),
415 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
416 Primitive::kPrimInt,
417 nullptr);
418 parallel_move.AddMove(
419 locations->InAt(2),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
421 Primitive::kPrimNot,
422 nullptr);
423 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
424
425 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100426 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000427 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100428 RestoreLiveRegisters(codegen, locations);
429 __ jmp(GetExitLabel());
430 }
431
432 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
433
434 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
436};
437
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100438// Slow path marking an object reference `ref` during a read
439// barrier. The field `obj.field` in the object `obj` holding this
440// reference does not get updated by this slow path after marking (see
441// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
442//
443// This means that after the execution of this slow path, `ref` will
444// always be up-to-date, but `obj.field` may not; i.e., after the
445// flip, `ref` will be a to-space reference, but `obj.field` will
446// probably still be a from-space reference (unless it gets updated by
447// another thread, or if another thread installed another object
448// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000449class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
450 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100451 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
452 Location ref,
453 bool unpoison_ref_before_marking)
454 : SlowPathCode(instruction),
455 ref_(ref),
456 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000457 DCHECK(kEmitCompilerReadBarrier);
458 }
459
460 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
461
462 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
463 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100464 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100466 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000467 DCHECK(instruction_->IsInstanceFieldGet() ||
468 instruction_->IsStaticFieldGet() ||
469 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100470 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 instruction_->IsLoadClass() ||
472 instruction_->IsLoadString() ||
473 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100474 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100475 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
476 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 << "Unexpected instruction in read barrier marking slow path: "
478 << instruction_->DebugName();
479
480 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100481 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000482 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000484 }
Roland Levillain4359e612016-07-20 11:32:19 +0100485 // No need to save live registers; it's taken care of by the
486 // entrypoint. Also, there is no need to update the stack mask,
487 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 DCHECK_NE(ref_reg, ESP);
490 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100491 // "Compact" slow path, saving two moves.
492 //
493 // Instead of using the standard runtime calling convention (input
494 // and output in EAX):
495 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100496 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100497 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100498 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100499 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100501 // of a dedicated entrypoint:
502 //
503 // rX <- ReadBarrierMarkRegX(rX)
504 //
505 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100507 // This runtime call does not require a stack map.
508 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000509 __ jmp(GetExitLabel());
510 }
511
512 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 // The location (register) of the marked object reference.
514 const Location ref_;
515 // Should the reference in `ref_` be unpoisoned prior to marking it?
516 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000517
518 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
519};
520
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100521// Slow path marking an object reference `ref` during a read barrier,
522// and if needed, atomically updating the field `obj.field` in the
523// object `obj` holding this reference after marking (contrary to
524// ReadBarrierMarkSlowPathX86 above, which never tries to update
525// `obj.field`).
526//
527// This means that after the execution of this slow path, both `ref`
528// and `obj.field` will be up-to-date; i.e., after the flip, both will
529// hold the same to-space reference (unless another thread installed
530// another object reference (different from `ref`) in `obj.field`).
531class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
532 public:
533 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
534 Location ref,
535 Register obj,
536 const Address& field_addr,
537 bool unpoison_ref_before_marking,
538 Register temp)
539 : SlowPathCode(instruction),
540 ref_(ref),
541 obj_(obj),
542 field_addr_(field_addr),
543 unpoison_ref_before_marking_(unpoison_ref_before_marking),
544 temp_(temp) {
545 DCHECK(kEmitCompilerReadBarrier);
546 }
547
548 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
551 LocationSummary* locations = instruction_->GetLocations();
552 Register ref_reg = ref_.AsRegister<Register>();
553 DCHECK(locations->CanCall());
554 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
555 // This slow path is only used by the UnsafeCASObject intrinsic.
556 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
557 << "Unexpected instruction in read barrier marking and field updating slow path: "
558 << instruction_->DebugName();
559 DCHECK(instruction_->GetLocations()->Intrinsified());
560 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
561
562 __ Bind(GetEntryLabel());
563 if (unpoison_ref_before_marking_) {
564 // Object* ref = ref_addr->AsMirrorPtr()
565 __ MaybeUnpoisonHeapReference(ref_reg);
566 }
567
568 // Save the old (unpoisoned) reference.
569 __ movl(temp_, ref_reg);
570
571 // No need to save live registers; it's taken care of by the
572 // entrypoint. Also, there is no need to update the stack mask,
573 // as this runtime call will not trigger a garbage collection.
574 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
575 DCHECK_NE(ref_reg, ESP);
576 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
577 // "Compact" slow path, saving two moves.
578 //
579 // Instead of using the standard runtime calling convention (input
580 // and output in EAX):
581 //
582 // EAX <- ref
583 // EAX <- ReadBarrierMark(EAX)
584 // ref <- EAX
585 //
586 // we just use rX (the register containing `ref`) as input and output
587 // of a dedicated entrypoint:
588 //
589 // rX <- ReadBarrierMarkRegX(rX)
590 //
591 int32_t entry_point_offset =
592 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
593 // This runtime call does not require a stack map.
594 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
595
596 // If the new reference is different from the old reference,
597 // update the field in the holder (`*field_addr`).
598 //
599 // Note that this field could also hold a different object, if
600 // another thread had concurrently changed it. In that case, the
601 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
602 // operation below would abort the CAS, leaving the field as-is.
603 NearLabel done;
604 __ cmpl(temp_, ref_reg);
605 __ j(kEqual, &done);
606
607 // Update the the holder's field atomically. This may fail if
608 // mutator updates before us, but it's OK. This is achieved
609 // using a strong compare-and-set (CAS) operation with relaxed
610 // memory synchronization ordering, where the expected value is
611 // the old reference and the desired value is the new reference.
612 // This operation is implemented with a 32-bit LOCK CMPXLCHG
613 // instruction, which requires the expected value (the old
614 // reference) to be in EAX. Save EAX beforehand, and move the
615 // expected value (stored in `temp_`) into EAX.
616 __ pushl(EAX);
617 __ movl(EAX, temp_);
618
619 // Convenience aliases.
620 Register base = obj_;
621 Register expected = EAX;
622 Register value = ref_reg;
623
624 bool base_equals_value = (base == value);
625 if (kPoisonHeapReferences) {
626 if (base_equals_value) {
627 // If `base` and `value` are the same register location, move
628 // `value` to a temporary register. This way, poisoning
629 // `value` won't invalidate `base`.
630 value = temp_;
631 __ movl(value, base);
632 }
633
634 // Check that the register allocator did not assign the location
635 // of `expected` (EAX) to `value` nor to `base`, so that heap
636 // poisoning (when enabled) works as intended below.
637 // - If `value` were equal to `expected`, both references would
638 // be poisoned twice, meaning they would not be poisoned at
639 // all, as heap poisoning uses address negation.
640 // - If `base` were equal to `expected`, poisoning `expected`
641 // would invalidate `base`.
642 DCHECK_NE(value, expected);
643 DCHECK_NE(base, expected);
644
645 __ PoisonHeapReference(expected);
646 __ PoisonHeapReference(value);
647 }
648
649 __ LockCmpxchgl(field_addr_, value);
650
651 // If heap poisoning is enabled, we need to unpoison the values
652 // that were poisoned earlier.
653 if (kPoisonHeapReferences) {
654 if (base_equals_value) {
655 // `value` has been moved to a temporary register, no need
656 // to unpoison it.
657 } else {
658 __ UnpoisonHeapReference(value);
659 }
660 // No need to unpoison `expected` (EAX), as it is be overwritten below.
661 }
662
663 // Restore EAX.
664 __ popl(EAX);
665
666 __ Bind(&done);
667 __ jmp(GetExitLabel());
668 }
669
670 private:
671 // The location (register) of the marked object reference.
672 const Location ref_;
673 // The register containing the object holding the marked object reference field.
674 const Register obj_;
675 // The address of the marked reference field. The base of this address must be `obj_`.
676 const Address field_addr_;
677
678 // Should the reference in `ref_` be unpoisoned prior to marking it?
679 const bool unpoison_ref_before_marking_;
680
681 const Register temp_;
682
683 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
684};
685
Roland Levillain0d5a2812015-11-13 10:07:31 +0000686// Slow path generating a read barrier for a heap reference.
687class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
688 public:
689 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
690 Location out,
691 Location ref,
692 Location obj,
693 uint32_t offset,
694 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000695 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000696 out_(out),
697 ref_(ref),
698 obj_(obj),
699 offset_(offset),
700 index_(index) {
701 DCHECK(kEmitCompilerReadBarrier);
702 // If `obj` is equal to `out` or `ref`, it means the initial object
703 // has been overwritten by (or after) the heap object reference load
704 // to be instrumented, e.g.:
705 //
706 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000707 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000708 //
709 // In that case, we have lost the information about the original
710 // object, and the emitted read barrier cannot work properly.
711 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
712 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
713 }
714
715 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
716 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
717 LocationSummary* locations = instruction_->GetLocations();
718 Register reg_out = out_.AsRegister<Register>();
719 DCHECK(locations->CanCall());
720 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100721 DCHECK(instruction_->IsInstanceFieldGet() ||
722 instruction_->IsStaticFieldGet() ||
723 instruction_->IsArrayGet() ||
724 instruction_->IsInstanceOf() ||
725 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700726 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000727 << "Unexpected instruction in read barrier for heap reference slow path: "
728 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000729
730 __ Bind(GetEntryLabel());
731 SaveLiveRegisters(codegen, locations);
732
733 // We may have to change the index's value, but as `index_` is a
734 // constant member (like other "inputs" of this slow path),
735 // introduce a copy of it, `index`.
736 Location index = index_;
737 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100738 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000739 if (instruction_->IsArrayGet()) {
740 // Compute the actual memory offset and store it in `index`.
741 Register index_reg = index_.AsRegister<Register>();
742 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
743 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
744 // We are about to change the value of `index_reg` (see the
745 // calls to art::x86::X86Assembler::shll and
746 // art::x86::X86Assembler::AddImmediate below), but it has
747 // not been saved by the previous call to
748 // art::SlowPathCode::SaveLiveRegisters, as it is a
749 // callee-save register --
750 // art::SlowPathCode::SaveLiveRegisters does not consider
751 // callee-save registers, as it has been designed with the
752 // assumption that callee-save registers are supposed to be
753 // handled by the called function. So, as a callee-save
754 // register, `index_reg` _would_ eventually be saved onto
755 // the stack, but it would be too late: we would have
756 // changed its value earlier. Therefore, we manually save
757 // it here into another freely available register,
758 // `free_reg`, chosen of course among the caller-save
759 // registers (as a callee-save `free_reg` register would
760 // exhibit the same problem).
761 //
762 // Note we could have requested a temporary register from
763 // the register allocator instead; but we prefer not to, as
764 // this is a slow path, and we know we can find a
765 // caller-save register that is available.
766 Register free_reg = FindAvailableCallerSaveRegister(codegen);
767 __ movl(free_reg, index_reg);
768 index_reg = free_reg;
769 index = Location::RegisterLocation(index_reg);
770 } else {
771 // The initial register stored in `index_` has already been
772 // saved in the call to art::SlowPathCode::SaveLiveRegisters
773 // (as it is not a callee-save register), so we can freely
774 // use it.
775 }
776 // Shifting the index value contained in `index_reg` by the scale
777 // factor (2) cannot overflow in practice, as the runtime is
778 // unable to allocate object arrays with a size larger than
779 // 2^26 - 1 (that is, 2^28 - 4 bytes).
780 __ shll(index_reg, Immediate(TIMES_4));
781 static_assert(
782 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
783 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
784 __ AddImmediate(index_reg, Immediate(offset_));
785 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100786 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
787 // intrinsics, `index_` is not shifted by a scale factor of 2
788 // (as in the case of ArrayGet), as it is actually an offset
789 // to an object field within an object.
790 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000791 DCHECK(instruction_->GetLocations()->Intrinsified());
792 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
793 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
794 << instruction_->AsInvoke()->GetIntrinsic();
795 DCHECK_EQ(offset_, 0U);
796 DCHECK(index_.IsRegisterPair());
797 // UnsafeGet's offset location is a register pair, the low
798 // part contains the correct offset.
799 index = index_.ToLow();
800 }
801 }
802
803 // We're moving two or three locations to locations that could
804 // overlap, so we need a parallel move resolver.
805 InvokeRuntimeCallingConvention calling_convention;
806 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
807 parallel_move.AddMove(ref_,
808 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
809 Primitive::kPrimNot,
810 nullptr);
811 parallel_move.AddMove(obj_,
812 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
813 Primitive::kPrimNot,
814 nullptr);
815 if (index.IsValid()) {
816 parallel_move.AddMove(index,
817 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
818 Primitive::kPrimInt,
819 nullptr);
820 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
821 } else {
822 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
823 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
824 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100825 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000826 CheckEntrypointTypes<
827 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
828 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
829
830 RestoreLiveRegisters(codegen, locations);
831 __ jmp(GetExitLabel());
832 }
833
834 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
835
836 private:
837 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
838 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
839 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
840 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
841 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
842 return static_cast<Register>(i);
843 }
844 }
845 // We shall never fail to find a free caller-save register, as
846 // there are more than two core caller-save registers on x86
847 // (meaning it is possible to find one which is different from
848 // `ref` and `obj`).
849 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
850 LOG(FATAL) << "Could not find a free caller-save register";
851 UNREACHABLE();
852 }
853
Roland Levillain0d5a2812015-11-13 10:07:31 +0000854 const Location out_;
855 const Location ref_;
856 const Location obj_;
857 const uint32_t offset_;
858 // An additional location containing an index to an array.
859 // Only used for HArrayGet and the UnsafeGetObject &
860 // UnsafeGetObjectVolatile intrinsics.
861 const Location index_;
862
863 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
864};
865
866// Slow path generating a read barrier for a GC root.
867class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
868 public:
869 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000870 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000871 DCHECK(kEmitCompilerReadBarrier);
872 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000873
874 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
875 LocationSummary* locations = instruction_->GetLocations();
876 Register reg_out = out_.AsRegister<Register>();
877 DCHECK(locations->CanCall());
878 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000879 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
880 << "Unexpected instruction in read barrier for GC root slow path: "
881 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000882
883 __ Bind(GetEntryLabel());
884 SaveLiveRegisters(codegen, locations);
885
886 InvokeRuntimeCallingConvention calling_convention;
887 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
888 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100889 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890 instruction_,
891 instruction_->GetDexPc(),
892 this);
893 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
894 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
895
896 RestoreLiveRegisters(codegen, locations);
897 __ jmp(GetExitLabel());
898 }
899
900 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
901
902 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000903 const Location out_;
904 const Location root_;
905
906 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
907};
908
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100910// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
911#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100912
Aart Bike9f37602015-10-09 11:15:55 -0700913inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700914 switch (cond) {
915 case kCondEQ: return kEqual;
916 case kCondNE: return kNotEqual;
917 case kCondLT: return kLess;
918 case kCondLE: return kLessEqual;
919 case kCondGT: return kGreater;
920 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700921 case kCondB: return kBelow;
922 case kCondBE: return kBelowEqual;
923 case kCondA: return kAbove;
924 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700925 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100926 LOG(FATAL) << "Unreachable";
927 UNREACHABLE();
928}
929
Aart Bike9f37602015-10-09 11:15:55 -0700930// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100931inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
932 switch (cond) {
933 case kCondEQ: return kEqual;
934 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700935 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100936 case kCondLT: return kBelow;
937 case kCondLE: return kBelowEqual;
938 case kCondGT: return kAbove;
939 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700940 // Unsigned remain unchanged.
941 case kCondB: return kBelow;
942 case kCondBE: return kBelowEqual;
943 case kCondA: return kAbove;
944 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945 }
946 LOG(FATAL) << "Unreachable";
947 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700948}
949
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100950void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100951 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100952}
953
954void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100955 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100956}
957
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100958size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
959 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
960 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100961}
962
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100963size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
964 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
965 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100966}
967
Mark Mendell7c8d0092015-01-26 11:21:33 -0500968size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700969 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700970 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700971 } else {
972 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
973 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500974 return GetFloatingPointSpillSlotSize();
975}
976
977size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700978 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700979 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700980 } else {
981 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
982 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500983 return GetFloatingPointSpillSlotSize();
984}
985
Calin Juravle175dc732015-08-25 15:42:32 +0100986void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
987 HInstruction* instruction,
988 uint32_t dex_pc,
989 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100990 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100991 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
992 if (EntrypointRequiresStackMap(entrypoint)) {
993 RecordPcInfo(instruction, dex_pc, slow_path);
994 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100995}
996
Roland Levillaindec8f632016-07-22 17:10:06 +0100997void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
998 HInstruction* instruction,
999 SlowPathCode* slow_path) {
1000 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001001 GenerateInvokeRuntime(entry_point_offset);
1002}
1003
1004void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001005 __ fs()->call(Address::Absolute(entry_point_offset));
1006}
1007
Mark Mendellfb8d2792015-03-31 22:16:59 -04001008CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001009 const X86InstructionSetFeatures& isa_features,
1010 const CompilerOptions& compiler_options,
1011 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001012 : CodeGenerator(graph,
1013 kNumberOfCpuRegisters,
1014 kNumberOfXmmRegisters,
1015 kNumberOfRegisterPairs,
1016 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1017 arraysize(kCoreCalleeSaves))
1018 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001019 0,
1020 compiler_options,
1021 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001022 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001023 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001024 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001025 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001026 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001027 isa_features_(isa_features),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001028 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001029 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001030 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1031 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001032 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001033 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001034 constant_area_start_(-1),
1035 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001036 method_address_offset_(std::less<uint32_t>(),
1037 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001038 // Use a fake return address register to mimic Quick.
1039 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001040}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041
David Brazdil58282f42016-01-14 12:45:10 +00001042void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001043 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001044 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001045}
1046
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001047InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001048 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001049 assembler_(codegen->GetAssembler()),
1050 codegen_(codegen) {}
1051
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001052static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001053 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001054}
1055
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001057 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001058 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001059 bool skip_overflow_check =
1060 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001061 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001062
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001063 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001064 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001065 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001066 }
1067
Mark Mendell5f874182015-03-04 15:42:45 -05001068 if (HasEmptyFrame()) {
1069 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001070 }
Mark Mendell5f874182015-03-04 15:42:45 -05001071
1072 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1073 Register reg = kCoreCalleeSaves[i];
1074 if (allocated_registers_.ContainsCoreRegister(reg)) {
1075 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 __ cfi().AdjustCFAOffset(kX86WordSize);
1077 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001078 }
1079 }
1080
Mingyao Yang063fc772016-08-02 11:02:54 -07001081 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1082 // Initialize should_deoptimize flag to 0.
1083 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1084 }
1085
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 int adjust = GetFrameSize() - FrameEntrySpillSize();
1087 __ subl(ESP, Immediate(adjust));
1088 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001089 // Save the current method if we need it. Note that we do not
1090 // do this in HCurrentMethod, as the instruction might have been removed
1091 // in the SSA graph.
1092 if (RequiresCurrentMethod()) {
1093 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1094 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001095}
1096
1097void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001098 __ cfi().RememberState();
1099 if (!HasEmptyFrame()) {
1100 int adjust = GetFrameSize() - FrameEntrySpillSize();
1101 __ addl(ESP, Immediate(adjust));
1102 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001103
David Srbeckyc34dc932015-04-12 09:27:43 +01001104 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1105 Register reg = kCoreCalleeSaves[i];
1106 if (allocated_registers_.ContainsCoreRegister(reg)) {
1107 __ popl(reg);
1108 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1109 __ cfi().Restore(DWARFReg(reg));
1110 }
Mark Mendell5f874182015-03-04 15:42:45 -05001111 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001112 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001113 __ ret();
1114 __ cfi().RestoreState();
1115 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001116}
1117
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001118void CodeGeneratorX86::Bind(HBasicBlock* block) {
1119 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001120}
1121
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001122Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1123 switch (type) {
1124 case Primitive::kPrimBoolean:
1125 case Primitive::kPrimByte:
1126 case Primitive::kPrimChar:
1127 case Primitive::kPrimShort:
1128 case Primitive::kPrimInt:
1129 case Primitive::kPrimNot:
1130 return Location::RegisterLocation(EAX);
1131
1132 case Primitive::kPrimLong:
1133 return Location::RegisterPairLocation(EAX, EDX);
1134
1135 case Primitive::kPrimVoid:
1136 return Location::NoLocation();
1137
1138 case Primitive::kPrimDouble:
1139 case Primitive::kPrimFloat:
1140 return Location::FpuRegisterLocation(XMM0);
1141 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001142
1143 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001144}
1145
1146Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1147 return Location::RegisterLocation(kMethodRegisterArgument);
1148}
1149
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001150Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001151 switch (type) {
1152 case Primitive::kPrimBoolean:
1153 case Primitive::kPrimByte:
1154 case Primitive::kPrimChar:
1155 case Primitive::kPrimShort:
1156 case Primitive::kPrimInt:
1157 case Primitive::kPrimNot: {
1158 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001159 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001160 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001161 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001162 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001163 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001164 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001165 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001166
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001167 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 uint32_t index = gp_index_;
1169 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001170 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001171 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1173 calling_convention.GetRegisterPairAt(index));
1174 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1177 }
1178 }
1179
1180 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001181 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001182 stack_index_++;
1183 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1184 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1185 } else {
1186 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1187 }
1188 }
1189
1190 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001191 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001192 stack_index_ += 2;
1193 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1194 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1195 } else {
1196 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001197 }
1198 }
1199
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001200 case Primitive::kPrimVoid:
1201 LOG(FATAL) << "Unexpected parameter type " << type;
1202 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001203 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001204 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001205}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001206
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207void CodeGeneratorX86::Move32(Location destination, Location source) {
1208 if (source.Equals(destination)) {
1209 return;
1210 }
1211 if (destination.IsRegister()) {
1212 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001213 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 } else {
1217 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001218 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001219 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 } else if (destination.IsFpuRegister()) {
1221 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 } else {
1226 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001230 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001231 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001235 } else if (source.IsConstant()) {
1236 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001237 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001238 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 } else {
1240 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001241 __ pushl(Address(ESP, source.GetStackIndex()));
1242 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 }
1244 }
1245}
1246
1247void CodeGeneratorX86::Move64(Location destination, Location source) {
1248 if (source.Equals(destination)) {
1249 return;
1250 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001251 if (destination.IsRegisterPair()) {
1252 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001253 EmitParallelMoves(
1254 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1255 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001256 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001257 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001258 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1259 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001260 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1262 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1263 __ psrlq(src_reg, Immediate(32));
1264 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001265 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001266 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001268 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1269 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1271 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001273 if (source.IsFpuRegister()) {
1274 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1275 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001276 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 } else if (source.IsRegisterPair()) {
1278 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1279 // Create stack space for 2 elements.
1280 __ subl(ESP, Immediate(2 * elem_size));
1281 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1282 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1283 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1284 // And remove the temporary stack space we allocated.
1285 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001286 } else {
1287 LOG(FATAL) << "Unimplemented";
1288 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001290 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001291 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001292 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001293 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001294 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001295 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001296 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001297 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001298 } else if (source.IsConstant()) {
1299 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001300 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1301 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001302 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001303 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1304 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001305 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001306 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001307 EmitParallelMoves(
1308 Location::StackSlot(source.GetStackIndex()),
1309 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001310 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001311 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001312 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1313 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 }
1315 }
1316}
1317
Calin Juravle175dc732015-08-25 15:42:32 +01001318void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1319 DCHECK(location.IsRegister());
1320 __ movl(location.AsRegister<Register>(), Immediate(value));
1321}
1322
Calin Juravlee460d1d2015-09-29 04:52:17 +01001323void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001324 HParallelMove move(GetGraph()->GetArena());
1325 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1326 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1327 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001328 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001329 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001330 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001331 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001332}
1333
1334void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1335 if (location.IsRegister()) {
1336 locations->AddTemp(location);
1337 } else if (location.IsRegisterPair()) {
1338 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1339 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1340 } else {
1341 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1342 }
1343}
1344
David Brazdilfc6a86a2015-06-26 10:33:45 +00001345void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001346 DCHECK(!successor->IsExitBlock());
1347
1348 HBasicBlock* block = got->GetBlock();
1349 HInstruction* previous = got->GetPrevious();
1350
1351 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001352 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001353 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1354 return;
1355 }
1356
1357 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1358 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1359 }
1360 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001361 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001362 }
1363}
1364
David Brazdilfc6a86a2015-06-26 10:33:45 +00001365void LocationsBuilderX86::VisitGoto(HGoto* got) {
1366 got->SetLocations(nullptr);
1367}
1368
1369void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1370 HandleGoto(got, got->GetSuccessor());
1371}
1372
1373void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1374 try_boundary->SetLocations(nullptr);
1375}
1376
1377void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1378 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1379 if (!successor->IsExitBlock()) {
1380 HandleGoto(try_boundary, successor);
1381 }
1382}
1383
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001384void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001385 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001386}
1387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001388void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001389}
1390
Mark Mendell152408f2015-12-31 12:28:50 -05001391template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001392void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001393 LabelType* true_label,
1394 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001395 if (cond->IsFPConditionTrueIfNaN()) {
1396 __ j(kUnordered, true_label);
1397 } else if (cond->IsFPConditionFalseIfNaN()) {
1398 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001399 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001401}
1402
Mark Mendell152408f2015-12-31 12:28:50 -05001403template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001404void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001405 LabelType* true_label,
1406 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001407 LocationSummary* locations = cond->GetLocations();
1408 Location left = locations->InAt(0);
1409 Location right = locations->InAt(1);
1410 IfCondition if_cond = cond->GetCondition();
1411
Mark Mendellc4701932015-04-10 13:18:51 -04001412 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001414 IfCondition true_high_cond = if_cond;
1415 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001416 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001417
1418 // Set the conditions for the test, remembering that == needs to be
1419 // decided using the low words.
1420 switch (if_cond) {
1421 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001422 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001424 break;
1425 case kCondLT:
1426 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001427 break;
1428 case kCondLE:
1429 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001430 break;
1431 case kCondGT:
1432 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001433 break;
1434 case kCondGE:
1435 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001436 break;
Aart Bike9f37602015-10-09 11:15:55 -07001437 case kCondB:
1438 false_high_cond = kCondA;
1439 break;
1440 case kCondBE:
1441 true_high_cond = kCondB;
1442 break;
1443 case kCondA:
1444 false_high_cond = kCondB;
1445 break;
1446 case kCondAE:
1447 true_high_cond = kCondA;
1448 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001449 }
1450
1451 if (right.IsConstant()) {
1452 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001453 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001454 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001455
Aart Bika19616e2016-02-01 18:57:58 -08001456 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001457 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001458 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001459 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001460 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001461 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001462 __ j(X86Condition(true_high_cond), true_label);
1463 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001464 }
1465 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001466 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001467 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001468 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001469 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001470
1471 __ cmpl(left_high, right_high);
1472 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001473 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001474 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001475 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001476 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001477 __ j(X86Condition(true_high_cond), true_label);
1478 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001479 }
1480 // Must be equal high, so compare the lows.
1481 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001482 } else {
1483 DCHECK(right.IsDoubleStackSlot());
1484 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1485 if (if_cond == kCondNE) {
1486 __ j(X86Condition(true_high_cond), true_label);
1487 } else if (if_cond == kCondEQ) {
1488 __ j(X86Condition(false_high_cond), false_label);
1489 } else {
1490 __ j(X86Condition(true_high_cond), true_label);
1491 __ j(X86Condition(false_high_cond), false_label);
1492 }
1493 // Must be equal high, so compare the lows.
1494 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001495 }
1496 // The last comparison might be unsigned.
1497 __ j(final_condition, true_label);
1498}
1499
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001500void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1501 Location rhs,
1502 HInstruction* insn,
1503 bool is_double) {
1504 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1505 if (is_double) {
1506 if (rhs.IsFpuRegister()) {
1507 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1508 } else if (const_area != nullptr) {
1509 DCHECK(const_area->IsEmittedAtUseSite());
1510 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1511 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001512 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1513 const_area->GetBaseMethodAddress(),
1514 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001515 } else {
1516 DCHECK(rhs.IsDoubleStackSlot());
1517 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1518 }
1519 } else {
1520 if (rhs.IsFpuRegister()) {
1521 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1522 } else if (const_area != nullptr) {
1523 DCHECK(const_area->IsEmittedAtUseSite());
1524 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1525 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001526 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1527 const_area->GetBaseMethodAddress(),
1528 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001529 } else {
1530 DCHECK(rhs.IsStackSlot());
1531 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1532 }
1533 }
1534}
1535
Mark Mendell152408f2015-12-31 12:28:50 -05001536template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001537void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001538 LabelType* true_target_in,
1539 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001540 // Generated branching requires both targets to be explicit. If either of the
1541 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001542 LabelType fallthrough_target;
1543 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1544 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001545
Mark Mendellc4701932015-04-10 13:18:51 -04001546 LocationSummary* locations = condition->GetLocations();
1547 Location left = locations->InAt(0);
1548 Location right = locations->InAt(1);
1549
Mark Mendellc4701932015-04-10 13:18:51 -04001550 Primitive::Type type = condition->InputAt(0)->GetType();
1551 switch (type) {
1552 case Primitive::kPrimLong:
1553 GenerateLongComparesAndJumps(condition, true_target, false_target);
1554 break;
1555 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001556 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001557 GenerateFPJumps(condition, true_target, false_target);
1558 break;
1559 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001560 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001561 GenerateFPJumps(condition, true_target, false_target);
1562 break;
1563 default:
1564 LOG(FATAL) << "Unexpected compare type " << type;
1565 }
1566
David Brazdil0debae72015-11-12 18:37:00 +00001567 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001568 __ jmp(false_target);
1569 }
David Brazdil0debae72015-11-12 18:37:00 +00001570
1571 if (fallthrough_target.IsLinked()) {
1572 __ Bind(&fallthrough_target);
1573 }
Mark Mendellc4701932015-04-10 13:18:51 -04001574}
1575
David Brazdil0debae72015-11-12 18:37:00 +00001576static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1577 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1578 // are set only strictly before `branch`. We can't use the eflags on long/FP
1579 // conditions if they are materialized due to the complex branching.
1580 return cond->IsCondition() &&
1581 cond->GetNext() == branch &&
1582 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1583 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1584}
1585
Mark Mendell152408f2015-12-31 12:28:50 -05001586template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001588 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001589 LabelType* true_target,
1590 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001591 HInstruction* cond = instruction->InputAt(condition_input_index);
1592
1593 if (true_target == nullptr && false_target == nullptr) {
1594 // Nothing to do. The code always falls through.
1595 return;
1596 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001597 // Constant condition, statically compared against "true" (integer value 1).
1598 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001599 if (true_target != nullptr) {
1600 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001601 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001602 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001603 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001604 if (false_target != nullptr) {
1605 __ jmp(false_target);
1606 }
1607 }
1608 return;
1609 }
1610
1611 // The following code generates these patterns:
1612 // (1) true_target == nullptr && false_target != nullptr
1613 // - opposite condition true => branch to false_target
1614 // (2) true_target != nullptr && false_target == nullptr
1615 // - condition true => branch to true_target
1616 // (3) true_target != nullptr && false_target != nullptr
1617 // - condition true => branch to true_target
1618 // - branch to false_target
1619 if (IsBooleanValueOrMaterializedCondition(cond)) {
1620 if (AreEflagsSetFrom(cond, instruction)) {
1621 if (true_target == nullptr) {
1622 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1623 } else {
1624 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1625 }
1626 } else {
1627 // Materialized condition, compare against 0.
1628 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1629 if (lhs.IsRegister()) {
1630 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1631 } else {
1632 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1633 }
1634 if (true_target == nullptr) {
1635 __ j(kEqual, false_target);
1636 } else {
1637 __ j(kNotEqual, true_target);
1638 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001639 }
1640 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001641 // Condition has not been materialized, use its inputs as the comparison and
1642 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001643 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001644
1645 // If this is a long or FP comparison that has been folded into
1646 // the HCondition, generate the comparison directly.
1647 Primitive::Type type = condition->InputAt(0)->GetType();
1648 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1649 GenerateCompareTestAndBranch(condition, true_target, false_target);
1650 return;
1651 }
1652
1653 Location lhs = condition->GetLocations()->InAt(0);
1654 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001655 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001656 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001657 if (true_target == nullptr) {
1658 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1659 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001660 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001661 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001662 }
David Brazdil0debae72015-11-12 18:37:00 +00001663
1664 // If neither branch falls through (case 3), the conditional branch to `true_target`
1665 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1666 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001668 }
1669}
1670
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001671void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1673 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674 locations->SetInAt(0, Location::Any());
1675 }
1676}
1677
1678void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001679 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1680 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1681 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1682 nullptr : codegen_->GetLabelOf(true_successor);
1683 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1684 nullptr : codegen_->GetLabelOf(false_successor);
1685 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001686}
1687
1688void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1689 LocationSummary* locations = new (GetGraph()->GetArena())
1690 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001691 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001692 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693 locations->SetInAt(0, Location::Any());
1694 }
1695}
1696
1697void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001698 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001699 GenerateTestAndBranch<Label>(deoptimize,
1700 /* condition_input_index */ 0,
1701 slow_path->GetEntryLabel(),
1702 /* false_target */ nullptr);
1703}
1704
Mingyao Yang063fc772016-08-02 11:02:54 -07001705void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1706 LocationSummary* locations = new (GetGraph()->GetArena())
1707 LocationSummary(flag, LocationSummary::kNoCall);
1708 locations->SetOut(Location::RequiresRegister());
1709}
1710
1711void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1712 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1713 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1714}
1715
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001716static bool SelectCanUseCMOV(HSelect* select) {
1717 // There are no conditional move instructions for XMMs.
1718 if (Primitive::IsFloatingPointType(select->GetType())) {
1719 return false;
1720 }
1721
1722 // A FP condition doesn't generate the single CC that we need.
1723 // In 32 bit mode, a long condition doesn't generate a single CC either.
1724 HInstruction* condition = select->GetCondition();
1725 if (condition->IsCondition()) {
1726 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1727 if (compare_type == Primitive::kPrimLong ||
1728 Primitive::IsFloatingPointType(compare_type)) {
1729 return false;
1730 }
1731 }
1732
1733 // We can generate a CMOV for this Select.
1734 return true;
1735}
1736
David Brazdil74eb1b22015-12-14 11:44:01 +00001737void LocationsBuilderX86::VisitSelect(HSelect* select) {
1738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001739 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001740 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001741 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001742 } else {
1743 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744 if (SelectCanUseCMOV(select)) {
1745 if (select->InputAt(1)->IsConstant()) {
1746 // Cmov can't handle a constant value.
1747 locations->SetInAt(1, Location::RequiresRegister());
1748 } else {
1749 locations->SetInAt(1, Location::Any());
1750 }
1751 } else {
1752 locations->SetInAt(1, Location::Any());
1753 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001754 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001755 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1756 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001757 }
1758 locations->SetOut(Location::SameAsFirstInput());
1759}
1760
1761void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1762 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001763 DCHECK(locations->InAt(0).Equals(locations->Out()));
1764 if (SelectCanUseCMOV(select)) {
1765 // If both the condition and the source types are integer, we can generate
1766 // a CMOV to implement Select.
1767
1768 HInstruction* select_condition = select->GetCondition();
1769 Condition cond = kNotEqual;
1770
1771 // Figure out how to test the 'condition'.
1772 if (select_condition->IsCondition()) {
1773 HCondition* condition = select_condition->AsCondition();
1774 if (!condition->IsEmittedAtUseSite()) {
1775 // This was a previously materialized condition.
1776 // Can we use the existing condition code?
1777 if (AreEflagsSetFrom(condition, select)) {
1778 // Materialization was the previous instruction. Condition codes are right.
1779 cond = X86Condition(condition->GetCondition());
1780 } else {
1781 // No, we have to recreate the condition code.
1782 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1783 __ testl(cond_reg, cond_reg);
1784 }
1785 } else {
1786 // We can't handle FP or long here.
1787 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1788 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1789 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001790 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001791 cond = X86Condition(condition->GetCondition());
1792 }
1793 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001794 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001795 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1796 __ testl(cond_reg, cond_reg);
1797 }
1798
1799 // If the condition is true, overwrite the output, which already contains false.
1800 Location false_loc = locations->InAt(0);
1801 Location true_loc = locations->InAt(1);
1802 if (select->GetType() == Primitive::kPrimLong) {
1803 // 64 bit conditional move.
1804 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1805 Register false_low = false_loc.AsRegisterPairLow<Register>();
1806 if (true_loc.IsRegisterPair()) {
1807 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1808 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1809 } else {
1810 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1811 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1812 }
1813 } else {
1814 // 32 bit conditional move.
1815 Register false_reg = false_loc.AsRegister<Register>();
1816 if (true_loc.IsRegister()) {
1817 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1818 } else {
1819 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1820 }
1821 }
1822 } else {
1823 NearLabel false_target;
1824 GenerateTestAndBranch<NearLabel>(
1825 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1826 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1827 __ Bind(&false_target);
1828 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001829}
1830
David Srbecky0cf44932015-12-09 14:09:59 +00001831void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1832 new (GetGraph()->GetArena()) LocationSummary(info);
1833}
1834
David Srbeckyd28f4a02016-03-14 17:14:24 +00001835void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1836 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001837}
1838
1839void CodeGeneratorX86::GenerateNop() {
1840 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001841}
1842
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001845 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001846 // Handle the long/FP comparisons made in instruction simplification.
1847 switch (cond->InputAt(0)->GetType()) {
1848 case Primitive::kPrimLong: {
1849 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001850 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001851 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001852 locations->SetOut(Location::RequiresRegister());
1853 }
1854 break;
1855 }
1856 case Primitive::kPrimFloat:
1857 case Primitive::kPrimDouble: {
1858 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001859 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1860 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1861 } else if (cond->InputAt(1)->IsConstant()) {
1862 locations->SetInAt(1, Location::RequiresFpuRegister());
1863 } else {
1864 locations->SetInAt(1, Location::Any());
1865 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001866 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001867 locations->SetOut(Location::RequiresRegister());
1868 }
1869 break;
1870 }
1871 default:
1872 locations->SetInAt(0, Location::RequiresRegister());
1873 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001874 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001875 // We need a byte register.
1876 locations->SetOut(Location::RegisterLocation(ECX));
1877 }
1878 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001879 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001880}
1881
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001882void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001885 }
Mark Mendellc4701932015-04-10 13:18:51 -04001886
1887 LocationSummary* locations = cond->GetLocations();
1888 Location lhs = locations->InAt(0);
1889 Location rhs = locations->InAt(1);
1890 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001891 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001892
1893 switch (cond->InputAt(0)->GetType()) {
1894 default: {
1895 // Integer case.
1896
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001897 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001898 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001899 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001900 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001901 return;
1902 }
1903 case Primitive::kPrimLong:
1904 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1905 break;
1906 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001907 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001908 GenerateFPJumps(cond, &true_label, &false_label);
1909 break;
1910 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001911 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001912 GenerateFPJumps(cond, &true_label, &false_label);
1913 break;
1914 }
1915
1916 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001917 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001918
Roland Levillain4fa13f62015-07-06 18:11:54 +01001919 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001920 __ Bind(&false_label);
1921 __ xorl(reg, reg);
1922 __ jmp(&done_label);
1923
Roland Levillain4fa13f62015-07-06 18:11:54 +01001924 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001925 __ Bind(&true_label);
1926 __ movl(reg, Immediate(1));
1927 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001928}
1929
1930void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001931 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001932}
1933
1934void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001935 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001936}
1937
1938void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001939 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001940}
1941
1942void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001943 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001944}
1945
1946void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001948}
1949
1950void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001951 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001952}
1953
1954void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001956}
1957
1958void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001976}
1977
Aart Bike9f37602015-10-09 11:15:55 -07001978void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001980}
1981
1982void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001984}
1985
1986void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001988}
1989
1990void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001992}
1993
1994void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001996}
1997
1998void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002000}
2001
2002void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002004}
2005
2006void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002008}
2009
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002013 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002017 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002018}
2019
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002020void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2023 locations->SetOut(Location::ConstantLocation(constant));
2024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002027 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002028}
2029
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002033 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002037 // Will be generated at use site.
2038}
2039
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002040void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2043 locations->SetOut(Location::ConstantLocation(constant));
2044}
2045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002047 // Will be generated at use site.
2048}
2049
2050void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* 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::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 // Will be generated at use site.
2058}
2059
Calin Juravle27df7582015-04-17 19:12:31 +01002060void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2061 memory_barrier->SetLocations(nullptr);
2062}
2063
2064void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002065 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002066}
2067
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002068void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002069 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002070}
2071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002072void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002073 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002074}
2075
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002076void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002077 LocationSummary* locations =
2078 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002079 switch (ret->InputAt(0)->GetType()) {
2080 case Primitive::kPrimBoolean:
2081 case Primitive::kPrimByte:
2082 case Primitive::kPrimChar:
2083 case Primitive::kPrimShort:
2084 case Primitive::kPrimInt:
2085 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002086 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002087 break;
2088
2089 case Primitive::kPrimLong:
2090 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002091 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002092 break;
2093
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002094 case Primitive::kPrimFloat:
2095 case Primitive::kPrimDouble:
2096 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002097 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 break;
2099
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002100 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002101 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002102 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002103}
2104
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002105void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002106 if (kIsDebugBuild) {
2107 switch (ret->InputAt(0)->GetType()) {
2108 case Primitive::kPrimBoolean:
2109 case Primitive::kPrimByte:
2110 case Primitive::kPrimChar:
2111 case Primitive::kPrimShort:
2112 case Primitive::kPrimInt:
2113 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002115 break;
2116
2117 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2119 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 break;
2121
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002122 case Primitive::kPrimFloat:
2123 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 break;
2126
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002127 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002128 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 }
2130 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002131 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002132}
2133
Calin Juravle175dc732015-08-25 15:42:32 +01002134void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2135 // The trampoline uses the same calling convention as dex calling conventions,
2136 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2137 // the method_idx.
2138 HandleInvoke(invoke);
2139}
2140
2141void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2142 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2143}
2144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002145void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002146 // Explicit clinit checks triggered by static invokes must have been pruned by
2147 // art::PrepareForRegisterAllocation.
2148 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002149
Mark Mendellfb8d2792015-03-31 22:16:59 -04002150 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002151 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002152 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002153 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002154 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002155 return;
2156 }
2157
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002158 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002159
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002160 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2161 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002162 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002163 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002164}
2165
Mark Mendell09ed1a32015-03-25 08:30:06 -04002166static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2167 if (invoke->GetLocations()->Intrinsified()) {
2168 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2169 intrinsic.Dispatch(invoke);
2170 return true;
2171 }
2172 return false;
2173}
2174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002175void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002176 // Explicit clinit checks triggered by static invokes must have been pruned by
2177 // art::PrepareForRegisterAllocation.
2178 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002179
Mark Mendell09ed1a32015-03-25 08:30:06 -04002180 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2181 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002182 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002184 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002185 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002186 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002188}
2189
2190void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002191 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2192 if (intrinsic.TryDispatch(invoke)) {
2193 return;
2194 }
2195
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002196 HandleInvoke(invoke);
2197}
2198
2199void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002200 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002201 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002202}
2203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002205 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2206 return;
2207 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002208
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002209 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002210 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002212}
2213
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002215 // This call to HandleInvoke allocates a temporary (core) register
2216 // which is also used to transfer the hidden argument from FP to
2217 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002218 HandleInvoke(invoke);
2219 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002220 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002221}
2222
2223void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2224 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002225 LocationSummary* locations = invoke->GetLocations();
2226 Register temp = locations->GetTemp(0).AsRegister<Register>();
2227 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002228 Location receiver = locations->InAt(0);
2229 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2230
Roland Levillain0d5a2812015-11-13 10:07:31 +00002231 // Set the hidden argument. This is safe to do this here, as XMM7
2232 // won't be modified thereafter, before the `call` instruction.
2233 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002235 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 if (receiver.IsStackSlot()) {
2238 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002239 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240 __ movl(temp, Address(temp, class_offset));
2241 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002242 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002243 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002244 }
Roland Levillain4d027112015-07-01 15:41:14 +01002245 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002246 // Instead of simply (possibly) unpoisoning `temp` here, we should
2247 // emit a read barrier for the previous class reference load.
2248 // However this is not required in practice, as this is an
2249 // intermediate/temporary reference and because the current
2250 // concurrent copying collector keeps the from-space memory
2251 // intact/accessible until the end of the marking phase (the
2252 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002253 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002254 // temp = temp->GetAddressOfIMT()
2255 __ movl(temp,
2256 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002258 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002259 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 __ movl(temp, Address(temp, method_offset));
2261 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002263 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264
2265 DCHECK(!codegen_->IsLeafMethod());
2266 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2267}
2268
Orion Hodsonac141392017-01-13 11:53:47 +00002269void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2270 HandleInvoke(invoke);
2271}
2272
2273void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2274 codegen_->GenerateInvokePolymorphicCall(invoke);
2275}
2276
Roland Levillain88cb1752014-10-20 16:36:47 +01002277void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2278 LocationSummary* locations =
2279 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2280 switch (neg->GetResultType()) {
2281 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002282 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetOut(Location::SameAsFirstInput());
2285 break;
2286
Roland Levillain88cb1752014-10-20 16:36:47 +01002287 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002288 locations->SetInAt(0, Location::RequiresFpuRegister());
2289 locations->SetOut(Location::SameAsFirstInput());
2290 locations->AddTemp(Location::RequiresRegister());
2291 locations->AddTemp(Location::RequiresFpuRegister());
2292 break;
2293
Roland Levillain88cb1752014-10-20 16:36:47 +01002294 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002295 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002296 locations->SetOut(Location::SameAsFirstInput());
2297 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002298 break;
2299
2300 default:
2301 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2302 }
2303}
2304
2305void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2306 LocationSummary* locations = neg->GetLocations();
2307 Location out = locations->Out();
2308 Location in = locations->InAt(0);
2309 switch (neg->GetResultType()) {
2310 case Primitive::kPrimInt:
2311 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002312 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002314 break;
2315
2316 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002317 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002318 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002319 __ negl(out.AsRegisterPairLow<Register>());
2320 // Negation is similar to subtraction from zero. The least
2321 // significant byte triggers a borrow when it is different from
2322 // zero; to take it into account, add 1 to the most significant
2323 // byte if the carry flag (CF) is set to 1 after the first NEGL
2324 // operation.
2325 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2326 __ negl(out.AsRegisterPairHigh<Register>());
2327 break;
2328
Roland Levillain5368c212014-11-27 15:03:41 +00002329 case Primitive::kPrimFloat: {
2330 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 Register constant = locations->GetTemp(0).AsRegister<Register>();
2332 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002333 // Implement float negation with an exclusive or with value
2334 // 0x80000000 (mask for bit 31, representing the sign of a
2335 // single-precision floating-point number).
2336 __ movl(constant, Immediate(INT32_C(0x80000000)));
2337 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002338 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002339 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002340 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002341
Roland Levillain5368c212014-11-27 15:03:41 +00002342 case Primitive::kPrimDouble: {
2343 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002344 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002345 // Implement double negation with an exclusive or with value
2346 // 0x8000000000000000 (mask for bit 63, representing the sign of
2347 // a double-precision floating-point number).
2348 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002350 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002351 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002352
2353 default:
2354 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2355 }
2356}
2357
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002358void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2359 LocationSummary* locations =
2360 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2361 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2362 locations->SetInAt(0, Location::RequiresFpuRegister());
2363 locations->SetInAt(1, Location::RequiresRegister());
2364 locations->SetOut(Location::SameAsFirstInput());
2365 locations->AddTemp(Location::RequiresFpuRegister());
2366}
2367
2368void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2369 LocationSummary* locations = neg->GetLocations();
2370 Location out = locations->Out();
2371 DCHECK(locations->InAt(0).Equals(out));
2372
2373 Register constant_area = locations->InAt(1).AsRegister<Register>();
2374 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2375 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002376 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2377 neg->GetBaseMethodAddress(),
2378 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002379 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2380 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002381 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2382 neg->GetBaseMethodAddress(),
2383 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002384 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2385 }
2386}
2387
Roland Levillaindff1f282014-11-05 14:15:05 +00002388void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002389 Primitive::Type result_type = conversion->GetResultType();
2390 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002391 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002392
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002393 // The float-to-long and double-to-long type conversions rely on a
2394 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002395 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002396 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2397 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002398 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002399 : LocationSummary::kNoCall;
2400 LocationSummary* locations =
2401 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2402
David Brazdilb2bd1c52015-03-25 11:17:37 +00002403 // The Java language does not allow treating boolean as an integral type but
2404 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002405
Roland Levillaindff1f282014-11-05 14:15:05 +00002406 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002407 case Primitive::kPrimByte:
2408 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002409 case Primitive::kPrimLong: {
2410 // Type conversion from long to byte is a result of code transformations.
2411 HInstruction* input = conversion->InputAt(0);
2412 Location input_location = input->IsConstant()
2413 ? Location::ConstantLocation(input->AsConstant())
2414 : Location::RegisterPairLocation(EAX, EDX);
2415 locations->SetInAt(0, input_location);
2416 // Make the output overlap to please the register allocator. This greatly simplifies
2417 // the validation of the linear scan implementation
2418 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2419 break;
2420 }
David Brazdil46e2a392015-03-16 17:31:52 +00002421 case Primitive::kPrimBoolean:
2422 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002423 case Primitive::kPrimShort:
2424 case Primitive::kPrimInt:
2425 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002426 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002427 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2428 // Make the output overlap to please the register allocator. This greatly simplifies
2429 // the validation of the linear scan implementation
2430 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002431 break;
2432
2433 default:
2434 LOG(FATAL) << "Unexpected type conversion from " << input_type
2435 << " to " << result_type;
2436 }
2437 break;
2438
Roland Levillain01a8d712014-11-14 16:27:39 +00002439 case Primitive::kPrimShort:
2440 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002441 case Primitive::kPrimLong:
2442 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002443 case Primitive::kPrimBoolean:
2444 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002445 case Primitive::kPrimByte:
2446 case Primitive::kPrimInt:
2447 case Primitive::kPrimChar:
2448 // Processing a Dex `int-to-short' instruction.
2449 locations->SetInAt(0, Location::Any());
2450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Roland Levillain946e1432014-11-11 17:35:19 +00002459 case Primitive::kPrimInt:
2460 switch (input_type) {
2461 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002462 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002463 locations->SetInAt(0, Location::Any());
2464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2465 break;
2466
2467 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002468 // Processing a Dex `float-to-int' instruction.
2469 locations->SetInAt(0, Location::RequiresFpuRegister());
2470 locations->SetOut(Location::RequiresRegister());
2471 locations->AddTemp(Location::RequiresFpuRegister());
2472 break;
2473
Roland Levillain946e1432014-11-11 17:35:19 +00002474 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002475 // Processing a Dex `double-to-int' instruction.
2476 locations->SetInAt(0, Location::RequiresFpuRegister());
2477 locations->SetOut(Location::RequiresRegister());
2478 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002479 break;
2480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485 break;
2486
Roland Levillaindff1f282014-11-05 14:15:05 +00002487 case Primitive::kPrimLong:
2488 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002489 case Primitive::kPrimBoolean:
2490 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002491 case Primitive::kPrimByte:
2492 case Primitive::kPrimShort:
2493 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002494 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002495 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002496 locations->SetInAt(0, Location::RegisterLocation(EAX));
2497 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2498 break;
2499
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002500 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002501 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002502 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002503 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002504 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2505 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2506
Vladimir Marko949c91f2015-01-27 10:48:44 +00002507 // The runtime helper puts the result in EAX, EDX.
2508 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002509 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002510 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002511
2512 default:
2513 LOG(FATAL) << "Unexpected type conversion from " << input_type
2514 << " to " << result_type;
2515 }
2516 break;
2517
Roland Levillain981e4542014-11-14 11:47:14 +00002518 case Primitive::kPrimChar:
2519 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002520 case Primitive::kPrimLong:
2521 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002522 case Primitive::kPrimBoolean:
2523 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002524 case Primitive::kPrimByte:
2525 case Primitive::kPrimShort:
2526 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002527 // Processing a Dex `int-to-char' instruction.
2528 locations->SetInAt(0, Location::Any());
2529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2530 break;
2531
2532 default:
2533 LOG(FATAL) << "Unexpected type conversion from " << input_type
2534 << " to " << result_type;
2535 }
2536 break;
2537
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
2545 case Primitive::kPrimChar:
2546 // Processing a Dex `int-to-float' instruction.
2547 locations->SetInAt(0, Location::RequiresRegister());
2548 locations->SetOut(Location::RequiresFpuRegister());
2549 break;
2550
2551 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002552 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002553 locations->SetInAt(0, Location::Any());
2554 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002555 break;
2556
Roland Levillaincff13742014-11-17 14:32:17 +00002557 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002558 // Processing a Dex `double-to-float' instruction.
2559 locations->SetInAt(0, Location::RequiresFpuRegister());
2560 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002561 break;
2562
2563 default:
2564 LOG(FATAL) << "Unexpected type conversion from " << input_type
2565 << " to " << result_type;
2566 };
2567 break;
2568
Roland Levillaindff1f282014-11-05 14:15:05 +00002569 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002570 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002571 case Primitive::kPrimBoolean:
2572 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002573 case Primitive::kPrimByte:
2574 case Primitive::kPrimShort:
2575 case Primitive::kPrimInt:
2576 case Primitive::kPrimChar:
2577 // Processing a Dex `int-to-double' instruction.
2578 locations->SetInAt(0, Location::RequiresRegister());
2579 locations->SetOut(Location::RequiresFpuRegister());
2580 break;
2581
2582 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002583 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002584 locations->SetInAt(0, Location::Any());
2585 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002586 break;
2587
Roland Levillaincff13742014-11-17 14:32:17 +00002588 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002589 // Processing a Dex `float-to-double' instruction.
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002598 break;
2599
2600 default:
2601 LOG(FATAL) << "Unexpected type conversion from " << input_type
2602 << " to " << result_type;
2603 }
2604}
2605
2606void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2607 LocationSummary* locations = conversion->GetLocations();
2608 Location out = locations->Out();
2609 Location in = locations->InAt(0);
2610 Primitive::Type result_type = conversion->GetResultType();
2611 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002612 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002613 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002614 case Primitive::kPrimByte:
2615 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002616 case Primitive::kPrimLong:
2617 // Type conversion from long to byte is a result of code transformations.
2618 if (in.IsRegisterPair()) {
2619 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2620 } else {
2621 DCHECK(in.GetConstant()->IsLongConstant());
2622 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2623 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2624 }
2625 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002626 case Primitive::kPrimBoolean:
2627 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002628 case Primitive::kPrimShort:
2629 case Primitive::kPrimInt:
2630 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002631 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002632 if (in.IsRegister()) {
2633 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002634 } else {
2635 DCHECK(in.GetConstant()->IsIntConstant());
2636 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2637 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2638 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002639 break;
2640
2641 default:
2642 LOG(FATAL) << "Unexpected type conversion from " << input_type
2643 << " to " << result_type;
2644 }
2645 break;
2646
Roland Levillain01a8d712014-11-14 16:27:39 +00002647 case Primitive::kPrimShort:
2648 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002649 case Primitive::kPrimLong:
2650 // Type conversion from long to short is a result of code transformations.
2651 if (in.IsRegisterPair()) {
2652 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2653 } else if (in.IsDoubleStackSlot()) {
2654 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2655 } else {
2656 DCHECK(in.GetConstant()->IsLongConstant());
2657 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2658 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2659 }
2660 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002661 case Primitive::kPrimBoolean:
2662 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002663 case Primitive::kPrimByte:
2664 case Primitive::kPrimInt:
2665 case Primitive::kPrimChar:
2666 // Processing a Dex `int-to-short' instruction.
2667 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002669 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002671 } else {
2672 DCHECK(in.GetConstant()->IsIntConstant());
2673 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002674 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002675 }
2676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
2681 }
2682 break;
2683
Roland Levillain946e1432014-11-11 17:35:19 +00002684 case Primitive::kPrimInt:
2685 switch (input_type) {
2686 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002687 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002688 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002689 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002690 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002692 } else {
2693 DCHECK(in.IsConstant());
2694 DCHECK(in.GetConstant()->IsLongConstant());
2695 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002696 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002697 }
2698 break;
2699
Roland Levillain3f8f9362014-12-02 17:45:01 +00002700 case Primitive::kPrimFloat: {
2701 // Processing a Dex `float-to-int' instruction.
2702 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2703 Register output = out.AsRegister<Register>();
2704 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002705 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002706
2707 __ movl(output, Immediate(kPrimIntMax));
2708 // temp = int-to-float(output)
2709 __ cvtsi2ss(temp, output);
2710 // if input >= temp goto done
2711 __ comiss(input, temp);
2712 __ j(kAboveEqual, &done);
2713 // if input == NaN goto nan
2714 __ j(kUnordered, &nan);
2715 // output = float-to-int-truncate(input)
2716 __ cvttss2si(output, input);
2717 __ jmp(&done);
2718 __ Bind(&nan);
2719 // output = 0
2720 __ xorl(output, output);
2721 __ Bind(&done);
2722 break;
2723 }
2724
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002725 case Primitive::kPrimDouble: {
2726 // Processing a Dex `double-to-int' instruction.
2727 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2728 Register output = out.AsRegister<Register>();
2729 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002730 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002731
2732 __ movl(output, Immediate(kPrimIntMax));
2733 // temp = int-to-double(output)
2734 __ cvtsi2sd(temp, output);
2735 // if input >= temp goto done
2736 __ comisd(input, temp);
2737 __ j(kAboveEqual, &done);
2738 // if input == NaN goto nan
2739 __ j(kUnordered, &nan);
2740 // output = double-to-int-truncate(input)
2741 __ cvttsd2si(output, input);
2742 __ jmp(&done);
2743 __ Bind(&nan);
2744 // output = 0
2745 __ xorl(output, output);
2746 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002747 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002748 }
Roland Levillain946e1432014-11-11 17:35:19 +00002749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
2753 }
2754 break;
2755
Roland Levillaindff1f282014-11-05 14:15:05 +00002756 case Primitive::kPrimLong:
2757 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002758 case Primitive::kPrimBoolean:
2759 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002760 case Primitive::kPrimByte:
2761 case Primitive::kPrimShort:
2762 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002763 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002764 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002765 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2766 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002768 __ cdq();
2769 break;
2770
2771 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002772 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002773 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002774 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002775 break;
2776
Roland Levillaindff1f282014-11-05 14:15:05 +00002777 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002778 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002779 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002780 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002781 break;
2782
2783 default:
2784 LOG(FATAL) << "Unexpected type conversion from " << input_type
2785 << " to " << result_type;
2786 }
2787 break;
2788
Roland Levillain981e4542014-11-14 11:47:14 +00002789 case Primitive::kPrimChar:
2790 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002791 case Primitive::kPrimLong:
2792 // Type conversion from long to short is a result of code transformations.
2793 if (in.IsRegisterPair()) {
2794 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2795 } else if (in.IsDoubleStackSlot()) {
2796 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2797 } else {
2798 DCHECK(in.GetConstant()->IsLongConstant());
2799 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2800 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2801 }
2802 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002803 case Primitive::kPrimBoolean:
2804 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002805 case Primitive::kPrimByte:
2806 case Primitive::kPrimShort:
2807 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002808 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2809 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002810 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002811 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002812 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002813 } else {
2814 DCHECK(in.GetConstant()->IsIntConstant());
2815 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002817 }
2818 break;
2819
2820 default:
2821 LOG(FATAL) << "Unexpected type conversion from " << input_type
2822 << " to " << result_type;
2823 }
2824 break;
2825
Roland Levillaindff1f282014-11-05 14:15:05 +00002826 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002827 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002828 case Primitive::kPrimBoolean:
2829 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002830 case Primitive::kPrimByte:
2831 case Primitive::kPrimShort:
2832 case Primitive::kPrimInt:
2833 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002834 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002835 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002836 break;
2837
Roland Levillain6d0e4832014-11-27 18:31:21 +00002838 case Primitive::kPrimLong: {
2839 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002840 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002841
Roland Levillain232ade02015-04-20 15:14:36 +01002842 // Create stack space for the call to
2843 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2844 // TODO: enhance register allocator to ask for stack temporaries.
2845 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2846 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2847 __ subl(ESP, Immediate(adjustment));
2848 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002849
Roland Levillain232ade02015-04-20 15:14:36 +01002850 // Load the value to the FP stack, using temporaries if needed.
2851 PushOntoFPStack(in, 0, adjustment, false, true);
2852
2853 if (out.IsStackSlot()) {
2854 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2855 } else {
2856 __ fstps(Address(ESP, 0));
2857 Location stack_temp = Location::StackSlot(0);
2858 codegen_->Move32(out, stack_temp);
2859 }
2860
2861 // Remove the temporary stack space we allocated.
2862 if (adjustment != 0) {
2863 __ addl(ESP, Immediate(adjustment));
2864 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002865 break;
2866 }
2867
Roland Levillaincff13742014-11-17 14:32:17 +00002868 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002869 // Processing a Dex `double-to-float' instruction.
2870 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
2876 };
2877 break;
2878
Roland Levillaindff1f282014-11-05 14:15:05 +00002879 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002880 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002881 case Primitive::kPrimBoolean:
2882 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002883 case Primitive::kPrimByte:
2884 case Primitive::kPrimShort:
2885 case Primitive::kPrimInt:
2886 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002887 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002889 break;
2890
Roland Levillain647b9ed2014-11-27 12:06:00 +00002891 case Primitive::kPrimLong: {
2892 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002893 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002894
Roland Levillain232ade02015-04-20 15:14:36 +01002895 // Create stack space for the call to
2896 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2897 // TODO: enhance register allocator to ask for stack temporaries.
2898 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2899 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2900 __ subl(ESP, Immediate(adjustment));
2901 }
2902
2903 // Load the value to the FP stack, using temporaries if needed.
2904 PushOntoFPStack(in, 0, adjustment, false, true);
2905
2906 if (out.IsDoubleStackSlot()) {
2907 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2908 } else {
2909 __ fstpl(Address(ESP, 0));
2910 Location stack_temp = Location::DoubleStackSlot(0);
2911 codegen_->Move64(out, stack_temp);
2912 }
2913
2914 // Remove the temporary stack space we allocated.
2915 if (adjustment != 0) {
2916 __ addl(ESP, Immediate(adjustment));
2917 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002918 break;
2919 }
2920
Roland Levillaincff13742014-11-17 14:32:17 +00002921 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002922 // Processing a Dex `float-to-double' instruction.
2923 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002924 break;
2925
2926 default:
2927 LOG(FATAL) << "Unexpected type conversion from " << input_type
2928 << " to " << result_type;
2929 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002930 break;
2931
2932 default:
2933 LOG(FATAL) << "Unexpected type conversion from " << input_type
2934 << " to " << result_type;
2935 }
2936}
2937
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002939 LocationSummary* locations =
2940 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002942 case Primitive::kPrimInt: {
2943 locations->SetInAt(0, Location::RequiresRegister());
2944 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2946 break;
2947 }
2948
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002949 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetInAt(1, Location::Any());
2952 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002953 break;
2954 }
2955
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 case Primitive::kPrimFloat:
2957 case Primitive::kPrimDouble: {
2958 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002959 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2960 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002961 } else if (add->InputAt(1)->IsConstant()) {
2962 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002963 } else {
2964 locations->SetInAt(1, Location::Any());
2965 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002966 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002967 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002969
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002970 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002971 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2972 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002973 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002974}
2975
2976void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2977 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 Location first = locations->InAt(0);
2979 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002980 Location out = locations->Out();
2981
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002982 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002983 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002984 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002985 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2986 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002987 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2988 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002989 } else {
2990 __ leal(out.AsRegister<Register>(), Address(
2991 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2992 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002993 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002994 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2995 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2996 __ addl(out.AsRegister<Register>(), Immediate(value));
2997 } else {
2998 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2999 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003000 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003001 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003003 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003004 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003005 }
3006
3007 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003008 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003009 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3010 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003011 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003012 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3013 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003014 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003015 } else {
3016 DCHECK(second.IsConstant()) << second;
3017 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3018 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3019 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003021 break;
3022 }
3023
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003024 case Primitive::kPrimFloat: {
3025 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003027 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3028 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003029 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003030 __ addss(first.AsFpuRegister<XmmRegister>(),
3031 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003032 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3033 const_area->GetBaseMethodAddress(),
3034 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003035 } else {
3036 DCHECK(second.IsStackSlot());
3037 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003038 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 }
3041
3042 case Primitive::kPrimDouble: {
3043 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3046 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003047 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003048 __ addsd(first.AsFpuRegister<XmmRegister>(),
3049 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003050 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3051 const_area->GetBaseMethodAddress(),
3052 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 } else {
3054 DCHECK(second.IsDoubleStackSlot());
3055 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003056 }
3057 break;
3058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003060 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003061 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003062 }
3063}
3064
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003065void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003066 LocationSummary* locations =
3067 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003068 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003069 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003070 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003071 locations->SetInAt(0, Location::RequiresRegister());
3072 locations->SetInAt(1, Location::Any());
3073 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003074 break;
3075 }
Calin Juravle11351682014-10-23 15:38:15 +01003076 case Primitive::kPrimFloat:
3077 case Primitive::kPrimDouble: {
3078 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003079 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3080 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003081 } else if (sub->InputAt(1)->IsConstant()) {
3082 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003083 } else {
3084 locations->SetInAt(1, Location::Any());
3085 }
Calin Juravle11351682014-10-23 15:38:15 +01003086 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003087 break;
Calin Juravle11351682014-10-23 15:38:15 +01003088 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003089
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003090 default:
Calin Juravle11351682014-10-23 15:38:15 +01003091 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003092 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003093}
3094
3095void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3096 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003097 Location first = locations->InAt(0);
3098 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003099 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003100 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003101 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003102 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003103 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003104 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003105 __ subl(first.AsRegister<Register>(),
3106 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003109 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003111 }
3112
3113 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003114 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003115 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3116 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003117 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003118 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003119 __ sbbl(first.AsRegisterPairHigh<Register>(),
3120 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003121 } else {
3122 DCHECK(second.IsConstant()) << second;
3123 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3124 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3125 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003126 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003127 break;
3128 }
3129
Calin Juravle11351682014-10-23 15:38:15 +01003130 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003131 if (second.IsFpuRegister()) {
3132 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3133 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3134 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003135 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003136 __ subss(first.AsFpuRegister<XmmRegister>(),
3137 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003138 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3139 const_area->GetBaseMethodAddress(),
3140 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003141 } else {
3142 DCHECK(second.IsStackSlot());
3143 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3144 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003145 break;
Calin Juravle11351682014-10-23 15:38:15 +01003146 }
3147
3148 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003149 if (second.IsFpuRegister()) {
3150 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3151 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3152 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003153 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003154 __ subsd(first.AsFpuRegister<XmmRegister>(),
3155 codegen_->LiteralDoubleAddress(
3156 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003157 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003158 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3159 } else {
3160 DCHECK(second.IsDoubleStackSlot());
3161 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3162 }
Calin Juravle11351682014-10-23 15:38:15 +01003163 break;
3164 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003165
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003166 default:
Calin Juravle11351682014-10-23 15:38:15 +01003167 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003168 }
3169}
3170
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171void LocationsBuilderX86::VisitMul(HMul* mul) {
3172 LocationSummary* locations =
3173 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3174 switch (mul->GetResultType()) {
3175 case Primitive::kPrimInt:
3176 locations->SetInAt(0, Location::RequiresRegister());
3177 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003178 if (mul->InputAt(1)->IsIntConstant()) {
3179 // Can use 3 operand multiply.
3180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3181 } else {
3182 locations->SetOut(Location::SameAsFirstInput());
3183 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 break;
3185 case Primitive::kPrimLong: {
3186 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 locations->SetInAt(1, Location::Any());
3188 locations->SetOut(Location::SameAsFirstInput());
3189 // Needed for imul on 32bits with 64bits output.
3190 locations->AddTemp(Location::RegisterLocation(EAX));
3191 locations->AddTemp(Location::RegisterLocation(EDX));
3192 break;
3193 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003194 case Primitive::kPrimFloat:
3195 case Primitive::kPrimDouble: {
3196 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003197 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3198 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003199 } else if (mul->InputAt(1)->IsConstant()) {
3200 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003201 } else {
3202 locations->SetInAt(1, Location::Any());
3203 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003204 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003205 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003206 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207
3208 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003209 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003210 }
3211}
3212
3213void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3214 LocationSummary* locations = mul->GetLocations();
3215 Location first = locations->InAt(0);
3216 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003217 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218
3219 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003220 case Primitive::kPrimInt:
3221 // The constant may have ended up in a register, so test explicitly to avoid
3222 // problems where the output may not be the same as the first operand.
3223 if (mul->InputAt(1)->IsIntConstant()) {
3224 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3225 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3226 } else if (second.IsRegister()) {
3227 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003229 } else {
3230 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003231 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003232 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003233 }
3234 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003235
3236 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237 Register in1_hi = first.AsRegisterPairHigh<Register>();
3238 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 Register eax = locations->GetTemp(0).AsRegister<Register>();
3240 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003241
3242 DCHECK_EQ(EAX, eax);
3243 DCHECK_EQ(EDX, edx);
3244
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003245 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246 // output: in1
3247 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3248 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3249 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003250 if (second.IsConstant()) {
3251 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003253 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3254 int32_t low_value = Low32Bits(value);
3255 int32_t high_value = High32Bits(value);
3256 Immediate low(low_value);
3257 Immediate high(high_value);
3258
3259 __ movl(eax, high);
3260 // eax <- in1.lo * in2.hi
3261 __ imull(eax, in1_lo);
3262 // in1.hi <- in1.hi * in2.lo
3263 __ imull(in1_hi, low);
3264 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3265 __ addl(in1_hi, eax);
3266 // move in2_lo to eax to prepare for double precision
3267 __ movl(eax, low);
3268 // edx:eax <- in1.lo * in2.lo
3269 __ mull(in1_lo);
3270 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3271 __ addl(in1_hi, edx);
3272 // in1.lo <- (in1.lo * in2.lo)[31:0];
3273 __ movl(in1_lo, eax);
3274 } else if (second.IsRegisterPair()) {
3275 Register in2_hi = second.AsRegisterPairHigh<Register>();
3276 Register in2_lo = second.AsRegisterPairLow<Register>();
3277
3278 __ movl(eax, in2_hi);
3279 // eax <- in1.lo * in2.hi
3280 __ imull(eax, in1_lo);
3281 // in1.hi <- in1.hi * in2.lo
3282 __ imull(in1_hi, in2_lo);
3283 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3284 __ addl(in1_hi, eax);
3285 // move in1_lo to eax to prepare for double precision
3286 __ movl(eax, in1_lo);
3287 // edx:eax <- in1.lo * in2.lo
3288 __ mull(in2_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 {
3294 DCHECK(second.IsDoubleStackSlot()) << second;
3295 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3296 Address in2_lo(ESP, second.GetStackIndex());
3297
3298 __ movl(eax, in2_hi);
3299 // eax <- in1.lo * in2.hi
3300 __ imull(eax, in1_lo);
3301 // in1.hi <- in1.hi * in2.lo
3302 __ imull(in1_hi, in2_lo);
3303 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3304 __ addl(in1_hi, eax);
3305 // move in1_lo to eax to prepare for double precision
3306 __ movl(eax, in1_lo);
3307 // edx:eax <- in1.lo * in2.lo
3308 __ mull(in2_lo);
3309 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3310 __ addl(in1_hi, edx);
3311 // in1.lo <- (in1.lo * in2.lo)[31:0];
3312 __ movl(in1_lo, eax);
3313 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003314
3315 break;
3316 }
3317
Calin Juravleb5bfa962014-10-21 18:02:24 +01003318 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003319 DCHECK(first.Equals(locations->Out()));
3320 if (second.IsFpuRegister()) {
3321 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3322 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3323 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003324 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003325 __ mulss(first.AsFpuRegister<XmmRegister>(),
3326 codegen_->LiteralFloatAddress(
3327 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003328 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003329 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3330 } else {
3331 DCHECK(second.IsStackSlot());
3332 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3333 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003335 }
3336
3337 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 DCHECK(first.Equals(locations->Out()));
3339 if (second.IsFpuRegister()) {
3340 __ mulsd(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 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3345 codegen_->LiteralDoubleAddress(
3346 const_area->GetConstant()->AsDoubleConstant()->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.IsDoubleStackSlot());
3351 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3352 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003353 break;
3354 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355
3356 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003357 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003358 }
3359}
3360
Roland Levillain232ade02015-04-20 15:14:36 +01003361void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3362 uint32_t temp_offset,
3363 uint32_t stack_adjustment,
3364 bool is_fp,
3365 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003366 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003367 DCHECK(!is_wide);
3368 if (is_fp) {
3369 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3370 } else {
3371 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3372 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003373 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003374 DCHECK(is_wide);
3375 if (is_fp) {
3376 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3377 } else {
3378 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3379 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003380 } else {
3381 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003382 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003383 Location stack_temp = Location::StackSlot(temp_offset);
3384 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003385 if (is_fp) {
3386 __ flds(Address(ESP, temp_offset));
3387 } else {
3388 __ filds(Address(ESP, temp_offset));
3389 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003390 } else {
3391 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3392 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003393 if (is_fp) {
3394 __ fldl(Address(ESP, temp_offset));
3395 } else {
3396 __ fildl(Address(ESP, temp_offset));
3397 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003398 }
3399 }
3400}
3401
3402void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3403 Primitive::Type type = rem->GetResultType();
3404 bool is_float = type == Primitive::kPrimFloat;
3405 size_t elem_size = Primitive::ComponentSize(type);
3406 LocationSummary* locations = rem->GetLocations();
3407 Location first = locations->InAt(0);
3408 Location second = locations->InAt(1);
3409 Location out = locations->Out();
3410
3411 // Create stack space for 2 elements.
3412 // TODO: enhance register allocator to ask for stack temporaries.
3413 __ subl(ESP, Immediate(2 * elem_size));
3414
3415 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003416 const bool is_wide = !is_float;
3417 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3418 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003419
3420 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003421 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003422 __ Bind(&retry);
3423 __ fprem();
3424
3425 // Move FP status to AX.
3426 __ fstsw();
3427
3428 // And see if the argument reduction is complete. This is signaled by the
3429 // C2 FPU flag bit set to 0.
3430 __ andl(EAX, Immediate(kC2ConditionMask));
3431 __ j(kNotEqual, &retry);
3432
3433 // We have settled on the final value. Retrieve it into an XMM register.
3434 // Store FP top of stack to real stack.
3435 if (is_float) {
3436 __ fsts(Address(ESP, 0));
3437 } else {
3438 __ fstl(Address(ESP, 0));
3439 }
3440
3441 // Pop the 2 items from the FP stack.
3442 __ fucompp();
3443
3444 // Load the value from the stack into an XMM register.
3445 DCHECK(out.IsFpuRegister()) << out;
3446 if (is_float) {
3447 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3448 } else {
3449 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3450 }
3451
3452 // And remove the temporary stack space we allocated.
3453 __ addl(ESP, Immediate(2 * elem_size));
3454}
3455
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003456
3457void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3458 DCHECK(instruction->IsDiv() || instruction->IsRem());
3459
3460 LocationSummary* locations = instruction->GetLocations();
3461 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003462 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003463
3464 Register out_register = locations->Out().AsRegister<Register>();
3465 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003466 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003467
3468 DCHECK(imm == 1 || imm == -1);
3469
3470 if (instruction->IsRem()) {
3471 __ xorl(out_register, out_register);
3472 } else {
3473 __ movl(out_register, input_register);
3474 if (imm == -1) {
3475 __ negl(out_register);
3476 }
3477 }
3478}
3479
3480
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003481void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482 LocationSummary* locations = instruction->GetLocations();
3483
3484 Register out_register = locations->Out().AsRegister<Register>();
3485 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003487 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3488 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 Register num = locations->GetTemp(0).AsRegister<Register>();
3491
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003492 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 __ testl(input_register, input_register);
3494 __ cmovl(kGreaterEqual, num, input_register);
3495 int shift = CTZ(imm);
3496 __ sarl(num, Immediate(shift));
3497
3498 if (imm < 0) {
3499 __ negl(num);
3500 }
3501
3502 __ movl(out_register, num);
3503}
3504
3505void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3506 DCHECK(instruction->IsDiv() || instruction->IsRem());
3507
3508 LocationSummary* locations = instruction->GetLocations();
3509 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3510
3511 Register eax = locations->InAt(0).AsRegister<Register>();
3512 Register out = locations->Out().AsRegister<Register>();
3513 Register num;
3514 Register edx;
3515
3516 if (instruction->IsDiv()) {
3517 edx = locations->GetTemp(0).AsRegister<Register>();
3518 num = locations->GetTemp(1).AsRegister<Register>();
3519 } else {
3520 edx = locations->Out().AsRegister<Register>();
3521 num = locations->GetTemp(0).AsRegister<Register>();
3522 }
3523
3524 DCHECK_EQ(EAX, eax);
3525 DCHECK_EQ(EDX, edx);
3526 if (instruction->IsDiv()) {
3527 DCHECK_EQ(EAX, out);
3528 } else {
3529 DCHECK_EQ(EDX, out);
3530 }
3531
3532 int64_t magic;
3533 int shift;
3534 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3535
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 // Save the numerator.
3537 __ movl(num, eax);
3538
3539 // EAX = magic
3540 __ movl(eax, Immediate(magic));
3541
3542 // EDX:EAX = magic * numerator
3543 __ imull(num);
3544
3545 if (imm > 0 && magic < 0) {
3546 // EDX += num
3547 __ addl(edx, num);
3548 } else if (imm < 0 && magic > 0) {
3549 __ subl(edx, num);
3550 }
3551
3552 // Shift if needed.
3553 if (shift != 0) {
3554 __ sarl(edx, Immediate(shift));
3555 }
3556
3557 // EDX += 1 if EDX < 0
3558 __ movl(eax, edx);
3559 __ shrl(edx, Immediate(31));
3560 __ addl(edx, eax);
3561
3562 if (instruction->IsRem()) {
3563 __ movl(eax, num);
3564 __ imull(edx, Immediate(imm));
3565 __ subl(eax, edx);
3566 __ movl(edx, eax);
3567 } else {
3568 __ movl(eax, edx);
3569 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003570}
3571
Calin Juravlebacfec32014-11-14 15:54:36 +00003572void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3573 DCHECK(instruction->IsDiv() || instruction->IsRem());
3574
3575 LocationSummary* locations = instruction->GetLocations();
3576 Location out = locations->Out();
3577 Location first = locations->InAt(0);
3578 Location second = locations->InAt(1);
3579 bool is_div = instruction->IsDiv();
3580
3581 switch (instruction->GetResultType()) {
3582 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003583 DCHECK_EQ(EAX, first.AsRegister<Register>());
3584 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003585
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003586 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003587 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588
3589 if (imm == 0) {
3590 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3591 } else if (imm == 1 || imm == -1) {
3592 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003593 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003594 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595 } else {
3596 DCHECK(imm <= -2 || imm >= 2);
3597 GenerateDivRemWithAnyConstant(instruction);
3598 }
3599 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003600 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3601 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003602 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003603
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003604 Register second_reg = second.AsRegister<Register>();
3605 // 0x80000000/-1 triggers an arithmetic exception!
3606 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3607 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003608
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 __ cmpl(second_reg, Immediate(-1));
3610 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003611
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003612 // edx:eax <- sign-extended of eax
3613 __ cdq();
3614 // eax = quotient, edx = remainder
3615 __ idivl(second_reg);
3616 __ Bind(slow_path->GetExitLabel());
3617 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003618 break;
3619 }
3620
3621 case Primitive::kPrimLong: {
3622 InvokeRuntimeCallingConvention calling_convention;
3623 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3624 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3625 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3626 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3627 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3628 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3629
3630 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003631 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003632 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003634 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003635 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003636 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003637 break;
3638 }
3639
3640 default:
3641 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3642 }
3643}
3644
Calin Juravle7c4954d2014-10-28 16:57:40 +00003645void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003646 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003647 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003648 : LocationSummary::kNoCall;
3649 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3650
Calin Juravle7c4954d2014-10-28 16:57:40 +00003651 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003652 case Primitive::kPrimInt: {
3653 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003654 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003655 locations->SetOut(Location::SameAsFirstInput());
3656 // Intel uses edx:eax as the dividend.
3657 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003658 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3659 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3660 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003661 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003662 locations->AddTemp(Location::RequiresRegister());
3663 }
Calin Juravled0d48522014-11-04 16:40:20 +00003664 break;
3665 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003666 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003667 InvokeRuntimeCallingConvention calling_convention;
3668 locations->SetInAt(0, Location::RegisterPairLocation(
3669 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3670 locations->SetInAt(1, Location::RegisterPairLocation(
3671 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3672 // Runtime helper puts the result in EAX, EDX.
3673 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003674 break;
3675 }
3676 case Primitive::kPrimFloat:
3677 case Primitive::kPrimDouble: {
3678 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003679 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3680 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003681 } else if (div->InputAt(1)->IsConstant()) {
3682 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003683 } else {
3684 locations->SetInAt(1, Location::Any());
3685 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003686 locations->SetOut(Location::SameAsFirstInput());
3687 break;
3688 }
3689
3690 default:
3691 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3692 }
3693}
3694
3695void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3696 LocationSummary* locations = div->GetLocations();
3697 Location first = locations->InAt(0);
3698 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003699
3700 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003701 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003702 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003703 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003704 break;
3705 }
3706
3707 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003708 if (second.IsFpuRegister()) {
3709 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3710 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3711 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003712 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003713 __ divss(first.AsFpuRegister<XmmRegister>(),
3714 codegen_->LiteralFloatAddress(
3715 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003716 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003717 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3718 } else {
3719 DCHECK(second.IsStackSlot());
3720 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3721 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003722 break;
3723 }
3724
3725 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003726 if (second.IsFpuRegister()) {
3727 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3728 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3729 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003730 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003731 __ divsd(first.AsFpuRegister<XmmRegister>(),
3732 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003733 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3734 const_area->GetBaseMethodAddress(),
3735 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003736 } else {
3737 DCHECK(second.IsDoubleStackSlot());
3738 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3739 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003740 break;
3741 }
3742
3743 default:
3744 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3745 }
3746}
3747
Calin Juravlebacfec32014-11-14 15:54:36 +00003748void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003749 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003750
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003751 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003752 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003753 : LocationSummary::kNoCall;
3754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003755
Calin Juravled2ec87d2014-12-08 14:24:46 +00003756 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003757 case Primitive::kPrimInt: {
3758 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003759 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003760 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003761 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3762 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3763 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003764 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003765 locations->AddTemp(Location::RequiresRegister());
3766 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003767 break;
3768 }
3769 case Primitive::kPrimLong: {
3770 InvokeRuntimeCallingConvention calling_convention;
3771 locations->SetInAt(0, Location::RegisterPairLocation(
3772 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3773 locations->SetInAt(1, Location::RegisterPairLocation(
3774 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3775 // Runtime helper puts the result in EAX, EDX.
3776 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3777 break;
3778 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003779 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003780 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003781 locations->SetInAt(0, Location::Any());
3782 locations->SetInAt(1, Location::Any());
3783 locations->SetOut(Location::RequiresFpuRegister());
3784 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003785 break;
3786 }
3787
3788 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003789 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003790 }
3791}
3792
3793void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3794 Primitive::Type type = rem->GetResultType();
3795 switch (type) {
3796 case Primitive::kPrimInt:
3797 case Primitive::kPrimLong: {
3798 GenerateDivRemIntegral(rem);
3799 break;
3800 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003801 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003802 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003803 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003804 break;
3805 }
3806 default:
3807 LOG(FATAL) << "Unexpected rem type " << type;
3808 }
3809}
3810
Calin Juravled0d48522014-11-04 16:40:20 +00003811void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003812 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003813 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003814 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003815 case Primitive::kPrimByte:
3816 case Primitive::kPrimChar:
3817 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003818 case Primitive::kPrimInt: {
3819 locations->SetInAt(0, Location::Any());
3820 break;
3821 }
3822 case Primitive::kPrimLong: {
3823 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3824 if (!instruction->IsConstant()) {
3825 locations->AddTemp(Location::RequiresRegister());
3826 }
3827 break;
3828 }
3829 default:
3830 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3831 }
Calin Juravled0d48522014-11-04 16:40:20 +00003832}
3833
3834void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003835 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003836 codegen_->AddSlowPath(slow_path);
3837
3838 LocationSummary* locations = instruction->GetLocations();
3839 Location value = locations->InAt(0);
3840
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003841 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003842 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003843 case Primitive::kPrimByte:
3844 case Primitive::kPrimChar:
3845 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003846 case Primitive::kPrimInt: {
3847 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003848 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003849 __ j(kEqual, slow_path->GetEntryLabel());
3850 } else if (value.IsStackSlot()) {
3851 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3852 __ j(kEqual, slow_path->GetEntryLabel());
3853 } else {
3854 DCHECK(value.IsConstant()) << value;
3855 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003856 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003857 }
3858 }
3859 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003860 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003861 case Primitive::kPrimLong: {
3862 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003863 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003864 __ movl(temp, value.AsRegisterPairLow<Register>());
3865 __ orl(temp, value.AsRegisterPairHigh<Register>());
3866 __ j(kEqual, slow_path->GetEntryLabel());
3867 } else {
3868 DCHECK(value.IsConstant()) << value;
3869 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3870 __ jmp(slow_path->GetEntryLabel());
3871 }
3872 }
3873 break;
3874 }
3875 default:
3876 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003877 }
Calin Juravled0d48522014-11-04 16:40:20 +00003878}
3879
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3881 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3882
3883 LocationSummary* locations =
3884 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3885
3886 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003887 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003888 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003889 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003890 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003891 // The shift count needs to be in CL or a constant.
3892 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003893 locations->SetOut(Location::SameAsFirstInput());
3894 break;
3895 }
3896 default:
3897 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3898 }
3899}
3900
3901void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3902 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3903
3904 LocationSummary* locations = op->GetLocations();
3905 Location first = locations->InAt(0);
3906 Location second = locations->InAt(1);
3907 DCHECK(first.Equals(locations->Out()));
3908
3909 switch (op->GetResultType()) {
3910 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003911 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003912 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003914 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003915 DCHECK_EQ(ECX, second_reg);
3916 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003917 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003918 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003919 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003921 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003922 }
3923 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003924 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003925 if (shift == 0) {
3926 return;
3927 }
3928 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003929 if (op->IsShl()) {
3930 __ shll(first_reg, imm);
3931 } else if (op->IsShr()) {
3932 __ sarl(first_reg, imm);
3933 } else {
3934 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003935 }
3936 }
3937 break;
3938 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003940 if (second.IsRegister()) {
3941 Register second_reg = second.AsRegister<Register>();
3942 DCHECK_EQ(ECX, second_reg);
3943 if (op->IsShl()) {
3944 GenerateShlLong(first, second_reg);
3945 } else if (op->IsShr()) {
3946 GenerateShrLong(first, second_reg);
3947 } else {
3948 GenerateUShrLong(first, second_reg);
3949 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003950 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003951 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003952 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003953 // Nothing to do if the shift is 0, as the input is already the output.
3954 if (shift != 0) {
3955 if (op->IsShl()) {
3956 GenerateShlLong(first, shift);
3957 } else if (op->IsShr()) {
3958 GenerateShrLong(first, shift);
3959 } else {
3960 GenerateUShrLong(first, shift);
3961 }
3962 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003963 }
3964 break;
3965 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003966 default:
3967 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3968 }
3969}
3970
Mark P Mendell73945692015-04-29 14:56:17 +00003971void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3972 Register low = loc.AsRegisterPairLow<Register>();
3973 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003974 if (shift == 1) {
3975 // This is just an addition.
3976 __ addl(low, low);
3977 __ adcl(high, high);
3978 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003979 // Shift by 32 is easy. High gets low, and low gets 0.
3980 codegen_->EmitParallelMoves(
3981 loc.ToLow(),
3982 loc.ToHigh(),
3983 Primitive::kPrimInt,
3984 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3985 loc.ToLow(),
3986 Primitive::kPrimInt);
3987 } else if (shift > 32) {
3988 // Low part becomes 0. High part is low part << (shift-32).
3989 __ movl(high, low);
3990 __ shll(high, Immediate(shift - 32));
3991 __ xorl(low, low);
3992 } else {
3993 // Between 1 and 31.
3994 __ shld(high, low, Immediate(shift));
3995 __ shll(low, Immediate(shift));
3996 }
3997}
3998
Calin Juravle9aec02f2014-11-18 23:06:35 +00003999void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004000 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004001 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4002 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4003 __ testl(shifter, Immediate(32));
4004 __ j(kEqual, &done);
4005 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4006 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4007 __ Bind(&done);
4008}
4009
Mark P Mendell73945692015-04-29 14:56:17 +00004010void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4011 Register low = loc.AsRegisterPairLow<Register>();
4012 Register high = loc.AsRegisterPairHigh<Register>();
4013 if (shift == 32) {
4014 // Need to copy the sign.
4015 DCHECK_NE(low, high);
4016 __ movl(low, high);
4017 __ sarl(high, Immediate(31));
4018 } else if (shift > 32) {
4019 DCHECK_NE(low, high);
4020 // High part becomes sign. Low part is shifted by shift - 32.
4021 __ movl(low, high);
4022 __ sarl(high, Immediate(31));
4023 __ sarl(low, Immediate(shift - 32));
4024 } else {
4025 // Between 1 and 31.
4026 __ shrd(low, high, Immediate(shift));
4027 __ sarl(high, Immediate(shift));
4028 }
4029}
4030
Calin Juravle9aec02f2014-11-18 23:06:35 +00004031void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004032 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004033 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4034 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4035 __ testl(shifter, Immediate(32));
4036 __ j(kEqual, &done);
4037 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4038 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4039 __ Bind(&done);
4040}
4041
Mark P Mendell73945692015-04-29 14:56:17 +00004042void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4043 Register low = loc.AsRegisterPairLow<Register>();
4044 Register high = loc.AsRegisterPairHigh<Register>();
4045 if (shift == 32) {
4046 // Shift by 32 is easy. Low gets high, and high gets 0.
4047 codegen_->EmitParallelMoves(
4048 loc.ToHigh(),
4049 loc.ToLow(),
4050 Primitive::kPrimInt,
4051 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4052 loc.ToHigh(),
4053 Primitive::kPrimInt);
4054 } else if (shift > 32) {
4055 // Low part is high >> (shift - 32). High part becomes 0.
4056 __ movl(low, high);
4057 __ shrl(low, Immediate(shift - 32));
4058 __ xorl(high, high);
4059 } else {
4060 // Between 1 and 31.
4061 __ shrd(low, high, Immediate(shift));
4062 __ shrl(high, Immediate(shift));
4063 }
4064}
4065
Calin Juravle9aec02f2014-11-18 23:06:35 +00004066void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004067 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004068 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4069 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4070 __ testl(shifter, Immediate(32));
4071 __ j(kEqual, &done);
4072 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4073 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4074 __ Bind(&done);
4075}
4076
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004077void LocationsBuilderX86::VisitRor(HRor* ror) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4080
4081 switch (ror->GetResultType()) {
4082 case Primitive::kPrimLong:
4083 // Add the temporary needed.
4084 locations->AddTemp(Location::RequiresRegister());
4085 FALLTHROUGH_INTENDED;
4086 case Primitive::kPrimInt:
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 // The shift count needs to be in CL (unless it is a constant).
4089 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4090 locations->SetOut(Location::SameAsFirstInput());
4091 break;
4092 default:
4093 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4094 UNREACHABLE();
4095 }
4096}
4097
4098void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4099 LocationSummary* locations = ror->GetLocations();
4100 Location first = locations->InAt(0);
4101 Location second = locations->InAt(1);
4102
4103 if (ror->GetResultType() == Primitive::kPrimInt) {
4104 Register first_reg = first.AsRegister<Register>();
4105 if (second.IsRegister()) {
4106 Register second_reg = second.AsRegister<Register>();
4107 __ rorl(first_reg, second_reg);
4108 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004109 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004110 __ rorl(first_reg, imm);
4111 }
4112 return;
4113 }
4114
4115 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4116 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4117 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4118 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4119 if (second.IsRegister()) {
4120 Register second_reg = second.AsRegister<Register>();
4121 DCHECK_EQ(second_reg, ECX);
4122 __ movl(temp_reg, first_reg_hi);
4123 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4124 __ shrd(first_reg_lo, temp_reg, second_reg);
4125 __ movl(temp_reg, first_reg_hi);
4126 __ testl(second_reg, Immediate(32));
4127 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4128 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4129 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004130 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004131 if (shift_amt == 0) {
4132 // Already fine.
4133 return;
4134 }
4135 if (shift_amt == 32) {
4136 // Just swap.
4137 __ movl(temp_reg, first_reg_lo);
4138 __ movl(first_reg_lo, first_reg_hi);
4139 __ movl(first_reg_hi, temp_reg);
4140 return;
4141 }
4142
4143 Immediate imm(shift_amt);
4144 // Save the constents of the low value.
4145 __ movl(temp_reg, first_reg_lo);
4146
4147 // Shift right into low, feeding bits from high.
4148 __ shrd(first_reg_lo, first_reg_hi, imm);
4149
4150 // Shift right into high, feeding bits from the original low.
4151 __ shrd(first_reg_hi, temp_reg, imm);
4152
4153 // Swap if needed.
4154 if (shift_amt > 32) {
4155 __ movl(temp_reg, first_reg_lo);
4156 __ movl(first_reg_lo, first_reg_hi);
4157 __ movl(first_reg_hi, temp_reg);
4158 }
4159 }
4160}
4161
Calin Juravle9aec02f2014-11-18 23:06:35 +00004162void LocationsBuilderX86::VisitShl(HShl* shl) {
4163 HandleShift(shl);
4164}
4165
4166void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4167 HandleShift(shl);
4168}
4169
4170void LocationsBuilderX86::VisitShr(HShr* shr) {
4171 HandleShift(shr);
4172}
4173
4174void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4175 HandleShift(shr);
4176}
4177
4178void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4179 HandleShift(ushr);
4180}
4181
4182void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4183 HandleShift(ushr);
4184}
4185
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004186void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004187 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004189 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004190 if (instruction->IsStringAlloc()) {
4191 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4192 } else {
4193 InvokeRuntimeCallingConvention calling_convention;
4194 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004195 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004196}
4197
4198void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004199 // Note: if heap poisoning is enabled, the entry point takes cares
4200 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004201 if (instruction->IsStringAlloc()) {
4202 // String is allocated through StringFactory. Call NewEmptyString entry point.
4203 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004204 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004205 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4206 __ call(Address(temp, code_offset.Int32Value()));
4207 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4208 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004209 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004210 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004211 DCHECK(!codegen_->IsLeafMethod());
4212 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004213}
4214
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004215void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4216 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 locations->SetOut(Location::RegisterLocation(EAX));
4219 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004220 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4221 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222}
4223
4224void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004225 // Note: if heap poisoning is enabled, the entry point takes cares
4226 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004227 QuickEntrypointEnum entrypoint =
4228 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4229 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004230 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231 DCHECK(!codegen_->IsLeafMethod());
4232}
4233
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004234void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004235 LocationSummary* locations =
4236 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004237 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4238 if (location.IsStackSlot()) {
4239 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4240 } else if (location.IsDoubleStackSlot()) {
4241 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004242 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004243 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004244}
4245
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004246void InstructionCodeGeneratorX86::VisitParameterValue(
4247 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4248}
4249
4250void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4251 LocationSummary* locations =
4252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4253 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4254}
4255
4256void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004257}
4258
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004259void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4260 LocationSummary* locations =
4261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4262 locations->SetInAt(0, Location::RequiresRegister());
4263 locations->SetOut(Location::RequiresRegister());
4264}
4265
4266void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4267 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004268 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004269 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004270 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004271 __ movl(locations->Out().AsRegister<Register>(),
4272 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004273 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004274 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004275 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004276 __ movl(locations->Out().AsRegister<Register>(),
4277 Address(locations->InAt(0).AsRegister<Register>(),
4278 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4279 // temp = temp->GetImtEntryAt(method_offset);
4280 __ movl(locations->Out().AsRegister<Register>(),
4281 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004282 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004283}
4284
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004285void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004286 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004287 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004288 locations->SetInAt(0, Location::RequiresRegister());
4289 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004290}
4291
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004292void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4293 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004294 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004295 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004296 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004297 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004298 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004299 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004300 break;
4301
4302 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004303 __ notl(out.AsRegisterPairLow<Register>());
4304 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004305 break;
4306
4307 default:
4308 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4309 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004310}
4311
David Brazdil66d126e2015-04-03 16:02:44 +01004312void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4313 LocationSummary* locations =
4314 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4315 locations->SetInAt(0, Location::RequiresRegister());
4316 locations->SetOut(Location::SameAsFirstInput());
4317}
4318
4319void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004320 LocationSummary* locations = bool_not->GetLocations();
4321 Location in = locations->InAt(0);
4322 Location out = locations->Out();
4323 DCHECK(in.Equals(out));
4324 __ xorl(out.AsRegister<Register>(), Immediate(1));
4325}
4326
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004327void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004328 LocationSummary* locations =
4329 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004330 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004331 case Primitive::kPrimBoolean:
4332 case Primitive::kPrimByte:
4333 case Primitive::kPrimShort:
4334 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004335 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004336 case Primitive::kPrimLong: {
4337 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004338 locations->SetInAt(1, Location::Any());
4339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4340 break;
4341 }
4342 case Primitive::kPrimFloat:
4343 case Primitive::kPrimDouble: {
4344 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004345 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4346 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4347 } else if (compare->InputAt(1)->IsConstant()) {
4348 locations->SetInAt(1, Location::RequiresFpuRegister());
4349 } else {
4350 locations->SetInAt(1, Location::Any());
4351 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004352 locations->SetOut(Location::RequiresRegister());
4353 break;
4354 }
4355 default:
4356 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4357 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004358}
4359
4360void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004361 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004363 Location left = locations->InAt(0);
4364 Location right = locations->InAt(1);
4365
Mark Mendell0c9497d2015-08-21 09:30:05 -04004366 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004367 Condition less_cond = kLess;
4368
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004370 case Primitive::kPrimBoolean:
4371 case Primitive::kPrimByte:
4372 case Primitive::kPrimShort:
4373 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004374 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004375 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004376 break;
4377 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004378 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004379 Register left_low = left.AsRegisterPairLow<Register>();
4380 Register left_high = left.AsRegisterPairHigh<Register>();
4381 int32_t val_low = 0;
4382 int32_t val_high = 0;
4383 bool right_is_const = false;
4384
4385 if (right.IsConstant()) {
4386 DCHECK(right.GetConstant()->IsLongConstant());
4387 right_is_const = true;
4388 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4389 val_low = Low32Bits(val);
4390 val_high = High32Bits(val);
4391 }
4392
Calin Juravleddb7df22014-11-25 20:56:51 +00004393 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004394 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004395 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004396 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004397 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004399 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004401 __ j(kLess, &less); // Signed compare.
4402 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004403 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004404 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004405 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004406 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004407 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004408 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004409 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004410 }
Aart Bika19616e2016-02-01 18:57:58 -08004411 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004412 break;
4413 }
4414 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004415 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004416 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004417 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004418 break;
4419 }
4420 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004421 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004422 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004423 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004424 break;
4425 }
4426 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004427 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004428 }
Aart Bika19616e2016-02-01 18:57:58 -08004429
Calin Juravleddb7df22014-11-25 20:56:51 +00004430 __ movl(out, Immediate(0));
4431 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004432 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004433
4434 __ Bind(&greater);
4435 __ movl(out, Immediate(1));
4436 __ jmp(&done);
4437
4438 __ Bind(&less);
4439 __ movl(out, Immediate(-1));
4440
4441 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004442}
4443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004444void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004445 LocationSummary* locations =
4446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004447 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004448 locations->SetInAt(i, Location::Any());
4449 }
4450 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004451}
4452
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004453void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004454 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004455}
4456
Roland Levillain7c1559a2015-12-15 10:55:36 +00004457void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004458 /*
4459 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4460 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4461 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4462 */
4463 switch (kind) {
4464 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004465 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004466 break;
4467 }
4468 case MemBarrierKind::kAnyStore:
4469 case MemBarrierKind::kLoadAny:
4470 case MemBarrierKind::kStoreStore: {
4471 // nop
4472 break;
4473 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004474 case MemBarrierKind::kNTStoreStore:
4475 // Non-Temporal Store/Store needs an explicit fence.
4476 MemoryFence(/* non-temporal */ true);
4477 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004478 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004479}
4480
Vladimir Markodc151b22015-10-15 18:02:30 +01004481HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4482 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004483 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004484 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004485}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004487Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4488 Register temp) {
4489 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004490 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004491 if (!invoke->GetLocations()->Intrinsified()) {
4492 return location.AsRegister<Register>();
4493 }
4494 // For intrinsics we allow any location, so it may be on the stack.
4495 if (!location.IsRegister()) {
4496 __ movl(temp, Address(ESP, location.GetStackIndex()));
4497 return temp;
4498 }
4499 // For register locations, check if the register was saved. If so, get it from the stack.
4500 // Note: There is a chance that the register was saved but not overwritten, so we could
4501 // save one load. However, since this is just an intrinsic slow path we prefer this
4502 // simple and more robust approach rather that trying to determine if that's the case.
4503 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004504 if (slow_path != nullptr) {
4505 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4506 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4507 __ movl(temp, Address(ESP, stack_offset));
4508 return temp;
4509 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004510 }
4511 return location.AsRegister<Register>();
4512}
4513
Serguei Katkov288c7a82016-05-16 11:53:15 +06004514Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4515 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004516 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4517 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004518 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004519 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004520 uint32_t offset =
4521 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4522 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004523 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004524 }
Vladimir Marko58155012015-08-19 12:49:41 +00004525 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004526 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004527 break;
4528 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4529 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4530 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004531 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4532 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4533 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004534 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004535 // Bind a new fixup label at the end of the "movl" insn.
4536 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004537 __ Bind(NewPcRelativeDexCacheArrayPatch(
4538 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4539 invoke->GetDexFileForPcRelativeDexCache(),
4540 offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004541 break;
4542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004544 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004545 Register method_reg;
4546 Register reg = temp.AsRegister<Register>();
4547 if (current_method.IsRegister()) {
4548 method_reg = current_method.AsRegister<Register>();
4549 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004550 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004551 DCHECK(!current_method.IsValid());
4552 method_reg = reg;
4553 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4554 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004555 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004556 __ movl(reg, Address(method_reg,
4557 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004558 // temp = temp[index_in_cache];
4559 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4560 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004561 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4562 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004563 }
Vladimir Marko58155012015-08-19 12:49:41 +00004564 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004565 return callee_method;
4566}
4567
4568void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4569 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004570
4571 switch (invoke->GetCodePtrLocation()) {
4572 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4573 __ call(GetFrameEntryLabel());
4574 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004575 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4576 // (callee_method + offset_of_quick_compiled_code)()
4577 __ call(Address(callee_method.AsRegister<Register>(),
4578 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004579 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004580 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004581 }
4582
4583 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004584}
4585
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004586void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4587 Register temp = temp_in.AsRegister<Register>();
4588 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4589 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004590
4591 // Use the calling convention instead of the location of the receiver, as
4592 // intrinsics may have put the receiver in a different register. In the intrinsics
4593 // slow path, the arguments have been moved to the right place, so here we are
4594 // guaranteed that the receiver is the first register of the calling convention.
4595 InvokeDexCallingConvention calling_convention;
4596 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004597 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004598 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004599 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004600 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004601 // Instead of simply (possibly) unpoisoning `temp` here, we should
4602 // emit a read barrier for the previous class reference load.
4603 // However this is not required in practice, as this is an
4604 // intermediate/temporary reference and because the current
4605 // concurrent copying collector keeps the from-space memory
4606 // intact/accessible until the end of the marking phase (the
4607 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004608 __ MaybeUnpoisonHeapReference(temp);
4609 // temp = temp->GetMethodAt(method_offset);
4610 __ movl(temp, Address(temp, method_offset));
4611 // call temp->GetEntryPoint();
4612 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004613 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004614}
4615
Vladimir Markoaad75c62016-10-03 08:46:48 +00004616void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4617 DCHECK(GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004618 HX86ComputeBaseMethodAddress* address = nullptr;
4619 if (GetCompilerOptions().GetCompilePic()) {
4620 address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4621 } else {
4622 DCHECK_EQ(load_string->InputCount(), 0u);
4623 }
4624 string_patches_.emplace_back(address,
4625 load_string->GetDexFile(),
4626 load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004627 __ Bind(&string_patches_.back().label);
4628}
4629
Vladimir Marko1998cd02017-01-13 13:02:58 +00004630void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004631 HX86ComputeBaseMethodAddress* address = nullptr;
4632 if (GetCompilerOptions().GetCompilePic()) {
4633 address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4634 } else {
4635 DCHECK_EQ(load_class->InputCount(), 0u);
4636 }
4637 boot_image_type_patches_.emplace_back(address,
4638 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004639 load_class->GetTypeIndex().index_);
4640 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004641}
4642
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004643Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004644 HX86ComputeBaseMethodAddress* address =
4645 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4646 type_bss_entry_patches_.emplace_back(
4647 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004648 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004649}
4650
Vladimir Markoaad75c62016-10-03 08:46:48 +00004651Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4652 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004653 HX86ComputeBaseMethodAddress* address =
4654 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4655 string_patches_.emplace_back(
4656 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004657 return &string_patches_.back().label;
4658}
4659
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004660Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(
4661 HX86ComputeBaseMethodAddress* method_address,
4662 const DexFile& dex_file,
4663 uint32_t element_offset) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004664 // Add the patch entry and bind its label at the end of the instruction.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004665 pc_relative_dex_cache_patches_.emplace_back(method_address, dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004666 return &pc_relative_dex_cache_patches_.back().label;
4667}
4668
Vladimir Markoaad75c62016-10-03 08:46:48 +00004669// The label points to the end of the "movl" or another instruction but the literal offset
4670// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4671constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4672
4673template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4674inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004675 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004676 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004677 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004678 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004679 linker_patches->push_back(Factory(
4680 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004681 }
4682}
4683
Vladimir Marko58155012015-08-19 12:49:41 +00004684void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4685 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004686 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004687 pc_relative_dex_cache_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004688 string_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004689 boot_image_type_patches_.size() +
4690 type_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004691 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004692 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4693 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004694 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004695 DCHECK(boot_image_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004696 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4697 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004698 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4699 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004700 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004701 } else {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004702 for (const PatchInfo<Label>& info : boot_image_type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004703 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004704 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004705 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004706 for (const PatchInfo<Label>& info : string_patches_) {
4707 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4708 linker_patches->push_back(
4709 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
4710 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004711 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004712 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4713 linker_patches);
4714 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004715}
4716
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004717void CodeGeneratorX86::MarkGCCard(Register temp,
4718 Register card,
4719 Register object,
4720 Register value,
4721 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004722 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004723 if (value_can_be_null) {
4724 __ testl(value, value);
4725 __ j(kEqual, &is_null);
4726 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004727 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004728 __ movl(temp, object);
4729 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004730 __ movb(Address(temp, card, TIMES_1, 0),
4731 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004732 if (value_can_be_null) {
4733 __ Bind(&is_null);
4734 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004735}
4736
Calin Juravle52c48962014-12-16 17:02:57 +00004737void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4738 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004739
4740 bool object_field_get_with_read_barrier =
4741 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004742 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004743 new (GetGraph()->GetArena()) LocationSummary(instruction,
4744 kEmitCompilerReadBarrier ?
4745 LocationSummary::kCallOnSlowPath :
4746 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004747 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004748 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004749 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004750 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004751
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004752 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4753 locations->SetOut(Location::RequiresFpuRegister());
4754 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004755 // The output overlaps in case of long: we don't want the low move
4756 // to overwrite the object's location. Likewise, in the case of
4757 // an object field get with read barriers enabled, we do not want
4758 // the move to overwrite the object's location, as we need it to emit
4759 // the read barrier.
4760 locations->SetOut(
4761 Location::RequiresRegister(),
4762 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4763 Location::kOutputOverlap :
4764 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004765 }
Calin Juravle52c48962014-12-16 17:02:57 +00004766
4767 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4768 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004769 // So we use an XMM register as a temp to achieve atomicity (first
4770 // load the temp into the XMM and then copy the XMM into the
4771 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004772 locations->AddTemp(Location::RequiresFpuRegister());
4773 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774}
4775
Calin Juravle52c48962014-12-16 17:02:57 +00004776void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4777 const FieldInfo& field_info) {
4778 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004779
Calin Juravle52c48962014-12-16 17:02:57 +00004780 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004781 Location base_loc = locations->InAt(0);
4782 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004783 Location out = locations->Out();
4784 bool is_volatile = field_info.IsVolatile();
4785 Primitive::Type field_type = field_info.GetFieldType();
4786 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4787
4788 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004790 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004791 break;
4792 }
4793
4794 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004795 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004796 break;
4797 }
4798
4799 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004800 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004801 break;
4802 }
4803
4804 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004805 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004806 break;
4807 }
4808
4809 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004810 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004811 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004812
4813 case Primitive::kPrimNot: {
4814 // /* HeapReference<Object> */ out = *(base + offset)
4815 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004816 // Note that a potential implicit null check is handled in this
4817 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4818 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004819 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004820 if (is_volatile) {
4821 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4822 }
4823 } else {
4824 __ movl(out.AsRegister<Register>(), Address(base, offset));
4825 codegen_->MaybeRecordImplicitNullCheck(instruction);
4826 if (is_volatile) {
4827 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4828 }
4829 // If read barriers are enabled, emit read barriers other than
4830 // Baker's using a slow path (and also unpoison the loaded
4831 // reference, if heap poisoning is enabled).
4832 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4833 }
4834 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004835 }
4836
4837 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004838 if (is_volatile) {
4839 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4840 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004842 __ movd(out.AsRegisterPairLow<Register>(), temp);
4843 __ psrlq(temp, Immediate(32));
4844 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4845 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004846 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004847 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004849 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4850 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004851 break;
4852 }
4853
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004854 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004855 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004856 break;
4857 }
4858
4859 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004860 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004861 break;
4862 }
4863
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004864 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004865 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004866 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004867 }
Calin Juravle52c48962014-12-16 17:02:57 +00004868
Roland Levillain7c1559a2015-12-15 10:55:36 +00004869 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4870 // Potential implicit null checks, in the case of reference or
4871 // long fields, are handled in the previous switch statement.
4872 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004873 codegen_->MaybeRecordImplicitNullCheck(instruction);
4874 }
4875
Calin Juravle52c48962014-12-16 17:02:57 +00004876 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004877 if (field_type == Primitive::kPrimNot) {
4878 // Memory barriers, in the case of references, are also handled
4879 // in the previous switch statement.
4880 } else {
4881 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4882 }
Roland Levillain4d027112015-07-01 15:41:14 +01004883 }
Calin Juravle52c48962014-12-16 17:02:57 +00004884}
4885
4886void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4887 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4888
4889 LocationSummary* locations =
4890 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4891 locations->SetInAt(0, Location::RequiresRegister());
4892 bool is_volatile = field_info.IsVolatile();
4893 Primitive::Type field_type = field_info.GetFieldType();
4894 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4895 || (field_type == Primitive::kPrimByte);
4896
4897 // The register allocator does not support multiple
4898 // inputs that die at entry with one in a specific register.
4899 if (is_byte_type) {
4900 // Ensure the value is in a byte register.
4901 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004902 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004903 if (is_volatile && field_type == Primitive::kPrimDouble) {
4904 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4905 locations->SetInAt(1, Location::RequiresFpuRegister());
4906 } else {
4907 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4908 }
4909 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4910 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004911 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004912
Calin Juravle52c48962014-12-16 17:02:57 +00004913 // 64bits value can be atomically written to an address with movsd and an XMM register.
4914 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4915 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4916 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4917 // isolated cases when we need this it isn't worth adding the extra complexity.
4918 locations->AddTemp(Location::RequiresFpuRegister());
4919 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004920 } else {
4921 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4922
4923 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4924 // Temporary registers for the write barrier.
4925 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4926 // Ensure the card is in a byte register.
4927 locations->AddTemp(Location::RegisterLocation(ECX));
4928 }
Calin Juravle52c48962014-12-16 17:02:57 +00004929 }
4930}
4931
4932void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004933 const FieldInfo& field_info,
4934 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004935 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4936
4937 LocationSummary* locations = instruction->GetLocations();
4938 Register base = locations->InAt(0).AsRegister<Register>();
4939 Location value = locations->InAt(1);
4940 bool is_volatile = field_info.IsVolatile();
4941 Primitive::Type field_type = field_info.GetFieldType();
4942 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004943 bool needs_write_barrier =
4944 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004945
4946 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004947 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004948 }
4949
Mark Mendell81489372015-11-04 11:30:41 -05004950 bool maybe_record_implicit_null_check_done = false;
4951
Calin Juravle52c48962014-12-16 17:02:57 +00004952 switch (field_type) {
4953 case Primitive::kPrimBoolean:
4954 case Primitive::kPrimByte: {
4955 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4956 break;
4957 }
4958
4959 case Primitive::kPrimShort:
4960 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004961 if (value.IsConstant()) {
4962 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4963 __ movw(Address(base, offset), Immediate(v));
4964 } else {
4965 __ movw(Address(base, offset), value.AsRegister<Register>());
4966 }
Calin Juravle52c48962014-12-16 17:02:57 +00004967 break;
4968 }
4969
4970 case Primitive::kPrimInt:
4971 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004972 if (kPoisonHeapReferences && needs_write_barrier) {
4973 // Note that in the case where `value` is a null reference,
4974 // we do not enter this block, as the reference does not
4975 // need poisoning.
4976 DCHECK_EQ(field_type, Primitive::kPrimNot);
4977 Register temp = locations->GetTemp(0).AsRegister<Register>();
4978 __ movl(temp, value.AsRegister<Register>());
4979 __ PoisonHeapReference(temp);
4980 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004981 } else if (value.IsConstant()) {
4982 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4983 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004984 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004985 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004986 __ movl(Address(base, offset), value.AsRegister<Register>());
4987 }
Calin Juravle52c48962014-12-16 17:02:57 +00004988 break;
4989 }
4990
4991 case Primitive::kPrimLong: {
4992 if (is_volatile) {
4993 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4994 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4995 __ movd(temp1, value.AsRegisterPairLow<Register>());
4996 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4997 __ punpckldq(temp1, temp2);
4998 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004999 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005000 } else if (value.IsConstant()) {
5001 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5002 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5003 codegen_->MaybeRecordImplicitNullCheck(instruction);
5004 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005005 } else {
5006 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005007 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005008 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5009 }
Mark Mendell81489372015-11-04 11:30:41 -05005010 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005011 break;
5012 }
5013
5014 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005015 if (value.IsConstant()) {
5016 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5017 __ movl(Address(base, offset), Immediate(v));
5018 } else {
5019 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5020 }
Calin Juravle52c48962014-12-16 17:02:57 +00005021 break;
5022 }
5023
5024 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005025 if (value.IsConstant()) {
5026 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5027 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5028 codegen_->MaybeRecordImplicitNullCheck(instruction);
5029 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5030 maybe_record_implicit_null_check_done = true;
5031 } else {
5032 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5033 }
Calin Juravle52c48962014-12-16 17:02:57 +00005034 break;
5035 }
5036
5037 case Primitive::kPrimVoid:
5038 LOG(FATAL) << "Unreachable type " << field_type;
5039 UNREACHABLE();
5040 }
5041
Mark Mendell81489372015-11-04 11:30:41 -05005042 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005043 codegen_->MaybeRecordImplicitNullCheck(instruction);
5044 }
5045
Roland Levillain4d027112015-07-01 15:41:14 +01005046 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005047 Register temp = locations->GetTemp(0).AsRegister<Register>();
5048 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005049 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005050 }
5051
Calin Juravle52c48962014-12-16 17:02:57 +00005052 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005053 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005054 }
5055}
5056
5057void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5058 HandleFieldGet(instruction, instruction->GetFieldInfo());
5059}
5060
5061void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5062 HandleFieldGet(instruction, instruction->GetFieldInfo());
5063}
5064
5065void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5066 HandleFieldSet(instruction, instruction->GetFieldInfo());
5067}
5068
5069void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005070 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005071}
5072
5073void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5074 HandleFieldSet(instruction, instruction->GetFieldInfo());
5075}
5076
5077void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005078 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005079}
5080
5081void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5082 HandleFieldGet(instruction, instruction->GetFieldInfo());
5083}
5084
5085void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5086 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005087}
5088
Calin Juravlee460d1d2015-09-29 04:52:17 +01005089void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5090 HUnresolvedInstanceFieldGet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->CreateUnresolvedFieldLocationSummary(
5093 instruction, instruction->GetFieldType(), calling_convention);
5094}
5095
5096void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5097 HUnresolvedInstanceFieldGet* instruction) {
5098 FieldAccessCallingConventionX86 calling_convention;
5099 codegen_->GenerateUnresolvedFieldAccess(instruction,
5100 instruction->GetFieldType(),
5101 instruction->GetFieldIndex(),
5102 instruction->GetDexPc(),
5103 calling_convention);
5104}
5105
5106void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5107 HUnresolvedInstanceFieldSet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->CreateUnresolvedFieldLocationSummary(
5110 instruction, instruction->GetFieldType(), calling_convention);
5111}
5112
5113void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5114 HUnresolvedInstanceFieldSet* instruction) {
5115 FieldAccessCallingConventionX86 calling_convention;
5116 codegen_->GenerateUnresolvedFieldAccess(instruction,
5117 instruction->GetFieldType(),
5118 instruction->GetFieldIndex(),
5119 instruction->GetDexPc(),
5120 calling_convention);
5121}
5122
5123void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5124 HUnresolvedStaticFieldGet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->CreateUnresolvedFieldLocationSummary(
5127 instruction, instruction->GetFieldType(), calling_convention);
5128}
5129
5130void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5131 HUnresolvedStaticFieldGet* instruction) {
5132 FieldAccessCallingConventionX86 calling_convention;
5133 codegen_->GenerateUnresolvedFieldAccess(instruction,
5134 instruction->GetFieldType(),
5135 instruction->GetFieldIndex(),
5136 instruction->GetDexPc(),
5137 calling_convention);
5138}
5139
5140void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5141 HUnresolvedStaticFieldSet* instruction) {
5142 FieldAccessCallingConventionX86 calling_convention;
5143 codegen_->CreateUnresolvedFieldLocationSummary(
5144 instruction, instruction->GetFieldType(), calling_convention);
5145}
5146
5147void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5148 HUnresolvedStaticFieldSet* instruction) {
5149 FieldAccessCallingConventionX86 calling_convention;
5150 codegen_->GenerateUnresolvedFieldAccess(instruction,
5151 instruction->GetFieldType(),
5152 instruction->GetFieldIndex(),
5153 instruction->GetDexPc(),
5154 calling_convention);
5155}
5156
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005158 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5159 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5160 ? Location::RequiresRegister()
5161 : Location::Any();
5162 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005163}
5164
Calin Juravle2ae48182016-03-16 14:05:09 +00005165void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5166 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005167 return;
5168 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005169 LocationSummary* locations = instruction->GetLocations();
5170 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005171
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005172 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005173 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005174}
5175
Calin Juravle2ae48182016-03-16 14:05:09 +00005176void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005177 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005178 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179
5180 LocationSummary* locations = instruction->GetLocations();
5181 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005182
5183 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005184 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005185 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005186 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005187 } else {
5188 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005189 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005190 __ jmp(slow_path->GetEntryLabel());
5191 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005192 }
5193 __ j(kEqual, slow_path->GetEntryLabel());
5194}
5195
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005196void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005197 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005198}
5199
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005200void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005201 bool object_array_get_with_read_barrier =
5202 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005203 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005204 new (GetGraph()->GetArena()) LocationSummary(instruction,
5205 object_array_get_with_read_barrier ?
5206 LocationSummary::kCallOnSlowPath :
5207 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005208 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005209 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005210 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005211 locations->SetInAt(0, Location::RequiresRegister());
5212 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005213 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5214 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5215 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005216 // The output overlaps in case of long: we don't want the low move
5217 // to overwrite the array's location. Likewise, in the case of an
5218 // object array get with read barriers enabled, we do not want the
5219 // move to overwrite the array's location, as we need it to emit
5220 // the read barrier.
5221 locations->SetOut(
5222 Location::RequiresRegister(),
5223 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5224 Location::kOutputOverlap :
5225 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005226 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227}
5228
5229void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5230 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005231 Location obj_loc = locations->InAt(0);
5232 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005233 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005234 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005235 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 Primitive::Type type = instruction->GetType();
5238 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005240 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005241 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 break;
5243 }
5244
5245 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005246 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005247 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 break;
5249 }
5250
5251 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005252 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005253 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 break;
5255 }
5256
5257 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005259 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5260 // Branch cases into compressed and uncompressed for each index's type.
5261 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5262 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005263 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005264 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005265 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5266 "Expecting 0=compressed, 1=uncompressed");
5267 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005268 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5269 __ jmp(&done);
5270 __ Bind(&not_compressed);
5271 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5272 __ Bind(&done);
5273 } else {
5274 // Common case for charAt of array of char or when string compression's
5275 // feature is turned off.
5276 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5277 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 break;
5279 }
5280
Roland Levillain7c1559a2015-12-15 10:55:36 +00005281 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005282 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005283 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 break;
5285 }
5286
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 case Primitive::kPrimNot: {
5288 static_assert(
5289 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5290 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 // /* HeapReference<Object> */ out =
5292 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5293 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 // Note that a potential implicit null check is handled in this
5295 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5296 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005297 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005298 } else {
5299 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005300 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5301 codegen_->MaybeRecordImplicitNullCheck(instruction);
5302 // If read barriers are enabled, emit read barriers other than
5303 // Baker's using a slow path (and also unpoison the loaded
5304 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005305 if (index.IsConstant()) {
5306 uint32_t offset =
5307 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5309 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005310 codegen_->MaybeGenerateReadBarrierSlow(
5311 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5312 }
5313 }
5314 break;
5315 }
5316
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005318 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005319 __ movl(out_loc.AsRegisterPairLow<Register>(),
5320 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5321 codegen_->MaybeRecordImplicitNullCheck(instruction);
5322 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5323 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005324 break;
5325 }
5326
Mark Mendell7c8d0092015-01-26 11:21:33 -05005327 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005328 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005329 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005330 break;
5331 }
5332
5333 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005334 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005335 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005336 break;
5337 }
5338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005340 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005341 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005343
Roland Levillain7c1559a2015-12-15 10:55:36 +00005344 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5345 // Potential implicit null checks, in the case of reference or
5346 // long arrays, are handled in the previous switch statement.
5347 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005348 codegen_->MaybeRecordImplicitNullCheck(instruction);
5349 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350}
5351
5352void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005353 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005355 bool needs_write_barrier =
5356 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005357 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005358
Nicolas Geoffray39468442014-09-02 15:17:15 +01005359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5360 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005361 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005362 LocationSummary::kCallOnSlowPath :
5363 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005364
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005365 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5366 || (value_type == Primitive::kPrimByte);
5367 // We need the inputs to be different than the output in case of long operation.
5368 // In case of a byte operation, the register allocator does not support multiple
5369 // inputs that die at entry with one in a specific register.
5370 locations->SetInAt(0, Location::RequiresRegister());
5371 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5372 if (is_byte_type) {
5373 // Ensure the value is in a byte register.
5374 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5375 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005376 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5379 }
5380 if (needs_write_barrier) {
5381 // Temporary registers for the write barrier.
5382 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5383 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005384 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386}
5387
5388void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5389 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005390 Location array_loc = locations->InAt(0);
5391 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005393 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005394 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005395 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5396 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5397 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005398 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005399 bool needs_write_barrier =
5400 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401
5402 switch (value_type) {
5403 case Primitive::kPrimBoolean:
5404 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005405 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005406 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 if (value.IsRegister()) {
5408 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005412 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 break;
5414 }
5415
5416 case Primitive::kPrimShort:
5417 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005419 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 if (value.IsRegister()) {
5421 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005425 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 break;
5427 }
5428
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005429 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005431 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005432
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 if (!value.IsRegister()) {
5434 // Just setting null.
5435 DCHECK(instruction->InputAt(2)->IsNullConstant());
5436 DCHECK(value.IsConstant()) << value;
5437 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005438 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005440 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005442 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443
5444 DCHECK(needs_write_barrier);
5445 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005446 // We cannot use a NearLabel for `done`, as its range may be too
5447 // short when Baker read barriers are enabled.
5448 Label done;
5449 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005450 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005451 Location temp_loc = locations->GetTemp(0);
5452 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005453 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005454 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5455 codegen_->AddSlowPath(slow_path);
5456 if (instruction->GetValueCanBeNull()) {
5457 __ testl(register_value, register_value);
5458 __ j(kNotEqual, &not_null);
5459 __ movl(address, Immediate(0));
5460 codegen_->MaybeRecordImplicitNullCheck(instruction);
5461 __ jmp(&done);
5462 __ Bind(&not_null);
5463 }
5464
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005465 // Note that when Baker read barriers are enabled, the type
5466 // checks are performed without read barriers. This is fine,
5467 // even in the case where a class object is in the from-space
5468 // after the flip, as a comparison involving such a type would
5469 // not produce a false positive; it may of course produce a
5470 // false negative, in which case we would take the ArraySet
5471 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005472
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005473 // /* HeapReference<Class> */ temp = array->klass_
5474 __ movl(temp, Address(array, class_offset));
5475 codegen_->MaybeRecordImplicitNullCheck(instruction);
5476 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005477
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005478 // /* HeapReference<Class> */ temp = temp->component_type_
5479 __ movl(temp, Address(temp, component_offset));
5480 // If heap poisoning is enabled, no need to unpoison `temp`
5481 // nor the object reference in `register_value->klass`, as
5482 // we are comparing two poisoned references.
5483 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005484
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005485 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5486 __ j(kEqual, &do_put);
5487 // If heap poisoning is enabled, the `temp` reference has
5488 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005489 __ MaybeUnpoisonHeapReference(temp);
5490
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005491 // If heap poisoning is enabled, no need to unpoison the
5492 // heap reference loaded below, as it is only used for a
5493 // comparison with null.
5494 __ cmpl(Address(temp, super_offset), Immediate(0));
5495 __ j(kNotEqual, slow_path->GetEntryLabel());
5496 __ Bind(&do_put);
5497 } else {
5498 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005499 }
5500 }
5501
5502 if (kPoisonHeapReferences) {
5503 __ movl(temp, register_value);
5504 __ PoisonHeapReference(temp);
5505 __ movl(address, temp);
5506 } else {
5507 __ movl(address, register_value);
5508 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005509 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005510 codegen_->MaybeRecordImplicitNullCheck(instruction);
5511 }
5512
5513 Register card = locations->GetTemp(1).AsRegister<Register>();
5514 codegen_->MarkGCCard(
5515 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5516 __ Bind(&done);
5517
5518 if (slow_path != nullptr) {
5519 __ Bind(slow_path->GetExitLabel());
5520 }
5521
5522 break;
5523 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005524
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005525 case Primitive::kPrimInt: {
5526 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005527 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005528 if (value.IsRegister()) {
5529 __ movl(address, value.AsRegister<Register>());
5530 } else {
5531 DCHECK(value.IsConstant()) << value;
5532 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5533 __ movl(address, Immediate(v));
5534 }
5535 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536 break;
5537 }
5538
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539 case Primitive::kPrimLong: {
5540 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005541 if (value.IsRegisterPair()) {
5542 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5543 value.AsRegisterPairLow<Register>());
5544 codegen_->MaybeRecordImplicitNullCheck(instruction);
5545 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5546 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005548 DCHECK(value.IsConstant());
5549 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5550 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5551 Immediate(Low32Bits(val)));
5552 codegen_->MaybeRecordImplicitNullCheck(instruction);
5553 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5554 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005555 }
5556 break;
5557 }
5558
Mark Mendell7c8d0092015-01-26 11:21:33 -05005559 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005560 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005561 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005562 if (value.IsFpuRegister()) {
5563 __ movss(address, value.AsFpuRegister<XmmRegister>());
5564 } else {
5565 DCHECK(value.IsConstant());
5566 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5567 __ movl(address, Immediate(v));
5568 }
5569 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005570 break;
5571 }
5572
5573 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005574 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005575 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005576 if (value.IsFpuRegister()) {
5577 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5578 } else {
5579 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005580 Address address_hi =
5581 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005582 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5583 __ movl(address, Immediate(Low32Bits(v)));
5584 codegen_->MaybeRecordImplicitNullCheck(instruction);
5585 __ movl(address_hi, Immediate(High32Bits(v)));
5586 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005587 break;
5588 }
5589
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590 case Primitive::kPrimVoid:
5591 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005592 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593 }
5594}
5595
5596void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005598 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005599 if (!instruction->IsEmittedAtUseSite()) {
5600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5601 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005602}
5603
5604void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005605 if (instruction->IsEmittedAtUseSite()) {
5606 return;
5607 }
5608
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005609 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005610 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005611 Register obj = locations->InAt(0).AsRegister<Register>();
5612 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005614 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005615 // Mask out most significant bit in case the array is String's array of char.
5616 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005617 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005619}
5620
5621void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005622 RegisterSet caller_saves = RegisterSet::Empty();
5623 InvokeRuntimeCallingConvention calling_convention;
5624 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5625 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5626 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005627 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005628 HInstruction* length = instruction->InputAt(1);
5629 if (!length->IsEmittedAtUseSite()) {
5630 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5631 }
jessicahandojo4877b792016-09-08 19:49:13 -07005632 // Need register to see array's length.
5633 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5634 locations->AddTemp(Location::RequiresRegister());
5635 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005636}
5637
5638void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005639 const bool is_string_compressed_char_at =
5640 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005641 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005642 Location index_loc = locations->InAt(0);
5643 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005644 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005645 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005646
Mark Mendell99dbd682015-04-22 16:18:52 -04005647 if (length_loc.IsConstant()) {
5648 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5649 if (index_loc.IsConstant()) {
5650 // BCE will remove the bounds check if we are guarenteed to pass.
5651 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5652 if (index < 0 || index >= length) {
5653 codegen_->AddSlowPath(slow_path);
5654 __ jmp(slow_path->GetEntryLabel());
5655 } else {
5656 // Some optimization after BCE may have generated this, and we should not
5657 // generate a bounds check if it is a valid range.
5658 }
5659 return;
5660 }
5661
5662 // We have to reverse the jump condition because the length is the constant.
5663 Register index_reg = index_loc.AsRegister<Register>();
5664 __ cmpl(index_reg, Immediate(length));
5665 codegen_->AddSlowPath(slow_path);
5666 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005667 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005668 HInstruction* array_length = instruction->InputAt(1);
5669 if (array_length->IsEmittedAtUseSite()) {
5670 // Address the length field in the array.
5671 DCHECK(array_length->IsArrayLength());
5672 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5673 Location array_loc = array_length->GetLocations()->InAt(0);
5674 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005675 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005676 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5677 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005678 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5679 __ movl(length_reg, array_len);
5680 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005681 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005682 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005683 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005684 // Checking bounds for general case:
5685 // Array of char or string's array with feature compression off.
5686 if (index_loc.IsConstant()) {
5687 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5688 __ cmpl(array_len, Immediate(value));
5689 } else {
5690 __ cmpl(array_len, index_loc.AsRegister<Register>());
5691 }
5692 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005693 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005694 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005695 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005696 }
5697 codegen_->AddSlowPath(slow_path);
5698 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005699 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005700}
5701
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005702void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005703 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005704}
5705
5706void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005707 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5708}
5709
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005710void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005711 LocationSummary* locations =
5712 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005713 // In suspend check slow path, usually there are no caller-save registers at all.
5714 // If SIMD instructions are present, however, we force spilling all live SIMD
5715 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005716 locations->SetCustomSlowPathCallerSaves(
5717 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005718}
5719
5720void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005721 HBasicBlock* block = instruction->GetBlock();
5722 if (block->GetLoopInformation() != nullptr) {
5723 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5724 // The back edge will generate the suspend check.
5725 return;
5726 }
5727 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5728 // The goto will generate the suspend check.
5729 return;
5730 }
5731 GenerateSuspendCheck(instruction, nullptr);
5732}
5733
5734void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5735 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005736 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005737 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5738 if (slow_path == nullptr) {
5739 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5740 instruction->SetSlowPath(slow_path);
5741 codegen_->AddSlowPath(slow_path);
5742 if (successor != nullptr) {
5743 DCHECK(successor->IsLoopHeader());
5744 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5745 }
5746 } else {
5747 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5748 }
5749
Andreas Gampe542451c2016-07-26 09:02:02 -07005750 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005751 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005752 if (successor == nullptr) {
5753 __ j(kNotEqual, slow_path->GetEntryLabel());
5754 __ Bind(slow_path->GetReturnLabel());
5755 } else {
5756 __ j(kEqual, codegen_->GetLabelOf(successor));
5757 __ jmp(slow_path->GetEntryLabel());
5758 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005759}
5760
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5762 return codegen_->GetAssembler();
5763}
5764
Mark Mendell7c8d0092015-01-26 11:21:33 -05005765void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005766 ScratchRegisterScope ensure_scratch(
5767 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5768 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5769 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5770 __ movl(temp_reg, Address(ESP, src + stack_offset));
5771 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005772}
5773
5774void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005775 ScratchRegisterScope ensure_scratch(
5776 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5777 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5778 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5779 __ movl(temp_reg, Address(ESP, src + stack_offset));
5780 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5781 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5782 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783}
5784
5785void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005786 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005787 Location source = move->GetSource();
5788 Location destination = move->GetDestination();
5789
5790 if (source.IsRegister()) {
5791 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005792 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005793 } else if (destination.IsFpuRegister()) {
5794 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005795 } else {
5796 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005797 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005798 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005799 } else if (source.IsRegisterPair()) {
5800 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5801 // Create stack space for 2 elements.
5802 __ subl(ESP, Immediate(2 * elem_size));
5803 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5804 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5805 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5806 // And remove the temporary stack space we allocated.
5807 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005808 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005809 if (destination.IsRegister()) {
5810 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5811 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005812 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005813 } else if (destination.IsRegisterPair()) {
5814 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5815 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5816 __ psrlq(src_reg, Immediate(32));
5817 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005818 } else if (destination.IsStackSlot()) {
5819 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005820 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005821 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005822 } else {
5823 DCHECK(destination.IsSIMDStackSlot());
5824 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005825 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005826 } else if (source.IsStackSlot()) {
5827 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005828 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 } else if (destination.IsFpuRegister()) {
5830 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005831 } else {
5832 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5834 }
5835 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005836 if (destination.IsRegisterPair()) {
5837 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5838 __ movl(destination.AsRegisterPairHigh<Register>(),
5839 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5840 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005841 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5842 } else {
5843 DCHECK(destination.IsDoubleStackSlot()) << destination;
5844 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005845 }
Aart Bik5576f372017-03-23 16:17:37 -07005846 } else if (source.IsSIMDStackSlot()) {
5847 DCHECK(destination.IsFpuRegister());
5848 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005849 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005851 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005852 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005853 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005854 if (value == 0) {
5855 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5856 } else {
5857 __ movl(destination.AsRegister<Register>(), Immediate(value));
5858 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005859 } else {
5860 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005861 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005862 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005863 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005864 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005865 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005866 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005867 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005868 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5869 if (value == 0) {
5870 // Easy handling of 0.0.
5871 __ xorps(dest, dest);
5872 } else {
5873 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005874 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5875 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5876 __ movl(temp, Immediate(value));
5877 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005878 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005879 } else {
5880 DCHECK(destination.IsStackSlot()) << destination;
5881 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5882 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005883 } else if (constant->IsLongConstant()) {
5884 int64_t value = constant->AsLongConstant()->GetValue();
5885 int32_t low_value = Low32Bits(value);
5886 int32_t high_value = High32Bits(value);
5887 Immediate low(low_value);
5888 Immediate high(high_value);
5889 if (destination.IsDoubleStackSlot()) {
5890 __ movl(Address(ESP, destination.GetStackIndex()), low);
5891 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5892 } else {
5893 __ movl(destination.AsRegisterPairLow<Register>(), low);
5894 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5895 }
5896 } else {
5897 DCHECK(constant->IsDoubleConstant());
5898 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005899 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005900 int32_t low_value = Low32Bits(value);
5901 int32_t high_value = High32Bits(value);
5902 Immediate low(low_value);
5903 Immediate high(high_value);
5904 if (destination.IsFpuRegister()) {
5905 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5906 if (value == 0) {
5907 // Easy handling of 0.0.
5908 __ xorpd(dest, dest);
5909 } else {
5910 __ pushl(high);
5911 __ pushl(low);
5912 __ movsd(dest, Address(ESP, 0));
5913 __ addl(ESP, Immediate(8));
5914 }
5915 } else {
5916 DCHECK(destination.IsDoubleStackSlot()) << destination;
5917 __ movl(Address(ESP, destination.GetStackIndex()), low);
5918 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5919 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005920 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005921 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005922 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005923 }
5924}
5925
Mark Mendella5c19ce2015-04-01 12:51:05 -04005926void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005927 Register suggested_scratch = reg == EAX ? EBX : EAX;
5928 ScratchRegisterScope ensure_scratch(
5929 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5930
5931 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5932 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5933 __ movl(Address(ESP, mem + stack_offset), reg);
5934 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005935}
5936
Mark Mendell7c8d0092015-01-26 11:21:33 -05005937void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005938 ScratchRegisterScope ensure_scratch(
5939 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5940
5941 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5942 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5943 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5944 __ movss(Address(ESP, mem + stack_offset), reg);
5945 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005946}
5947
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005948void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005949 ScratchRegisterScope ensure_scratch1(
5950 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005951
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005952 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5953 ScratchRegisterScope ensure_scratch2(
5954 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005955
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005956 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5957 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5958 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5959 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5960 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5961 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005962}
5963
5964void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005965 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005966 Location source = move->GetSource();
5967 Location destination = move->GetDestination();
5968
5969 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005970 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5971 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5972 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5973 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5974 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005975 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005976 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005977 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005978 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005979 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5980 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005981 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5982 // Use XOR Swap algorithm to avoid a temporary.
5983 DCHECK_NE(source.reg(), destination.reg());
5984 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5985 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5986 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5987 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5988 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5989 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5990 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005991 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5992 // Take advantage of the 16 bytes in the XMM register.
5993 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5994 Address stack(ESP, destination.GetStackIndex());
5995 // Load the double into the high doubleword.
5996 __ movhpd(reg, stack);
5997
5998 // Store the low double into the destination.
5999 __ movsd(stack, reg);
6000
6001 // Move the high double to the low double.
6002 __ psrldq(reg, Immediate(8));
6003 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6004 // Take advantage of the 16 bytes in the XMM register.
6005 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6006 Address stack(ESP, source.GetStackIndex());
6007 // Load the double into the high doubleword.
6008 __ movhpd(reg, stack);
6009
6010 // Store the low double into the destination.
6011 __ movsd(stack, reg);
6012
6013 // Move the high double to the low double.
6014 __ psrldq(reg, Immediate(8));
6015 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6016 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6017 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006018 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006019 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006020 }
6021}
6022
6023void ParallelMoveResolverX86::SpillScratch(int reg) {
6024 __ pushl(static_cast<Register>(reg));
6025}
6026
6027void ParallelMoveResolverX86::RestoreScratch(int reg) {
6028 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006029}
6030
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6032 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006034 case HLoadClass::LoadKind::kInvalid:
6035 LOG(FATAL) << "UNREACHABLE";
6036 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037 case HLoadClass::LoadKind::kReferrersClass:
6038 break;
6039 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6040 DCHECK(!GetCompilerOptions().GetCompilePic());
6041 break;
6042 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6043 DCHECK(GetCompilerOptions().GetCompilePic());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006044 FALLTHROUGH_INTENDED;
6045 case HLoadClass::LoadKind::kBssEntry:
6046 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 break;
6048 case HLoadClass::LoadKind::kBootImageAddress:
6049 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006050 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006051 DCHECK(Runtime::Current()->UseJitCompilation());
6052 break;
6053 case HLoadClass::LoadKind::kDexCacheViaMethod:
6054 break;
6055 }
6056 return desired_class_load_kind;
6057}
6058
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006059void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006060 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6061 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006062 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006063 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006064 cls,
6065 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006066 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006067 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006068 return;
6069 }
Vladimir Marko41559982017-01-06 14:04:23 +00006070 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006071
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006072 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6073 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006074 ? LocationSummary::kCallOnSlowPath
6075 : LocationSummary::kNoCall;
6076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006077 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006078 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006079 }
6080
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006081 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006082 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6083 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006084 locations->SetInAt(0, Location::RequiresRegister());
6085 }
6086 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006087 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6088 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6089 // Rely on the type resolution and/or initialization to save everything.
6090 RegisterSet caller_saves = RegisterSet::Empty();
6091 InvokeRuntimeCallingConvention calling_convention;
6092 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6093 locations->SetCustomSlowPathCallerSaves(caller_saves);
6094 } else {
6095 // For non-Baker read barrier we have a temp-clobbering call.
6096 }
6097 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098}
6099
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006100Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6101 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006102 Handle<mirror::Class> handle) {
6103 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6104 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006105 // Add a patch entry and return the label.
6106 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6107 PatchInfo<Label>* info = &jit_class_patches_.back();
6108 return &info->label;
6109}
6110
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006111// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6112// move.
6113void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006114 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6115 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6116 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006117 return;
6118 }
Vladimir Marko41559982017-01-06 14:04:23 +00006119 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006120
Vladimir Marko41559982017-01-06 14:04:23 +00006121 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 Location out_loc = locations->Out();
6123 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006126 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6127 ? kWithoutReadBarrier
6128 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006129 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006130 case HLoadClass::LoadKind::kReferrersClass: {
6131 DCHECK(!cls->CanCallRuntime());
6132 DCHECK(!cls->MustGenerateClinitCheck());
6133 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6134 Register current_method = locations->InAt(0).AsRegister<Register>();
6135 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006136 cls,
6137 out_loc,
6138 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006139 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006140 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 break;
6142 }
6143 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006144 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006145 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006147 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 break;
6149 }
6150 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006151 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006152 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006153 Register method_address = locations->InAt(0).AsRegister<Register>();
6154 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006155 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 break;
6157 }
6158 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006159 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006160 uint32_t address = dchecked_integral_cast<uint32_t>(
6161 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6162 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 break;
6165 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006166 case HLoadClass::LoadKind::kBssEntry: {
6167 Register method_address = locations->InAt(0).AsRegister<Register>();
6168 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6169 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6170 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6171 generate_null_check = true;
6172 break;
6173 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006174 case HLoadClass::LoadKind::kJitTableAddress: {
6175 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6176 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006177 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006179 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006180 break;
6181 }
Vladimir Marko41559982017-01-06 14:04:23 +00006182 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006183 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006184 LOG(FATAL) << "UNREACHABLE";
6185 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006186 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006187
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006188 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6189 DCHECK(cls->CanCallRuntime());
6190 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6191 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6192 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006193
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006194 if (generate_null_check) {
6195 __ testl(out, out);
6196 __ j(kEqual, slow_path->GetEntryLabel());
6197 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006198
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 if (cls->MustGenerateClinitCheck()) {
6200 GenerateClassInitializationCheck(slow_path, out);
6201 } else {
6202 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006204 }
6205}
6206
6207void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6208 LocationSummary* locations =
6209 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6210 locations->SetInAt(0, Location::RequiresRegister());
6211 if (check->HasUses()) {
6212 locations->SetOut(Location::SameAsFirstInput());
6213 }
6214}
6215
6216void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006217 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006218 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006219 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006220 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006221 GenerateClassInitializationCheck(slow_path,
6222 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006223}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006224
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006225void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006226 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006227 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6228 Immediate(mirror::Class::kStatusInitialized));
6229 __ j(kLess, slow_path->GetEntryLabel());
6230 __ Bind(slow_path->GetExitLabel());
6231 // No need for memory fence, thanks to the X86 memory model.
6232}
6233
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006234HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6235 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006236 switch (desired_string_load_kind) {
6237 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6238 DCHECK(!GetCompilerOptions().GetCompilePic());
6239 break;
6240 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6241 DCHECK(GetCompilerOptions().GetCompilePic());
6242 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006243 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006244 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006245 break;
6246 case HLoadString::LoadKind::kBootImageAddress:
6247 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006248 case HLoadString::LoadKind::kJitTableAddress:
6249 DCHECK(Runtime::Current()->UseJitCompilation());
6250 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006251 case HLoadString::LoadKind::kDexCacheViaMethod:
6252 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006253 }
6254 return desired_string_load_kind;
6255}
6256
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006257void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006258 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006260 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006261 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006262 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006263 locations->SetInAt(0, Location::RequiresRegister());
6264 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006265 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6266 locations->SetOut(Location::RegisterLocation(EAX));
6267 } else {
6268 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006269 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6270 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006271 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006272 RegisterSet caller_saves = RegisterSet::Empty();
6273 InvokeRuntimeCallingConvention calling_convention;
6274 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6275 locations->SetCustomSlowPathCallerSaves(caller_saves);
6276 } else {
6277 // For non-Baker read barrier we have a temp-clobbering call.
6278 }
6279 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006280 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006281}
6282
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006283Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006284 dex::StringIndex dex_index,
6285 Handle<mirror::String> handle) {
6286 jit_string_roots_.Overwrite(
6287 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006288 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006289 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006290 PatchInfo<Label>* info = &jit_string_patches_.back();
6291 return &info->label;
6292}
6293
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006294// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6295// move.
6296void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006297 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 Location out_loc = locations->Out();
6299 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006301 switch (load->GetLoadKind()) {
6302 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006303 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006304 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006305 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 return; // No dex cache slow path.
6307 }
6308 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006309 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006310 Register method_address = locations->InAt(0).AsRegister<Register>();
6311 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006312 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006313 return; // No dex cache slow path.
6314 }
6315 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006316 uint32_t address = dchecked_integral_cast<uint32_t>(
6317 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6318 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006319 __ movl(out, Immediate(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006320 return; // No dex cache slow path.
6321 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006322 case HLoadString::LoadKind::kBssEntry: {
6323 Register method_address = locations->InAt(0).AsRegister<Register>();
6324 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6325 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006326 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006327 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006328 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6329 codegen_->AddSlowPath(slow_path);
6330 __ testl(out, out);
6331 __ j(kEqual, slow_path->GetEntryLabel());
6332 __ Bind(slow_path->GetExitLabel());
6333 return;
6334 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006335 case HLoadString::LoadKind::kJitTableAddress: {
6336 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6337 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006338 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006339 // /* GcRoot<mirror::String> */ out = *address
6340 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6341 return;
6342 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006343 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006344 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006345 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006347 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006348 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006349 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006350 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006351 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6352 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006353}
6354
David Brazdilcb1c0552015-08-04 16:22:25 +01006355static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006356 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006357}
6358
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006359void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6360 LocationSummary* locations =
6361 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6362 locations->SetOut(Location::RequiresRegister());
6363}
6364
6365void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006366 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6367}
6368
6369void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6370 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6371}
6372
6373void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6374 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006375}
6376
6377void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6378 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006379 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006380 InvokeRuntimeCallingConvention calling_convention;
6381 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6382}
6383
6384void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006385 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006386 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006387}
6388
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006389// Temp is used for read barrier.
6390static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6391 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006392 !kUseBakerReadBarrier &&
6393 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006394 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006395 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6396 return 1;
6397 }
6398 return 0;
6399}
6400
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006401// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006402// interface pointer, one for loading the current interface.
6403// The other checks have one temp for loading the object's class.
6404static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6405 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6406 return 2;
6407 }
6408 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006409}
6410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006411void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006414 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 case TypeCheckKind::kExactCheck:
6417 case TypeCheckKind::kAbstractClassCheck:
6418 case TypeCheckKind::kClassHierarchyCheck:
6419 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 call_kind =
6421 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006422 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 break;
6424 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 case TypeCheckKind::kUnresolvedCheck:
6426 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 call_kind = LocationSummary::kCallOnSlowPath;
6428 break;
6429 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006432 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006433 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006434 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006435 locations->SetInAt(0, Location::RequiresRegister());
6436 locations->SetInAt(1, Location::Any());
6437 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6438 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006439 // When read barriers are enabled, we need a temporary register for some cases.
6440 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006441}
6442
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006443void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006444 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006445 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 Location obj_loc = locations->InAt(0);
6447 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006448 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 Location out_loc = locations->Out();
6450 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006451 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6452 DCHECK_LE(num_temps, 1u);
6453 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006454 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6456 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6457 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006458 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006460
6461 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006462 // Avoid null check if we know obj is not null.
6463 if (instruction->MustDoNullCheck()) {
6464 __ testl(obj, obj);
6465 __ j(kEqual, &zero);
6466 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006467
Roland Levillain7c1559a2015-12-15 10:55:36 +00006468 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006470 // /* HeapReference<Class> */ out = obj->klass_
6471 GenerateReferenceLoadTwoRegisters(instruction,
6472 out_loc,
6473 obj_loc,
6474 class_offset,
6475 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006476 if (cls.IsRegister()) {
6477 __ cmpl(out, cls.AsRegister<Register>());
6478 } else {
6479 DCHECK(cls.IsStackSlot()) << cls;
6480 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6481 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006482
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006483 // Classes must be equal for the instanceof to succeed.
6484 __ j(kNotEqual, &zero);
6485 __ movl(out, Immediate(1));
6486 __ jmp(&done);
6487 break;
6488 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006491 // /* HeapReference<Class> */ out = obj->klass_
6492 GenerateReferenceLoadTwoRegisters(instruction,
6493 out_loc,
6494 obj_loc,
6495 class_offset,
6496 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 // If the class is abstract, we eagerly fetch the super class of the
6498 // object to avoid doing a comparison we know will fail.
6499 NearLabel loop;
6500 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006501 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006502 GenerateReferenceLoadOneRegister(instruction,
6503 out_loc,
6504 super_offset,
6505 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006506 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 __ testl(out, out);
6508 // If `out` is null, we use it for the result, and jump to `done`.
6509 __ j(kEqual, &done);
6510 if (cls.IsRegister()) {
6511 __ cmpl(out, cls.AsRegister<Register>());
6512 } else {
6513 DCHECK(cls.IsStackSlot()) << cls;
6514 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6515 }
6516 __ j(kNotEqual, &loop);
6517 __ movl(out, Immediate(1));
6518 if (zero.IsLinked()) {
6519 __ jmp(&done);
6520 }
6521 break;
6522 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006525 // /* HeapReference<Class> */ out = obj->klass_
6526 GenerateReferenceLoadTwoRegisters(instruction,
6527 out_loc,
6528 obj_loc,
6529 class_offset,
6530 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 // Walk over the class hierarchy to find a match.
6532 NearLabel loop, success;
6533 __ Bind(&loop);
6534 if (cls.IsRegister()) {
6535 __ cmpl(out, cls.AsRegister<Register>());
6536 } else {
6537 DCHECK(cls.IsStackSlot()) << cls;
6538 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6539 }
6540 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006542 GenerateReferenceLoadOneRegister(instruction,
6543 out_loc,
6544 super_offset,
6545 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006546 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006547 __ testl(out, out);
6548 __ j(kNotEqual, &loop);
6549 // If `out` is null, we use it for the result, and jump to `done`.
6550 __ jmp(&done);
6551 __ Bind(&success);
6552 __ movl(out, Immediate(1));
6553 if (zero.IsLinked()) {
6554 __ jmp(&done);
6555 }
6556 break;
6557 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006559 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006560 // /* HeapReference<Class> */ out = obj->klass_
6561 GenerateReferenceLoadTwoRegisters(instruction,
6562 out_loc,
6563 obj_loc,
6564 class_offset,
6565 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006566 // Do an exact check.
6567 NearLabel exact_check;
6568 if (cls.IsRegister()) {
6569 __ cmpl(out, cls.AsRegister<Register>());
6570 } else {
6571 DCHECK(cls.IsStackSlot()) << cls;
6572 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6573 }
6574 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006577 GenerateReferenceLoadOneRegister(instruction,
6578 out_loc,
6579 component_offset,
6580 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006581 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006582 __ testl(out, out);
6583 // If `out` is null, we use it for the result, and jump to `done`.
6584 __ j(kEqual, &done);
6585 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6586 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006587 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 __ movl(out, Immediate(1));
6589 __ jmp(&done);
6590 break;
6591 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006594 // No read barrier since the slow path will retry upon failure.
6595 // /* HeapReference<Class> */ out = obj->klass_
6596 GenerateReferenceLoadTwoRegisters(instruction,
6597 out_loc,
6598 obj_loc,
6599 class_offset,
6600 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006601 if (cls.IsRegister()) {
6602 __ cmpl(out, cls.AsRegister<Register>());
6603 } else {
6604 DCHECK(cls.IsStackSlot()) << cls;
6605 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6606 }
6607 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6609 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006610 codegen_->AddSlowPath(slow_path);
6611 __ j(kNotEqual, slow_path->GetEntryLabel());
6612 __ movl(out, Immediate(1));
6613 if (zero.IsLinked()) {
6614 __ jmp(&done);
6615 }
6616 break;
6617 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618
Calin Juravle98893e12015-10-02 21:05:03 +01006619 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620 case TypeCheckKind::kInterfaceCheck: {
6621 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006622 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623 // cases.
6624 //
6625 // We cannot directly call the InstanceofNonTrivial runtime
6626 // entry point without resorting to a type checking slow path
6627 // here (i.e. by calling InvokeRuntime directly), as it would
6628 // require to assign fixed registers for the inputs of this
6629 // HInstanceOf instruction (following the runtime calling
6630 // convention), which might be cluttered by the potential first
6631 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006632 //
6633 // TODO: Introduce a new runtime entry point taking the object
6634 // to test (instead of its class) as argument, and let it deal
6635 // with the read barrier issues. This will let us refactor this
6636 // case of the `switch` code as it was previously (with a direct
6637 // call to the runtime not using a type checking slow path).
6638 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006639 DCHECK(locations->OnlyCallsOnSlowPath());
6640 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6641 /* is_fatal */ false);
6642 codegen_->AddSlowPath(slow_path);
6643 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006644 if (zero.IsLinked()) {
6645 __ jmp(&done);
6646 }
6647 break;
6648 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006649 }
6650
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006651 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006652 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 __ xorl(out, out);
6654 }
6655
6656 if (done.IsLinked()) {
6657 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006658 }
6659
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006660 if (slow_path != nullptr) {
6661 __ Bind(slow_path->GetExitLabel());
6662 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006663}
6664
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006665static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6666 switch (type_check_kind) {
6667 case TypeCheckKind::kExactCheck:
6668 case TypeCheckKind::kAbstractClassCheck:
6669 case TypeCheckKind::kClassHierarchyCheck:
6670 case TypeCheckKind::kArrayObjectCheck:
6671 return !throws_into_catch && !kEmitCompilerReadBarrier;
6672 case TypeCheckKind::kInterfaceCheck:
6673 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6674 case TypeCheckKind::kArrayCheck:
6675 case TypeCheckKind::kUnresolvedCheck:
6676 return false;
6677 }
6678 LOG(FATAL) << "Unreachable";
6679 UNREACHABLE();
6680}
6681
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006682void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006683 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006685 LocationSummary::CallKind call_kind =
6686 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6687 ? LocationSummary::kNoCall
6688 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6690 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006691 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6692 // Require a register for the interface check since there is a loop that compares the class to
6693 // a memory address.
6694 locations->SetInAt(1, Location::RequiresRegister());
6695 } else {
6696 locations->SetInAt(1, Location::Any());
6697 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6699 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006700 // When read barriers are enabled, we need an additional temporary register for some cases.
6701 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6702}
6703
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006704void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006705 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006706 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707 Location obj_loc = locations->InAt(0);
6708 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006709 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 Location temp_loc = locations->GetTemp(0);
6711 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006712 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6713 DCHECK_GE(num_temps, 1u);
6714 DCHECK_LE(num_temps, 2u);
6715 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6716 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6717 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6718 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6719 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6720 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6721 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6722 const uint32_t object_array_data_offset =
6723 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006724
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006725 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6726 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6727 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006729 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6730
Roland Levillain0d5a2812015-11-13 10:07:31 +00006731 SlowPathCode* type_check_slow_path =
6732 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6733 is_type_check_slow_path_fatal);
6734 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006735
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006737 // Avoid null check if we know obj is not null.
6738 if (instruction->MustDoNullCheck()) {
6739 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006741 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006742
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744 case TypeCheckKind::kExactCheck:
6745 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006746 // /* HeapReference<Class> */ temp = obj->klass_
6747 GenerateReferenceLoadTwoRegisters(instruction,
6748 temp_loc,
6749 obj_loc,
6750 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006751 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006752
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006753 if (cls.IsRegister()) {
6754 __ cmpl(temp, cls.AsRegister<Register>());
6755 } else {
6756 DCHECK(cls.IsStackSlot()) << cls;
6757 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6758 }
6759 // Jump to slow path for throwing the exception or doing a
6760 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006762 break;
6763 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006764
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006765 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006766 // /* HeapReference<Class> */ temp = obj->klass_
6767 GenerateReferenceLoadTwoRegisters(instruction,
6768 temp_loc,
6769 obj_loc,
6770 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006771 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006773 // If the class is abstract, we eagerly fetch the super class of the
6774 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006775 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006776 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006777 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006778 GenerateReferenceLoadOneRegister(instruction,
6779 temp_loc,
6780 super_offset,
6781 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006782 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006783
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006784 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6785 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006786 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006787 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006788
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006789 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006790 if (cls.IsRegister()) {
6791 __ cmpl(temp, cls.AsRegister<Register>());
6792 } else {
6793 DCHECK(cls.IsStackSlot()) << cls;
6794 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6795 }
6796 __ j(kNotEqual, &loop);
6797 break;
6798 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006799
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006800 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006801 // /* HeapReference<Class> */ temp = obj->klass_
6802 GenerateReferenceLoadTwoRegisters(instruction,
6803 temp_loc,
6804 obj_loc,
6805 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006806 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006807
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006808 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006809 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006810 __ Bind(&loop);
6811 if (cls.IsRegister()) {
6812 __ cmpl(temp, cls.AsRegister<Register>());
6813 } else {
6814 DCHECK(cls.IsStackSlot()) << cls;
6815 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6816 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006817 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006818
Roland Levillain0d5a2812015-11-13 10:07:31 +00006819 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006820 GenerateReferenceLoadOneRegister(instruction,
6821 temp_loc,
6822 super_offset,
6823 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006824 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006825
6826 // If the class reference currently in `temp` is not null, jump
6827 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006828 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006829 __ j(kNotZero, &loop);
6830 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006831 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006832 break;
6833 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006834
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006835 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006836 // /* HeapReference<Class> */ temp = obj->klass_
6837 GenerateReferenceLoadTwoRegisters(instruction,
6838 temp_loc,
6839 obj_loc,
6840 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006841 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006842
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006843 // Do an exact check.
6844 if (cls.IsRegister()) {
6845 __ cmpl(temp, cls.AsRegister<Register>());
6846 } else {
6847 DCHECK(cls.IsStackSlot()) << cls;
6848 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6849 }
6850 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006851
6852 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006853 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006854 GenerateReferenceLoadOneRegister(instruction,
6855 temp_loc,
6856 component_offset,
6857 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006858 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006859
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006860 // If the component type is null (i.e. the object not an array), jump to the slow path to
6861 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006862 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006863 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006864
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006865 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006866 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006867 break;
6868 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006869
Calin Juravle98893e12015-10-02 21:05:03 +01006870 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006872 // We cannot directly call the CheckCast runtime entry point
6873 // without resorting to a type checking slow path here (i.e. by
6874 // calling InvokeRuntime directly), as it would require to
6875 // assign fixed registers for the inputs of this HInstanceOf
6876 // instruction (following the runtime calling convention), which
6877 // might be cluttered by the potential first read barrier
6878 // emission at the beginning of this method.
6879 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006880 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006881
6882 case TypeCheckKind::kInterfaceCheck: {
6883 // Fast path for the interface check. Since we compare with a memory location in the inner
6884 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6885 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006886 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006887 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006888 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6889 // doing this.
6890 // /* HeapReference<Class> */ temp = obj->klass_
6891 GenerateReferenceLoadTwoRegisters(instruction,
6892 temp_loc,
6893 obj_loc,
6894 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006895 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006896
6897 // /* HeapReference<Class> */ temp = temp->iftable_
6898 GenerateReferenceLoadTwoRegisters(instruction,
6899 temp_loc,
6900 temp_loc,
6901 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006902 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006903 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006904 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006905 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006906 NearLabel start_loop;
6907 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006908 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006909 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006910 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6911 // Go to next interface if the classes do not match.
6912 __ cmpl(cls.AsRegister<Register>(),
6913 CodeGeneratorX86::ArrayAddress(temp,
6914 maybe_temp2_loc,
6915 TIMES_4,
6916 object_array_data_offset));
6917 __ j(kNotEqual, &start_loop);
6918 } else {
6919 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006920 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006921 break;
6922 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006923 }
6924 __ Bind(&done);
6925
Roland Levillain0d5a2812015-11-13 10:07:31 +00006926 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006927}
6928
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006929void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6930 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006932 InvokeRuntimeCallingConvention calling_convention;
6933 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6934}
6935
6936void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006937 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6938 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006939 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006940 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006941 if (instruction->IsEnter()) {
6942 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6943 } else {
6944 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6945 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006946}
6947
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006948void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6949void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6950void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6951
6952void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6953 LocationSummary* locations =
6954 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6955 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6956 || instruction->GetResultType() == Primitive::kPrimLong);
6957 locations->SetInAt(0, Location::RequiresRegister());
6958 locations->SetInAt(1, Location::Any());
6959 locations->SetOut(Location::SameAsFirstInput());
6960}
6961
6962void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6963 HandleBitwiseOperation(instruction);
6964}
6965
6966void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6967 HandleBitwiseOperation(instruction);
6968}
6969
6970void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6971 HandleBitwiseOperation(instruction);
6972}
6973
6974void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6975 LocationSummary* locations = instruction->GetLocations();
6976 Location first = locations->InAt(0);
6977 Location second = locations->InAt(1);
6978 DCHECK(first.Equals(locations->Out()));
6979
6980 if (instruction->GetResultType() == Primitive::kPrimInt) {
6981 if (second.IsRegister()) {
6982 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006983 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006984 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006985 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 } else {
6987 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006988 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006989 }
6990 } else if (second.IsConstant()) {
6991 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006992 __ andl(first.AsRegister<Register>(),
6993 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006994 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006995 __ orl(first.AsRegister<Register>(),
6996 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006997 } else {
6998 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006999 __ xorl(first.AsRegister<Register>(),
7000 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007001 }
7002 } else {
7003 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007004 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007005 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007006 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007007 } else {
7008 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007009 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007010 }
7011 }
7012 } else {
7013 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7014 if (second.IsRegisterPair()) {
7015 if (instruction->IsAnd()) {
7016 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7017 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7018 } else if (instruction->IsOr()) {
7019 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7020 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7021 } else {
7022 DCHECK(instruction->IsXor());
7023 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7024 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7025 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007026 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007027 if (instruction->IsAnd()) {
7028 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7029 __ andl(first.AsRegisterPairHigh<Register>(),
7030 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7031 } else if (instruction->IsOr()) {
7032 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7033 __ orl(first.AsRegisterPairHigh<Register>(),
7034 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7035 } else {
7036 DCHECK(instruction->IsXor());
7037 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7038 __ xorl(first.AsRegisterPairHigh<Register>(),
7039 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7040 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007041 } else {
7042 DCHECK(second.IsConstant()) << second;
7043 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007044 int32_t low_value = Low32Bits(value);
7045 int32_t high_value = High32Bits(value);
7046 Immediate low(low_value);
7047 Immediate high(high_value);
7048 Register first_low = first.AsRegisterPairLow<Register>();
7049 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007050 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007051 if (low_value == 0) {
7052 __ xorl(first_low, first_low);
7053 } else if (low_value != -1) {
7054 __ andl(first_low, low);
7055 }
7056 if (high_value == 0) {
7057 __ xorl(first_high, first_high);
7058 } else if (high_value != -1) {
7059 __ andl(first_high, high);
7060 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007061 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007062 if (low_value != 0) {
7063 __ orl(first_low, low);
7064 }
7065 if (high_value != 0) {
7066 __ orl(first_high, high);
7067 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007068 } else {
7069 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007070 if (low_value != 0) {
7071 __ xorl(first_low, low);
7072 }
7073 if (high_value != 0) {
7074 __ xorl(first_high, high);
7075 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007076 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007077 }
7078 }
7079}
7080
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007081void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7082 HInstruction* instruction,
7083 Location out,
7084 uint32_t offset,
7085 Location maybe_temp,
7086 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007088 if (read_barrier_option == kWithReadBarrier) {
7089 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007090 if (kUseBakerReadBarrier) {
7091 // Load with fast path based Baker's read barrier.
7092 // /* HeapReference<Object> */ out = *(out + offset)
7093 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007094 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 } else {
7096 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007097 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007098 // in the following move operation, as we will need it for the
7099 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007100 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007101 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102 // /* HeapReference<Object> */ out = *(out + offset)
7103 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007104 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007105 }
7106 } else {
7107 // Plain load with no read barrier.
7108 // /* HeapReference<Object> */ out = *(out + offset)
7109 __ movl(out_reg, Address(out_reg, offset));
7110 __ MaybeUnpoisonHeapReference(out_reg);
7111 }
7112}
7113
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007114void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7115 HInstruction* instruction,
7116 Location out,
7117 Location obj,
7118 uint32_t offset,
7119 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007120 Register out_reg = out.AsRegister<Register>();
7121 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007122 if (read_barrier_option == kWithReadBarrier) {
7123 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007124 if (kUseBakerReadBarrier) {
7125 // Load with fast path based Baker's read barrier.
7126 // /* HeapReference<Object> */ out = *(obj + offset)
7127 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007128 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 } else {
7130 // Load with slow path based read barrier.
7131 // /* HeapReference<Object> */ out = *(obj + offset)
7132 __ movl(out_reg, Address(obj_reg, offset));
7133 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7134 }
7135 } else {
7136 // Plain load with no read barrier.
7137 // /* HeapReference<Object> */ out = *(obj + offset)
7138 __ movl(out_reg, Address(obj_reg, offset));
7139 __ MaybeUnpoisonHeapReference(out_reg);
7140 }
7141}
7142
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007143void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7144 HInstruction* instruction,
7145 Location root,
7146 const Address& address,
7147 Label* fixup_label,
7148 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007149 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007150 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007151 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152 if (kUseBakerReadBarrier) {
7153 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7154 // Baker's read barrier are used:
7155 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007156 // root = obj.field;
7157 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7158 // if (temp != null) {
7159 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007160 // }
7161
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007162 // /* GcRoot<mirror::Object> */ root = *address
7163 __ movl(root_reg, address);
7164 if (fixup_label != nullptr) {
7165 __ Bind(fixup_label);
7166 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007167 static_assert(
7168 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7169 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7170 "have different sizes.");
7171 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7172 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7173 "have different sizes.");
7174
Vladimir Marko953437b2016-08-24 08:30:46 +00007175 // Slow path marking the GC root `root`.
7176 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007177 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007178 codegen_->AddSlowPath(slow_path);
7179
Roland Levillaind966ce72017-02-09 16:20:14 +00007180 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7181 const int32_t entry_point_offset =
7182 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
7183 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7184 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007185 __ j(kNotEqual, slow_path->GetEntryLabel());
7186 __ Bind(slow_path->GetExitLabel());
7187 } else {
7188 // GC root loaded through a slow path for read barriers other
7189 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007190 // /* GcRoot<mirror::Object>* */ root = address
7191 __ leal(root_reg, address);
7192 if (fixup_label != nullptr) {
7193 __ Bind(fixup_label);
7194 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007195 // /* mirror::Object* */ root = root->Read()
7196 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7197 }
7198 } else {
7199 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007200 // /* GcRoot<mirror::Object> */ root = *address
7201 __ movl(root_reg, address);
7202 if (fixup_label != nullptr) {
7203 __ Bind(fixup_label);
7204 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007205 // Note that GC roots are not affected by heap poisoning, thus we
7206 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207 }
7208}
7209
7210void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7211 Location ref,
7212 Register obj,
7213 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214 bool needs_null_check) {
7215 DCHECK(kEmitCompilerReadBarrier);
7216 DCHECK(kUseBakerReadBarrier);
7217
7218 // /* HeapReference<Object> */ ref = *(obj + offset)
7219 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007220 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007221}
7222
7223void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7224 Location ref,
7225 Register obj,
7226 uint32_t data_offset,
7227 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007228 bool needs_null_check) {
7229 DCHECK(kEmitCompilerReadBarrier);
7230 DCHECK(kUseBakerReadBarrier);
7231
Roland Levillain3d312422016-06-23 13:53:42 +01007232 static_assert(
7233 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7234 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007235 // /* HeapReference<Object> */ ref =
7236 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007237 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007238 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007239}
7240
7241void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7242 Location ref,
7243 Register obj,
7244 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007245 bool needs_null_check,
7246 bool always_update_field,
7247 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007248 DCHECK(kEmitCompilerReadBarrier);
7249 DCHECK(kUseBakerReadBarrier);
7250
7251 // In slow path based read barriers, the read barrier call is
7252 // inserted after the original load. However, in fast path based
7253 // Baker's read barriers, we need to perform the load of
7254 // mirror::Object::monitor_ *before* the original reference load.
7255 // This load-load ordering is required by the read barrier.
7256 // The fast path/slow path (for Baker's algorithm) should look like:
7257 //
7258 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7259 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7260 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007261 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007262 // if (is_gray) {
7263 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7264 // }
7265 //
7266 // Note: the original implementation in ReadBarrier::Barrier is
7267 // slightly more complex as:
7268 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007269 // the high-bits of rb_state, which are expected to be all zeroes
7270 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7271 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007272 // - it performs additional checks that we do not do here for
7273 // performance reasons.
7274
7275 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007276 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7277
Vladimir Marko953437b2016-08-24 08:30:46 +00007278 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007279 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7280 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007281 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7282 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7283 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7284
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007285 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007286 // ref = ReadBarrier::Mark(ref);
7287 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7288 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007289 if (needs_null_check) {
7290 MaybeRecordImplicitNullCheck(instruction);
7291 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007292
7293 // Load fence to prevent load-load reordering.
7294 // Note that this is a no-op, thanks to the x86 memory model.
7295 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7296
7297 // The actual reference load.
7298 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007299 __ movl(ref_reg, src); // Flags are unaffected.
7300
7301 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7302 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007303 SlowPathCode* slow_path;
7304 if (always_update_field) {
7305 DCHECK(temp != nullptr);
7306 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7307 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7308 } else {
7309 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7310 instruction, ref, /* unpoison_ref_before_marking */ true);
7311 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007312 AddSlowPath(slow_path);
7313
7314 // We have done the "if" of the gray bit check above, now branch based on the flags.
7315 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007316
7317 // Object* ref = ref_addr->AsMirrorPtr()
7318 __ MaybeUnpoisonHeapReference(ref_reg);
7319
Roland Levillain7c1559a2015-12-15 10:55:36 +00007320 __ Bind(slow_path->GetExitLabel());
7321}
7322
7323void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7324 Location out,
7325 Location ref,
7326 Location obj,
7327 uint32_t offset,
7328 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 DCHECK(kEmitCompilerReadBarrier);
7330
Roland Levillain7c1559a2015-12-15 10:55:36 +00007331 // Insert a slow path based read barrier *after* the reference load.
7332 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007333 // If heap poisoning is enabled, the unpoisoning of the loaded
7334 // reference will be carried out by the runtime within the slow
7335 // path.
7336 //
7337 // Note that `ref` currently does not get unpoisoned (when heap
7338 // poisoning is enabled), which is alright as the `ref` argument is
7339 // not used by the artReadBarrierSlow entry point.
7340 //
7341 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7342 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7343 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7344 AddSlowPath(slow_path);
7345
Roland Levillain0d5a2812015-11-13 10:07:31 +00007346 __ jmp(slow_path->GetEntryLabel());
7347 __ Bind(slow_path->GetExitLabel());
7348}
7349
Roland Levillain7c1559a2015-12-15 10:55:36 +00007350void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7351 Location out,
7352 Location ref,
7353 Location obj,
7354 uint32_t offset,
7355 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007357 // Baker's read barriers shall be handled by the fast path
7358 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7359 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007360 // If heap poisoning is enabled, unpoisoning will be taken care of
7361 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007362 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007363 } else if (kPoisonHeapReferences) {
7364 __ UnpoisonHeapReference(out.AsRegister<Register>());
7365 }
7366}
7367
Roland Levillain7c1559a2015-12-15 10:55:36 +00007368void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7369 Location out,
7370 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007371 DCHECK(kEmitCompilerReadBarrier);
7372
Roland Levillain7c1559a2015-12-15 10:55:36 +00007373 // Insert a slow path based read barrier *after* the GC root load.
7374 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007375 // Note that GC roots are not affected by heap poisoning, so we do
7376 // not need to do anything special for this here.
7377 SlowPathCode* slow_path =
7378 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7379 AddSlowPath(slow_path);
7380
Roland Levillain0d5a2812015-11-13 10:07:31 +00007381 __ jmp(slow_path->GetEntryLabel());
7382 __ Bind(slow_path->GetExitLabel());
7383}
7384
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007385void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007386 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007387 LOG(FATAL) << "Unreachable";
7388}
7389
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007390void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007391 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007392 LOG(FATAL) << "Unreachable";
7393}
7394
Mark Mendellfe57faa2015-09-18 09:26:15 -04007395// Simple implementation of packed switch - generate cascaded compare/jumps.
7396void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7397 LocationSummary* locations =
7398 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7399 locations->SetInAt(0, Location::RequiresRegister());
7400}
7401
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007402void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7403 int32_t lower_bound,
7404 uint32_t num_entries,
7405 HBasicBlock* switch_block,
7406 HBasicBlock* default_block) {
7407 // Figure out the correct compare values and jump conditions.
7408 // Handle the first compare/branch as a special case because it might
7409 // jump to the default case.
7410 DCHECK_GT(num_entries, 2u);
7411 Condition first_condition;
7412 uint32_t index;
7413 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7414 if (lower_bound != 0) {
7415 first_condition = kLess;
7416 __ cmpl(value_reg, Immediate(lower_bound));
7417 __ j(first_condition, codegen_->GetLabelOf(default_block));
7418 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007419
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007420 index = 1;
7421 } else {
7422 // Handle all the compare/jumps below.
7423 first_condition = kBelow;
7424 index = 0;
7425 }
7426
7427 // Handle the rest of the compare/jumps.
7428 for (; index + 1 < num_entries; index += 2) {
7429 int32_t compare_to_value = lower_bound + index + 1;
7430 __ cmpl(value_reg, Immediate(compare_to_value));
7431 // Jump to successors[index] if value < case_value[index].
7432 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7433 // Jump to successors[index + 1] if value == case_value[index + 1].
7434 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7435 }
7436
7437 if (index != num_entries) {
7438 // There are an odd number of entries. Handle the last one.
7439 DCHECK_EQ(index + 1, num_entries);
7440 __ cmpl(value_reg, Immediate(lower_bound + index));
7441 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007442 }
7443
7444 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007445 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7446 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007447 }
7448}
7449
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007450void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7451 int32_t lower_bound = switch_instr->GetStartValue();
7452 uint32_t num_entries = switch_instr->GetNumEntries();
7453 LocationSummary* locations = switch_instr->GetLocations();
7454 Register value_reg = locations->InAt(0).AsRegister<Register>();
7455
7456 GenPackedSwitchWithCompares(value_reg,
7457 lower_bound,
7458 num_entries,
7459 switch_instr->GetBlock(),
7460 switch_instr->GetDefaultBlock());
7461}
7462
Mark Mendell805b3b52015-09-18 14:10:29 -04007463void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7464 LocationSummary* locations =
7465 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7466 locations->SetInAt(0, Location::RequiresRegister());
7467
7468 // Constant area pointer.
7469 locations->SetInAt(1, Location::RequiresRegister());
7470
7471 // And the temporary we need.
7472 locations->AddTemp(Location::RequiresRegister());
7473}
7474
7475void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7476 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007477 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007478 LocationSummary* locations = switch_instr->GetLocations();
7479 Register value_reg = locations->InAt(0).AsRegister<Register>();
7480 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7481
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007482 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7483 GenPackedSwitchWithCompares(value_reg,
7484 lower_bound,
7485 num_entries,
7486 switch_instr->GetBlock(),
7487 default_block);
7488 return;
7489 }
7490
Mark Mendell805b3b52015-09-18 14:10:29 -04007491 // Optimizing has a jump area.
7492 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7493 Register constant_area = locations->InAt(1).AsRegister<Register>();
7494
7495 // Remove the bias, if needed.
7496 if (lower_bound != 0) {
7497 __ leal(temp_reg, Address(value_reg, -lower_bound));
7498 value_reg = temp_reg;
7499 }
7500
7501 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007502 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007503 __ cmpl(value_reg, Immediate(num_entries - 1));
7504 __ j(kAbove, codegen_->GetLabelOf(default_block));
7505
7506 // We are in the range of the table.
7507 // Load (target-constant_area) from the jump table, indexing by the value.
7508 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7509
7510 // Compute the actual target address by adding in constant_area.
7511 __ addl(temp_reg, constant_area);
7512
7513 // And jump.
7514 __ jmp(temp_reg);
7515}
7516
Mark Mendell0616ae02015-04-17 12:49:27 -04007517void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7518 HX86ComputeBaseMethodAddress* insn) {
7519 LocationSummary* locations =
7520 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7521 locations->SetOut(Location::RequiresRegister());
7522}
7523
7524void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7525 HX86ComputeBaseMethodAddress* insn) {
7526 LocationSummary* locations = insn->GetLocations();
7527 Register reg = locations->Out().AsRegister<Register>();
7528
7529 // Generate call to next instruction.
7530 Label next_instruction;
7531 __ call(&next_instruction);
7532 __ Bind(&next_instruction);
7533
7534 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007535 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007536
7537 // Grab the return address off the stack.
7538 __ popl(reg);
7539}
7540
7541void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7542 HX86LoadFromConstantTable* insn) {
7543 LocationSummary* locations =
7544 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7545
7546 locations->SetInAt(0, Location::RequiresRegister());
7547 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7548
7549 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007550 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007551 return;
7552 }
7553
7554 switch (insn->GetType()) {
7555 case Primitive::kPrimFloat:
7556 case Primitive::kPrimDouble:
7557 locations->SetOut(Location::RequiresFpuRegister());
7558 break;
7559
7560 case Primitive::kPrimInt:
7561 locations->SetOut(Location::RequiresRegister());
7562 break;
7563
7564 default:
7565 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7566 }
7567}
7568
7569void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007570 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007571 return;
7572 }
7573
7574 LocationSummary* locations = insn->GetLocations();
7575 Location out = locations->Out();
7576 Register const_area = locations->InAt(0).AsRegister<Register>();
7577 HConstant *value = insn->GetConstant();
7578
7579 switch (insn->GetType()) {
7580 case Primitive::kPrimFloat:
7581 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007582 codegen_->LiteralFloatAddress(
7583 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007584 break;
7585
7586 case Primitive::kPrimDouble:
7587 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007588 codegen_->LiteralDoubleAddress(
7589 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007590 break;
7591
7592 case Primitive::kPrimInt:
7593 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007594 codegen_->LiteralInt32Address(
7595 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007596 break;
7597
7598 default:
7599 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7600 }
7601}
7602
Mark Mendell0616ae02015-04-17 12:49:27 -04007603/**
7604 * Class to handle late fixup of offsets into constant area.
7605 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007606class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007607 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007608 RIPFixup(CodeGeneratorX86& codegen,
7609 HX86ComputeBaseMethodAddress* base_method_address,
7610 size_t offset)
7611 : codegen_(&codegen),
7612 base_method_address_(base_method_address),
7613 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007614
7615 protected:
7616 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7617
7618 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007619 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007620
7621 private:
7622 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7623 // Patch the correct offset for the instruction. The place to patch is the
7624 // last 4 bytes of the instruction.
7625 // The value to patch is the distance from the offset in the constant area
7626 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007627 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007628 int32_t relative_position =
7629 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007630
7631 // Patch in the right value.
7632 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7633 }
7634
Mark Mendell0616ae02015-04-17 12:49:27 -04007635 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007636 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007637};
7638
Mark Mendell805b3b52015-09-18 14:10:29 -04007639/**
7640 * Class to handle late fixup of offsets to a jump table that will be created in the
7641 * constant area.
7642 */
7643class JumpTableRIPFixup : public RIPFixup {
7644 public:
7645 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007646 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7647 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007648
7649 void CreateJumpTable() {
7650 X86Assembler* assembler = codegen_->GetAssembler();
7651
7652 // Ensure that the reference to the jump table has the correct offset.
7653 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7654 SetOffset(offset_in_constant_table);
7655
7656 // The label values in the jump table are computed relative to the
7657 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007658 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007659
7660 // Populate the jump table with the correct values for the jump table.
7661 int32_t num_entries = switch_instr_->GetNumEntries();
7662 HBasicBlock* block = switch_instr_->GetBlock();
7663 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7664 // The value that we want is the target offset - the position of the table.
7665 for (int32_t i = 0; i < num_entries; i++) {
7666 HBasicBlock* b = successors[i];
7667 Label* l = codegen_->GetLabelOf(b);
7668 DCHECK(l->IsBound());
7669 int32_t offset_to_block = l->Position() - relative_offset;
7670 assembler->AppendInt32(offset_to_block);
7671 }
7672 }
7673
7674 private:
7675 const HX86PackedSwitch* switch_instr_;
7676};
7677
7678void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7679 // Generate the constant area if needed.
7680 X86Assembler* assembler = GetAssembler();
7681 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7682 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7683 // byte values.
7684 assembler->Align(4, 0);
7685 constant_area_start_ = assembler->CodeSize();
7686
7687 // Populate any jump tables.
7688 for (auto jump_table : fixups_to_jump_tables_) {
7689 jump_table->CreateJumpTable();
7690 }
7691
7692 // And now add the constant area to the generated code.
7693 assembler->AddConstantArea();
7694 }
7695
7696 // And finish up.
7697 CodeGenerator::Finalize(allocator);
7698}
7699
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007700Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7701 HX86ComputeBaseMethodAddress* method_base,
7702 Register reg) {
7703 AssemblerFixup* fixup =
7704 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007705 return Address(reg, kDummy32BitOffset, fixup);
7706}
7707
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007708Address CodeGeneratorX86::LiteralFloatAddress(float v,
7709 HX86ComputeBaseMethodAddress* method_base,
7710 Register reg) {
7711 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007712 return Address(reg, kDummy32BitOffset, fixup);
7713}
7714
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007715Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7716 HX86ComputeBaseMethodAddress* method_base,
7717 Register reg) {
7718 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007719 return Address(reg, kDummy32BitOffset, fixup);
7720}
7721
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007722Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7723 HX86ComputeBaseMethodAddress* method_base,
7724 Register reg) {
7725 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007726 return Address(reg, kDummy32BitOffset, fixup);
7727}
7728
Aart Bika19616e2016-02-01 18:57:58 -08007729void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7730 if (value == 0) {
7731 __ xorl(dest, dest);
7732 } else {
7733 __ movl(dest, Immediate(value));
7734 }
7735}
7736
7737void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7738 if (value == 0) {
7739 __ testl(dest, dest);
7740 } else {
7741 __ cmpl(dest, Immediate(value));
7742 }
7743}
7744
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007745void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7746 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007747 GenerateIntCompare(lhs_reg, rhs);
7748}
7749
7750void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007751 if (rhs.IsConstant()) {
7752 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007753 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007754 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007755 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007756 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007757 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007758 }
7759}
7760
7761Address CodeGeneratorX86::ArrayAddress(Register obj,
7762 Location index,
7763 ScaleFactor scale,
7764 uint32_t data_offset) {
7765 return index.IsConstant() ?
7766 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7767 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7768}
7769
Mark Mendell805b3b52015-09-18 14:10:29 -04007770Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7771 Register reg,
7772 Register value) {
7773 // Create a fixup to be used to create and address the jump table.
7774 JumpTableRIPFixup* table_fixup =
7775 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7776
7777 // We have to populate the jump tables.
7778 fixups_to_jump_tables_.push_back(table_fixup);
7779
7780 // We want a scaled address, as we are extracting the correct offset from the table.
7781 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7782}
7783
Andreas Gampe85b62f22015-09-09 13:15:38 -07007784// TODO: target as memory.
7785void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7786 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007787 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007788 return;
7789 }
7790
7791 DCHECK_NE(type, Primitive::kPrimVoid);
7792
7793 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7794 if (target.Equals(return_loc)) {
7795 return;
7796 }
7797
7798 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7799 // with the else branch.
7800 if (type == Primitive::kPrimLong) {
7801 HParallelMove parallel_move(GetGraph()->GetArena());
7802 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7803 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7804 GetMoveResolver()->EmitNativeCode(&parallel_move);
7805 } else {
7806 // Let the parallel move resolver take care of all of this.
7807 HParallelMove parallel_move(GetGraph()->GetArena());
7808 parallel_move.AddMove(return_loc, target, type, nullptr);
7809 GetMoveResolver()->EmitNativeCode(&parallel_move);
7810 }
7811}
7812
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007813void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7814 const uint8_t* roots_data,
7815 const PatchInfo<Label>& info,
7816 uint64_t index_in_table) const {
7817 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7818 uintptr_t address =
7819 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7820 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7821 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7822 dchecked_integral_cast<uint32_t>(address);
7823}
7824
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007825void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7826 for (const PatchInfo<Label>& info : jit_string_patches_) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007827 const auto& it = jit_string_roots_.find(
7828 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007829 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007830 PatchJitRootUse(code, roots_data, info, it->second);
7831 }
7832
7833 for (const PatchInfo<Label>& info : jit_class_patches_) {
7834 const auto& it = jit_class_roots_.find(
7835 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7836 DCHECK(it != jit_class_roots_.end());
7837 PatchJitRootUse(code, roots_data, info, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007838 }
7839}
7840
Roland Levillain4d027112015-07-01 15:41:14 +01007841#undef __
7842
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007843} // namespace x86
7844} // namespace art