blob: 99b7793c81b0c66d46b3037e6ef906fbfc33d53e [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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace x86 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050045static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000050
Roland Levillain7cbd27f2016-08-11 23:53:33 +010051// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
52#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070053#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Andreas Gampe85b62f22015-09-09 13:15:38 -070055class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000057 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058
Alexandre Rames2ed20af2015-03-06 13:55:35 +000059 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010060 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000062 if (instruction_->CanThrowIntoCatchBlock()) {
63 // Live registers will be restored in the catch block if caught.
64 SaveLiveRegisters(codegen, instruction_->GetLocations());
65 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010066 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010067 instruction_,
68 instruction_->GetDexPc(),
69 this);
Roland Levillain888d0672015-11-23 18:53:50 +000070 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 }
72
Alexandre Rames8158f282015-08-07 10:26:17 +010073 bool IsFatal() const OVERRIDE { return true; }
74
Alexandre Rames9931f312015-06-19 14:47:01 +010075 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
76
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
79};
80
Andreas Gampe85b62f22015-09-09 13:15:38 -070081class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000082 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000083 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000084
Alexandre Rames2ed20af2015-03-06 13:55:35 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000087 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010088 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000089 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000090 }
91
Alexandre Rames8158f282015-08-07 10:26:17 +010092 bool IsFatal() const OVERRIDE { return true; }
93
Alexandre Rames9931f312015-06-19 14:47:01 +010094 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
95
Calin Juravled0d48522014-11-04 16:40:20 +000096 private:
Calin Juravled0d48522014-11-04 16:40:20 +000097 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
98};
99
Andreas Gampe85b62f22015-09-09 13:15:38 -0700100class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
103 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000104
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000105 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000107 if (is_div_) {
108 __ negl(reg_);
109 } else {
110 __ movl(reg_, Immediate(0));
111 }
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ jmp(GetExitLabel());
113 }
114
Alexandre Rames9931f312015-06-19 14:47:01 +0100115 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
116
Calin Juravled0d48522014-11-04 16:40:20 +0000117 private:
118 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 bool is_div_;
120 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000121};
122
Andreas Gampe85b62f22015-09-09 13:15:38 -0700123class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100124 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000125 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000127 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100129 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000131 // We're moving two locations to locations that could overlap, so we need a parallel
132 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000133 if (instruction_->CanThrowIntoCatchBlock()) {
134 // Live registers will be restored in the catch block if caught.
135 SaveLiveRegisters(codegen, instruction_->GetLocations());
136 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400137
138 // Are we using an array length from memory?
139 HInstruction* array_length = instruction_->InputAt(1);
140 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400142 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
143 // Load the array length into our temporary.
144 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
145 Location array_loc = array_length->GetLocations()->InAt(0);
146 Address array_len(array_loc.AsRegister<Register>(), len_offset);
147 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
148 // Check for conflicts with index.
149 if (length_loc.Equals(locations->InAt(0))) {
150 // We know we aren't using parameter 2.
151 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
152 }
153 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700154 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100155 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700156 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400157 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000158 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100159 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000160 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100161 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400162 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100163 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
164 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100165 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
166 ? kQuickThrowStringBounds
167 : kQuickThrowArrayBounds;
168 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100169 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000170 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 }
172
Alexandre Rames8158f282015-08-07 10:26:17 +0100173 bool IsFatal() const OVERRIDE { return true; }
174
Alexandre Rames9931f312015-06-19 14:47:01 +0100175 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
176
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100178 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
179};
180
Andreas Gampe85b62f22015-09-09 13:15:38 -0700181class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000184 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700187 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700190 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100191 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000192 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700193 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 if (successor_ == nullptr) {
195 __ jmp(GetReturnLabel());
196 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100197 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 }
200
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100201 Label* GetReturnLabel() {
202 DCHECK(successor_ == nullptr);
203 return &return_label_;
204 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100206 HBasicBlock* GetSuccessor() const {
207 return successor_;
208 }
209
Alexandre Rames9931f312015-06-19 14:47:01 +0100210 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
211
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100213 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000214 Label return_label_;
215
216 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
217};
218
Vladimir Markoaad75c62016-10-03 08:46:48 +0000219class LoadStringSlowPathX86 : public SlowPathCode {
220 public:
221 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
222
223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
224 LocationSummary* locations = instruction_->GetLocations();
225 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
226
227 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
228 __ Bind(GetEntryLabel());
229 SaveLiveRegisters(codegen, locations);
230
231 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000232 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
233 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000234 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
235 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
236 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
237 RestoreLiveRegisters(codegen, locations);
238
239 // Store the resolved String to the BSS entry.
240 Register method_address = locations->InAt(0).AsRegister<Register>();
241 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
242 locations->Out().AsRegister<Register>());
243 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
244 __ Bind(fixup_label);
245
246 __ jmp(GetExitLabel());
247 }
248
249 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
250
251 private:
252 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
253};
254
Andreas Gampe85b62f22015-09-09 13:15:38 -0700255class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 public:
257 LoadClassSlowPathX86(HLoadClass* cls,
258 HInstruction* at,
259 uint32_t dex_pc,
260 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000261 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
263 }
264
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000265 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000266 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000267 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
268 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270
271 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 dex::TypeIndex type_index = cls_->GetTypeIndex();
273 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100274 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
275 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 instruction_,
277 dex_pc_,
278 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 if (do_clinit_) {
280 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
281 } else {
282 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
283 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000284
285 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 Location out = locations->Out();
287 if (out.IsValid()) {
288 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
289 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000292 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
293 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
294 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
295 DCHECK(out.IsValid());
296 Register method_address = locations->InAt(0).AsRegister<Register>();
297 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
298 locations->Out().AsRegister<Register>());
299 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
300 __ Bind(fixup_label);
301 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000302 __ jmp(GetExitLabel());
303 }
304
Alexandre Rames9931f312015-06-19 14:47:01 +0100305 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
306
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000307 private:
308 // The class this slow path will load.
309 HLoadClass* const cls_;
310
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 // The dex PC of `at_`.
312 const uint32_t dex_pc_;
313
314 // Whether to initialize the class.
315 const bool do_clinit_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
318};
319
Andreas Gampe85b62f22015-09-09 13:15:38 -0700320class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000323 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000325 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 DCHECK(instruction_->IsCheckCast()
328 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
331 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333 if (!is_fatal_) {
334 SaveLiveRegisters(codegen, locations);
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
337 // We're moving two locations to locations that could overlap, so we need a parallel
338 // move resolver.
339 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800340 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800341 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
342 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800343 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800344 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
345 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100347 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100348 instruction_,
349 instruction_->GetDexPc(),
350 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800351 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352 } else {
353 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
355 instruction_,
356 instruction_->GetDexPc(),
357 this);
358 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 }
360
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 if (!is_fatal_) {
362 if (instruction_->IsInstanceOf()) {
363 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
364 }
365 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367 __ jmp(GetExitLabel());
368 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000369 }
370
Alexandre Rames9931f312015-06-19 14:47:01 +0100371 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100373
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000374 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000376
377 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
378};
379
Andreas Gampe85b62f22015-09-09 13:15:38 -0700380class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 public:
Aart Bik42249c32016-01-07 15:33:50 -0800382 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100388 LocationSummary* locations = instruction_->GetLocations();
389 SaveLiveRegisters(codegen, locations);
390 InvokeRuntimeCallingConvention calling_convention;
391 x86_codegen->Load32BitValue(
392 calling_convention.GetRegisterAt(0),
393 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100394 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100395 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396 }
397
Alexandre Rames9931f312015-06-19 14:47:01 +0100398 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
399
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700401 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
402};
403
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100404class ArraySetSlowPathX86 : public SlowPathCode {
405 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000406 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407
408 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
409 LocationSummary* locations = instruction_->GetLocations();
410 __ Bind(GetEntryLabel());
411 SaveLiveRegisters(codegen, locations);
412
413 InvokeRuntimeCallingConvention calling_convention;
414 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
415 parallel_move.AddMove(
416 locations->InAt(0),
417 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
418 Primitive::kPrimNot,
419 nullptr);
420 parallel_move.AddMove(
421 locations->InAt(1),
422 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
423 Primitive::kPrimInt,
424 nullptr);
425 parallel_move.AddMove(
426 locations->InAt(2),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
428 Primitive::kPrimNot,
429 nullptr);
430 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
431
432 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100433 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000434 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435 RestoreLiveRegisters(codegen, locations);
436 __ jmp(GetExitLabel());
437 }
438
439 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
440
441 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100442 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
443};
444
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100445// Slow path marking an object reference `ref` during a read
446// barrier. The field `obj.field` in the object `obj` holding this
447// reference does not get updated by this slow path after marking (see
448// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
449//
450// This means that after the execution of this slow path, `ref` will
451// always be up-to-date, but `obj.field` may not; i.e., after the
452// flip, `ref` will be a to-space reference, but `obj.field` will
453// probably still be a from-space reference (unless it gets updated by
454// another thread, or if another thread installed another object
455// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000456class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
457 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100458 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
459 Location ref,
460 bool unpoison_ref_before_marking)
461 : SlowPathCode(instruction),
462 ref_(ref),
463 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000464 DCHECK(kEmitCompilerReadBarrier);
465 }
466
467 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
468
469 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
470 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100471 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000472 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100473 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 DCHECK(instruction_->IsInstanceFieldGet() ||
475 instruction_->IsStaticFieldGet() ||
476 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100477 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478 instruction_->IsLoadClass() ||
479 instruction_->IsLoadString() ||
480 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100481 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100482 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
483 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 << "Unexpected instruction in read barrier marking slow path: "
485 << instruction_->DebugName();
486
487 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100488 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000489 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000491 }
Roland Levillain4359e612016-07-20 11:32:19 +0100492 // No need to save live registers; it's taken care of by the
493 // entrypoint. Also, there is no need to update the stack mask,
494 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000495 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100496 DCHECK_NE(ref_reg, ESP);
497 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100498 // "Compact" slow path, saving two moves.
499 //
500 // Instead of using the standard runtime calling convention (input
501 // and output in EAX):
502 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100504 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100506 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // of a dedicated entrypoint:
509 //
510 // rX <- ReadBarrierMarkRegX(rX)
511 //
Roland Levillain97c46462017-05-11 14:04:03 +0100512 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100513 // This runtime call does not require a stack map.
514 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000515 __ jmp(GetExitLabel());
516 }
517
518 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100519 // The location (register) of the marked object reference.
520 const Location ref_;
521 // Should the reference in `ref_` be unpoisoned prior to marking it?
522 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000523
524 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
525};
526
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100527// Slow path marking an object reference `ref` during a read barrier,
528// and if needed, atomically updating the field `obj.field` in the
529// object `obj` holding this reference after marking (contrary to
530// ReadBarrierMarkSlowPathX86 above, which never tries to update
531// `obj.field`).
532//
533// This means that after the execution of this slow path, both `ref`
534// and `obj.field` will be up-to-date; i.e., after the flip, both will
535// hold the same to-space reference (unless another thread installed
536// another object reference (different from `ref`) in `obj.field`).
537class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
538 public:
539 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
540 Location ref,
541 Register obj,
542 const Address& field_addr,
543 bool unpoison_ref_before_marking,
544 Register temp)
545 : SlowPathCode(instruction),
546 ref_(ref),
547 obj_(obj),
548 field_addr_(field_addr),
549 unpoison_ref_before_marking_(unpoison_ref_before_marking),
550 temp_(temp) {
551 DCHECK(kEmitCompilerReadBarrier);
552 }
553
554 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
555
556 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
557 LocationSummary* locations = instruction_->GetLocations();
558 Register ref_reg = ref_.AsRegister<Register>();
559 DCHECK(locations->CanCall());
560 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
561 // This slow path is only used by the UnsafeCASObject intrinsic.
562 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
563 << "Unexpected instruction in read barrier marking and field updating slow path: "
564 << instruction_->DebugName();
565 DCHECK(instruction_->GetLocations()->Intrinsified());
566 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
567
568 __ Bind(GetEntryLabel());
569 if (unpoison_ref_before_marking_) {
570 // Object* ref = ref_addr->AsMirrorPtr()
571 __ MaybeUnpoisonHeapReference(ref_reg);
572 }
573
574 // Save the old (unpoisoned) reference.
575 __ movl(temp_, ref_reg);
576
577 // No need to save live registers; it's taken care of by the
578 // entrypoint. Also, there is no need to update the stack mask,
579 // as this runtime call will not trigger a garbage collection.
580 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
581 DCHECK_NE(ref_reg, ESP);
582 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
583 // "Compact" slow path, saving two moves.
584 //
585 // Instead of using the standard runtime calling convention (input
586 // and output in EAX):
587 //
588 // EAX <- ref
589 // EAX <- ReadBarrierMark(EAX)
590 // ref <- EAX
591 //
592 // we just use rX (the register containing `ref`) as input and output
593 // of a dedicated entrypoint:
594 //
595 // rX <- ReadBarrierMarkRegX(rX)
596 //
Roland Levillain97c46462017-05-11 14:04:03 +0100597 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100598 // This runtime call does not require a stack map.
599 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
600
601 // If the new reference is different from the old reference,
602 // update the field in the holder (`*field_addr`).
603 //
604 // Note that this field could also hold a different object, if
605 // another thread had concurrently changed it. In that case, the
606 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
607 // operation below would abort the CAS, leaving the field as-is.
608 NearLabel done;
609 __ cmpl(temp_, ref_reg);
610 __ j(kEqual, &done);
611
612 // Update the the holder's field atomically. This may fail if
613 // mutator updates before us, but it's OK. This is achieved
614 // using a strong compare-and-set (CAS) operation with relaxed
615 // memory synchronization ordering, where the expected value is
616 // the old reference and the desired value is the new reference.
617 // This operation is implemented with a 32-bit LOCK CMPXLCHG
618 // instruction, which requires the expected value (the old
619 // reference) to be in EAX. Save EAX beforehand, and move the
620 // expected value (stored in `temp_`) into EAX.
621 __ pushl(EAX);
622 __ movl(EAX, temp_);
623
624 // Convenience aliases.
625 Register base = obj_;
626 Register expected = EAX;
627 Register value = ref_reg;
628
629 bool base_equals_value = (base == value);
630 if (kPoisonHeapReferences) {
631 if (base_equals_value) {
632 // If `base` and `value` are the same register location, move
633 // `value` to a temporary register. This way, poisoning
634 // `value` won't invalidate `base`.
635 value = temp_;
636 __ movl(value, base);
637 }
638
639 // Check that the register allocator did not assign the location
640 // of `expected` (EAX) to `value` nor to `base`, so that heap
641 // poisoning (when enabled) works as intended below.
642 // - If `value` were equal to `expected`, both references would
643 // be poisoned twice, meaning they would not be poisoned at
644 // all, as heap poisoning uses address negation.
645 // - If `base` were equal to `expected`, poisoning `expected`
646 // would invalidate `base`.
647 DCHECK_NE(value, expected);
648 DCHECK_NE(base, expected);
649
650 __ PoisonHeapReference(expected);
651 __ PoisonHeapReference(value);
652 }
653
654 __ LockCmpxchgl(field_addr_, value);
655
656 // If heap poisoning is enabled, we need to unpoison the values
657 // that were poisoned earlier.
658 if (kPoisonHeapReferences) {
659 if (base_equals_value) {
660 // `value` has been moved to a temporary register, no need
661 // to unpoison it.
662 } else {
663 __ UnpoisonHeapReference(value);
664 }
665 // No need to unpoison `expected` (EAX), as it is be overwritten below.
666 }
667
668 // Restore EAX.
669 __ popl(EAX);
670
671 __ Bind(&done);
672 __ jmp(GetExitLabel());
673 }
674
675 private:
676 // The location (register) of the marked object reference.
677 const Location ref_;
678 // The register containing the object holding the marked object reference field.
679 const Register obj_;
680 // The address of the marked reference field. The base of this address must be `obj_`.
681 const Address field_addr_;
682
683 // Should the reference in `ref_` be unpoisoned prior to marking it?
684 const bool unpoison_ref_before_marking_;
685
686 const Register temp_;
687
688 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
689};
690
Roland Levillain0d5a2812015-11-13 10:07:31 +0000691// Slow path generating a read barrier for a heap reference.
692class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
693 public:
694 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
695 Location out,
696 Location ref,
697 Location obj,
698 uint32_t offset,
699 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000700 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000701 out_(out),
702 ref_(ref),
703 obj_(obj),
704 offset_(offset),
705 index_(index) {
706 DCHECK(kEmitCompilerReadBarrier);
707 // If `obj` is equal to `out` or `ref`, it means the initial object
708 // has been overwritten by (or after) the heap object reference load
709 // to be instrumented, e.g.:
710 //
711 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000712 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000713 //
714 // In that case, we have lost the information about the original
715 // object, and the emitted read barrier cannot work properly.
716 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
717 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
718 }
719
720 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
721 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
722 LocationSummary* locations = instruction_->GetLocations();
723 Register reg_out = out_.AsRegister<Register>();
724 DCHECK(locations->CanCall());
725 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100726 DCHECK(instruction_->IsInstanceFieldGet() ||
727 instruction_->IsStaticFieldGet() ||
728 instruction_->IsArrayGet() ||
729 instruction_->IsInstanceOf() ||
730 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700731 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000732 << "Unexpected instruction in read barrier for heap reference slow path: "
733 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000734
735 __ Bind(GetEntryLabel());
736 SaveLiveRegisters(codegen, locations);
737
738 // We may have to change the index's value, but as `index_` is a
739 // constant member (like other "inputs" of this slow path),
740 // introduce a copy of it, `index`.
741 Location index = index_;
742 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100743 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000744 if (instruction_->IsArrayGet()) {
745 // Compute the actual memory offset and store it in `index`.
746 Register index_reg = index_.AsRegister<Register>();
747 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
748 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
749 // We are about to change the value of `index_reg` (see the
750 // calls to art::x86::X86Assembler::shll and
751 // art::x86::X86Assembler::AddImmediate below), but it has
752 // not been saved by the previous call to
753 // art::SlowPathCode::SaveLiveRegisters, as it is a
754 // callee-save register --
755 // art::SlowPathCode::SaveLiveRegisters does not consider
756 // callee-save registers, as it has been designed with the
757 // assumption that callee-save registers are supposed to be
758 // handled by the called function. So, as a callee-save
759 // register, `index_reg` _would_ eventually be saved onto
760 // the stack, but it would be too late: we would have
761 // changed its value earlier. Therefore, we manually save
762 // it here into another freely available register,
763 // `free_reg`, chosen of course among the caller-save
764 // registers (as a callee-save `free_reg` register would
765 // exhibit the same problem).
766 //
767 // Note we could have requested a temporary register from
768 // the register allocator instead; but we prefer not to, as
769 // this is a slow path, and we know we can find a
770 // caller-save register that is available.
771 Register free_reg = FindAvailableCallerSaveRegister(codegen);
772 __ movl(free_reg, index_reg);
773 index_reg = free_reg;
774 index = Location::RegisterLocation(index_reg);
775 } else {
776 // The initial register stored in `index_` has already been
777 // saved in the call to art::SlowPathCode::SaveLiveRegisters
778 // (as it is not a callee-save register), so we can freely
779 // use it.
780 }
781 // Shifting the index value contained in `index_reg` by the scale
782 // factor (2) cannot overflow in practice, as the runtime is
783 // unable to allocate object arrays with a size larger than
784 // 2^26 - 1 (that is, 2^28 - 4 bytes).
785 __ shll(index_reg, Immediate(TIMES_4));
786 static_assert(
787 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
788 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
789 __ AddImmediate(index_reg, Immediate(offset_));
790 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100791 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
792 // intrinsics, `index_` is not shifted by a scale factor of 2
793 // (as in the case of ArrayGet), as it is actually an offset
794 // to an object field within an object.
795 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000796 DCHECK(instruction_->GetLocations()->Intrinsified());
797 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
798 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
799 << instruction_->AsInvoke()->GetIntrinsic();
800 DCHECK_EQ(offset_, 0U);
801 DCHECK(index_.IsRegisterPair());
802 // UnsafeGet's offset location is a register pair, the low
803 // part contains the correct offset.
804 index = index_.ToLow();
805 }
806 }
807
808 // We're moving two or three locations to locations that could
809 // overlap, so we need a parallel move resolver.
810 InvokeRuntimeCallingConvention calling_convention;
811 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
812 parallel_move.AddMove(ref_,
813 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
814 Primitive::kPrimNot,
815 nullptr);
816 parallel_move.AddMove(obj_,
817 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
818 Primitive::kPrimNot,
819 nullptr);
820 if (index.IsValid()) {
821 parallel_move.AddMove(index,
822 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
823 Primitive::kPrimInt,
824 nullptr);
825 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
826 } else {
827 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
828 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
829 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100830 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000831 CheckEntrypointTypes<
832 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
833 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
834
835 RestoreLiveRegisters(codegen, locations);
836 __ jmp(GetExitLabel());
837 }
838
839 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
840
841 private:
842 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
843 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
844 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
845 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
846 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
847 return static_cast<Register>(i);
848 }
849 }
850 // We shall never fail to find a free caller-save register, as
851 // there are more than two core caller-save registers on x86
852 // (meaning it is possible to find one which is different from
853 // `ref` and `obj`).
854 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
855 LOG(FATAL) << "Could not find a free caller-save register";
856 UNREACHABLE();
857 }
858
Roland Levillain0d5a2812015-11-13 10:07:31 +0000859 const Location out_;
860 const Location ref_;
861 const Location obj_;
862 const uint32_t offset_;
863 // An additional location containing an index to an array.
864 // Only used for HArrayGet and the UnsafeGetObject &
865 // UnsafeGetObjectVolatile intrinsics.
866 const Location index_;
867
868 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
869};
870
871// Slow path generating a read barrier for a GC root.
872class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
873 public:
874 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000875 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000876 DCHECK(kEmitCompilerReadBarrier);
877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000878
879 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
880 LocationSummary* locations = instruction_->GetLocations();
881 Register reg_out = out_.AsRegister<Register>();
882 DCHECK(locations->CanCall());
883 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000884 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
885 << "Unexpected instruction in read barrier for GC root slow path: "
886 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887
888 __ Bind(GetEntryLabel());
889 SaveLiveRegisters(codegen, locations);
890
891 InvokeRuntimeCallingConvention calling_convention;
892 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
893 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100894 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000895 instruction_,
896 instruction_->GetDexPc(),
897 this);
898 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
899 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
900
901 RestoreLiveRegisters(codegen, locations);
902 __ jmp(GetExitLabel());
903 }
904
905 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
906
907 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000908 const Location out_;
909 const Location root_;
910
911 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
912};
913
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100914#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100915// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
916#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100917
Aart Bike9f37602015-10-09 11:15:55 -0700918inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700919 switch (cond) {
920 case kCondEQ: return kEqual;
921 case kCondNE: return kNotEqual;
922 case kCondLT: return kLess;
923 case kCondLE: return kLessEqual;
924 case kCondGT: return kGreater;
925 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700926 case kCondB: return kBelow;
927 case kCondBE: return kBelowEqual;
928 case kCondA: return kAbove;
929 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700930 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100931 LOG(FATAL) << "Unreachable";
932 UNREACHABLE();
933}
934
Aart Bike9f37602015-10-09 11:15:55 -0700935// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100936inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
937 switch (cond) {
938 case kCondEQ: return kEqual;
939 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700940 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100941 case kCondLT: return kBelow;
942 case kCondLE: return kBelowEqual;
943 case kCondGT: return kAbove;
944 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700945 // Unsigned remain unchanged.
946 case kCondB: return kBelow;
947 case kCondBE: return kBelowEqual;
948 case kCondA: return kAbove;
949 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100950 }
951 LOG(FATAL) << "Unreachable";
952 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700953}
954
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100955void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100956 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100957}
958
959void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100960 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100961}
962
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100963size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
964 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
965 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100966}
967
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100968size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
969 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
970 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100971}
972
Mark Mendell7c8d0092015-01-26 11:21:33 -0500973size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700974 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700975 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700976 } else {
977 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
978 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500979 return GetFloatingPointSpillSlotSize();
980}
981
982size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700983 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700984 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700985 } else {
986 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
987 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500988 return GetFloatingPointSpillSlotSize();
989}
990
Calin Juravle175dc732015-08-25 15:42:32 +0100991void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
992 HInstruction* instruction,
993 uint32_t dex_pc,
994 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100995 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100996 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
997 if (EntrypointRequiresStackMap(entrypoint)) {
998 RecordPcInfo(instruction, dex_pc, slow_path);
999 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001000}
1001
Roland Levillaindec8f632016-07-22 17:10:06 +01001002void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1003 HInstruction* instruction,
1004 SlowPathCode* slow_path) {
1005 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001006 GenerateInvokeRuntime(entry_point_offset);
1007}
1008
1009void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001010 __ fs()->call(Address::Absolute(entry_point_offset));
1011}
1012
Mark Mendellfb8d2792015-03-31 22:16:59 -04001013CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001014 const X86InstructionSetFeatures& isa_features,
1015 const CompilerOptions& compiler_options,
1016 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001017 : CodeGenerator(graph,
1018 kNumberOfCpuRegisters,
1019 kNumberOfXmmRegisters,
1020 kNumberOfRegisterPairs,
1021 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1022 arraysize(kCoreCalleeSaves))
1023 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001024 0,
1025 compiler_options,
1026 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001027 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001028 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001029 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001030 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001031 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001032 isa_features_(isa_features),
Vladimir Marko65979462017-05-19 17:25:12 +01001033 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001034 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001035 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1036 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001037 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001038 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001039 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001040 constant_area_start_(-1),
1041 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001042 method_address_offset_(std::less<uint32_t>(),
1043 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 // Use a fake return address register to mimic Quick.
1045 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001046}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001047
David Brazdil58282f42016-01-14 12:45:10 +00001048void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001050 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051}
1052
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001053InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001054 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055 assembler_(codegen->GetAssembler()),
1056 codegen_(codegen) {}
1057
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001059 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060}
1061
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001062void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001064 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001065 bool skip_overflow_check =
1066 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001067 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001068
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001070 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001072 }
1073
Mark Mendell5f874182015-03-04 15:42:45 -05001074 if (HasEmptyFrame()) {
1075 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001076 }
Mark Mendell5f874182015-03-04 15:42:45 -05001077
1078 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1079 Register reg = kCoreCalleeSaves[i];
1080 if (allocated_registers_.ContainsCoreRegister(reg)) {
1081 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001082 __ cfi().AdjustCFAOffset(kX86WordSize);
1083 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001084 }
1085 }
1086
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001087 int adjust = GetFrameSize() - FrameEntrySpillSize();
1088 __ subl(ESP, Immediate(adjust));
1089 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001090 // Save the current method if we need it. Note that we do not
1091 // do this in HCurrentMethod, as the instruction might have been removed
1092 // in the SSA graph.
1093 if (RequiresCurrentMethod()) {
1094 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1095 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001096
1097 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1098 // Initialize should_deoptimize flag to 0.
1099 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1100 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001101}
1102
1103void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001104 __ cfi().RememberState();
1105 if (!HasEmptyFrame()) {
1106 int adjust = GetFrameSize() - FrameEntrySpillSize();
1107 __ addl(ESP, Immediate(adjust));
1108 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001109
David Srbeckyc34dc932015-04-12 09:27:43 +01001110 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1111 Register reg = kCoreCalleeSaves[i];
1112 if (allocated_registers_.ContainsCoreRegister(reg)) {
1113 __ popl(reg);
1114 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1115 __ cfi().Restore(DWARFReg(reg));
1116 }
Mark Mendell5f874182015-03-04 15:42:45 -05001117 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001118 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001119 __ ret();
1120 __ cfi().RestoreState();
1121 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001122}
1123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001124void CodeGeneratorX86::Bind(HBasicBlock* block) {
1125 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001126}
1127
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001128Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1129 switch (type) {
1130 case Primitive::kPrimBoolean:
1131 case Primitive::kPrimByte:
1132 case Primitive::kPrimChar:
1133 case Primitive::kPrimShort:
1134 case Primitive::kPrimInt:
1135 case Primitive::kPrimNot:
1136 return Location::RegisterLocation(EAX);
1137
1138 case Primitive::kPrimLong:
1139 return Location::RegisterPairLocation(EAX, EDX);
1140
1141 case Primitive::kPrimVoid:
1142 return Location::NoLocation();
1143
1144 case Primitive::kPrimDouble:
1145 case Primitive::kPrimFloat:
1146 return Location::FpuRegisterLocation(XMM0);
1147 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001148
1149 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150}
1151
1152Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1153 return Location::RegisterLocation(kMethodRegisterArgument);
1154}
1155
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001156Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001157 switch (type) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 case Primitive::kPrimChar:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimNot: {
1164 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001165 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001166 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001169 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001170 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001171 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174 uint32_t index = gp_index_;
1175 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1179 calling_convention.GetRegisterPairAt(index));
1180 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001182 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1183 }
1184 }
1185
1186 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001187 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001188 stack_index_++;
1189 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1190 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1191 } else {
1192 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1193 }
1194 }
1195
1196 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001197 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001198 stack_index_ += 2;
1199 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1200 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1201 } else {
1202 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001203 }
1204 }
1205
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001206 case Primitive::kPrimVoid:
1207 LOG(FATAL) << "Unexpected parameter type " << type;
1208 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001209 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001210 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001212
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213void CodeGeneratorX86::Move32(Location destination, Location source) {
1214 if (source.Equals(destination)) {
1215 return;
1216 }
1217 if (destination.IsRegister()) {
1218 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 } else {
1223 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001226 } else if (destination.IsFpuRegister()) {
1227 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 } else {
1232 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001236 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001241 } else if (source.IsConstant()) {
1242 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001243 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001244 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 } else {
1246 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001247 __ pushl(Address(ESP, source.GetStackIndex()));
1248 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 }
1250 }
1251}
1252
1253void CodeGeneratorX86::Move64(Location destination, Location source) {
1254 if (source.Equals(destination)) {
1255 return;
1256 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001257 if (destination.IsRegisterPair()) {
1258 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001259 EmitParallelMoves(
1260 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1261 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001262 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001263 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001264 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1265 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001266 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1268 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1269 __ psrlq(src_reg, Immediate(32));
1270 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001272 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001274 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1275 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001276 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1277 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001278 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001279 if (source.IsFpuRegister()) {
1280 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1281 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001282 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 } else if (source.IsRegisterPair()) {
1284 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1285 // Create stack space for 2 elements.
1286 __ subl(ESP, Immediate(2 * elem_size));
1287 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1288 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1289 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1290 // And remove the temporary stack space we allocated.
1291 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 } else {
1293 LOG(FATAL) << "Unimplemented";
1294 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001296 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001298 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001300 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001302 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001303 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001304 } else if (source.IsConstant()) {
1305 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001306 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1307 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001308 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001309 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1310 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001312 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001313 EmitParallelMoves(
1314 Location::StackSlot(source.GetStackIndex()),
1315 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001316 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001317 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001318 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1319 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001320 }
1321 }
1322}
1323
Calin Juravle175dc732015-08-25 15:42:32 +01001324void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1325 DCHECK(location.IsRegister());
1326 __ movl(location.AsRegister<Register>(), Immediate(value));
1327}
1328
Calin Juravlee460d1d2015-09-29 04:52:17 +01001329void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001330 HParallelMove move(GetGraph()->GetArena());
1331 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1332 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1333 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001335 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001336 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001337 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338}
1339
1340void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1341 if (location.IsRegister()) {
1342 locations->AddTemp(location);
1343 } else if (location.IsRegisterPair()) {
1344 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1345 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1346 } else {
1347 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1348 }
1349}
1350
David Brazdilfc6a86a2015-06-26 10:33:45 +00001351void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001352 DCHECK(!successor->IsExitBlock());
1353
1354 HBasicBlock* block = got->GetBlock();
1355 HInstruction* previous = got->GetPrevious();
1356
1357 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001358 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001359 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1360 return;
1361 }
1362
1363 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1364 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1365 }
1366 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001367 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001368 }
1369}
1370
David Brazdilfc6a86a2015-06-26 10:33:45 +00001371void LocationsBuilderX86::VisitGoto(HGoto* got) {
1372 got->SetLocations(nullptr);
1373}
1374
1375void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1376 HandleGoto(got, got->GetSuccessor());
1377}
1378
1379void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1380 try_boundary->SetLocations(nullptr);
1381}
1382
1383void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1384 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1385 if (!successor->IsExitBlock()) {
1386 HandleGoto(try_boundary, successor);
1387 }
1388}
1389
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001390void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001391 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001392}
1393
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001394void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001395}
1396
Mark Mendell152408f2015-12-31 12:28:50 -05001397template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001398void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001399 LabelType* true_label,
1400 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001401 if (cond->IsFPConditionTrueIfNaN()) {
1402 __ j(kUnordered, true_label);
1403 } else if (cond->IsFPConditionFalseIfNaN()) {
1404 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001405 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001406 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001407}
1408
Mark Mendell152408f2015-12-31 12:28:50 -05001409template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001410void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001411 LabelType* true_label,
1412 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001413 LocationSummary* locations = cond->GetLocations();
1414 Location left = locations->InAt(0);
1415 Location right = locations->InAt(1);
1416 IfCondition if_cond = cond->GetCondition();
1417
Mark Mendellc4701932015-04-10 13:18:51 -04001418 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001419 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001420 IfCondition true_high_cond = if_cond;
1421 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001422 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001423
1424 // Set the conditions for the test, remembering that == needs to be
1425 // decided using the low words.
1426 switch (if_cond) {
1427 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001428 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001429 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001430 break;
1431 case kCondLT:
1432 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001433 break;
1434 case kCondLE:
1435 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001436 break;
1437 case kCondGT:
1438 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001439 break;
1440 case kCondGE:
1441 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001442 break;
Aart Bike9f37602015-10-09 11:15:55 -07001443 case kCondB:
1444 false_high_cond = kCondA;
1445 break;
1446 case kCondBE:
1447 true_high_cond = kCondB;
1448 break;
1449 case kCondA:
1450 false_high_cond = kCondB;
1451 break;
1452 case kCondAE:
1453 true_high_cond = kCondA;
1454 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001455 }
1456
1457 if (right.IsConstant()) {
1458 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001459 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001460 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001461
Aart Bika19616e2016-02-01 18:57:58 -08001462 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001463 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001464 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001465 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001466 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001467 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001468 __ j(X86Condition(true_high_cond), true_label);
1469 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001470 }
1471 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001472 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001473 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001474 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001475 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001476
1477 __ cmpl(left_high, right_high);
1478 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001479 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001480 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001481 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(true_high_cond), true_label);
1484 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001485 }
1486 // Must be equal high, so compare the lows.
1487 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001488 } else {
1489 DCHECK(right.IsDoubleStackSlot());
1490 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1491 if (if_cond == kCondNE) {
1492 __ j(X86Condition(true_high_cond), true_label);
1493 } else if (if_cond == kCondEQ) {
1494 __ j(X86Condition(false_high_cond), false_label);
1495 } else {
1496 __ j(X86Condition(true_high_cond), true_label);
1497 __ j(X86Condition(false_high_cond), false_label);
1498 }
1499 // Must be equal high, so compare the lows.
1500 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001501 }
1502 // The last comparison might be unsigned.
1503 __ j(final_condition, true_label);
1504}
1505
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001506void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1507 Location rhs,
1508 HInstruction* insn,
1509 bool is_double) {
1510 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1511 if (is_double) {
1512 if (rhs.IsFpuRegister()) {
1513 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1514 } else if (const_area != nullptr) {
1515 DCHECK(const_area->IsEmittedAtUseSite());
1516 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1517 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001518 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1519 const_area->GetBaseMethodAddress(),
1520 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001521 } else {
1522 DCHECK(rhs.IsDoubleStackSlot());
1523 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1524 }
1525 } else {
1526 if (rhs.IsFpuRegister()) {
1527 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1528 } else if (const_area != nullptr) {
1529 DCHECK(const_area->IsEmittedAtUseSite());
1530 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1531 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001532 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1533 const_area->GetBaseMethodAddress(),
1534 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001535 } else {
1536 DCHECK(rhs.IsStackSlot());
1537 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1538 }
1539 }
1540}
1541
Mark Mendell152408f2015-12-31 12:28:50 -05001542template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001543void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001544 LabelType* true_target_in,
1545 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001546 // Generated branching requires both targets to be explicit. If either of the
1547 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001548 LabelType fallthrough_target;
1549 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1550 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001551
Mark Mendellc4701932015-04-10 13:18:51 -04001552 LocationSummary* locations = condition->GetLocations();
1553 Location left = locations->InAt(0);
1554 Location right = locations->InAt(1);
1555
Mark Mendellc4701932015-04-10 13:18:51 -04001556 Primitive::Type type = condition->InputAt(0)->GetType();
1557 switch (type) {
1558 case Primitive::kPrimLong:
1559 GenerateLongComparesAndJumps(condition, true_target, false_target);
1560 break;
1561 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001562 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001563 GenerateFPJumps(condition, true_target, false_target);
1564 break;
1565 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001566 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001567 GenerateFPJumps(condition, true_target, false_target);
1568 break;
1569 default:
1570 LOG(FATAL) << "Unexpected compare type " << type;
1571 }
1572
David Brazdil0debae72015-11-12 18:37:00 +00001573 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001574 __ jmp(false_target);
1575 }
David Brazdil0debae72015-11-12 18:37:00 +00001576
1577 if (fallthrough_target.IsLinked()) {
1578 __ Bind(&fallthrough_target);
1579 }
Mark Mendellc4701932015-04-10 13:18:51 -04001580}
1581
David Brazdil0debae72015-11-12 18:37:00 +00001582static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1583 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1584 // are set only strictly before `branch`. We can't use the eflags on long/FP
1585 // conditions if they are materialized due to the complex branching.
1586 return cond->IsCondition() &&
1587 cond->GetNext() == branch &&
1588 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1589 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1590}
1591
Mark Mendell152408f2015-12-31 12:28:50 -05001592template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001593void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001594 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001595 LabelType* true_target,
1596 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001597 HInstruction* cond = instruction->InputAt(condition_input_index);
1598
1599 if (true_target == nullptr && false_target == nullptr) {
1600 // Nothing to do. The code always falls through.
1601 return;
1602 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001603 // Constant condition, statically compared against "true" (integer value 1).
1604 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001605 if (true_target != nullptr) {
1606 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001607 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001608 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001609 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001610 if (false_target != nullptr) {
1611 __ jmp(false_target);
1612 }
1613 }
1614 return;
1615 }
1616
1617 // The following code generates these patterns:
1618 // (1) true_target == nullptr && false_target != nullptr
1619 // - opposite condition true => branch to false_target
1620 // (2) true_target != nullptr && false_target == nullptr
1621 // - condition true => branch to true_target
1622 // (3) true_target != nullptr && false_target != nullptr
1623 // - condition true => branch to true_target
1624 // - branch to false_target
1625 if (IsBooleanValueOrMaterializedCondition(cond)) {
1626 if (AreEflagsSetFrom(cond, instruction)) {
1627 if (true_target == nullptr) {
1628 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1629 } else {
1630 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1631 }
1632 } else {
1633 // Materialized condition, compare against 0.
1634 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1635 if (lhs.IsRegister()) {
1636 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1637 } else {
1638 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1639 }
1640 if (true_target == nullptr) {
1641 __ j(kEqual, false_target);
1642 } else {
1643 __ j(kNotEqual, true_target);
1644 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001645 }
1646 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001647 // Condition has not been materialized, use its inputs as the comparison and
1648 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001649 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001650
1651 // If this is a long or FP comparison that has been folded into
1652 // the HCondition, generate the comparison directly.
1653 Primitive::Type type = condition->InputAt(0)->GetType();
1654 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1655 GenerateCompareTestAndBranch(condition, true_target, false_target);
1656 return;
1657 }
1658
1659 Location lhs = condition->GetLocations()->InAt(0);
1660 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001661 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001662 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001663 if (true_target == nullptr) {
1664 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1665 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001666 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001667 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001668 }
David Brazdil0debae72015-11-12 18:37:00 +00001669
1670 // If neither branch falls through (case 3), the conditional branch to `true_target`
1671 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1672 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001673 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001674 }
1675}
1676
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001677void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1679 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001680 locations->SetInAt(0, Location::Any());
1681 }
1682}
1683
1684void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001685 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1686 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1687 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1688 nullptr : codegen_->GetLabelOf(true_successor);
1689 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1690 nullptr : codegen_->GetLabelOf(false_successor);
1691 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001692}
1693
1694void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1695 LocationSummary* locations = new (GetGraph()->GetArena())
1696 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001697 InvokeRuntimeCallingConvention calling_convention;
1698 RegisterSet caller_saves = RegisterSet::Empty();
1699 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1700 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001701 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001702 locations->SetInAt(0, Location::Any());
1703 }
1704}
1705
1706void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001707 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001708 GenerateTestAndBranch<Label>(deoptimize,
1709 /* condition_input_index */ 0,
1710 slow_path->GetEntryLabel(),
1711 /* false_target */ nullptr);
1712}
1713
Mingyao Yang063fc772016-08-02 11:02:54 -07001714void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1715 LocationSummary* locations = new (GetGraph()->GetArena())
1716 LocationSummary(flag, LocationSummary::kNoCall);
1717 locations->SetOut(Location::RequiresRegister());
1718}
1719
1720void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1721 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1722 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1723}
1724
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001725static bool SelectCanUseCMOV(HSelect* select) {
1726 // There are no conditional move instructions for XMMs.
1727 if (Primitive::IsFloatingPointType(select->GetType())) {
1728 return false;
1729 }
1730
1731 // A FP condition doesn't generate the single CC that we need.
1732 // In 32 bit mode, a long condition doesn't generate a single CC either.
1733 HInstruction* condition = select->GetCondition();
1734 if (condition->IsCondition()) {
1735 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1736 if (compare_type == Primitive::kPrimLong ||
1737 Primitive::IsFloatingPointType(compare_type)) {
1738 return false;
1739 }
1740 }
1741
1742 // We can generate a CMOV for this Select.
1743 return true;
1744}
1745
David Brazdil74eb1b22015-12-14 11:44:01 +00001746void LocationsBuilderX86::VisitSelect(HSelect* select) {
1747 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001748 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001749 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001750 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001751 } else {
1752 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001753 if (SelectCanUseCMOV(select)) {
1754 if (select->InputAt(1)->IsConstant()) {
1755 // Cmov can't handle a constant value.
1756 locations->SetInAt(1, Location::RequiresRegister());
1757 } else {
1758 locations->SetInAt(1, Location::Any());
1759 }
1760 } else {
1761 locations->SetInAt(1, Location::Any());
1762 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001763 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001764 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1765 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001766 }
1767 locations->SetOut(Location::SameAsFirstInput());
1768}
1769
1770void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1771 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772 DCHECK(locations->InAt(0).Equals(locations->Out()));
1773 if (SelectCanUseCMOV(select)) {
1774 // If both the condition and the source types are integer, we can generate
1775 // a CMOV to implement Select.
1776
1777 HInstruction* select_condition = select->GetCondition();
1778 Condition cond = kNotEqual;
1779
1780 // Figure out how to test the 'condition'.
1781 if (select_condition->IsCondition()) {
1782 HCondition* condition = select_condition->AsCondition();
1783 if (!condition->IsEmittedAtUseSite()) {
1784 // This was a previously materialized condition.
1785 // Can we use the existing condition code?
1786 if (AreEflagsSetFrom(condition, select)) {
1787 // Materialization was the previous instruction. Condition codes are right.
1788 cond = X86Condition(condition->GetCondition());
1789 } else {
1790 // No, we have to recreate the condition code.
1791 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1792 __ testl(cond_reg, cond_reg);
1793 }
1794 } else {
1795 // We can't handle FP or long here.
1796 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1797 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1798 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001799 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001800 cond = X86Condition(condition->GetCondition());
1801 }
1802 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001803 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001804 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1805 __ testl(cond_reg, cond_reg);
1806 }
1807
1808 // If the condition is true, overwrite the output, which already contains false.
1809 Location false_loc = locations->InAt(0);
1810 Location true_loc = locations->InAt(1);
1811 if (select->GetType() == Primitive::kPrimLong) {
1812 // 64 bit conditional move.
1813 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1814 Register false_low = false_loc.AsRegisterPairLow<Register>();
1815 if (true_loc.IsRegisterPair()) {
1816 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1817 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1818 } else {
1819 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1820 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1821 }
1822 } else {
1823 // 32 bit conditional move.
1824 Register false_reg = false_loc.AsRegister<Register>();
1825 if (true_loc.IsRegister()) {
1826 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1827 } else {
1828 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1829 }
1830 }
1831 } else {
1832 NearLabel false_target;
1833 GenerateTestAndBranch<NearLabel>(
1834 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1835 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1836 __ Bind(&false_target);
1837 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001838}
1839
David Srbecky0cf44932015-12-09 14:09:59 +00001840void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1841 new (GetGraph()->GetArena()) LocationSummary(info);
1842}
1843
David Srbeckyd28f4a02016-03-14 17:14:24 +00001844void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1845 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001846}
1847
1848void CodeGeneratorX86::GenerateNop() {
1849 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001850}
1851
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001853 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001854 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001855 // Handle the long/FP comparisons made in instruction simplification.
1856 switch (cond->InputAt(0)->GetType()) {
1857 case Primitive::kPrimLong: {
1858 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001859 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001860 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001861 locations->SetOut(Location::RequiresRegister());
1862 }
1863 break;
1864 }
1865 case Primitive::kPrimFloat:
1866 case Primitive::kPrimDouble: {
1867 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001868 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1869 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1870 } else if (cond->InputAt(1)->IsConstant()) {
1871 locations->SetInAt(1, Location::RequiresFpuRegister());
1872 } else {
1873 locations->SetInAt(1, Location::Any());
1874 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001875 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001876 locations->SetOut(Location::RequiresRegister());
1877 }
1878 break;
1879 }
1880 default:
1881 locations->SetInAt(0, Location::RequiresRegister());
1882 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 // We need a byte register.
1885 locations->SetOut(Location::RegisterLocation(ECX));
1886 }
1887 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001888 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001889}
1890
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001891void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001892 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001893 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001894 }
Mark Mendellc4701932015-04-10 13:18:51 -04001895
1896 LocationSummary* locations = cond->GetLocations();
1897 Location lhs = locations->InAt(0);
1898 Location rhs = locations->InAt(1);
1899 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001900 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001901
1902 switch (cond->InputAt(0)->GetType()) {
1903 default: {
1904 // Integer case.
1905
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001906 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001907 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001908 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001909 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001910 return;
1911 }
1912 case Primitive::kPrimLong:
1913 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1914 break;
1915 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001916 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001917 GenerateFPJumps(cond, &true_label, &false_label);
1918 break;
1919 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001920 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001921 GenerateFPJumps(cond, &true_label, &false_label);
1922 break;
1923 }
1924
1925 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001926 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001927
Roland Levillain4fa13f62015-07-06 18:11:54 +01001928 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001929 __ Bind(&false_label);
1930 __ xorl(reg, reg);
1931 __ jmp(&done_label);
1932
Roland Levillain4fa13f62015-07-06 18:11:54 +01001933 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001934 __ Bind(&true_label);
1935 __ movl(reg, Immediate(1));
1936 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001937}
1938
1939void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001940 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001941}
1942
1943void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001945}
1946
1947void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001985}
1986
Aart Bike9f37602015-10-09 11:15:55 -07001987void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001989}
1990
1991void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001993}
1994
1995void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001997}
1998
1999void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002001}
2002
2003void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002019void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002022 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023}
2024
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002025void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002026 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027}
2028
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002029void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2032 locations->SetOut(Location::ConstantLocation(constant));
2033}
2034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002035void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002036 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002037}
2038
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002039void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002042 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043}
2044
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002045void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046 // Will be generated at use site.
2047}
2048
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002049void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2050 LocationSummary* locations =
2051 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2052 locations->SetOut(Location::ConstantLocation(constant));
2053}
2054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002055void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056 // Will be generated at use site.
2057}
2058
2059void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2060 LocationSummary* locations =
2061 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2062 locations->SetOut(Location::ConstantLocation(constant));
2063}
2064
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002065void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002066 // Will be generated at use site.
2067}
2068
Igor Murashkind01745e2017-04-05 16:40:31 -07002069void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2070 constructor_fence->SetLocations(nullptr);
2071}
2072
2073void InstructionCodeGeneratorX86::VisitConstructorFence(
2074 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2075 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2076}
2077
Calin Juravle27df7582015-04-17 19:12:31 +01002078void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2079 memory_barrier->SetLocations(nullptr);
2080}
2081
2082void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002083 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002084}
2085
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002086void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002087 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002088}
2089
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002090void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002091 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002092}
2093
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002095 LocationSummary* locations =
2096 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002097 switch (ret->InputAt(0)->GetType()) {
2098 case Primitive::kPrimBoolean:
2099 case Primitive::kPrimByte:
2100 case Primitive::kPrimChar:
2101 case Primitive::kPrimShort:
2102 case Primitive::kPrimInt:
2103 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002105 break;
2106
2107 case Primitive::kPrimLong:
2108 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002109 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 break;
2111
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002112 case Primitive::kPrimFloat:
2113 case Primitive::kPrimDouble:
2114 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002116 break;
2117
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002118 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121}
2122
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002123void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002124 if (kIsDebugBuild) {
2125 switch (ret->InputAt(0)->GetType()) {
2126 case Primitive::kPrimBoolean:
2127 case Primitive::kPrimByte:
2128 case Primitive::kPrimChar:
2129 case Primitive::kPrimShort:
2130 case Primitive::kPrimInt:
2131 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133 break;
2134
2135 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002136 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2137 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 break;
2139
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 case Primitive::kPrimFloat:
2141 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 break;
2144
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002145 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002146 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 }
2148 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002149 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002150}
2151
Calin Juravle175dc732015-08-25 15:42:32 +01002152void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2153 // The trampoline uses the same calling convention as dex calling conventions,
2154 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2155 // the method_idx.
2156 HandleInvoke(invoke);
2157}
2158
2159void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2160 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2161}
2162
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002163void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002164 // Explicit clinit checks triggered by static invokes must have been pruned by
2165 // art::PrepareForRegisterAllocation.
2166 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002167
Mark Mendellfb8d2792015-03-31 22:16:59 -04002168 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002169 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002170 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002171 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002172 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002173 return;
2174 }
2175
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002176 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002177
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002178 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002179 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002180 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002181 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002182}
2183
Mark Mendell09ed1a32015-03-25 08:30:06 -04002184static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2185 if (invoke->GetLocations()->Intrinsified()) {
2186 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2187 intrinsic.Dispatch(invoke);
2188 return true;
2189 }
2190 return false;
2191}
2192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002193void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002194 // Explicit clinit checks triggered by static invokes must have been pruned by
2195 // art::PrepareForRegisterAllocation.
2196 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002197
Mark Mendell09ed1a32015-03-25 08:30:06 -04002198 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2199 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002200 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002201
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002202 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002203 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002204 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002205}
2206
2207void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002208 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2209 if (intrinsic.TryDispatch(invoke)) {
2210 return;
2211 }
2212
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002213 HandleInvoke(invoke);
2214}
2215
2216void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002217 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002218 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002219}
2220
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002221void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002222 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2223 return;
2224 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002225
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002226 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002227 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228}
2229
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002231 // This call to HandleInvoke allocates a temporary (core) register
2232 // which is also used to transfer the hidden argument from FP to
2233 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234 HandleInvoke(invoke);
2235 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002236 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237}
2238
2239void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2240 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002241 LocationSummary* locations = invoke->GetLocations();
2242 Register temp = locations->GetTemp(0).AsRegister<Register>();
2243 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002244 Location receiver = locations->InAt(0);
2245 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2246
Roland Levillain0d5a2812015-11-13 10:07:31 +00002247 // Set the hidden argument. This is safe to do this here, as XMM7
2248 // won't be modified thereafter, before the `call` instruction.
2249 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002250 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 if (receiver.IsStackSlot()) {
2254 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 __ movl(temp, Address(temp, class_offset));
2257 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002259 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 }
Roland Levillain4d027112015-07-01 15:41:14 +01002261 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 // Instead of simply (possibly) unpoisoning `temp` here, we should
2263 // emit a read barrier for the previous class reference load.
2264 // However this is not required in practice, as this is an
2265 // intermediate/temporary reference and because the current
2266 // concurrent copying collector keeps the from-space memory
2267 // intact/accessible until the end of the marking phase (the
2268 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002269 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002270 // temp = temp->GetAddressOfIMT()
2271 __ movl(temp,
2272 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002274 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002275 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276 __ movl(temp, Address(temp, method_offset));
2277 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002278 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002279 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280
2281 DCHECK(!codegen_->IsLeafMethod());
2282 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2283}
2284
Orion Hodsonac141392017-01-13 11:53:47 +00002285void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2286 HandleInvoke(invoke);
2287}
2288
2289void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2290 codegen_->GenerateInvokePolymorphicCall(invoke);
2291}
2292
Roland Levillain88cb1752014-10-20 16:36:47 +01002293void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2294 LocationSummary* locations =
2295 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2296 switch (neg->GetResultType()) {
2297 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002298 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002299 locations->SetInAt(0, Location::RequiresRegister());
2300 locations->SetOut(Location::SameAsFirstInput());
2301 break;
2302
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002304 locations->SetInAt(0, Location::RequiresFpuRegister());
2305 locations->SetOut(Location::SameAsFirstInput());
2306 locations->AddTemp(Location::RequiresRegister());
2307 locations->AddTemp(Location::RequiresFpuRegister());
2308 break;
2309
Roland Levillain88cb1752014-10-20 16:36:47 +01002310 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002311 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002312 locations->SetOut(Location::SameAsFirstInput());
2313 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2318 }
2319}
2320
2321void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2322 LocationSummary* locations = neg->GetLocations();
2323 Location out = locations->Out();
2324 Location in = locations->InAt(0);
2325 switch (neg->GetResultType()) {
2326 case Primitive::kPrimInt:
2327 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002328 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002330 break;
2331
2332 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002333 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002334 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002335 __ negl(out.AsRegisterPairLow<Register>());
2336 // Negation is similar to subtraction from zero. The least
2337 // significant byte triggers a borrow when it is different from
2338 // zero; to take it into account, add 1 to the most significant
2339 // byte if the carry flag (CF) is set to 1 after the first NEGL
2340 // operation.
2341 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2342 __ negl(out.AsRegisterPairHigh<Register>());
2343 break;
2344
Roland Levillain5368c212014-11-27 15:03:41 +00002345 case Primitive::kPrimFloat: {
2346 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 Register constant = locations->GetTemp(0).AsRegister<Register>();
2348 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002349 // Implement float negation with an exclusive or with value
2350 // 0x80000000 (mask for bit 31, representing the sign of a
2351 // single-precision floating-point number).
2352 __ movl(constant, Immediate(INT32_C(0x80000000)));
2353 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002355 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002356 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002357
Roland Levillain5368c212014-11-27 15:03:41 +00002358 case Primitive::kPrimDouble: {
2359 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002360 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002361 // Implement double negation with an exclusive or with value
2362 // 0x8000000000000000 (mask for bit 63, representing the sign of
2363 // a double-precision floating-point number).
2364 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002365 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002366 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002367 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002368
2369 default:
2370 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2371 }
2372}
2373
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002374void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2375 LocationSummary* locations =
2376 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2377 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2378 locations->SetInAt(0, Location::RequiresFpuRegister());
2379 locations->SetInAt(1, Location::RequiresRegister());
2380 locations->SetOut(Location::SameAsFirstInput());
2381 locations->AddTemp(Location::RequiresFpuRegister());
2382}
2383
2384void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2385 LocationSummary* locations = neg->GetLocations();
2386 Location out = locations->Out();
2387 DCHECK(locations->InAt(0).Equals(out));
2388
2389 Register constant_area = locations->InAt(1).AsRegister<Register>();
2390 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2391 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002392 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2393 neg->GetBaseMethodAddress(),
2394 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002395 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2396 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002397 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2398 neg->GetBaseMethodAddress(),
2399 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002400 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2401 }
2402}
2403
Roland Levillaindff1f282014-11-05 14:15:05 +00002404void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 Primitive::Type result_type = conversion->GetResultType();
2406 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002407 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002408
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002409 // The float-to-long and double-to-long type conversions rely on a
2410 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002411 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002412 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2413 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002414 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002415 : LocationSummary::kNoCall;
2416 LocationSummary* locations =
2417 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2418
David Brazdilb2bd1c52015-03-25 11:17:37 +00002419 // The Java language does not allow treating boolean as an integral type but
2420 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002421
Roland Levillaindff1f282014-11-05 14:15:05 +00002422 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002423 case Primitive::kPrimByte:
2424 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002425 case Primitive::kPrimLong: {
2426 // Type conversion from long to byte is a result of code transformations.
2427 HInstruction* input = conversion->InputAt(0);
2428 Location input_location = input->IsConstant()
2429 ? Location::ConstantLocation(input->AsConstant())
2430 : Location::RegisterPairLocation(EAX, EDX);
2431 locations->SetInAt(0, input_location);
2432 // Make the output overlap to please the register allocator. This greatly simplifies
2433 // the validation of the linear scan implementation
2434 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2435 break;
2436 }
David Brazdil46e2a392015-03-16 17:31:52 +00002437 case Primitive::kPrimBoolean:
2438 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002439 case Primitive::kPrimShort:
2440 case Primitive::kPrimInt:
2441 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002442 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002443 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2444 // Make the output overlap to please the register allocator. This greatly simplifies
2445 // the validation of the linear scan implementation
2446 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002447 break;
2448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453 break;
2454
Roland Levillain01a8d712014-11-14 16:27:39 +00002455 case Primitive::kPrimShort:
2456 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002457 case Primitive::kPrimLong:
2458 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002459 case Primitive::kPrimBoolean:
2460 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002461 case Primitive::kPrimByte:
2462 case Primitive::kPrimInt:
2463 case Primitive::kPrimChar:
2464 // Processing a Dex `int-to-short' instruction.
2465 locations->SetInAt(0, Location::Any());
2466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2467 break;
2468
2469 default:
2470 LOG(FATAL) << "Unexpected type conversion from " << input_type
2471 << " to " << result_type;
2472 }
2473 break;
2474
Roland Levillain946e1432014-11-11 17:35:19 +00002475 case Primitive::kPrimInt:
2476 switch (input_type) {
2477 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002478 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002479 locations->SetInAt(0, Location::Any());
2480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2481 break;
2482
2483 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002484 // Processing a Dex `float-to-int' instruction.
2485 locations->SetInAt(0, Location::RequiresFpuRegister());
2486 locations->SetOut(Location::RequiresRegister());
2487 locations->AddTemp(Location::RequiresFpuRegister());
2488 break;
2489
Roland Levillain946e1432014-11-11 17:35:19 +00002490 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002491 // Processing a Dex `double-to-int' instruction.
2492 locations->SetInAt(0, Location::RequiresFpuRegister());
2493 locations->SetOut(Location::RequiresRegister());
2494 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002495 break;
2496
2497 default:
2498 LOG(FATAL) << "Unexpected type conversion from " << input_type
2499 << " to " << result_type;
2500 }
2501 break;
2502
Roland Levillaindff1f282014-11-05 14:15:05 +00002503 case Primitive::kPrimLong:
2504 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002505 case Primitive::kPrimBoolean:
2506 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002507 case Primitive::kPrimByte:
2508 case Primitive::kPrimShort:
2509 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002510 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002511 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002512 locations->SetInAt(0, Location::RegisterLocation(EAX));
2513 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2514 break;
2515
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002516 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002517 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002518 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002519 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002520 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2521 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2522
Vladimir Marko949c91f2015-01-27 10:48:44 +00002523 // The runtime helper puts the result in EAX, EDX.
2524 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002525 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002526 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Roland Levillain981e4542014-11-14 11:47:14 +00002534 case Primitive::kPrimChar:
2535 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002536 case Primitive::kPrimLong:
2537 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002538 case Primitive::kPrimBoolean:
2539 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002540 case Primitive::kPrimByte:
2541 case Primitive::kPrimShort:
2542 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002543 // Processing a Dex `int-to-char' instruction.
2544 locations->SetInAt(0, Location::Any());
2545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2546 break;
2547
2548 default:
2549 LOG(FATAL) << "Unexpected type conversion from " << input_type
2550 << " to " << result_type;
2551 }
2552 break;
2553
Roland Levillaindff1f282014-11-05 14:15:05 +00002554 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002555 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002556 case Primitive::kPrimBoolean:
2557 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002558 case Primitive::kPrimByte:
2559 case Primitive::kPrimShort:
2560 case Primitive::kPrimInt:
2561 case Primitive::kPrimChar:
2562 // Processing a Dex `int-to-float' instruction.
2563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetOut(Location::RequiresFpuRegister());
2565 break;
2566
2567 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002568 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002569 locations->SetInAt(0, Location::Any());
2570 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002571 break;
2572
Roland Levillaincff13742014-11-17 14:32:17 +00002573 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002574 // Processing a Dex `double-to-float' instruction.
2575 locations->SetInAt(0, Location::RequiresFpuRegister());
2576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002577 break;
2578
2579 default:
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type
2581 << " to " << result_type;
2582 };
2583 break;
2584
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002587 case Primitive::kPrimBoolean:
2588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002589 case Primitive::kPrimByte:
2590 case Primitive::kPrimShort:
2591 case Primitive::kPrimInt:
2592 case Primitive::kPrimChar:
2593 // Processing a Dex `int-to-double' instruction.
2594 locations->SetInAt(0, Location::RequiresRegister());
2595 locations->SetOut(Location::RequiresFpuRegister());
2596 break;
2597
2598 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002599 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002600 locations->SetInAt(0, Location::Any());
2601 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002602 break;
2603
Roland Levillaincff13742014-11-17 14:32:17 +00002604 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002605 // Processing a Dex `float-to-double' instruction.
2606 locations->SetInAt(0, Location::RequiresFpuRegister());
2607 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002608 break;
2609
2610 default:
2611 LOG(FATAL) << "Unexpected type conversion from " << input_type
2612 << " to " << result_type;
2613 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 }
2620}
2621
2622void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2623 LocationSummary* locations = conversion->GetLocations();
2624 Location out = locations->Out();
2625 Location in = locations->InAt(0);
2626 Primitive::Type result_type = conversion->GetResultType();
2627 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002628 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002629 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002630 case Primitive::kPrimByte:
2631 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002632 case Primitive::kPrimLong:
2633 // Type conversion from long to byte is a result of code transformations.
2634 if (in.IsRegisterPair()) {
2635 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2636 } else {
2637 DCHECK(in.GetConstant()->IsLongConstant());
2638 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2640 }
2641 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002642 case Primitive::kPrimBoolean:
2643 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002644 case Primitive::kPrimShort:
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002647 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002648 if (in.IsRegister()) {
2649 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002650 } else {
2651 DCHECK(in.GetConstant()->IsIntConstant());
2652 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2653 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2654 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002655 break;
2656
2657 default:
2658 LOG(FATAL) << "Unexpected type conversion from " << input_type
2659 << " to " << result_type;
2660 }
2661 break;
2662
Roland Levillain01a8d712014-11-14 16:27:39 +00002663 case Primitive::kPrimShort:
2664 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002665 case Primitive::kPrimLong:
2666 // Type conversion from long to short is a result of code transformations.
2667 if (in.IsRegisterPair()) {
2668 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2669 } else if (in.IsDoubleStackSlot()) {
2670 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2671 } else {
2672 DCHECK(in.GetConstant()->IsLongConstant());
2673 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2674 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2675 }
2676 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002677 case Primitive::kPrimBoolean:
2678 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002679 case Primitive::kPrimByte:
2680 case Primitive::kPrimInt:
2681 case Primitive::kPrimChar:
2682 // Processing a Dex `int-to-short' instruction.
2683 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002685 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002687 } else {
2688 DCHECK(in.GetConstant()->IsIntConstant());
2689 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002690 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002691 }
2692 break;
2693
2694 default:
2695 LOG(FATAL) << "Unexpected type conversion from " << input_type
2696 << " to " << result_type;
2697 }
2698 break;
2699
Roland Levillain946e1432014-11-11 17:35:19 +00002700 case Primitive::kPrimInt:
2701 switch (input_type) {
2702 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002703 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002704 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002706 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002708 } else {
2709 DCHECK(in.IsConstant());
2710 DCHECK(in.GetConstant()->IsLongConstant());
2711 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002712 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002713 }
2714 break;
2715
Roland Levillain3f8f9362014-12-02 17:45:01 +00002716 case Primitive::kPrimFloat: {
2717 // Processing a Dex `float-to-int' instruction.
2718 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2719 Register output = out.AsRegister<Register>();
2720 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002721 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002722
2723 __ movl(output, Immediate(kPrimIntMax));
2724 // temp = int-to-float(output)
2725 __ cvtsi2ss(temp, output);
2726 // if input >= temp goto done
2727 __ comiss(input, temp);
2728 __ j(kAboveEqual, &done);
2729 // if input == NaN goto nan
2730 __ j(kUnordered, &nan);
2731 // output = float-to-int-truncate(input)
2732 __ cvttss2si(output, input);
2733 __ jmp(&done);
2734 __ Bind(&nan);
2735 // output = 0
2736 __ xorl(output, output);
2737 __ Bind(&done);
2738 break;
2739 }
2740
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002741 case Primitive::kPrimDouble: {
2742 // Processing a Dex `double-to-int' instruction.
2743 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2744 Register output = out.AsRegister<Register>();
2745 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002746 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002747
2748 __ movl(output, Immediate(kPrimIntMax));
2749 // temp = int-to-double(output)
2750 __ cvtsi2sd(temp, output);
2751 // if input >= temp goto done
2752 __ comisd(input, temp);
2753 __ j(kAboveEqual, &done);
2754 // if input == NaN goto nan
2755 __ j(kUnordered, &nan);
2756 // output = double-to-int-truncate(input)
2757 __ cvttsd2si(output, input);
2758 __ jmp(&done);
2759 __ Bind(&nan);
2760 // output = 0
2761 __ xorl(output, output);
2762 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002763 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002764 }
Roland Levillain946e1432014-11-11 17:35:19 +00002765
2766 default:
2767 LOG(FATAL) << "Unexpected type conversion from " << input_type
2768 << " to " << result_type;
2769 }
2770 break;
2771
Roland Levillaindff1f282014-11-05 14:15:05 +00002772 case Primitive::kPrimLong:
2773 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002774 case Primitive::kPrimBoolean:
2775 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002776 case Primitive::kPrimByte:
2777 case Primitive::kPrimShort:
2778 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002779 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002780 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002781 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2782 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 __ cdq();
2785 break;
2786
2787 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002788 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002789 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002790 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002791 break;
2792
Roland Levillaindff1f282014-11-05 14:15:05 +00002793 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002794 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002795 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002796 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002797 break;
2798
2799 default:
2800 LOG(FATAL) << "Unexpected type conversion from " << input_type
2801 << " to " << result_type;
2802 }
2803 break;
2804
Roland Levillain981e4542014-11-14 11:47:14 +00002805 case Primitive::kPrimChar:
2806 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002807 case Primitive::kPrimLong:
2808 // Type conversion from long to short is a result of code transformations.
2809 if (in.IsRegisterPair()) {
2810 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2811 } else if (in.IsDoubleStackSlot()) {
2812 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2813 } else {
2814 DCHECK(in.GetConstant()->IsLongConstant());
2815 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2816 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2817 }
2818 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002819 case Primitive::kPrimBoolean:
2820 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002821 case Primitive::kPrimByte:
2822 case Primitive::kPrimShort:
2823 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002824 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2825 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002826 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002827 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002829 } else {
2830 DCHECK(in.GetConstant()->IsIntConstant());
2831 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002832 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002833 }
2834 break;
2835
2836 default:
2837 LOG(FATAL) << "Unexpected type conversion from " << input_type
2838 << " to " << result_type;
2839 }
2840 break;
2841
Roland Levillaindff1f282014-11-05 14:15:05 +00002842 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002843 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002844 case Primitive::kPrimBoolean:
2845 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002846 case Primitive::kPrimByte:
2847 case Primitive::kPrimShort:
2848 case Primitive::kPrimInt:
2849 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002850 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002851 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002852 break;
2853
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 case Primitive::kPrimLong: {
2855 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002856 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002857
Roland Levillain232ade02015-04-20 15:14:36 +01002858 // Create stack space for the call to
2859 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2860 // TODO: enhance register allocator to ask for stack temporaries.
2861 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2862 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2863 __ subl(ESP, Immediate(adjustment));
2864 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002865
Roland Levillain232ade02015-04-20 15:14:36 +01002866 // Load the value to the FP stack, using temporaries if needed.
2867 PushOntoFPStack(in, 0, adjustment, false, true);
2868
2869 if (out.IsStackSlot()) {
2870 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2871 } else {
2872 __ fstps(Address(ESP, 0));
2873 Location stack_temp = Location::StackSlot(0);
2874 codegen_->Move32(out, stack_temp);
2875 }
2876
2877 // Remove the temporary stack space we allocated.
2878 if (adjustment != 0) {
2879 __ addl(ESP, Immediate(adjustment));
2880 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002881 break;
2882 }
2883
Roland Levillaincff13742014-11-17 14:32:17 +00002884 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002885 // Processing a Dex `double-to-float' instruction.
2886 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002887 break;
2888
2889 default:
2890 LOG(FATAL) << "Unexpected type conversion from " << input_type
2891 << " to " << result_type;
2892 };
2893 break;
2894
Roland Levillaindff1f282014-11-05 14:15:05 +00002895 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002896 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002897 case Primitive::kPrimBoolean:
2898 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002899 case Primitive::kPrimByte:
2900 case Primitive::kPrimShort:
2901 case Primitive::kPrimInt:
2902 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002903 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002904 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002905 break;
2906
Roland Levillain647b9ed2014-11-27 12:06:00 +00002907 case Primitive::kPrimLong: {
2908 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002909 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002910
Roland Levillain232ade02015-04-20 15:14:36 +01002911 // Create stack space for the call to
2912 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2913 // TODO: enhance register allocator to ask for stack temporaries.
2914 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2915 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2916 __ subl(ESP, Immediate(adjustment));
2917 }
2918
2919 // Load the value to the FP stack, using temporaries if needed.
2920 PushOntoFPStack(in, 0, adjustment, false, true);
2921
2922 if (out.IsDoubleStackSlot()) {
2923 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2924 } else {
2925 __ fstpl(Address(ESP, 0));
2926 Location stack_temp = Location::DoubleStackSlot(0);
2927 codegen_->Move64(out, stack_temp);
2928 }
2929
2930 // Remove the temporary stack space we allocated.
2931 if (adjustment != 0) {
2932 __ addl(ESP, Immediate(adjustment));
2933 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002934 break;
2935 }
2936
Roland Levillaincff13742014-11-17 14:32:17 +00002937 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002938 // Processing a Dex `float-to-double' instruction.
2939 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002940 break;
2941
2942 default:
2943 LOG(FATAL) << "Unexpected type conversion from " << input_type
2944 << " to " << result_type;
2945 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002946 break;
2947
2948 default:
2949 LOG(FATAL) << "Unexpected type conversion from " << input_type
2950 << " to " << result_type;
2951 }
2952}
2953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002955 LocationSummary* locations =
2956 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002958 case Primitive::kPrimInt: {
2959 locations->SetInAt(0, Location::RequiresRegister());
2960 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2962 break;
2963 }
2964
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002965 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002966 locations->SetInAt(0, Location::RequiresRegister());
2967 locations->SetInAt(1, Location::Any());
2968 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002969 break;
2970 }
2971
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002972 case Primitive::kPrimFloat:
2973 case Primitive::kPrimDouble: {
2974 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002975 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2976 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002977 } else if (add->InputAt(1)->IsConstant()) {
2978 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 } else {
2980 locations->SetInAt(1, Location::Any());
2981 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002983 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002984 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002985
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002986 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2988 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002989 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990}
2991
2992void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2993 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002994 Location first = locations->InAt(0);
2995 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002996 Location out = locations->Out();
2997
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002998 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002999 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003001 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3002 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003003 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3004 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003005 } else {
3006 __ leal(out.AsRegister<Register>(), Address(
3007 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3008 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003009 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003010 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3011 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3012 __ addl(out.AsRegister<Register>(), Immediate(value));
3013 } else {
3014 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3015 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003016 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003017 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003019 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003020 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003021 }
3022
3023 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003024 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003025 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3026 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003027 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003028 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3029 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003030 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 } else {
3032 DCHECK(second.IsConstant()) << second;
3033 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3034 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3035 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003037 break;
3038 }
3039
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 case Primitive::kPrimFloat: {
3041 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003043 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3044 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003045 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003046 __ addss(first.AsFpuRegister<XmmRegister>(),
3047 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003048 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3049 const_area->GetBaseMethodAddress(),
3050 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003051 } else {
3052 DCHECK(second.IsStackSlot());
3053 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003054 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003055 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003056 }
3057
3058 case Primitive::kPrimDouble: {
3059 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003061 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3062 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003063 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003064 __ addsd(first.AsFpuRegister<XmmRegister>(),
3065 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003066 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3067 const_area->GetBaseMethodAddress(),
3068 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003069 } else {
3070 DCHECK(second.IsDoubleStackSlot());
3071 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003072 }
3073 break;
3074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003075
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003076 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003077 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078 }
3079}
3080
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003081void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003082 LocationSummary* locations =
3083 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003084 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003085 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003086 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003087 locations->SetInAt(0, Location::RequiresRegister());
3088 locations->SetInAt(1, Location::Any());
3089 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003090 break;
3091 }
Calin Juravle11351682014-10-23 15:38:15 +01003092 case Primitive::kPrimFloat:
3093 case Primitive::kPrimDouble: {
3094 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003095 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3096 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003097 } else if (sub->InputAt(1)->IsConstant()) {
3098 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 } else {
3100 locations->SetInAt(1, Location::Any());
3101 }
Calin Juravle11351682014-10-23 15:38:15 +01003102 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003103 break;
Calin Juravle11351682014-10-23 15:38:15 +01003104 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003105
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003106 default:
Calin Juravle11351682014-10-23 15:38:15 +01003107 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003108 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003109}
3110
3111void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3112 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003113 Location first = locations->InAt(0);
3114 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003115 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003116 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003117 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003118 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003120 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003121 __ subl(first.AsRegister<Register>(),
3122 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003123 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003124 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003125 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003126 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003127 }
3128
3129 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003130 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003131 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3132 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003133 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003134 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003135 __ sbbl(first.AsRegisterPairHigh<Register>(),
3136 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003137 } else {
3138 DCHECK(second.IsConstant()) << second;
3139 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3140 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3141 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003142 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003143 break;
3144 }
3145
Calin Juravle11351682014-10-23 15:38:15 +01003146 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 if (second.IsFpuRegister()) {
3148 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3149 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3150 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003151 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003152 __ subss(first.AsFpuRegister<XmmRegister>(),
3153 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003154 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3155 const_area->GetBaseMethodAddress(),
3156 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003157 } else {
3158 DCHECK(second.IsStackSlot());
3159 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3160 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003161 break;
Calin Juravle11351682014-10-23 15:38:15 +01003162 }
3163
3164 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 if (second.IsFpuRegister()) {
3166 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3167 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3168 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003169 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003170 __ subsd(first.AsFpuRegister<XmmRegister>(),
3171 codegen_->LiteralDoubleAddress(
3172 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003173 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003174 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3175 } else {
3176 DCHECK(second.IsDoubleStackSlot());
3177 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3178 }
Calin Juravle11351682014-10-23 15:38:15 +01003179 break;
3180 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003181
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003182 default:
Calin Juravle11351682014-10-23 15:38:15 +01003183 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003184 }
3185}
3186
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187void LocationsBuilderX86::VisitMul(HMul* mul) {
3188 LocationSummary* locations =
3189 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3190 switch (mul->GetResultType()) {
3191 case Primitive::kPrimInt:
3192 locations->SetInAt(0, Location::RequiresRegister());
3193 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003194 if (mul->InputAt(1)->IsIntConstant()) {
3195 // Can use 3 operand multiply.
3196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3197 } else {
3198 locations->SetOut(Location::SameAsFirstInput());
3199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 break;
3201 case Primitive::kPrimLong: {
3202 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203 locations->SetInAt(1, Location::Any());
3204 locations->SetOut(Location::SameAsFirstInput());
3205 // Needed for imul on 32bits with 64bits output.
3206 locations->AddTemp(Location::RegisterLocation(EAX));
3207 locations->AddTemp(Location::RegisterLocation(EDX));
3208 break;
3209 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003210 case Primitive::kPrimFloat:
3211 case Primitive::kPrimDouble: {
3212 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003213 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3214 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003215 } else if (mul->InputAt(1)->IsConstant()) {
3216 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003217 } else {
3218 locations->SetInAt(1, Location::Any());
3219 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003220 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003222 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223
3224 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003225 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226 }
3227}
3228
3229void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3230 LocationSummary* locations = mul->GetLocations();
3231 Location first = locations->InAt(0);
3232 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003233 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003234
3235 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003236 case Primitive::kPrimInt:
3237 // The constant may have ended up in a register, so test explicitly to avoid
3238 // problems where the output may not be the same as the first operand.
3239 if (mul->InputAt(1)->IsIntConstant()) {
3240 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3241 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3242 } else if (second.IsRegister()) {
3243 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003245 } else {
3246 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003247 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249 }
3250 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251
3252 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253 Register in1_hi = first.AsRegisterPairHigh<Register>();
3254 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003255 Register eax = locations->GetTemp(0).AsRegister<Register>();
3256 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257
3258 DCHECK_EQ(EAX, eax);
3259 DCHECK_EQ(EDX, edx);
3260
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003261 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003262 // output: in1
3263 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3264 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3265 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003266 if (second.IsConstant()) {
3267 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003268
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003269 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3270 int32_t low_value = Low32Bits(value);
3271 int32_t high_value = High32Bits(value);
3272 Immediate low(low_value);
3273 Immediate high(high_value);
3274
3275 __ movl(eax, high);
3276 // eax <- in1.lo * in2.hi
3277 __ imull(eax, in1_lo);
3278 // in1.hi <- in1.hi * in2.lo
3279 __ imull(in1_hi, low);
3280 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3281 __ addl(in1_hi, eax);
3282 // move in2_lo to eax to prepare for double precision
3283 __ movl(eax, low);
3284 // edx:eax <- in1.lo * in2.lo
3285 __ mull(in1_lo);
3286 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3287 __ addl(in1_hi, edx);
3288 // in1.lo <- (in1.lo * in2.lo)[31:0];
3289 __ movl(in1_lo, eax);
3290 } else if (second.IsRegisterPair()) {
3291 Register in2_hi = second.AsRegisterPairHigh<Register>();
3292 Register in2_lo = second.AsRegisterPairLow<Register>();
3293
3294 __ movl(eax, in2_hi);
3295 // eax <- in1.lo * in2.hi
3296 __ imull(eax, in1_lo);
3297 // in1.hi <- in1.hi * in2.lo
3298 __ imull(in1_hi, in2_lo);
3299 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3300 __ addl(in1_hi, eax);
3301 // move in1_lo to eax to prepare for double precision
3302 __ movl(eax, in1_lo);
3303 // edx:eax <- in1.lo * in2.lo
3304 __ mull(in2_lo);
3305 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3306 __ addl(in1_hi, edx);
3307 // in1.lo <- (in1.lo * in2.lo)[31:0];
3308 __ movl(in1_lo, eax);
3309 } else {
3310 DCHECK(second.IsDoubleStackSlot()) << second;
3311 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3312 Address in2_lo(ESP, second.GetStackIndex());
3313
3314 __ movl(eax, in2_hi);
3315 // eax <- in1.lo * in2.hi
3316 __ imull(eax, in1_lo);
3317 // in1.hi <- in1.hi * in2.lo
3318 __ imull(in1_hi, in2_lo);
3319 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3320 __ addl(in1_hi, eax);
3321 // move in1_lo to eax to prepare for double precision
3322 __ movl(eax, in1_lo);
3323 // edx:eax <- in1.lo * in2.lo
3324 __ mull(in2_lo);
3325 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3326 __ addl(in1_hi, edx);
3327 // in1.lo <- (in1.lo * in2.lo)[31:0];
3328 __ movl(in1_lo, eax);
3329 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003330
3331 break;
3332 }
3333
Calin Juravleb5bfa962014-10-21 18:02:24 +01003334 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003335 DCHECK(first.Equals(locations->Out()));
3336 if (second.IsFpuRegister()) {
3337 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3338 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3339 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003340 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003341 __ mulss(first.AsFpuRegister<XmmRegister>(),
3342 codegen_->LiteralFloatAddress(
3343 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003344 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3346 } else {
3347 DCHECK(second.IsStackSlot());
3348 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3349 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003350 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003351 }
3352
3353 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003354 DCHECK(first.Equals(locations->Out()));
3355 if (second.IsFpuRegister()) {
3356 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3357 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3358 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003359 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003360 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3361 codegen_->LiteralDoubleAddress(
3362 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003363 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003364 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3365 } else {
3366 DCHECK(second.IsDoubleStackSlot());
3367 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3368 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003369 break;
3370 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003371
3372 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003373 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374 }
3375}
3376
Roland Levillain232ade02015-04-20 15:14:36 +01003377void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3378 uint32_t temp_offset,
3379 uint32_t stack_adjustment,
3380 bool is_fp,
3381 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003382 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003383 DCHECK(!is_wide);
3384 if (is_fp) {
3385 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3386 } else {
3387 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3388 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003389 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003390 DCHECK(is_wide);
3391 if (is_fp) {
3392 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3393 } else {
3394 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3395 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003396 } else {
3397 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003398 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003399 Location stack_temp = Location::StackSlot(temp_offset);
3400 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003401 if (is_fp) {
3402 __ flds(Address(ESP, temp_offset));
3403 } else {
3404 __ filds(Address(ESP, temp_offset));
3405 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003406 } else {
3407 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3408 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003409 if (is_fp) {
3410 __ fldl(Address(ESP, temp_offset));
3411 } else {
3412 __ fildl(Address(ESP, temp_offset));
3413 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003414 }
3415 }
3416}
3417
3418void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3419 Primitive::Type type = rem->GetResultType();
3420 bool is_float = type == Primitive::kPrimFloat;
3421 size_t elem_size = Primitive::ComponentSize(type);
3422 LocationSummary* locations = rem->GetLocations();
3423 Location first = locations->InAt(0);
3424 Location second = locations->InAt(1);
3425 Location out = locations->Out();
3426
3427 // Create stack space for 2 elements.
3428 // TODO: enhance register allocator to ask for stack temporaries.
3429 __ subl(ESP, Immediate(2 * elem_size));
3430
3431 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003432 const bool is_wide = !is_float;
3433 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3434 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003435
3436 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003437 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003438 __ Bind(&retry);
3439 __ fprem();
3440
3441 // Move FP status to AX.
3442 __ fstsw();
3443
3444 // And see if the argument reduction is complete. This is signaled by the
3445 // C2 FPU flag bit set to 0.
3446 __ andl(EAX, Immediate(kC2ConditionMask));
3447 __ j(kNotEqual, &retry);
3448
3449 // We have settled on the final value. Retrieve it into an XMM register.
3450 // Store FP top of stack to real stack.
3451 if (is_float) {
3452 __ fsts(Address(ESP, 0));
3453 } else {
3454 __ fstl(Address(ESP, 0));
3455 }
3456
3457 // Pop the 2 items from the FP stack.
3458 __ fucompp();
3459
3460 // Load the value from the stack into an XMM register.
3461 DCHECK(out.IsFpuRegister()) << out;
3462 if (is_float) {
3463 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3464 } else {
3465 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3466 }
3467
3468 // And remove the temporary stack space we allocated.
3469 __ addl(ESP, Immediate(2 * elem_size));
3470}
3471
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472
3473void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3474 DCHECK(instruction->IsDiv() || instruction->IsRem());
3475
3476 LocationSummary* locations = instruction->GetLocations();
3477 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003478 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479
3480 Register out_register = locations->Out().AsRegister<Register>();
3481 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003482 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483
3484 DCHECK(imm == 1 || imm == -1);
3485
3486 if (instruction->IsRem()) {
3487 __ xorl(out_register, out_register);
3488 } else {
3489 __ movl(out_register, input_register);
3490 if (imm == -1) {
3491 __ negl(out_register);
3492 }
3493 }
3494}
3495
3496
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003497void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 LocationSummary* locations = instruction->GetLocations();
3499
3500 Register out_register = locations->Out().AsRegister<Register>();
3501 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003502 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003503 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3504 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003505
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 Register num = locations->GetTemp(0).AsRegister<Register>();
3507
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003508 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 __ testl(input_register, input_register);
3510 __ cmovl(kGreaterEqual, num, input_register);
3511 int shift = CTZ(imm);
3512 __ sarl(num, Immediate(shift));
3513
3514 if (imm < 0) {
3515 __ negl(num);
3516 }
3517
3518 __ movl(out_register, num);
3519}
3520
3521void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3522 DCHECK(instruction->IsDiv() || instruction->IsRem());
3523
3524 LocationSummary* locations = instruction->GetLocations();
3525 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3526
3527 Register eax = locations->InAt(0).AsRegister<Register>();
3528 Register out = locations->Out().AsRegister<Register>();
3529 Register num;
3530 Register edx;
3531
3532 if (instruction->IsDiv()) {
3533 edx = locations->GetTemp(0).AsRegister<Register>();
3534 num = locations->GetTemp(1).AsRegister<Register>();
3535 } else {
3536 edx = locations->Out().AsRegister<Register>();
3537 num = locations->GetTemp(0).AsRegister<Register>();
3538 }
3539
3540 DCHECK_EQ(EAX, eax);
3541 DCHECK_EQ(EDX, edx);
3542 if (instruction->IsDiv()) {
3543 DCHECK_EQ(EAX, out);
3544 } else {
3545 DCHECK_EQ(EDX, out);
3546 }
3547
3548 int64_t magic;
3549 int shift;
3550 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3551
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003552 // Save the numerator.
3553 __ movl(num, eax);
3554
3555 // EAX = magic
3556 __ movl(eax, Immediate(magic));
3557
3558 // EDX:EAX = magic * numerator
3559 __ imull(num);
3560
3561 if (imm > 0 && magic < 0) {
3562 // EDX += num
3563 __ addl(edx, num);
3564 } else if (imm < 0 && magic > 0) {
3565 __ subl(edx, num);
3566 }
3567
3568 // Shift if needed.
3569 if (shift != 0) {
3570 __ sarl(edx, Immediate(shift));
3571 }
3572
3573 // EDX += 1 if EDX < 0
3574 __ movl(eax, edx);
3575 __ shrl(edx, Immediate(31));
3576 __ addl(edx, eax);
3577
3578 if (instruction->IsRem()) {
3579 __ movl(eax, num);
3580 __ imull(edx, Immediate(imm));
3581 __ subl(eax, edx);
3582 __ movl(edx, eax);
3583 } else {
3584 __ movl(eax, edx);
3585 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586}
3587
Calin Juravlebacfec32014-11-14 15:54:36 +00003588void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3589 DCHECK(instruction->IsDiv() || instruction->IsRem());
3590
3591 LocationSummary* locations = instruction->GetLocations();
3592 Location out = locations->Out();
3593 Location first = locations->InAt(0);
3594 Location second = locations->InAt(1);
3595 bool is_div = instruction->IsDiv();
3596
3597 switch (instruction->GetResultType()) {
3598 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003599 DCHECK_EQ(EAX, first.AsRegister<Register>());
3600 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003601
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003602 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003603 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003604
3605 if (imm == 0) {
3606 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3607 } else if (imm == 1 || imm == -1) {
3608 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003609 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003610 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003611 } else {
3612 DCHECK(imm <= -2 || imm >= 2);
3613 GenerateDivRemWithAnyConstant(instruction);
3614 }
3615 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003616 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3617 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003618 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003619
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 Register second_reg = second.AsRegister<Register>();
3621 // 0x80000000/-1 triggers an arithmetic exception!
3622 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3623 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003624
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003625 __ cmpl(second_reg, Immediate(-1));
3626 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003627
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 // edx:eax <- sign-extended of eax
3629 __ cdq();
3630 // eax = quotient, edx = remainder
3631 __ idivl(second_reg);
3632 __ Bind(slow_path->GetExitLabel());
3633 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636
3637 case Primitive::kPrimLong: {
3638 InvokeRuntimeCallingConvention calling_convention;
3639 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3640 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3641 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3642 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3643 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3644 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3645
3646 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003647 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003648 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003649 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003650 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003651 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 break;
3654 }
3655
3656 default:
3657 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3658 }
3659}
3660
Calin Juravle7c4954d2014-10-28 16:57:40 +00003661void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003662 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003663 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 : LocationSummary::kNoCall;
3665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3666
Calin Juravle7c4954d2014-10-28 16:57:40 +00003667 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003668 case Primitive::kPrimInt: {
3669 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003670 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003671 locations->SetOut(Location::SameAsFirstInput());
3672 // Intel uses edx:eax as the dividend.
3673 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003674 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3675 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3676 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003677 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003678 locations->AddTemp(Location::RequiresRegister());
3679 }
Calin Juravled0d48522014-11-04 16:40:20 +00003680 break;
3681 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003682 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003683 InvokeRuntimeCallingConvention calling_convention;
3684 locations->SetInAt(0, Location::RegisterPairLocation(
3685 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3686 locations->SetInAt(1, Location::RegisterPairLocation(
3687 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3688 // Runtime helper puts the result in EAX, EDX.
3689 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003690 break;
3691 }
3692 case Primitive::kPrimFloat:
3693 case Primitive::kPrimDouble: {
3694 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003695 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3696 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003697 } else if (div->InputAt(1)->IsConstant()) {
3698 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003699 } else {
3700 locations->SetInAt(1, Location::Any());
3701 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003702 locations->SetOut(Location::SameAsFirstInput());
3703 break;
3704 }
3705
3706 default:
3707 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3708 }
3709}
3710
3711void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3712 LocationSummary* locations = div->GetLocations();
3713 Location first = locations->InAt(0);
3714 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003715
3716 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003717 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003718 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003719 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003720 break;
3721 }
3722
3723 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003724 if (second.IsFpuRegister()) {
3725 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3726 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3727 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003728 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003729 __ divss(first.AsFpuRegister<XmmRegister>(),
3730 codegen_->LiteralFloatAddress(
3731 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003732 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3734 } else {
3735 DCHECK(second.IsStackSlot());
3736 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3737 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003738 break;
3739 }
3740
3741 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003742 if (second.IsFpuRegister()) {
3743 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3744 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3745 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003746 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003747 __ divsd(first.AsFpuRegister<XmmRegister>(),
3748 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003749 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3750 const_area->GetBaseMethodAddress(),
3751 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003752 } else {
3753 DCHECK(second.IsDoubleStackSlot());
3754 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3755 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003756 break;
3757 }
3758
3759 default:
3760 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3761 }
3762}
3763
Calin Juravlebacfec32014-11-14 15:54:36 +00003764void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003765 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003766
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003767 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003768 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003769 : LocationSummary::kNoCall;
3770 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003771
Calin Juravled2ec87d2014-12-08 14:24:46 +00003772 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003773 case Primitive::kPrimInt: {
3774 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003775 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003776 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003777 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3778 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3779 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003780 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003781 locations->AddTemp(Location::RequiresRegister());
3782 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003783 break;
3784 }
3785 case Primitive::kPrimLong: {
3786 InvokeRuntimeCallingConvention calling_convention;
3787 locations->SetInAt(0, Location::RegisterPairLocation(
3788 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3789 locations->SetInAt(1, Location::RegisterPairLocation(
3790 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3791 // Runtime helper puts the result in EAX, EDX.
3792 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3793 break;
3794 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003795 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003796 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003797 locations->SetInAt(0, Location::Any());
3798 locations->SetInAt(1, Location::Any());
3799 locations->SetOut(Location::RequiresFpuRegister());
3800 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 break;
3802 }
3803
3804 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003805 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003806 }
3807}
3808
3809void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3810 Primitive::Type type = rem->GetResultType();
3811 switch (type) {
3812 case Primitive::kPrimInt:
3813 case Primitive::kPrimLong: {
3814 GenerateDivRemIntegral(rem);
3815 break;
3816 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003817 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003819 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003820 break;
3821 }
3822 default:
3823 LOG(FATAL) << "Unexpected rem type " << type;
3824 }
3825}
3826
Calin Juravled0d48522014-11-04 16:40:20 +00003827void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003828 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003829 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003830 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003831 case Primitive::kPrimByte:
3832 case Primitive::kPrimChar:
3833 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003834 case Primitive::kPrimInt: {
3835 locations->SetInAt(0, Location::Any());
3836 break;
3837 }
3838 case Primitive::kPrimLong: {
3839 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3840 if (!instruction->IsConstant()) {
3841 locations->AddTemp(Location::RequiresRegister());
3842 }
3843 break;
3844 }
3845 default:
3846 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3847 }
Calin Juravled0d48522014-11-04 16:40:20 +00003848}
3849
3850void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003851 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003852 codegen_->AddSlowPath(slow_path);
3853
3854 LocationSummary* locations = instruction->GetLocations();
3855 Location value = locations->InAt(0);
3856
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003857 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003858 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003859 case Primitive::kPrimByte:
3860 case Primitive::kPrimChar:
3861 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003862 case Primitive::kPrimInt: {
3863 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003865 __ j(kEqual, slow_path->GetEntryLabel());
3866 } else if (value.IsStackSlot()) {
3867 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3868 __ j(kEqual, slow_path->GetEntryLabel());
3869 } else {
3870 DCHECK(value.IsConstant()) << value;
3871 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003872 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003873 }
3874 }
3875 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003876 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003877 case Primitive::kPrimLong: {
3878 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003880 __ movl(temp, value.AsRegisterPairLow<Register>());
3881 __ orl(temp, value.AsRegisterPairHigh<Register>());
3882 __ j(kEqual, slow_path->GetEntryLabel());
3883 } else {
3884 DCHECK(value.IsConstant()) << value;
3885 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3886 __ jmp(slow_path->GetEntryLabel());
3887 }
3888 }
3889 break;
3890 }
3891 default:
3892 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003893 }
Calin Juravled0d48522014-11-04 16:40:20 +00003894}
3895
Calin Juravle9aec02f2014-11-18 23:06:35 +00003896void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3897 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3898
3899 LocationSummary* locations =
3900 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3901
3902 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003903 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003905 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003907 // The shift count needs to be in CL or a constant.
3908 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 locations->SetOut(Location::SameAsFirstInput());
3910 break;
3911 }
3912 default:
3913 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3914 }
3915}
3916
3917void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3918 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3919
3920 LocationSummary* locations = op->GetLocations();
3921 Location first = locations->InAt(0);
3922 Location second = locations->InAt(1);
3923 DCHECK(first.Equals(locations->Out()));
3924
3925 switch (op->GetResultType()) {
3926 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003927 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003928 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003929 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003931 DCHECK_EQ(ECX, second_reg);
3932 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003933 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003934 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003935 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003937 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 }
3939 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003940 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003941 if (shift == 0) {
3942 return;
3943 }
3944 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003945 if (op->IsShl()) {
3946 __ shll(first_reg, imm);
3947 } else if (op->IsShr()) {
3948 __ sarl(first_reg, imm);
3949 } else {
3950 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951 }
3952 }
3953 break;
3954 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003955 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003956 if (second.IsRegister()) {
3957 Register second_reg = second.AsRegister<Register>();
3958 DCHECK_EQ(ECX, second_reg);
3959 if (op->IsShl()) {
3960 GenerateShlLong(first, second_reg);
3961 } else if (op->IsShr()) {
3962 GenerateShrLong(first, second_reg);
3963 } else {
3964 GenerateUShrLong(first, second_reg);
3965 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003966 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003967 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003968 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003969 // Nothing to do if the shift is 0, as the input is already the output.
3970 if (shift != 0) {
3971 if (op->IsShl()) {
3972 GenerateShlLong(first, shift);
3973 } else if (op->IsShr()) {
3974 GenerateShrLong(first, shift);
3975 } else {
3976 GenerateUShrLong(first, shift);
3977 }
3978 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003979 }
3980 break;
3981 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003982 default:
3983 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3984 }
3985}
3986
Mark P Mendell73945692015-04-29 14:56:17 +00003987void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3988 Register low = loc.AsRegisterPairLow<Register>();
3989 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003990 if (shift == 1) {
3991 // This is just an addition.
3992 __ addl(low, low);
3993 __ adcl(high, high);
3994 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003995 // Shift by 32 is easy. High gets low, and low gets 0.
3996 codegen_->EmitParallelMoves(
3997 loc.ToLow(),
3998 loc.ToHigh(),
3999 Primitive::kPrimInt,
4000 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4001 loc.ToLow(),
4002 Primitive::kPrimInt);
4003 } else if (shift > 32) {
4004 // Low part becomes 0. High part is low part << (shift-32).
4005 __ movl(high, low);
4006 __ shll(high, Immediate(shift - 32));
4007 __ xorl(low, low);
4008 } else {
4009 // Between 1 and 31.
4010 __ shld(high, low, Immediate(shift));
4011 __ shll(low, Immediate(shift));
4012 }
4013}
4014
Calin Juravle9aec02f2014-11-18 23:06:35 +00004015void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004016 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004017 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4018 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4019 __ testl(shifter, Immediate(32));
4020 __ j(kEqual, &done);
4021 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4022 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4023 __ Bind(&done);
4024}
4025
Mark P Mendell73945692015-04-29 14:56:17 +00004026void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4027 Register low = loc.AsRegisterPairLow<Register>();
4028 Register high = loc.AsRegisterPairHigh<Register>();
4029 if (shift == 32) {
4030 // Need to copy the sign.
4031 DCHECK_NE(low, high);
4032 __ movl(low, high);
4033 __ sarl(high, Immediate(31));
4034 } else if (shift > 32) {
4035 DCHECK_NE(low, high);
4036 // High part becomes sign. Low part is shifted by shift - 32.
4037 __ movl(low, high);
4038 __ sarl(high, Immediate(31));
4039 __ sarl(low, Immediate(shift - 32));
4040 } else {
4041 // Between 1 and 31.
4042 __ shrd(low, high, Immediate(shift));
4043 __ sarl(high, Immediate(shift));
4044 }
4045}
4046
Calin Juravle9aec02f2014-11-18 23:06:35 +00004047void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004048 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4050 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4051 __ testl(shifter, Immediate(32));
4052 __ j(kEqual, &done);
4053 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4054 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4055 __ Bind(&done);
4056}
4057
Mark P Mendell73945692015-04-29 14:56:17 +00004058void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4059 Register low = loc.AsRegisterPairLow<Register>();
4060 Register high = loc.AsRegisterPairHigh<Register>();
4061 if (shift == 32) {
4062 // Shift by 32 is easy. Low gets high, and high gets 0.
4063 codegen_->EmitParallelMoves(
4064 loc.ToHigh(),
4065 loc.ToLow(),
4066 Primitive::kPrimInt,
4067 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4068 loc.ToHigh(),
4069 Primitive::kPrimInt);
4070 } else if (shift > 32) {
4071 // Low part is high >> (shift - 32). High part becomes 0.
4072 __ movl(low, high);
4073 __ shrl(low, Immediate(shift - 32));
4074 __ xorl(high, high);
4075 } else {
4076 // Between 1 and 31.
4077 __ shrd(low, high, Immediate(shift));
4078 __ shrl(high, Immediate(shift));
4079 }
4080}
4081
Calin Juravle9aec02f2014-11-18 23:06:35 +00004082void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004083 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004084 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4085 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4086 __ testl(shifter, Immediate(32));
4087 __ j(kEqual, &done);
4088 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4089 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4090 __ Bind(&done);
4091}
4092
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004093void LocationsBuilderX86::VisitRor(HRor* ror) {
4094 LocationSummary* locations =
4095 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4096
4097 switch (ror->GetResultType()) {
4098 case Primitive::kPrimLong:
4099 // Add the temporary needed.
4100 locations->AddTemp(Location::RequiresRegister());
4101 FALLTHROUGH_INTENDED;
4102 case Primitive::kPrimInt:
4103 locations->SetInAt(0, Location::RequiresRegister());
4104 // The shift count needs to be in CL (unless it is a constant).
4105 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4106 locations->SetOut(Location::SameAsFirstInput());
4107 break;
4108 default:
4109 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4110 UNREACHABLE();
4111 }
4112}
4113
4114void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4115 LocationSummary* locations = ror->GetLocations();
4116 Location first = locations->InAt(0);
4117 Location second = locations->InAt(1);
4118
4119 if (ror->GetResultType() == Primitive::kPrimInt) {
4120 Register first_reg = first.AsRegister<Register>();
4121 if (second.IsRegister()) {
4122 Register second_reg = second.AsRegister<Register>();
4123 __ rorl(first_reg, second_reg);
4124 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004125 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004126 __ rorl(first_reg, imm);
4127 }
4128 return;
4129 }
4130
4131 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4132 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4133 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4134 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4135 if (second.IsRegister()) {
4136 Register second_reg = second.AsRegister<Register>();
4137 DCHECK_EQ(second_reg, ECX);
4138 __ movl(temp_reg, first_reg_hi);
4139 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4140 __ shrd(first_reg_lo, temp_reg, second_reg);
4141 __ movl(temp_reg, first_reg_hi);
4142 __ testl(second_reg, Immediate(32));
4143 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4144 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4145 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004146 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004147 if (shift_amt == 0) {
4148 // Already fine.
4149 return;
4150 }
4151 if (shift_amt == 32) {
4152 // Just swap.
4153 __ movl(temp_reg, first_reg_lo);
4154 __ movl(first_reg_lo, first_reg_hi);
4155 __ movl(first_reg_hi, temp_reg);
4156 return;
4157 }
4158
4159 Immediate imm(shift_amt);
4160 // Save the constents of the low value.
4161 __ movl(temp_reg, first_reg_lo);
4162
4163 // Shift right into low, feeding bits from high.
4164 __ shrd(first_reg_lo, first_reg_hi, imm);
4165
4166 // Shift right into high, feeding bits from the original low.
4167 __ shrd(first_reg_hi, temp_reg, imm);
4168
4169 // Swap if needed.
4170 if (shift_amt > 32) {
4171 __ movl(temp_reg, first_reg_lo);
4172 __ movl(first_reg_lo, first_reg_hi);
4173 __ movl(first_reg_hi, temp_reg);
4174 }
4175 }
4176}
4177
Calin Juravle9aec02f2014-11-18 23:06:35 +00004178void LocationsBuilderX86::VisitShl(HShl* shl) {
4179 HandleShift(shl);
4180}
4181
4182void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4183 HandleShift(shl);
4184}
4185
4186void LocationsBuilderX86::VisitShr(HShr* shr) {
4187 HandleShift(shr);
4188}
4189
4190void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4191 HandleShift(shr);
4192}
4193
4194void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4195 HandleShift(ushr);
4196}
4197
4198void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4199 HandleShift(ushr);
4200}
4201
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004202void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004203 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004205 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004206 if (instruction->IsStringAlloc()) {
4207 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4208 } else {
4209 InvokeRuntimeCallingConvention calling_convention;
4210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004211 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004212}
4213
4214void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004215 // Note: if heap poisoning is enabled, the entry point takes cares
4216 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004217 if (instruction->IsStringAlloc()) {
4218 // String is allocated through StringFactory. Call NewEmptyString entry point.
4219 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004220 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004221 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4222 __ call(Address(temp, code_offset.Int32Value()));
4223 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4224 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004225 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004226 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004227 DCHECK(!codegen_->IsLeafMethod());
4228 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004229}
4230
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4232 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234 locations->SetOut(Location::RegisterLocation(EAX));
4235 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004236 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4237 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238}
4239
4240void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004241 // Note: if heap poisoning is enabled, the entry point takes cares
4242 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004243 QuickEntrypointEnum entrypoint =
4244 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4245 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004246 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247 DCHECK(!codegen_->IsLeafMethod());
4248}
4249
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004250void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004251 LocationSummary* locations =
4252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004253 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4254 if (location.IsStackSlot()) {
4255 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4256 } else if (location.IsDoubleStackSlot()) {
4257 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004258 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004259 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004260}
4261
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004262void InstructionCodeGeneratorX86::VisitParameterValue(
4263 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4264}
4265
4266void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4267 LocationSummary* locations =
4268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4269 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4270}
4271
4272void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004273}
4274
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004275void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4276 LocationSummary* locations =
4277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4278 locations->SetInAt(0, Location::RequiresRegister());
4279 locations->SetOut(Location::RequiresRegister());
4280}
4281
4282void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4283 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004284 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004285 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004286 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004287 __ movl(locations->Out().AsRegister<Register>(),
4288 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004289 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004290 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004291 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004292 __ movl(locations->Out().AsRegister<Register>(),
4293 Address(locations->InAt(0).AsRegister<Register>(),
4294 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4295 // temp = temp->GetImtEntryAt(method_offset);
4296 __ movl(locations->Out().AsRegister<Register>(),
4297 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004298 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004299}
4300
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004301void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004302 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004303 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004304 locations->SetInAt(0, Location::RequiresRegister());
4305 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004306}
4307
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004308void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4309 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004310 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004311 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004312 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004313 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004314 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004315 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004316 break;
4317
4318 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004319 __ notl(out.AsRegisterPairLow<Register>());
4320 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004321 break;
4322
4323 default:
4324 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4325 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004326}
4327
David Brazdil66d126e2015-04-03 16:02:44 +01004328void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4329 LocationSummary* locations =
4330 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4331 locations->SetInAt(0, Location::RequiresRegister());
4332 locations->SetOut(Location::SameAsFirstInput());
4333}
4334
4335void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004336 LocationSummary* locations = bool_not->GetLocations();
4337 Location in = locations->InAt(0);
4338 Location out = locations->Out();
4339 DCHECK(in.Equals(out));
4340 __ xorl(out.AsRegister<Register>(), Immediate(1));
4341}
4342
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004343void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004344 LocationSummary* locations =
4345 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004346 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004347 case Primitive::kPrimBoolean:
4348 case Primitive::kPrimByte:
4349 case Primitive::kPrimShort:
4350 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004351 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004352 case Primitive::kPrimLong: {
4353 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004354 locations->SetInAt(1, Location::Any());
4355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4356 break;
4357 }
4358 case Primitive::kPrimFloat:
4359 case Primitive::kPrimDouble: {
4360 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004361 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4362 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4363 } else if (compare->InputAt(1)->IsConstant()) {
4364 locations->SetInAt(1, Location::RequiresFpuRegister());
4365 } else {
4366 locations->SetInAt(1, Location::Any());
4367 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004368 locations->SetOut(Location::RequiresRegister());
4369 break;
4370 }
4371 default:
4372 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4373 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004374}
4375
4376void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004377 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004378 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004379 Location left = locations->InAt(0);
4380 Location right = locations->InAt(1);
4381
Mark Mendell0c9497d2015-08-21 09:30:05 -04004382 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004383 Condition less_cond = kLess;
4384
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004385 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004386 case Primitive::kPrimBoolean:
4387 case Primitive::kPrimByte:
4388 case Primitive::kPrimShort:
4389 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004390 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004391 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004392 break;
4393 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004394 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004395 Register left_low = left.AsRegisterPairLow<Register>();
4396 Register left_high = left.AsRegisterPairHigh<Register>();
4397 int32_t val_low = 0;
4398 int32_t val_high = 0;
4399 bool right_is_const = false;
4400
4401 if (right.IsConstant()) {
4402 DCHECK(right.GetConstant()->IsLongConstant());
4403 right_is_const = true;
4404 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4405 val_low = Low32Bits(val);
4406 val_high = High32Bits(val);
4407 }
4408
Calin Juravleddb7df22014-11-25 20:56:51 +00004409 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004410 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004411 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004412 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004413 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004414 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004415 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004417 __ j(kLess, &less); // Signed compare.
4418 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004419 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004420 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004421 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004423 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004425 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426 }
Aart Bika19616e2016-02-01 18:57:58 -08004427 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004428 break;
4429 }
4430 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004431 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004432 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004433 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004434 break;
4435 }
4436 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004437 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004438 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004439 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004440 break;
4441 }
4442 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004443 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004444 }
Aart Bika19616e2016-02-01 18:57:58 -08004445
Calin Juravleddb7df22014-11-25 20:56:51 +00004446 __ movl(out, Immediate(0));
4447 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004448 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004449
4450 __ Bind(&greater);
4451 __ movl(out, Immediate(1));
4452 __ jmp(&done);
4453
4454 __ Bind(&less);
4455 __ movl(out, Immediate(-1));
4456
4457 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004458}
4459
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004460void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004461 LocationSummary* locations =
4462 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004463 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004464 locations->SetInAt(i, Location::Any());
4465 }
4466 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004467}
4468
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004469void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004470 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004471}
4472
Roland Levillain7c1559a2015-12-15 10:55:36 +00004473void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004474 /*
4475 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4476 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4477 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4478 */
4479 switch (kind) {
4480 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004481 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004482 break;
4483 }
4484 case MemBarrierKind::kAnyStore:
4485 case MemBarrierKind::kLoadAny:
4486 case MemBarrierKind::kStoreStore: {
4487 // nop
4488 break;
4489 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004490 case MemBarrierKind::kNTStoreStore:
4491 // Non-Temporal Store/Store needs an explicit fence.
4492 MemoryFence(/* non-temporal */ true);
4493 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004494 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004495}
4496
Vladimir Markodc151b22015-10-15 18:02:30 +01004497HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4498 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004499 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004500 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004501}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004503Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4504 Register temp) {
4505 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004506 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004507 if (!invoke->GetLocations()->Intrinsified()) {
4508 return location.AsRegister<Register>();
4509 }
4510 // For intrinsics we allow any location, so it may be on the stack.
4511 if (!location.IsRegister()) {
4512 __ movl(temp, Address(ESP, location.GetStackIndex()));
4513 return temp;
4514 }
4515 // For register locations, check if the register was saved. If so, get it from the stack.
4516 // Note: There is a chance that the register was saved but not overwritten, so we could
4517 // save one load. However, since this is just an intrinsic slow path we prefer this
4518 // simple and more robust approach rather that trying to determine if that's the case.
4519 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004520 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4521 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4522 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4523 __ movl(temp, Address(ESP, stack_offset));
4524 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004525 }
4526 return location.AsRegister<Register>();
4527}
4528
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004529void CodeGeneratorX86::GenerateStaticOrDirectCall(
4530 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004531 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4532 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004533 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004534 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004535 uint32_t offset =
4536 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4537 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004538 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004539 }
Vladimir Marko58155012015-08-19 12:49:41 +00004540 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004541 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004542 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004543 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4544 DCHECK(GetCompilerOptions().IsBootImage());
4545 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4546 temp.AsRegister<Register>());
4547 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4548 RecordBootMethodPatch(invoke);
4549 break;
4550 }
Vladimir Marko58155012015-08-19 12:49:41 +00004551 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4552 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4553 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004554 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004555 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4556 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004557 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004558 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004559 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004560 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004561 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004562 break;
4563 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004564 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4565 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4566 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004567 }
Vladimir Marko58155012015-08-19 12:49:41 +00004568 }
4569
4570 switch (invoke->GetCodePtrLocation()) {
4571 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4572 __ call(GetFrameEntryLabel());
4573 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004574 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4575 // (callee_method + offset_of_quick_compiled_code)()
4576 __ call(Address(callee_method.AsRegister<Register>(),
4577 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004578 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004579 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004580 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004581 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004582
4583 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004584}
4585
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004586void CodeGeneratorX86::GenerateVirtualCall(
4587 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004588 Register temp = temp_in.AsRegister<Register>();
4589 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4590 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004591
4592 // Use the calling convention instead of the location of the receiver, as
4593 // intrinsics may have put the receiver in a different register. In the intrinsics
4594 // slow path, the arguments have been moved to the right place, so here we are
4595 // guaranteed that the receiver is the first register of the calling convention.
4596 InvokeDexCallingConvention calling_convention;
4597 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004599 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004600 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004601 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004602 // Instead of simply (possibly) unpoisoning `temp` here, we should
4603 // emit a read barrier for the previous class reference load.
4604 // However this is not required in practice, as this is an
4605 // intermediate/temporary reference and because the current
4606 // concurrent copying collector keeps the from-space memory
4607 // intact/accessible until the end of the marking phase (the
4608 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004609 __ MaybeUnpoisonHeapReference(temp);
4610 // temp = temp->GetMethodAt(method_offset);
4611 __ movl(temp, Address(temp, method_offset));
4612 // call temp->GetEntryPoint();
4613 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004614 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004615 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004616}
4617
Vladimir Marko65979462017-05-19 17:25:12 +01004618void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4619 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4620 HX86ComputeBaseMethodAddress* address =
4621 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4622 boot_image_method_patches_.emplace_back(address,
4623 *invoke->GetTargetMethod().dex_file,
4624 invoke->GetTargetMethod().dex_method_index);
4625 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004626}
4627
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004628Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4629 HX86ComputeBaseMethodAddress* method_address,
4630 MethodReference target_method) {
4631 // Add the patch entry and bind its label at the end of the instruction.
4632 method_bss_entry_patches_.emplace_back(method_address,
4633 *target_method.dex_file,
4634 target_method.dex_method_index);
4635 return &method_bss_entry_patches_.back().label;
4636}
4637
Vladimir Marko1998cd02017-01-13 13:02:58 +00004638void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004639 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004640 boot_image_type_patches_.emplace_back(address,
4641 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004642 load_class->GetTypeIndex().index_);
4643 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004644}
4645
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004646Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004647 HX86ComputeBaseMethodAddress* address =
4648 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4649 type_bss_entry_patches_.emplace_back(
4650 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004651 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004652}
4653
Vladimir Marko65979462017-05-19 17:25:12 +01004654void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4655 DCHECK(GetCompilerOptions().IsBootImage());
4656 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4657 string_patches_.emplace_back(address,
4658 load_string->GetDexFile(),
4659 load_string->GetStringIndex().index_);
4660 __ Bind(&string_patches_.back().label);
4661}
4662
Vladimir Markoaad75c62016-10-03 08:46:48 +00004663Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4664 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004665 HX86ComputeBaseMethodAddress* address =
4666 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4667 string_patches_.emplace_back(
4668 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004669 return &string_patches_.back().label;
4670}
4671
Vladimir Markoaad75c62016-10-03 08:46:48 +00004672// The label points to the end of the "movl" or another instruction but the literal offset
4673// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4674constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4675
4676template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4677inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004678 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004680 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004681 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004682 linker_patches->push_back(Factory(
4683 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004684 }
4685}
4686
Vladimir Marko58155012015-08-19 12:49:41 +00004687void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4688 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004689 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004690 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004691 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004692 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004693 type_bss_entry_patches_.size() +
4694 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004695 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004696 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01004697 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
4698 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004699 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4700 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004701 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004702 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004703 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01004704 DCHECK(boot_image_type_patches_.empty());
4705 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004706 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004707 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4708 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004709 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4710 linker_patches);
4711 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004712}
4713
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004714void CodeGeneratorX86::MarkGCCard(Register temp,
4715 Register card,
4716 Register object,
4717 Register value,
4718 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004719 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004720 if (value_can_be_null) {
4721 __ testl(value, value);
4722 __ j(kEqual, &is_null);
4723 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004724 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004725 __ movl(temp, object);
4726 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004727 __ movb(Address(temp, card, TIMES_1, 0),
4728 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 if (value_can_be_null) {
4730 __ Bind(&is_null);
4731 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732}
4733
Calin Juravle52c48962014-12-16 17:02:57 +00004734void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4735 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004736
4737 bool object_field_get_with_read_barrier =
4738 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004739 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004740 new (GetGraph()->GetArena()) LocationSummary(instruction,
4741 kEmitCompilerReadBarrier ?
4742 LocationSummary::kCallOnSlowPath :
4743 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004744 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004745 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004746 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004747 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004748
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004749 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4750 locations->SetOut(Location::RequiresFpuRegister());
4751 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004752 // The output overlaps in case of long: we don't want the low move
4753 // to overwrite the object's location. Likewise, in the case of
4754 // an object field get with read barriers enabled, we do not want
4755 // the move to overwrite the object's location, as we need it to emit
4756 // the read barrier.
4757 locations->SetOut(
4758 Location::RequiresRegister(),
4759 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4760 Location::kOutputOverlap :
4761 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004762 }
Calin Juravle52c48962014-12-16 17:02:57 +00004763
4764 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4765 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004766 // So we use an XMM register as a temp to achieve atomicity (first
4767 // load the temp into the XMM and then copy the XMM into the
4768 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004769 locations->AddTemp(Location::RequiresFpuRegister());
4770 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004771}
4772
Calin Juravle52c48962014-12-16 17:02:57 +00004773void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4774 const FieldInfo& field_info) {
4775 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004776
Calin Juravle52c48962014-12-16 17:02:57 +00004777 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004778 Location base_loc = locations->InAt(0);
4779 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004780 Location out = locations->Out();
4781 bool is_volatile = field_info.IsVolatile();
4782 Primitive::Type field_type = field_info.GetFieldType();
4783 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4784
4785 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004786 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004787 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004788 break;
4789 }
4790
4791 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004792 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004793 break;
4794 }
4795
4796 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004797 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004798 break;
4799 }
4800
4801 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004802 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004803 break;
4804 }
4805
4806 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004807 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004808 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004809
4810 case Primitive::kPrimNot: {
4811 // /* HeapReference<Object> */ out = *(base + offset)
4812 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004813 // Note that a potential implicit null check is handled in this
4814 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4815 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004816 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004817 if (is_volatile) {
4818 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4819 }
4820 } else {
4821 __ movl(out.AsRegister<Register>(), Address(base, offset));
4822 codegen_->MaybeRecordImplicitNullCheck(instruction);
4823 if (is_volatile) {
4824 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4825 }
4826 // If read barriers are enabled, emit read barriers other than
4827 // Baker's using a slow path (and also unpoison the loaded
4828 // reference, if heap poisoning is enabled).
4829 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4830 }
4831 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832 }
4833
4834 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004835 if (is_volatile) {
4836 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4837 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004838 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004839 __ movd(out.AsRegisterPairLow<Register>(), temp);
4840 __ psrlq(temp, Immediate(32));
4841 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4842 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004843 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004844 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004845 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004846 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4847 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004848 break;
4849 }
4850
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004851 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004852 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004853 break;
4854 }
4855
4856 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004857 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004858 break;
4859 }
4860
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004861 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004862 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004863 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004864 }
Calin Juravle52c48962014-12-16 17:02:57 +00004865
Roland Levillain7c1559a2015-12-15 10:55:36 +00004866 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4867 // Potential implicit null checks, in the case of reference or
4868 // long fields, are handled in the previous switch statement.
4869 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004870 codegen_->MaybeRecordImplicitNullCheck(instruction);
4871 }
4872
Calin Juravle52c48962014-12-16 17:02:57 +00004873 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004874 if (field_type == Primitive::kPrimNot) {
4875 // Memory barriers, in the case of references, are also handled
4876 // in the previous switch statement.
4877 } else {
4878 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4879 }
Roland Levillain4d027112015-07-01 15:41:14 +01004880 }
Calin Juravle52c48962014-12-16 17:02:57 +00004881}
4882
4883void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4884 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4885
4886 LocationSummary* locations =
4887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4888 locations->SetInAt(0, Location::RequiresRegister());
4889 bool is_volatile = field_info.IsVolatile();
4890 Primitive::Type field_type = field_info.GetFieldType();
4891 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4892 || (field_type == Primitive::kPrimByte);
4893
4894 // The register allocator does not support multiple
4895 // inputs that die at entry with one in a specific register.
4896 if (is_byte_type) {
4897 // Ensure the value is in a byte register.
4898 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004899 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004900 if (is_volatile && field_type == Primitive::kPrimDouble) {
4901 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4902 locations->SetInAt(1, Location::RequiresFpuRegister());
4903 } else {
4904 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4905 }
4906 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4907 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004908 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004909
Calin Juravle52c48962014-12-16 17:02:57 +00004910 // 64bits value can be atomically written to an address with movsd and an XMM register.
4911 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4912 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4913 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4914 // isolated cases when we need this it isn't worth adding the extra complexity.
4915 locations->AddTemp(Location::RequiresFpuRegister());
4916 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004917 } else {
4918 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4919
4920 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4921 // Temporary registers for the write barrier.
4922 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4923 // Ensure the card is in a byte register.
4924 locations->AddTemp(Location::RegisterLocation(ECX));
4925 }
Calin Juravle52c48962014-12-16 17:02:57 +00004926 }
4927}
4928
4929void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004930 const FieldInfo& field_info,
4931 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004932 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4933
4934 LocationSummary* locations = instruction->GetLocations();
4935 Register base = locations->InAt(0).AsRegister<Register>();
4936 Location value = locations->InAt(1);
4937 bool is_volatile = field_info.IsVolatile();
4938 Primitive::Type field_type = field_info.GetFieldType();
4939 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004940 bool needs_write_barrier =
4941 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004942
4943 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004944 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004945 }
4946
Mark Mendell81489372015-11-04 11:30:41 -05004947 bool maybe_record_implicit_null_check_done = false;
4948
Calin Juravle52c48962014-12-16 17:02:57 +00004949 switch (field_type) {
4950 case Primitive::kPrimBoolean:
4951 case Primitive::kPrimByte: {
4952 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4953 break;
4954 }
4955
4956 case Primitive::kPrimShort:
4957 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004958 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004959 __ movw(Address(base, offset),
4960 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004961 } else {
4962 __ movw(Address(base, offset), value.AsRegister<Register>());
4963 }
Calin Juravle52c48962014-12-16 17:02:57 +00004964 break;
4965 }
4966
4967 case Primitive::kPrimInt:
4968 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004969 if (kPoisonHeapReferences && needs_write_barrier) {
4970 // Note that in the case where `value` is a null reference,
4971 // we do not enter this block, as the reference does not
4972 // need poisoning.
4973 DCHECK_EQ(field_type, Primitive::kPrimNot);
4974 Register temp = locations->GetTemp(0).AsRegister<Register>();
4975 __ movl(temp, value.AsRegister<Register>());
4976 __ PoisonHeapReference(temp);
4977 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004978 } else if (value.IsConstant()) {
4979 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4980 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004981 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004982 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004983 __ movl(Address(base, offset), value.AsRegister<Register>());
4984 }
Calin Juravle52c48962014-12-16 17:02:57 +00004985 break;
4986 }
4987
4988 case Primitive::kPrimLong: {
4989 if (is_volatile) {
4990 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4991 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4992 __ movd(temp1, value.AsRegisterPairLow<Register>());
4993 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4994 __ punpckldq(temp1, temp2);
4995 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004996 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004997 } else if (value.IsConstant()) {
4998 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4999 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5000 codegen_->MaybeRecordImplicitNullCheck(instruction);
5001 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005002 } else {
5003 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005004 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005005 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5006 }
Mark Mendell81489372015-11-04 11:30:41 -05005007 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005008 break;
5009 }
5010
5011 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005012 if (value.IsConstant()) {
5013 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5014 __ movl(Address(base, offset), Immediate(v));
5015 } else {
5016 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5017 }
Calin Juravle52c48962014-12-16 17:02:57 +00005018 break;
5019 }
5020
5021 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005022 if (value.IsConstant()) {
5023 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5024 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5025 codegen_->MaybeRecordImplicitNullCheck(instruction);
5026 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5027 maybe_record_implicit_null_check_done = true;
5028 } else {
5029 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5030 }
Calin Juravle52c48962014-12-16 17:02:57 +00005031 break;
5032 }
5033
5034 case Primitive::kPrimVoid:
5035 LOG(FATAL) << "Unreachable type " << field_type;
5036 UNREACHABLE();
5037 }
5038
Mark Mendell81489372015-11-04 11:30:41 -05005039 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 codegen_->MaybeRecordImplicitNullCheck(instruction);
5041 }
5042
Roland Levillain4d027112015-07-01 15:41:14 +01005043 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005044 Register temp = locations->GetTemp(0).AsRegister<Register>();
5045 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005046 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005047 }
5048
Calin Juravle52c48962014-12-16 17:02:57 +00005049 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005050 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005051 }
5052}
5053
5054void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5055 HandleFieldGet(instruction, instruction->GetFieldInfo());
5056}
5057
5058void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5059 HandleFieldGet(instruction, instruction->GetFieldInfo());
5060}
5061
5062void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5063 HandleFieldSet(instruction, instruction->GetFieldInfo());
5064}
5065
5066void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005067 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005068}
5069
5070void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5071 HandleFieldSet(instruction, instruction->GetFieldInfo());
5072}
5073
5074void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005075 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005076}
5077
5078void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5079 HandleFieldGet(instruction, instruction->GetFieldInfo());
5080}
5081
5082void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5083 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005084}
5085
Calin Juravlee460d1d2015-09-29 04:52:17 +01005086void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5087 HUnresolvedInstanceFieldGet* instruction) {
5088 FieldAccessCallingConventionX86 calling_convention;
5089 codegen_->CreateUnresolvedFieldLocationSummary(
5090 instruction, instruction->GetFieldType(), calling_convention);
5091}
5092
5093void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5094 HUnresolvedInstanceFieldGet* instruction) {
5095 FieldAccessCallingConventionX86 calling_convention;
5096 codegen_->GenerateUnresolvedFieldAccess(instruction,
5097 instruction->GetFieldType(),
5098 instruction->GetFieldIndex(),
5099 instruction->GetDexPc(),
5100 calling_convention);
5101}
5102
5103void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5104 HUnresolvedInstanceFieldSet* instruction) {
5105 FieldAccessCallingConventionX86 calling_convention;
5106 codegen_->CreateUnresolvedFieldLocationSummary(
5107 instruction, instruction->GetFieldType(), calling_convention);
5108}
5109
5110void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5111 HUnresolvedInstanceFieldSet* instruction) {
5112 FieldAccessCallingConventionX86 calling_convention;
5113 codegen_->GenerateUnresolvedFieldAccess(instruction,
5114 instruction->GetFieldType(),
5115 instruction->GetFieldIndex(),
5116 instruction->GetDexPc(),
5117 calling_convention);
5118}
5119
5120void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5121 HUnresolvedStaticFieldGet* instruction) {
5122 FieldAccessCallingConventionX86 calling_convention;
5123 codegen_->CreateUnresolvedFieldLocationSummary(
5124 instruction, instruction->GetFieldType(), calling_convention);
5125}
5126
5127void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5128 HUnresolvedStaticFieldGet* instruction) {
5129 FieldAccessCallingConventionX86 calling_convention;
5130 codegen_->GenerateUnresolvedFieldAccess(instruction,
5131 instruction->GetFieldType(),
5132 instruction->GetFieldIndex(),
5133 instruction->GetDexPc(),
5134 calling_convention);
5135}
5136
5137void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5138 HUnresolvedStaticFieldSet* instruction) {
5139 FieldAccessCallingConventionX86 calling_convention;
5140 codegen_->CreateUnresolvedFieldLocationSummary(
5141 instruction, instruction->GetFieldType(), calling_convention);
5142}
5143
5144void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5145 HUnresolvedStaticFieldSet* instruction) {
5146 FieldAccessCallingConventionX86 calling_convention;
5147 codegen_->GenerateUnresolvedFieldAccess(instruction,
5148 instruction->GetFieldType(),
5149 instruction->GetFieldIndex(),
5150 instruction->GetDexPc(),
5151 calling_convention);
5152}
5153
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005155 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5156 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5157 ? Location::RequiresRegister()
5158 : Location::Any();
5159 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005160}
5161
Calin Juravle2ae48182016-03-16 14:05:09 +00005162void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5163 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 return;
5165 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005166 LocationSummary* locations = instruction->GetLocations();
5167 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005168
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005169 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005170 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005171}
5172
Calin Juravle2ae48182016-03-16 14:05:09 +00005173void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005174 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005175 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005176
5177 LocationSummary* locations = instruction->GetLocations();
5178 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179
5180 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005181 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005182 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005184 } else {
5185 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005186 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005187 __ jmp(slow_path->GetEntryLabel());
5188 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005189 }
5190 __ j(kEqual, slow_path->GetEntryLabel());
5191}
5192
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005193void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005194 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005195}
5196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005198 bool object_array_get_with_read_barrier =
5199 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005200 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005201 new (GetGraph()->GetArena()) LocationSummary(instruction,
5202 object_array_get_with_read_barrier ?
5203 LocationSummary::kCallOnSlowPath :
5204 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005205 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005206 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005207 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005208 locations->SetInAt(0, Location::RequiresRegister());
5209 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005210 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5211 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5212 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005213 // The output overlaps in case of long: we don't want the low move
5214 // to overwrite the array's location. Likewise, in the case of an
5215 // object array get with read barriers enabled, we do not want the
5216 // move to overwrite the array's location, as we need it to emit
5217 // the read barrier.
5218 locations->SetOut(
5219 Location::RequiresRegister(),
5220 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5221 Location::kOutputOverlap :
5222 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005223 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224}
5225
5226void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5227 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005228 Location obj_loc = locations->InAt(0);
5229 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005231 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005232 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005233
Calin Juravle77520bc2015-01-12 18:45:46 +00005234 Primitive::Type type = instruction->GetType();
5235 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005237 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005238 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 break;
5240 }
5241
5242 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005243 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005244 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 break;
5246 }
5247
5248 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005249 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005250 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 break;
5252 }
5253
5254 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005255 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005256 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5257 // Branch cases into compressed and uncompressed for each index's type.
5258 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5259 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005260 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005261 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005262 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5263 "Expecting 0=compressed, 1=uncompressed");
5264 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005265 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5266 __ jmp(&done);
5267 __ Bind(&not_compressed);
5268 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5269 __ Bind(&done);
5270 } else {
5271 // Common case for charAt of array of char or when string compression's
5272 // feature is turned off.
5273 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5274 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 break;
5276 }
5277
Roland Levillain7c1559a2015-12-15 10:55:36 +00005278 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005279 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005280 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 break;
5282 }
5283
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 case Primitive::kPrimNot: {
5285 static_assert(
5286 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5287 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005288 // /* HeapReference<Object> */ out =
5289 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5290 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 // Note that a potential implicit null check is handled in this
5292 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5293 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005294 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005295 } else {
5296 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005297 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5298 codegen_->MaybeRecordImplicitNullCheck(instruction);
5299 // If read barriers are enabled, emit read barriers other than
5300 // Baker's using a slow path (and also unpoison the loaded
5301 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005302 if (index.IsConstant()) {
5303 uint32_t offset =
5304 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005305 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5306 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005307 codegen_->MaybeGenerateReadBarrierSlow(
5308 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5309 }
5310 }
5311 break;
5312 }
5313
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005315 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005316 __ movl(out_loc.AsRegisterPairLow<Register>(),
5317 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5318 codegen_->MaybeRecordImplicitNullCheck(instruction);
5319 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5320 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 break;
5322 }
5323
Mark Mendell7c8d0092015-01-26 11:21:33 -05005324 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005325 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005326 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005327 break;
5328 }
5329
5330 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005331 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005332 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005333 break;
5334 }
5335
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005338 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005340
Roland Levillain7c1559a2015-12-15 10:55:36 +00005341 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5342 // Potential implicit null checks, in the case of reference or
5343 // long arrays, are handled in the previous switch statement.
5344 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005345 codegen_->MaybeRecordImplicitNullCheck(instruction);
5346 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005347}
5348
5349void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005350 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005351
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005352 bool needs_write_barrier =
5353 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005355
Nicolas Geoffray39468442014-09-02 15:17:15 +01005356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5357 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005358 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005359 LocationSummary::kCallOnSlowPath :
5360 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005361
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5363 || (value_type == Primitive::kPrimByte);
5364 // We need the inputs to be different than the output in case of long operation.
5365 // In case of a byte operation, the register allocator does not support multiple
5366 // inputs that die at entry with one in a specific register.
5367 locations->SetInAt(0, Location::RequiresRegister());
5368 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5369 if (is_byte_type) {
5370 // Ensure the value is in a byte register.
5371 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5372 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005373 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005375 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5376 }
5377 if (needs_write_barrier) {
5378 // Temporary registers for the write barrier.
5379 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5380 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005381 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383}
5384
5385void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5386 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005387 Location array_loc = locations->InAt(0);
5388 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005390 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005391 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5393 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5394 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005395 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005396 bool needs_write_barrier =
5397 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398
5399 switch (value_type) {
5400 case Primitive::kPrimBoolean:
5401 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005403 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005404 if (value.IsRegister()) {
5405 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005407 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005409 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410 break;
5411 }
5412
5413 case Primitive::kPrimShort:
5414 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005416 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 if (value.IsRegister()) {
5418 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005420 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005422 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 break;
5424 }
5425
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005426 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005427 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005428 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005429
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 if (!value.IsRegister()) {
5431 // Just setting null.
5432 DCHECK(instruction->InputAt(2)->IsNullConstant());
5433 DCHECK(value.IsConstant()) << value;
5434 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005437 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005438 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005439 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440
5441 DCHECK(needs_write_barrier);
5442 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005443 // We cannot use a NearLabel for `done`, as its range may be too
5444 // short when Baker read barriers are enabled.
5445 Label done;
5446 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005447 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005448 Location temp_loc = locations->GetTemp(0);
5449 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005450 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005451 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5452 codegen_->AddSlowPath(slow_path);
5453 if (instruction->GetValueCanBeNull()) {
5454 __ testl(register_value, register_value);
5455 __ j(kNotEqual, &not_null);
5456 __ movl(address, Immediate(0));
5457 codegen_->MaybeRecordImplicitNullCheck(instruction);
5458 __ jmp(&done);
5459 __ Bind(&not_null);
5460 }
5461
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005462 // Note that when Baker read barriers are enabled, the type
5463 // checks are performed without read barriers. This is fine,
5464 // even in the case where a class object is in the from-space
5465 // after the flip, as a comparison involving such a type would
5466 // not produce a false positive; it may of course produce a
5467 // false negative, in which case we would take the ArraySet
5468 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005469
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005470 // /* HeapReference<Class> */ temp = array->klass_
5471 __ movl(temp, Address(array, class_offset));
5472 codegen_->MaybeRecordImplicitNullCheck(instruction);
5473 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005474
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005475 // /* HeapReference<Class> */ temp = temp->component_type_
5476 __ movl(temp, Address(temp, component_offset));
5477 // If heap poisoning is enabled, no need to unpoison `temp`
5478 // nor the object reference in `register_value->klass`, as
5479 // we are comparing two poisoned references.
5480 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005481
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005482 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5483 __ j(kEqual, &do_put);
5484 // If heap poisoning is enabled, the `temp` reference has
5485 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005486 __ MaybeUnpoisonHeapReference(temp);
5487
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005488 // If heap poisoning is enabled, no need to unpoison the
5489 // heap reference loaded below, as it is only used for a
5490 // comparison with null.
5491 __ cmpl(Address(temp, super_offset), Immediate(0));
5492 __ j(kNotEqual, slow_path->GetEntryLabel());
5493 __ Bind(&do_put);
5494 } else {
5495 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005496 }
5497 }
5498
5499 if (kPoisonHeapReferences) {
5500 __ movl(temp, register_value);
5501 __ PoisonHeapReference(temp);
5502 __ movl(address, temp);
5503 } else {
5504 __ movl(address, register_value);
5505 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005506 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005507 codegen_->MaybeRecordImplicitNullCheck(instruction);
5508 }
5509
5510 Register card = locations->GetTemp(1).AsRegister<Register>();
5511 codegen_->MarkGCCard(
5512 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5513 __ Bind(&done);
5514
5515 if (slow_path != nullptr) {
5516 __ Bind(slow_path->GetExitLabel());
5517 }
5518
5519 break;
5520 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005521
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005522 case Primitive::kPrimInt: {
5523 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005524 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005525 if (value.IsRegister()) {
5526 __ movl(address, value.AsRegister<Register>());
5527 } else {
5528 DCHECK(value.IsConstant()) << value;
5529 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5530 __ movl(address, Immediate(v));
5531 }
5532 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005533 break;
5534 }
5535
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536 case Primitive::kPrimLong: {
5537 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005538 if (value.IsRegisterPair()) {
5539 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5540 value.AsRegisterPairLow<Register>());
5541 codegen_->MaybeRecordImplicitNullCheck(instruction);
5542 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5543 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005545 DCHECK(value.IsConstant());
5546 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5547 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5548 Immediate(Low32Bits(val)));
5549 codegen_->MaybeRecordImplicitNullCheck(instruction);
5550 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5551 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005552 }
5553 break;
5554 }
5555
Mark Mendell7c8d0092015-01-26 11:21:33 -05005556 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005557 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005558 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005559 if (value.IsFpuRegister()) {
5560 __ movss(address, value.AsFpuRegister<XmmRegister>());
5561 } else {
5562 DCHECK(value.IsConstant());
5563 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5564 __ movl(address, Immediate(v));
5565 }
5566 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005567 break;
5568 }
5569
5570 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005571 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005572 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005573 if (value.IsFpuRegister()) {
5574 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5575 } else {
5576 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005577 Address address_hi =
5578 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005579 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5580 __ movl(address, Immediate(Low32Bits(v)));
5581 codegen_->MaybeRecordImplicitNullCheck(instruction);
5582 __ movl(address_hi, Immediate(High32Bits(v)));
5583 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005584 break;
5585 }
5586
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005587 case Primitive::kPrimVoid:
5588 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005589 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590 }
5591}
5592
5593void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005595 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005596 if (!instruction->IsEmittedAtUseSite()) {
5597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5598 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005599}
5600
5601void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005602 if (instruction->IsEmittedAtUseSite()) {
5603 return;
5604 }
5605
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005606 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005607 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005608 Register obj = locations->InAt(0).AsRegister<Register>();
5609 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005610 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005611 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005612 // Mask out most significant bit in case the array is String's array of char.
5613 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005614 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005615 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616}
5617
5618void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005619 RegisterSet caller_saves = RegisterSet::Empty();
5620 InvokeRuntimeCallingConvention calling_convention;
5621 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5622 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5623 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005624 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005625 HInstruction* length = instruction->InputAt(1);
5626 if (!length->IsEmittedAtUseSite()) {
5627 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5628 }
jessicahandojo4877b792016-09-08 19:49:13 -07005629 // Need register to see array's length.
5630 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5631 locations->AddTemp(Location::RequiresRegister());
5632 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005633}
5634
5635void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005636 const bool is_string_compressed_char_at =
5637 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005639 Location index_loc = locations->InAt(0);
5640 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005641 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005642 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643
Mark Mendell99dbd682015-04-22 16:18:52 -04005644 if (length_loc.IsConstant()) {
5645 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5646 if (index_loc.IsConstant()) {
5647 // BCE will remove the bounds check if we are guarenteed to pass.
5648 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5649 if (index < 0 || index >= length) {
5650 codegen_->AddSlowPath(slow_path);
5651 __ jmp(slow_path->GetEntryLabel());
5652 } else {
5653 // Some optimization after BCE may have generated this, and we should not
5654 // generate a bounds check if it is a valid range.
5655 }
5656 return;
5657 }
5658
5659 // We have to reverse the jump condition because the length is the constant.
5660 Register index_reg = index_loc.AsRegister<Register>();
5661 __ cmpl(index_reg, Immediate(length));
5662 codegen_->AddSlowPath(slow_path);
5663 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005664 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005665 HInstruction* array_length = instruction->InputAt(1);
5666 if (array_length->IsEmittedAtUseSite()) {
5667 // Address the length field in the array.
5668 DCHECK(array_length->IsArrayLength());
5669 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5670 Location array_loc = array_length->GetLocations()->InAt(0);
5671 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005672 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005673 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5674 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005675 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5676 __ movl(length_reg, array_len);
5677 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005678 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005679 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005680 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005681 // Checking bounds for general case:
5682 // Array of char or string's array with feature compression off.
5683 if (index_loc.IsConstant()) {
5684 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5685 __ cmpl(array_len, Immediate(value));
5686 } else {
5687 __ cmpl(array_len, index_loc.AsRegister<Register>());
5688 }
5689 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005690 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005691 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005692 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005693 }
5694 codegen_->AddSlowPath(slow_path);
5695 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005696 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005697}
5698
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005699void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005700 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005701}
5702
5703void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005704 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5705}
5706
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005707void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005708 LocationSummary* locations =
5709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005710 // In suspend check slow path, usually there are no caller-save registers at all.
5711 // If SIMD instructions are present, however, we force spilling all live SIMD
5712 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005713 locations->SetCustomSlowPathCallerSaves(
5714 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005715}
5716
5717void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005718 HBasicBlock* block = instruction->GetBlock();
5719 if (block->GetLoopInformation() != nullptr) {
5720 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5721 // The back edge will generate the suspend check.
5722 return;
5723 }
5724 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5725 // The goto will generate the suspend check.
5726 return;
5727 }
5728 GenerateSuspendCheck(instruction, nullptr);
5729}
5730
5731void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5732 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005733 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005734 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5735 if (slow_path == nullptr) {
5736 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5737 instruction->SetSlowPath(slow_path);
5738 codegen_->AddSlowPath(slow_path);
5739 if (successor != nullptr) {
5740 DCHECK(successor->IsLoopHeader());
5741 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5742 }
5743 } else {
5744 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5745 }
5746
Andreas Gampe542451c2016-07-26 09:02:02 -07005747 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005748 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005749 if (successor == nullptr) {
5750 __ j(kNotEqual, slow_path->GetEntryLabel());
5751 __ Bind(slow_path->GetReturnLabel());
5752 } else {
5753 __ j(kEqual, codegen_->GetLabelOf(successor));
5754 __ jmp(slow_path->GetEntryLabel());
5755 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005756}
5757
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5759 return codegen_->GetAssembler();
5760}
5761
Mark Mendell7c8d0092015-01-26 11:21:33 -05005762void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005763 ScratchRegisterScope ensure_scratch(
5764 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5765 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5766 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5767 __ movl(temp_reg, Address(ESP, src + stack_offset));
5768 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005769}
5770
5771void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005772 ScratchRegisterScope ensure_scratch(
5773 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5774 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5775 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5776 __ movl(temp_reg, Address(ESP, src + stack_offset));
5777 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5778 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5779 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005780}
5781
5782void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005783 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005784 Location source = move->GetSource();
5785 Location destination = move->GetDestination();
5786
5787 if (source.IsRegister()) {
5788 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005789 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005790 } else if (destination.IsFpuRegister()) {
5791 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005792 } else {
5793 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005794 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005795 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005796 } else if (source.IsRegisterPair()) {
5797 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5798 // Create stack space for 2 elements.
5799 __ subl(ESP, Immediate(2 * elem_size));
5800 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5801 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5802 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5803 // And remove the temporary stack space we allocated.
5804 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005805 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005806 if (destination.IsRegister()) {
5807 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5808 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005809 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005810 } else if (destination.IsRegisterPair()) {
5811 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5812 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5813 __ psrlq(src_reg, Immediate(32));
5814 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 } else if (destination.IsStackSlot()) {
5816 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005817 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005818 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005819 } else {
5820 DCHECK(destination.IsSIMDStackSlot());
5821 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005822 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005823 } else if (source.IsStackSlot()) {
5824 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005825 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005826 } else if (destination.IsFpuRegister()) {
5827 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005828 } else {
5829 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5831 }
5832 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005833 if (destination.IsRegisterPair()) {
5834 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5835 __ movl(destination.AsRegisterPairHigh<Register>(),
5836 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5837 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005838 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5839 } else {
5840 DCHECK(destination.IsDoubleStackSlot()) << destination;
5841 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005842 }
Aart Bik5576f372017-03-23 16:17:37 -07005843 } else if (source.IsSIMDStackSlot()) {
5844 DCHECK(destination.IsFpuRegister());
5845 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005846 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005847 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005848 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005849 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005851 if (value == 0) {
5852 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5853 } else {
5854 __ movl(destination.AsRegister<Register>(), Immediate(value));
5855 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005856 } else {
5857 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005858 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005859 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005860 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005861 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005862 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005863 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005864 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005865 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5866 if (value == 0) {
5867 // Easy handling of 0.0.
5868 __ xorps(dest, dest);
5869 } else {
5870 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005871 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5872 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5873 __ movl(temp, Immediate(value));
5874 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005875 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005876 } else {
5877 DCHECK(destination.IsStackSlot()) << destination;
5878 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5879 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005880 } else if (constant->IsLongConstant()) {
5881 int64_t value = constant->AsLongConstant()->GetValue();
5882 int32_t low_value = Low32Bits(value);
5883 int32_t high_value = High32Bits(value);
5884 Immediate low(low_value);
5885 Immediate high(high_value);
5886 if (destination.IsDoubleStackSlot()) {
5887 __ movl(Address(ESP, destination.GetStackIndex()), low);
5888 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5889 } else {
5890 __ movl(destination.AsRegisterPairLow<Register>(), low);
5891 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5892 }
5893 } else {
5894 DCHECK(constant->IsDoubleConstant());
5895 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005896 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005897 int32_t low_value = Low32Bits(value);
5898 int32_t high_value = High32Bits(value);
5899 Immediate low(low_value);
5900 Immediate high(high_value);
5901 if (destination.IsFpuRegister()) {
5902 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5903 if (value == 0) {
5904 // Easy handling of 0.0.
5905 __ xorpd(dest, dest);
5906 } else {
5907 __ pushl(high);
5908 __ pushl(low);
5909 __ movsd(dest, Address(ESP, 0));
5910 __ addl(ESP, Immediate(8));
5911 }
5912 } else {
5913 DCHECK(destination.IsDoubleStackSlot()) << destination;
5914 __ movl(Address(ESP, destination.GetStackIndex()), low);
5915 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5916 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005917 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005918 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005919 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005920 }
5921}
5922
Mark Mendella5c19ce2015-04-01 12:51:05 -04005923void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005924 Register suggested_scratch = reg == EAX ? EBX : EAX;
5925 ScratchRegisterScope ensure_scratch(
5926 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5927
5928 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5929 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5930 __ movl(Address(ESP, mem + stack_offset), reg);
5931 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005932}
5933
Mark Mendell7c8d0092015-01-26 11:21:33 -05005934void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005935 ScratchRegisterScope ensure_scratch(
5936 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5937
5938 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5939 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5940 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5941 __ movss(Address(ESP, mem + stack_offset), reg);
5942 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005943}
5944
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005946 ScratchRegisterScope ensure_scratch1(
5947 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005948
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005949 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5950 ScratchRegisterScope ensure_scratch2(
5951 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005952
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005953 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5954 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5955 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5956 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5957 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5958 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005959}
5960
5961void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005962 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005963 Location source = move->GetSource();
5964 Location destination = move->GetDestination();
5965
5966 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005967 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5968 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5969 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5970 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5971 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005972 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005973 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005974 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005975 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005976 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5977 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005978 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5979 // Use XOR Swap algorithm to avoid a temporary.
5980 DCHECK_NE(source.reg(), destination.reg());
5981 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5982 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5983 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5984 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5985 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5986 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5987 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005988 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5989 // Take advantage of the 16 bytes in the XMM register.
5990 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5991 Address stack(ESP, destination.GetStackIndex());
5992 // Load the double into the high doubleword.
5993 __ movhpd(reg, stack);
5994
5995 // Store the low double into the destination.
5996 __ movsd(stack, reg);
5997
5998 // Move the high double to the low double.
5999 __ psrldq(reg, Immediate(8));
6000 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6001 // Take advantage of the 16 bytes in the XMM register.
6002 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6003 Address stack(ESP, source.GetStackIndex());
6004 // Load the double into the high doubleword.
6005 __ movhpd(reg, stack);
6006
6007 // Store the low double into the destination.
6008 __ movsd(stack, reg);
6009
6010 // Move the high double to the low double.
6011 __ psrldq(reg, Immediate(8));
6012 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6013 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6014 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006015 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006016 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006017 }
6018}
6019
6020void ParallelMoveResolverX86::SpillScratch(int reg) {
6021 __ pushl(static_cast<Register>(reg));
6022}
6023
6024void ParallelMoveResolverX86::RestoreScratch(int reg) {
6025 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006026}
6027
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006028HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6029 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006030 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006031 case HLoadClass::LoadKind::kInvalid:
6032 LOG(FATAL) << "UNREACHABLE";
6033 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034 case HLoadClass::LoadKind::kReferrersClass:
6035 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006036 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006037 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006038 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006039 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006040 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006041 DCHECK(Runtime::Current()->UseJitCompilation());
6042 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006043 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006044 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 break;
6046 }
6047 return desired_class_load_kind;
6048}
6049
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006050void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006051 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006052 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006054 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006055 cls,
6056 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006057 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006058 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006059 return;
6060 }
Vladimir Marko41559982017-01-06 14:04:23 +00006061 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006062
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006063 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6064 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 ? LocationSummary::kCallOnSlowPath
6066 : LocationSummary::kNoCall;
6067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006068 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006069 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006070 }
6071
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006072 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006073 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6074 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075 locations->SetInAt(0, Location::RequiresRegister());
6076 }
6077 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006078 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6079 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6080 // Rely on the type resolution and/or initialization to save everything.
6081 RegisterSet caller_saves = RegisterSet::Empty();
6082 InvokeRuntimeCallingConvention calling_convention;
6083 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6084 locations->SetCustomSlowPathCallerSaves(caller_saves);
6085 } else {
6086 // For non-Baker read barrier we have a temp-clobbering call.
6087 }
6088 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089}
6090
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006091Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6092 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006093 Handle<mirror::Class> handle) {
6094 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6095 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006096 // Add a patch entry and return the label.
6097 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6098 PatchInfo<Label>* info = &jit_class_patches_.back();
6099 return &info->label;
6100}
6101
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006102// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6103// move.
6104void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006105 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006106 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006107 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006108 return;
6109 }
Vladimir Marko41559982017-01-06 14:04:23 +00006110 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006111
Vladimir Marko41559982017-01-06 14:04:23 +00006112 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006113 Location out_loc = locations->Out();
6114 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006116 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006117 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6118 ? kWithoutReadBarrier
6119 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006120 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 case HLoadClass::LoadKind::kReferrersClass: {
6122 DCHECK(!cls->CanCallRuntime());
6123 DCHECK(!cls->MustGenerateClinitCheck());
6124 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6125 Register current_method = locations->InAt(0).AsRegister<Register>();
6126 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006127 cls,
6128 out_loc,
6129 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006130 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006131 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006132 break;
6133 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006134 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006135 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006136 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006137 Register method_address = locations->InAt(0).AsRegister<Register>();
6138 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006139 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006140 break;
6141 }
6142 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006143 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006144 uint32_t address = dchecked_integral_cast<uint32_t>(
6145 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6146 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006147 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 break;
6149 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006150 case HLoadClass::LoadKind::kBssEntry: {
6151 Register method_address = locations->InAt(0).AsRegister<Register>();
6152 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6153 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6154 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6155 generate_null_check = true;
6156 break;
6157 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006158 case HLoadClass::LoadKind::kJitTableAddress: {
6159 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6160 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006161 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006162 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006163 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 break;
6165 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006166 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006167 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006168 LOG(FATAL) << "UNREACHABLE";
6169 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006171
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6173 DCHECK(cls->CanCallRuntime());
6174 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6175 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6176 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006177
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 if (generate_null_check) {
6179 __ testl(out, out);
6180 __ j(kEqual, slow_path->GetEntryLabel());
6181 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006182
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006183 if (cls->MustGenerateClinitCheck()) {
6184 GenerateClassInitializationCheck(slow_path, out);
6185 } else {
6186 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006187 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006188 }
6189}
6190
6191void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6192 LocationSummary* locations =
6193 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6194 locations->SetInAt(0, Location::RequiresRegister());
6195 if (check->HasUses()) {
6196 locations->SetOut(Location::SameAsFirstInput());
6197 }
6198}
6199
6200void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006201 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006202 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006203 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006204 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006205 GenerateClassInitializationCheck(slow_path,
6206 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006207}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006208
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006209void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006210 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006211 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6212 Immediate(mirror::Class::kStatusInitialized));
6213 __ j(kLess, slow_path->GetEntryLabel());
6214 __ Bind(slow_path->GetExitLabel());
6215 // No need for memory fence, thanks to the X86 memory model.
6216}
6217
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006218HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6219 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006220 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006222 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006223 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006224 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006225 case HLoadString::LoadKind::kJitTableAddress:
6226 DCHECK(Runtime::Current()->UseJitCompilation());
6227 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006228 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006229 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006230 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006231 }
6232 return desired_string_load_kind;
6233}
6234
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006235void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006236 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006238 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006239 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006240 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 locations->SetInAt(0, Location::RequiresRegister());
6242 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006244 locations->SetOut(Location::RegisterLocation(EAX));
6245 } else {
6246 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006247 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6248 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006249 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006250 RegisterSet caller_saves = RegisterSet::Empty();
6251 InvokeRuntimeCallingConvention calling_convention;
6252 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6253 locations->SetCustomSlowPathCallerSaves(caller_saves);
6254 } else {
6255 // For non-Baker read barrier we have a temp-clobbering call.
6256 }
6257 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006258 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006259}
6260
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006261Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006262 dex::StringIndex dex_index,
6263 Handle<mirror::String> handle) {
6264 jit_string_roots_.Overwrite(
6265 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006266 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006267 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006268 PatchInfo<Label>* info = &jit_string_patches_.back();
6269 return &info->label;
6270}
6271
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006272// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6273// move.
6274void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006275 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276 Location out_loc = locations->Out();
6277 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006280 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006281 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 Register method_address = locations->InAt(0).AsRegister<Register>();
6283 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006284 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006285 return; // No dex cache slow path.
6286 }
6287 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006288 uint32_t address = dchecked_integral_cast<uint32_t>(
6289 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6290 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006291 __ movl(out, Immediate(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006292 return; // No dex cache slow path.
6293 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006294 case HLoadString::LoadKind::kBssEntry: {
6295 Register method_address = locations->InAt(0).AsRegister<Register>();
6296 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6297 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006298 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006299 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006300 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6301 codegen_->AddSlowPath(slow_path);
6302 __ testl(out, out);
6303 __ j(kEqual, slow_path->GetEntryLabel());
6304 __ Bind(slow_path->GetExitLabel());
6305 return;
6306 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006307 case HLoadString::LoadKind::kJitTableAddress: {
6308 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6309 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006310 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006311 // /* GcRoot<mirror::String> */ out = *address
6312 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6313 return;
6314 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006315 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006316 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006317 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006318
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006319 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006320 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006321 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006322 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006323 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6324 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006325}
6326
David Brazdilcb1c0552015-08-04 16:22:25 +01006327static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006328 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006329}
6330
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006331void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6332 LocationSummary* locations =
6333 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6334 locations->SetOut(Location::RequiresRegister());
6335}
6336
6337void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006338 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6339}
6340
6341void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6342 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6343}
6344
6345void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6346 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006347}
6348
6349void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6350 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006352 InvokeRuntimeCallingConvention calling_convention;
6353 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6354}
6355
6356void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006357 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006358 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006359}
6360
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006361// Temp is used for read barrier.
6362static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6363 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006364 !kUseBakerReadBarrier &&
6365 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006366 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006367 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6368 return 1;
6369 }
6370 return 0;
6371}
6372
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006373// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006374// interface pointer, one for loading the current interface.
6375// The other checks have one temp for loading the object's class.
6376static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6377 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6378 return 2;
6379 }
6380 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006381}
6382
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006383void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006386 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 case TypeCheckKind::kExactCheck:
6389 case TypeCheckKind::kAbstractClassCheck:
6390 case TypeCheckKind::kClassHierarchyCheck:
6391 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 call_kind =
6393 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006394 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006395 break;
6396 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397 case TypeCheckKind::kUnresolvedCheck:
6398 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399 call_kind = LocationSummary::kCallOnSlowPath;
6400 break;
6401 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006404 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006405 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006406 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006407 locations->SetInAt(0, Location::RequiresRegister());
6408 locations->SetInAt(1, Location::Any());
6409 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6410 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006411 // When read barriers are enabled, we need a temporary register for some cases.
6412 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006413}
6414
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006415void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006416 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006417 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 Location obj_loc = locations->InAt(0);
6419 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006420 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 Location out_loc = locations->Out();
6422 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006423 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6424 DCHECK_LE(num_temps, 1u);
6425 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006426 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6428 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6429 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006430 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006432
6433 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006434 // Avoid null check if we know obj is not null.
6435 if (instruction->MustDoNullCheck()) {
6436 __ testl(obj, obj);
6437 __ j(kEqual, &zero);
6438 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439
Roland Levillain7c1559a2015-12-15 10:55:36 +00006440 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006441 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006442 // /* HeapReference<Class> */ out = obj->klass_
6443 GenerateReferenceLoadTwoRegisters(instruction,
6444 out_loc,
6445 obj_loc,
6446 class_offset,
6447 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 if (cls.IsRegister()) {
6449 __ cmpl(out, cls.AsRegister<Register>());
6450 } else {
6451 DCHECK(cls.IsStackSlot()) << cls;
6452 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6453 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 // Classes must be equal for the instanceof to succeed.
6456 __ j(kNotEqual, &zero);
6457 __ movl(out, Immediate(1));
6458 __ jmp(&done);
6459 break;
6460 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006463 // /* HeapReference<Class> */ out = obj->klass_
6464 GenerateReferenceLoadTwoRegisters(instruction,
6465 out_loc,
6466 obj_loc,
6467 class_offset,
6468 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 // If the class is abstract, we eagerly fetch the super class of the
6470 // object to avoid doing a comparison we know will fail.
6471 NearLabel loop;
6472 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006474 GenerateReferenceLoadOneRegister(instruction,
6475 out_loc,
6476 super_offset,
6477 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006478 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 __ testl(out, out);
6480 // If `out` is null, we use it for the result, and jump to `done`.
6481 __ j(kEqual, &done);
6482 if (cls.IsRegister()) {
6483 __ cmpl(out, cls.AsRegister<Register>());
6484 } else {
6485 DCHECK(cls.IsStackSlot()) << cls;
6486 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6487 }
6488 __ j(kNotEqual, &loop);
6489 __ movl(out, Immediate(1));
6490 if (zero.IsLinked()) {
6491 __ jmp(&done);
6492 }
6493 break;
6494 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006495
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006497 // /* HeapReference<Class> */ out = obj->klass_
6498 GenerateReferenceLoadTwoRegisters(instruction,
6499 out_loc,
6500 obj_loc,
6501 class_offset,
6502 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 // Walk over the class hierarchy to find a match.
6504 NearLabel loop, success;
6505 __ Bind(&loop);
6506 if (cls.IsRegister()) {
6507 __ cmpl(out, cls.AsRegister<Register>());
6508 } else {
6509 DCHECK(cls.IsStackSlot()) << cls;
6510 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6511 }
6512 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006514 GenerateReferenceLoadOneRegister(instruction,
6515 out_loc,
6516 super_offset,
6517 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006518 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006519 __ testl(out, out);
6520 __ j(kNotEqual, &loop);
6521 // If `out` is null, we use it for the result, and jump to `done`.
6522 __ jmp(&done);
6523 __ Bind(&success);
6524 __ movl(out, Immediate(1));
6525 if (zero.IsLinked()) {
6526 __ jmp(&done);
6527 }
6528 break;
6529 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006530
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006532 // /* HeapReference<Class> */ out = obj->klass_
6533 GenerateReferenceLoadTwoRegisters(instruction,
6534 out_loc,
6535 obj_loc,
6536 class_offset,
6537 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006538 // Do an exact check.
6539 NearLabel exact_check;
6540 if (cls.IsRegister()) {
6541 __ cmpl(out, cls.AsRegister<Register>());
6542 } else {
6543 DCHECK(cls.IsStackSlot()) << cls;
6544 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6545 }
6546 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006549 GenerateReferenceLoadOneRegister(instruction,
6550 out_loc,
6551 component_offset,
6552 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006553 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006554 __ testl(out, out);
6555 // If `out` is null, we use it for the result, and jump to `done`.
6556 __ j(kEqual, &done);
6557 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6558 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006559 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006560 __ movl(out, Immediate(1));
6561 __ jmp(&done);
6562 break;
6563 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006564
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006566 // No read barrier since the slow path will retry upon failure.
6567 // /* HeapReference<Class> */ out = obj->klass_
6568 GenerateReferenceLoadTwoRegisters(instruction,
6569 out_loc,
6570 obj_loc,
6571 class_offset,
6572 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 if (cls.IsRegister()) {
6574 __ cmpl(out, cls.AsRegister<Register>());
6575 } else {
6576 DCHECK(cls.IsStackSlot()) << cls;
6577 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6578 }
6579 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006580 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6581 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006582 codegen_->AddSlowPath(slow_path);
6583 __ j(kNotEqual, slow_path->GetEntryLabel());
6584 __ movl(out, Immediate(1));
6585 if (zero.IsLinked()) {
6586 __ jmp(&done);
6587 }
6588 break;
6589 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006590
Calin Juravle98893e12015-10-02 21:05:03 +01006591 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592 case TypeCheckKind::kInterfaceCheck: {
6593 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006594 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595 // cases.
6596 //
6597 // We cannot directly call the InstanceofNonTrivial runtime
6598 // entry point without resorting to a type checking slow path
6599 // here (i.e. by calling InvokeRuntime directly), as it would
6600 // require to assign fixed registers for the inputs of this
6601 // HInstanceOf instruction (following the runtime calling
6602 // convention), which might be cluttered by the potential first
6603 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006604 //
6605 // TODO: Introduce a new runtime entry point taking the object
6606 // to test (instead of its class) as argument, and let it deal
6607 // with the read barrier issues. This will let us refactor this
6608 // case of the `switch` code as it was previously (with a direct
6609 // call to the runtime not using a type checking slow path).
6610 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006611 DCHECK(locations->OnlyCallsOnSlowPath());
6612 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6613 /* is_fatal */ false);
6614 codegen_->AddSlowPath(slow_path);
6615 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006616 if (zero.IsLinked()) {
6617 __ jmp(&done);
6618 }
6619 break;
6620 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006621 }
6622
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006623 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006624 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006625 __ xorl(out, out);
6626 }
6627
6628 if (done.IsLinked()) {
6629 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006630 }
6631
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006632 if (slow_path != nullptr) {
6633 __ Bind(slow_path->GetExitLabel());
6634 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006635}
6636
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006637static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6638 switch (type_check_kind) {
6639 case TypeCheckKind::kExactCheck:
6640 case TypeCheckKind::kAbstractClassCheck:
6641 case TypeCheckKind::kClassHierarchyCheck:
6642 case TypeCheckKind::kArrayObjectCheck:
6643 return !throws_into_catch && !kEmitCompilerReadBarrier;
6644 case TypeCheckKind::kInterfaceCheck:
6645 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6646 case TypeCheckKind::kArrayCheck:
6647 case TypeCheckKind::kUnresolvedCheck:
6648 return false;
6649 }
6650 LOG(FATAL) << "Unreachable";
6651 UNREACHABLE();
6652}
6653
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006654void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006655 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006656 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006657 LocationSummary::CallKind call_kind =
6658 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6659 ? LocationSummary::kNoCall
6660 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6662 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006663 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6664 // Require a register for the interface check since there is a loop that compares the class to
6665 // a memory address.
6666 locations->SetInAt(1, Location::RequiresRegister());
6667 } else {
6668 locations->SetInAt(1, Location::Any());
6669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6671 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006672 // When read barriers are enabled, we need an additional temporary register for some cases.
6673 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6674}
6675
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006676void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006677 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006678 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679 Location obj_loc = locations->InAt(0);
6680 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006681 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006682 Location temp_loc = locations->GetTemp(0);
6683 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006684 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6685 DCHECK_GE(num_temps, 1u);
6686 DCHECK_LE(num_temps, 2u);
6687 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6688 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6689 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6690 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6691 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6692 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6693 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6694 const uint32_t object_array_data_offset =
6695 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006696
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006697 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6698 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6699 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006701 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6702
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 SlowPathCode* type_check_slow_path =
6704 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6705 is_type_check_slow_path_fatal);
6706 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006707
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006709 // Avoid null check if we know obj is not null.
6710 if (instruction->MustDoNullCheck()) {
6711 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006712 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006713 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006714
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006716 case TypeCheckKind::kExactCheck:
6717 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006718 // /* HeapReference<Class> */ temp = obj->klass_
6719 GenerateReferenceLoadTwoRegisters(instruction,
6720 temp_loc,
6721 obj_loc,
6722 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006723 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006724
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725 if (cls.IsRegister()) {
6726 __ cmpl(temp, cls.AsRegister<Register>());
6727 } else {
6728 DCHECK(cls.IsStackSlot()) << cls;
6729 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6730 }
6731 // Jump to slow path for throwing the exception or doing a
6732 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006734 break;
6735 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006738 // /* HeapReference<Class> */ temp = obj->klass_
6739 GenerateReferenceLoadTwoRegisters(instruction,
6740 temp_loc,
6741 obj_loc,
6742 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006743 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006744
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 // If the class is abstract, we eagerly fetch the super class of the
6746 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006747 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006748 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006749 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006750 GenerateReferenceLoadOneRegister(instruction,
6751 temp_loc,
6752 super_offset,
6753 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006754 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006755
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006756 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6757 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006758 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006759 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006760
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006761 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006762 if (cls.IsRegister()) {
6763 __ cmpl(temp, cls.AsRegister<Register>());
6764 } else {
6765 DCHECK(cls.IsStackSlot()) << cls;
6766 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6767 }
6768 __ j(kNotEqual, &loop);
6769 break;
6770 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006771
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006772 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006773 // /* HeapReference<Class> */ temp = obj->klass_
6774 GenerateReferenceLoadTwoRegisters(instruction,
6775 temp_loc,
6776 obj_loc,
6777 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006778 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006779
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006780 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006781 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006782 __ Bind(&loop);
6783 if (cls.IsRegister()) {
6784 __ cmpl(temp, cls.AsRegister<Register>());
6785 } else {
6786 DCHECK(cls.IsStackSlot()) << cls;
6787 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6788 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006789 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006790
Roland Levillain0d5a2812015-11-13 10:07:31 +00006791 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006792 GenerateReferenceLoadOneRegister(instruction,
6793 temp_loc,
6794 super_offset,
6795 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006796 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006797
6798 // If the class reference currently in `temp` is not null, jump
6799 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006800 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006801 __ j(kNotZero, &loop);
6802 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006804 break;
6805 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006806
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006807 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006808 // /* HeapReference<Class> */ temp = obj->klass_
6809 GenerateReferenceLoadTwoRegisters(instruction,
6810 temp_loc,
6811 obj_loc,
6812 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006813 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006814
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006815 // Do an exact check.
6816 if (cls.IsRegister()) {
6817 __ cmpl(temp, cls.AsRegister<Register>());
6818 } else {
6819 DCHECK(cls.IsStackSlot()) << cls;
6820 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6821 }
6822 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006823
6824 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006825 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006826 GenerateReferenceLoadOneRegister(instruction,
6827 temp_loc,
6828 component_offset,
6829 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006830 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006831
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006832 // If the component type is null (i.e. the object not an array), jump to the slow path to
6833 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006834 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006835 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006837 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006838 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006839 break;
6840 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006841
Calin Juravle98893e12015-10-02 21:05:03 +01006842 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006843 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844 // We cannot directly call the CheckCast runtime entry point
6845 // without resorting to a type checking slow path here (i.e. by
6846 // calling InvokeRuntime directly), as it would require to
6847 // assign fixed registers for the inputs of this HInstanceOf
6848 // instruction (following the runtime calling convention), which
6849 // might be cluttered by the potential first read barrier
6850 // emission at the beginning of this method.
6851 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006852 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006853
6854 case TypeCheckKind::kInterfaceCheck: {
6855 // Fast path for the interface check. Since we compare with a memory location in the inner
6856 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6857 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006858 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006859 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006860 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6861 // doing this.
6862 // /* HeapReference<Class> */ temp = obj->klass_
6863 GenerateReferenceLoadTwoRegisters(instruction,
6864 temp_loc,
6865 obj_loc,
6866 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006867 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006868
6869 // /* HeapReference<Class> */ temp = temp->iftable_
6870 GenerateReferenceLoadTwoRegisters(instruction,
6871 temp_loc,
6872 temp_loc,
6873 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006874 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006875 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006876 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006877 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006878 NearLabel start_loop;
6879 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006880 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006881 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006882 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6883 // Go to next interface if the classes do not match.
6884 __ cmpl(cls.AsRegister<Register>(),
6885 CodeGeneratorX86::ArrayAddress(temp,
6886 maybe_temp2_loc,
6887 TIMES_4,
6888 object_array_data_offset));
6889 __ j(kNotEqual, &start_loop);
6890 } else {
6891 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006892 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006893 break;
6894 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006895 }
6896 __ Bind(&done);
6897
Roland Levillain0d5a2812015-11-13 10:07:31 +00006898 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006899}
6900
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006901void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6902 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006903 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006904 InvokeRuntimeCallingConvention calling_convention;
6905 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6906}
6907
6908void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006909 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6910 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006911 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006912 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006913 if (instruction->IsEnter()) {
6914 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6915 } else {
6916 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6917 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006918}
6919
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006920void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6921void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6922void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6923
6924void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6925 LocationSummary* locations =
6926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6927 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6928 || instruction->GetResultType() == Primitive::kPrimLong);
6929 locations->SetInAt(0, Location::RequiresRegister());
6930 locations->SetInAt(1, Location::Any());
6931 locations->SetOut(Location::SameAsFirstInput());
6932}
6933
6934void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6935 HandleBitwiseOperation(instruction);
6936}
6937
6938void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6939 HandleBitwiseOperation(instruction);
6940}
6941
6942void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6943 HandleBitwiseOperation(instruction);
6944}
6945
6946void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6947 LocationSummary* locations = instruction->GetLocations();
6948 Location first = locations->InAt(0);
6949 Location second = locations->InAt(1);
6950 DCHECK(first.Equals(locations->Out()));
6951
6952 if (instruction->GetResultType() == Primitive::kPrimInt) {
6953 if (second.IsRegister()) {
6954 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006955 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006956 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006957 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006958 } else {
6959 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006960 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006961 }
6962 } else if (second.IsConstant()) {
6963 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006964 __ andl(first.AsRegister<Register>(),
6965 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006966 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006967 __ orl(first.AsRegister<Register>(),
6968 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 } else {
6970 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006971 __ xorl(first.AsRegister<Register>(),
6972 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006973 }
6974 } else {
6975 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006976 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006977 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006978 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006979 } else {
6980 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006981 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006982 }
6983 }
6984 } else {
6985 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6986 if (second.IsRegisterPair()) {
6987 if (instruction->IsAnd()) {
6988 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6989 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6990 } else if (instruction->IsOr()) {
6991 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6992 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6993 } else {
6994 DCHECK(instruction->IsXor());
6995 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6996 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6997 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006998 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006999 if (instruction->IsAnd()) {
7000 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7001 __ andl(first.AsRegisterPairHigh<Register>(),
7002 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7003 } else if (instruction->IsOr()) {
7004 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7005 __ orl(first.AsRegisterPairHigh<Register>(),
7006 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7007 } else {
7008 DCHECK(instruction->IsXor());
7009 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7010 __ xorl(first.AsRegisterPairHigh<Register>(),
7011 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7012 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007013 } else {
7014 DCHECK(second.IsConstant()) << second;
7015 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007016 int32_t low_value = Low32Bits(value);
7017 int32_t high_value = High32Bits(value);
7018 Immediate low(low_value);
7019 Immediate high(high_value);
7020 Register first_low = first.AsRegisterPairLow<Register>();
7021 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007022 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007023 if (low_value == 0) {
7024 __ xorl(first_low, first_low);
7025 } else if (low_value != -1) {
7026 __ andl(first_low, low);
7027 }
7028 if (high_value == 0) {
7029 __ xorl(first_high, first_high);
7030 } else if (high_value != -1) {
7031 __ andl(first_high, high);
7032 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007033 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007034 if (low_value != 0) {
7035 __ orl(first_low, low);
7036 }
7037 if (high_value != 0) {
7038 __ orl(first_high, high);
7039 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007040 } else {
7041 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007042 if (low_value != 0) {
7043 __ xorl(first_low, low);
7044 }
7045 if (high_value != 0) {
7046 __ xorl(first_high, high);
7047 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007048 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007049 }
7050 }
7051}
7052
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007053void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7054 HInstruction* instruction,
7055 Location out,
7056 uint32_t offset,
7057 Location maybe_temp,
7058 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007059 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007060 if (read_barrier_option == kWithReadBarrier) {
7061 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007062 if (kUseBakerReadBarrier) {
7063 // Load with fast path based Baker's read barrier.
7064 // /* HeapReference<Object> */ out = *(out + offset)
7065 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007066 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007067 } else {
7068 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007069 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070 // in the following move operation, as we will need it for the
7071 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007072 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007073 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007074 // /* HeapReference<Object> */ out = *(out + offset)
7075 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007076 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007077 }
7078 } else {
7079 // Plain load with no read barrier.
7080 // /* HeapReference<Object> */ out = *(out + offset)
7081 __ movl(out_reg, Address(out_reg, offset));
7082 __ MaybeUnpoisonHeapReference(out_reg);
7083 }
7084}
7085
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007086void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7087 HInstruction* instruction,
7088 Location out,
7089 Location obj,
7090 uint32_t offset,
7091 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 Register out_reg = out.AsRegister<Register>();
7093 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007094 if (read_barrier_option == kWithReadBarrier) {
7095 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007096 if (kUseBakerReadBarrier) {
7097 // Load with fast path based Baker's read barrier.
7098 // /* HeapReference<Object> */ out = *(obj + offset)
7099 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007100 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007101 } else {
7102 // Load with slow path based read barrier.
7103 // /* HeapReference<Object> */ out = *(obj + offset)
7104 __ movl(out_reg, Address(obj_reg, offset));
7105 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7106 }
7107 } else {
7108 // Plain load with no read barrier.
7109 // /* HeapReference<Object> */ out = *(obj + offset)
7110 __ movl(out_reg, Address(obj_reg, offset));
7111 __ MaybeUnpoisonHeapReference(out_reg);
7112 }
7113}
7114
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007115void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7116 HInstruction* instruction,
7117 Location root,
7118 const Address& address,
7119 Label* fixup_label,
7120 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007121 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007122 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007123 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007124 if (kUseBakerReadBarrier) {
7125 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7126 // Baker's read barrier are used:
7127 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007128 // root = obj.field;
7129 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7130 // if (temp != null) {
7131 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 // }
7133
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007134 // /* GcRoot<mirror::Object> */ root = *address
7135 __ movl(root_reg, address);
7136 if (fixup_label != nullptr) {
7137 __ Bind(fixup_label);
7138 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007139 static_assert(
7140 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7141 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7142 "have different sizes.");
7143 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7144 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7145 "have different sizes.");
7146
Vladimir Marko953437b2016-08-24 08:30:46 +00007147 // Slow path marking the GC root `root`.
7148 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007149 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007150 codegen_->AddSlowPath(slow_path);
7151
Roland Levillaind966ce72017-02-09 16:20:14 +00007152 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7153 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007154 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007155 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7156 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007157 __ j(kNotEqual, slow_path->GetEntryLabel());
7158 __ Bind(slow_path->GetExitLabel());
7159 } else {
7160 // GC root loaded through a slow path for read barriers other
7161 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007162 // /* GcRoot<mirror::Object>* */ root = address
7163 __ leal(root_reg, address);
7164 if (fixup_label != nullptr) {
7165 __ Bind(fixup_label);
7166 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007167 // /* mirror::Object* */ root = root->Read()
7168 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7169 }
7170 } else {
7171 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007172 // /* GcRoot<mirror::Object> */ root = *address
7173 __ movl(root_reg, address);
7174 if (fixup_label != nullptr) {
7175 __ Bind(fixup_label);
7176 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007177 // Note that GC roots are not affected by heap poisoning, thus we
7178 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007179 }
7180}
7181
7182void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7183 Location ref,
7184 Register obj,
7185 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007186 bool needs_null_check) {
7187 DCHECK(kEmitCompilerReadBarrier);
7188 DCHECK(kUseBakerReadBarrier);
7189
7190 // /* HeapReference<Object> */ ref = *(obj + offset)
7191 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007192 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007193}
7194
7195void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7196 Location ref,
7197 Register obj,
7198 uint32_t data_offset,
7199 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007200 bool needs_null_check) {
7201 DCHECK(kEmitCompilerReadBarrier);
7202 DCHECK(kUseBakerReadBarrier);
7203
Roland Levillain3d312422016-06-23 13:53:42 +01007204 static_assert(
7205 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7206 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207 // /* HeapReference<Object> */ ref =
7208 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007209 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007210 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007211}
7212
7213void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7214 Location ref,
7215 Register obj,
7216 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007217 bool needs_null_check,
7218 bool always_update_field,
7219 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007220 DCHECK(kEmitCompilerReadBarrier);
7221 DCHECK(kUseBakerReadBarrier);
7222
7223 // In slow path based read barriers, the read barrier call is
7224 // inserted after the original load. However, in fast path based
7225 // Baker's read barriers, we need to perform the load of
7226 // mirror::Object::monitor_ *before* the original reference load.
7227 // This load-load ordering is required by the read barrier.
7228 // The fast path/slow path (for Baker's algorithm) should look like:
7229 //
7230 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7231 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7232 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007233 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007234 // if (is_gray) {
7235 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7236 // }
7237 //
7238 // Note: the original implementation in ReadBarrier::Barrier is
7239 // slightly more complex as:
7240 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007241 // the high-bits of rb_state, which are expected to be all zeroes
7242 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7243 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007244 // - it performs additional checks that we do not do here for
7245 // performance reasons.
7246
7247 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007248 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7249
Vladimir Marko953437b2016-08-24 08:30:46 +00007250 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007251 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7252 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007253 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7254 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7255 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7256
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007257 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007258 // ref = ReadBarrier::Mark(ref);
7259 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7260 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007261 if (needs_null_check) {
7262 MaybeRecordImplicitNullCheck(instruction);
7263 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007264
7265 // Load fence to prevent load-load reordering.
7266 // Note that this is a no-op, thanks to the x86 memory model.
7267 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7268
7269 // The actual reference load.
7270 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007271 __ movl(ref_reg, src); // Flags are unaffected.
7272
7273 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7274 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007275 SlowPathCode* slow_path;
7276 if (always_update_field) {
7277 DCHECK(temp != nullptr);
7278 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7279 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7280 } else {
7281 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7282 instruction, ref, /* unpoison_ref_before_marking */ true);
7283 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007284 AddSlowPath(slow_path);
7285
7286 // We have done the "if" of the gray bit check above, now branch based on the flags.
7287 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007288
7289 // Object* ref = ref_addr->AsMirrorPtr()
7290 __ MaybeUnpoisonHeapReference(ref_reg);
7291
Roland Levillain7c1559a2015-12-15 10:55:36 +00007292 __ Bind(slow_path->GetExitLabel());
7293}
7294
7295void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7296 Location out,
7297 Location ref,
7298 Location obj,
7299 uint32_t offset,
7300 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007301 DCHECK(kEmitCompilerReadBarrier);
7302
Roland Levillain7c1559a2015-12-15 10:55:36 +00007303 // Insert a slow path based read barrier *after* the reference load.
7304 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007305 // If heap poisoning is enabled, the unpoisoning of the loaded
7306 // reference will be carried out by the runtime within the slow
7307 // path.
7308 //
7309 // Note that `ref` currently does not get unpoisoned (when heap
7310 // poisoning is enabled), which is alright as the `ref` argument is
7311 // not used by the artReadBarrierSlow entry point.
7312 //
7313 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7314 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7315 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7316 AddSlowPath(slow_path);
7317
Roland Levillain0d5a2812015-11-13 10:07:31 +00007318 __ jmp(slow_path->GetEntryLabel());
7319 __ Bind(slow_path->GetExitLabel());
7320}
7321
Roland Levillain7c1559a2015-12-15 10:55:36 +00007322void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7323 Location out,
7324 Location ref,
7325 Location obj,
7326 uint32_t offset,
7327 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007329 // Baker's read barriers shall be handled by the fast path
7330 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7331 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007332 // If heap poisoning is enabled, unpoisoning will be taken care of
7333 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007334 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 } else if (kPoisonHeapReferences) {
7336 __ UnpoisonHeapReference(out.AsRegister<Register>());
7337 }
7338}
7339
Roland Levillain7c1559a2015-12-15 10:55:36 +00007340void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7341 Location out,
7342 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007343 DCHECK(kEmitCompilerReadBarrier);
7344
Roland Levillain7c1559a2015-12-15 10:55:36 +00007345 // Insert a slow path based read barrier *after* the GC root load.
7346 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007347 // Note that GC roots are not affected by heap poisoning, so we do
7348 // not need to do anything special for this here.
7349 SlowPathCode* slow_path =
7350 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7351 AddSlowPath(slow_path);
7352
Roland Levillain0d5a2812015-11-13 10:07:31 +00007353 __ jmp(slow_path->GetEntryLabel());
7354 __ Bind(slow_path->GetExitLabel());
7355}
7356
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007357void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007358 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007359 LOG(FATAL) << "Unreachable";
7360}
7361
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007362void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007363 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007364 LOG(FATAL) << "Unreachable";
7365}
7366
Mark Mendellfe57faa2015-09-18 09:26:15 -04007367// Simple implementation of packed switch - generate cascaded compare/jumps.
7368void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7369 LocationSummary* locations =
7370 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7371 locations->SetInAt(0, Location::RequiresRegister());
7372}
7373
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007374void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7375 int32_t lower_bound,
7376 uint32_t num_entries,
7377 HBasicBlock* switch_block,
7378 HBasicBlock* default_block) {
7379 // Figure out the correct compare values and jump conditions.
7380 // Handle the first compare/branch as a special case because it might
7381 // jump to the default case.
7382 DCHECK_GT(num_entries, 2u);
7383 Condition first_condition;
7384 uint32_t index;
7385 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7386 if (lower_bound != 0) {
7387 first_condition = kLess;
7388 __ cmpl(value_reg, Immediate(lower_bound));
7389 __ j(first_condition, codegen_->GetLabelOf(default_block));
7390 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007391
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007392 index = 1;
7393 } else {
7394 // Handle all the compare/jumps below.
7395 first_condition = kBelow;
7396 index = 0;
7397 }
7398
7399 // Handle the rest of the compare/jumps.
7400 for (; index + 1 < num_entries; index += 2) {
7401 int32_t compare_to_value = lower_bound + index + 1;
7402 __ cmpl(value_reg, Immediate(compare_to_value));
7403 // Jump to successors[index] if value < case_value[index].
7404 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7405 // Jump to successors[index + 1] if value == case_value[index + 1].
7406 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7407 }
7408
7409 if (index != num_entries) {
7410 // There are an odd number of entries. Handle the last one.
7411 DCHECK_EQ(index + 1, num_entries);
7412 __ cmpl(value_reg, Immediate(lower_bound + index));
7413 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007414 }
7415
7416 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007417 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7418 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007419 }
7420}
7421
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007422void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7423 int32_t lower_bound = switch_instr->GetStartValue();
7424 uint32_t num_entries = switch_instr->GetNumEntries();
7425 LocationSummary* locations = switch_instr->GetLocations();
7426 Register value_reg = locations->InAt(0).AsRegister<Register>();
7427
7428 GenPackedSwitchWithCompares(value_reg,
7429 lower_bound,
7430 num_entries,
7431 switch_instr->GetBlock(),
7432 switch_instr->GetDefaultBlock());
7433}
7434
Mark Mendell805b3b52015-09-18 14:10:29 -04007435void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7436 LocationSummary* locations =
7437 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7438 locations->SetInAt(0, Location::RequiresRegister());
7439
7440 // Constant area pointer.
7441 locations->SetInAt(1, Location::RequiresRegister());
7442
7443 // And the temporary we need.
7444 locations->AddTemp(Location::RequiresRegister());
7445}
7446
7447void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7448 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007449 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007450 LocationSummary* locations = switch_instr->GetLocations();
7451 Register value_reg = locations->InAt(0).AsRegister<Register>();
7452 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7453
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007454 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7455 GenPackedSwitchWithCompares(value_reg,
7456 lower_bound,
7457 num_entries,
7458 switch_instr->GetBlock(),
7459 default_block);
7460 return;
7461 }
7462
Mark Mendell805b3b52015-09-18 14:10:29 -04007463 // Optimizing has a jump area.
7464 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7465 Register constant_area = locations->InAt(1).AsRegister<Register>();
7466
7467 // Remove the bias, if needed.
7468 if (lower_bound != 0) {
7469 __ leal(temp_reg, Address(value_reg, -lower_bound));
7470 value_reg = temp_reg;
7471 }
7472
7473 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007474 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007475 __ cmpl(value_reg, Immediate(num_entries - 1));
7476 __ j(kAbove, codegen_->GetLabelOf(default_block));
7477
7478 // We are in the range of the table.
7479 // Load (target-constant_area) from the jump table, indexing by the value.
7480 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7481
7482 // Compute the actual target address by adding in constant_area.
7483 __ addl(temp_reg, constant_area);
7484
7485 // And jump.
7486 __ jmp(temp_reg);
7487}
7488
Mark Mendell0616ae02015-04-17 12:49:27 -04007489void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7490 HX86ComputeBaseMethodAddress* insn) {
7491 LocationSummary* locations =
7492 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7493 locations->SetOut(Location::RequiresRegister());
7494}
7495
7496void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7497 HX86ComputeBaseMethodAddress* insn) {
7498 LocationSummary* locations = insn->GetLocations();
7499 Register reg = locations->Out().AsRegister<Register>();
7500
7501 // Generate call to next instruction.
7502 Label next_instruction;
7503 __ call(&next_instruction);
7504 __ Bind(&next_instruction);
7505
7506 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007507 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007508
7509 // Grab the return address off the stack.
7510 __ popl(reg);
7511}
7512
7513void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7514 HX86LoadFromConstantTable* insn) {
7515 LocationSummary* locations =
7516 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7517
7518 locations->SetInAt(0, Location::RequiresRegister());
7519 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7520
7521 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007522 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007523 return;
7524 }
7525
7526 switch (insn->GetType()) {
7527 case Primitive::kPrimFloat:
7528 case Primitive::kPrimDouble:
7529 locations->SetOut(Location::RequiresFpuRegister());
7530 break;
7531
7532 case Primitive::kPrimInt:
7533 locations->SetOut(Location::RequiresRegister());
7534 break;
7535
7536 default:
7537 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7538 }
7539}
7540
7541void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007542 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007543 return;
7544 }
7545
7546 LocationSummary* locations = insn->GetLocations();
7547 Location out = locations->Out();
7548 Register const_area = locations->InAt(0).AsRegister<Register>();
7549 HConstant *value = insn->GetConstant();
7550
7551 switch (insn->GetType()) {
7552 case Primitive::kPrimFloat:
7553 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007554 codegen_->LiteralFloatAddress(
7555 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007556 break;
7557
7558 case Primitive::kPrimDouble:
7559 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007560 codegen_->LiteralDoubleAddress(
7561 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007562 break;
7563
7564 case Primitive::kPrimInt:
7565 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007566 codegen_->LiteralInt32Address(
7567 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007568 break;
7569
7570 default:
7571 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7572 }
7573}
7574
Mark Mendell0616ae02015-04-17 12:49:27 -04007575/**
7576 * Class to handle late fixup of offsets into constant area.
7577 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007578class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007579 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007580 RIPFixup(CodeGeneratorX86& codegen,
7581 HX86ComputeBaseMethodAddress* base_method_address,
7582 size_t offset)
7583 : codegen_(&codegen),
7584 base_method_address_(base_method_address),
7585 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007586
7587 protected:
7588 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7589
7590 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007591 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007592
7593 private:
7594 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7595 // Patch the correct offset for the instruction. The place to patch is the
7596 // last 4 bytes of the instruction.
7597 // The value to patch is the distance from the offset in the constant area
7598 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007599 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007600 int32_t relative_position =
7601 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007602
7603 // Patch in the right value.
7604 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7605 }
7606
Mark Mendell0616ae02015-04-17 12:49:27 -04007607 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007608 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007609};
7610
Mark Mendell805b3b52015-09-18 14:10:29 -04007611/**
7612 * Class to handle late fixup of offsets to a jump table that will be created in the
7613 * constant area.
7614 */
7615class JumpTableRIPFixup : public RIPFixup {
7616 public:
7617 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007618 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7619 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007620
7621 void CreateJumpTable() {
7622 X86Assembler* assembler = codegen_->GetAssembler();
7623
7624 // Ensure that the reference to the jump table has the correct offset.
7625 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7626 SetOffset(offset_in_constant_table);
7627
7628 // The label values in the jump table are computed relative to the
7629 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007630 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007631
7632 // Populate the jump table with the correct values for the jump table.
7633 int32_t num_entries = switch_instr_->GetNumEntries();
7634 HBasicBlock* block = switch_instr_->GetBlock();
7635 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7636 // The value that we want is the target offset - the position of the table.
7637 for (int32_t i = 0; i < num_entries; i++) {
7638 HBasicBlock* b = successors[i];
7639 Label* l = codegen_->GetLabelOf(b);
7640 DCHECK(l->IsBound());
7641 int32_t offset_to_block = l->Position() - relative_offset;
7642 assembler->AppendInt32(offset_to_block);
7643 }
7644 }
7645
7646 private:
7647 const HX86PackedSwitch* switch_instr_;
7648};
7649
7650void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7651 // Generate the constant area if needed.
7652 X86Assembler* assembler = GetAssembler();
7653 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7654 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7655 // byte values.
7656 assembler->Align(4, 0);
7657 constant_area_start_ = assembler->CodeSize();
7658
7659 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007660 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007661 jump_table->CreateJumpTable();
7662 }
7663
7664 // And now add the constant area to the generated code.
7665 assembler->AddConstantArea();
7666 }
7667
7668 // And finish up.
7669 CodeGenerator::Finalize(allocator);
7670}
7671
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007672Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7673 HX86ComputeBaseMethodAddress* method_base,
7674 Register reg) {
7675 AssemblerFixup* fixup =
7676 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007677 return Address(reg, kDummy32BitOffset, fixup);
7678}
7679
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007680Address CodeGeneratorX86::LiteralFloatAddress(float v,
7681 HX86ComputeBaseMethodAddress* method_base,
7682 Register reg) {
7683 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007684 return Address(reg, kDummy32BitOffset, fixup);
7685}
7686
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007687Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7688 HX86ComputeBaseMethodAddress* method_base,
7689 Register reg) {
7690 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007691 return Address(reg, kDummy32BitOffset, fixup);
7692}
7693
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007694Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7695 HX86ComputeBaseMethodAddress* method_base,
7696 Register reg) {
7697 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007698 return Address(reg, kDummy32BitOffset, fixup);
7699}
7700
Aart Bika19616e2016-02-01 18:57:58 -08007701void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7702 if (value == 0) {
7703 __ xorl(dest, dest);
7704 } else {
7705 __ movl(dest, Immediate(value));
7706 }
7707}
7708
7709void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7710 if (value == 0) {
7711 __ testl(dest, dest);
7712 } else {
7713 __ cmpl(dest, Immediate(value));
7714 }
7715}
7716
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007717void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7718 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007719 GenerateIntCompare(lhs_reg, rhs);
7720}
7721
7722void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007723 if (rhs.IsConstant()) {
7724 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007725 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007726 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007727 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007728 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007729 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007730 }
7731}
7732
7733Address CodeGeneratorX86::ArrayAddress(Register obj,
7734 Location index,
7735 ScaleFactor scale,
7736 uint32_t data_offset) {
7737 return index.IsConstant() ?
7738 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7739 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7740}
7741
Mark Mendell805b3b52015-09-18 14:10:29 -04007742Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7743 Register reg,
7744 Register value) {
7745 // Create a fixup to be used to create and address the jump table.
7746 JumpTableRIPFixup* table_fixup =
7747 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7748
7749 // We have to populate the jump tables.
7750 fixups_to_jump_tables_.push_back(table_fixup);
7751
7752 // We want a scaled address, as we are extracting the correct offset from the table.
7753 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7754}
7755
Andreas Gampe85b62f22015-09-09 13:15:38 -07007756// TODO: target as memory.
7757void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7758 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007759 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007760 return;
7761 }
7762
7763 DCHECK_NE(type, Primitive::kPrimVoid);
7764
7765 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7766 if (target.Equals(return_loc)) {
7767 return;
7768 }
7769
7770 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7771 // with the else branch.
7772 if (type == Primitive::kPrimLong) {
7773 HParallelMove parallel_move(GetGraph()->GetArena());
7774 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7775 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7776 GetMoveResolver()->EmitNativeCode(&parallel_move);
7777 } else {
7778 // Let the parallel move resolver take care of all of this.
7779 HParallelMove parallel_move(GetGraph()->GetArena());
7780 parallel_move.AddMove(return_loc, target, type, nullptr);
7781 GetMoveResolver()->EmitNativeCode(&parallel_move);
7782 }
7783}
7784
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007785void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7786 const uint8_t* roots_data,
7787 const PatchInfo<Label>& info,
7788 uint64_t index_in_table) const {
7789 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7790 uintptr_t address =
7791 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7792 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7793 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7794 dchecked_integral_cast<uint32_t>(address);
7795}
7796
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007797void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7798 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007799 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007800 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007801 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007802 uint64_t index_in_table = it->second;
7803 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007804 }
7805
7806 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007807 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007808 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7809 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007810 uint64_t index_in_table = it->second;
7811 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007812 }
7813}
7814
Roland Levillain4d027112015-07-01 15:41:14 +01007815#undef __
7816
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007817} // namespace x86
7818} // namespace art