blob: 512968f01d2e7330a8cedd223ba59b59bf8c1a49 [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)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001038 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001039 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001040 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001041 constant_area_start_(-1),
1042 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001043 method_address_offset_(std::less<uint32_t>(),
1044 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001045 // Use a fake return address register to mimic Quick.
1046 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001047}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048
David Brazdil58282f42016-01-14 12:45:10 +00001049void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001050 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001051 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052}
1053
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001054InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001055 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001056 assembler_(codegen->GetAssembler()),
1057 codegen_(codegen) {}
1058
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001059static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001060 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061}
1062
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001063void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001064 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001065 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001066 bool skip_overflow_check =
1067 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001068 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001069
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001071 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001072 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001073 }
1074
Mark Mendell5f874182015-03-04 15:42:45 -05001075 if (HasEmptyFrame()) {
1076 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001077 }
Mark Mendell5f874182015-03-04 15:42:45 -05001078
1079 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1080 Register reg = kCoreCalleeSaves[i];
1081 if (allocated_registers_.ContainsCoreRegister(reg)) {
1082 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083 __ cfi().AdjustCFAOffset(kX86WordSize);
1084 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001085 }
1086 }
1087
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001088 int adjust = GetFrameSize() - FrameEntrySpillSize();
1089 __ subl(ESP, Immediate(adjust));
1090 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001091 // Save the current method if we need it. Note that we do not
1092 // do this in HCurrentMethod, as the instruction might have been removed
1093 // in the SSA graph.
1094 if (RequiresCurrentMethod()) {
1095 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1096 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001097
1098 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1099 // Initialize should_deoptimize flag to 0.
1100 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1101 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001102}
1103
1104void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001105 __ cfi().RememberState();
1106 if (!HasEmptyFrame()) {
1107 int adjust = GetFrameSize() - FrameEntrySpillSize();
1108 __ addl(ESP, Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001110
David Srbeckyc34dc932015-04-12 09:27:43 +01001111 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1112 Register reg = kCoreCalleeSaves[i];
1113 if (allocated_registers_.ContainsCoreRegister(reg)) {
1114 __ popl(reg);
1115 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1116 __ cfi().Restore(DWARFReg(reg));
1117 }
Mark Mendell5f874182015-03-04 15:42:45 -05001118 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001119 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001120 __ ret();
1121 __ cfi().RestoreState();
1122 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001123}
1124
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001125void CodeGeneratorX86::Bind(HBasicBlock* block) {
1126 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001129Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1130 switch (type) {
1131 case Primitive::kPrimBoolean:
1132 case Primitive::kPrimByte:
1133 case Primitive::kPrimChar:
1134 case Primitive::kPrimShort:
1135 case Primitive::kPrimInt:
1136 case Primitive::kPrimNot:
1137 return Location::RegisterLocation(EAX);
1138
1139 case Primitive::kPrimLong:
1140 return Location::RegisterPairLocation(EAX, EDX);
1141
1142 case Primitive::kPrimVoid:
1143 return Location::NoLocation();
1144
1145 case Primitive::kPrimDouble:
1146 case Primitive::kPrimFloat:
1147 return Location::FpuRegisterLocation(XMM0);
1148 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001149
1150 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001151}
1152
1153Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1154 return Location::RegisterLocation(kMethodRegisterArgument);
1155}
1156
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001157Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001158 switch (type) {
1159 case Primitive::kPrimBoolean:
1160 case Primitive::kPrimByte:
1161 case Primitive::kPrimChar:
1162 case Primitive::kPrimShort:
1163 case Primitive::kPrimInt:
1164 case Primitive::kPrimNot: {
1165 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001166 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001167 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001169 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001170 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001171 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001172 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001174 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 uint32_t index = gp_index_;
1176 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1180 calling_convention.GetRegisterPairAt(index));
1181 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001183 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1184 }
1185 }
1186
1187 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001188 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001189 stack_index_++;
1190 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1191 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1192 } else {
1193 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1194 }
1195 }
1196
1197 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001198 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001199 stack_index_ += 2;
1200 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1201 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1202 } else {
1203 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001204 }
1205 }
1206
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001207 case Primitive::kPrimVoid:
1208 LOG(FATAL) << "Unexpected parameter type " << type;
1209 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001210 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001211 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001212}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001213
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214void CodeGeneratorX86::Move32(Location destination, Location source) {
1215 if (source.Equals(destination)) {
1216 return;
1217 }
1218 if (destination.IsRegister()) {
1219 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001220 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 } else {
1224 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001225 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else if (destination.IsFpuRegister()) {
1228 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 } else {
1233 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001237 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001242 } else if (source.IsConstant()) {
1243 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001244 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001245 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 } else {
1247 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001248 __ pushl(Address(ESP, source.GetStackIndex()));
1249 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 }
1251 }
1252}
1253
1254void CodeGeneratorX86::Move64(Location destination, Location source) {
1255 if (source.Equals(destination)) {
1256 return;
1257 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001258 if (destination.IsRegisterPair()) {
1259 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001260 EmitParallelMoves(
1261 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1262 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001263 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001264 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001265 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1266 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001267 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1269 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1270 __ psrlq(src_reg, Immediate(32));
1271 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1276 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1278 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001280 if (source.IsFpuRegister()) {
1281 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1282 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001284 } else if (source.IsRegisterPair()) {
1285 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1286 // Create stack space for 2 elements.
1287 __ subl(ESP, Immediate(2 * elem_size));
1288 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1289 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1290 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1291 // And remove the temporary stack space we allocated.
1292 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001293 } else {
1294 LOG(FATAL) << "Unimplemented";
1295 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001297 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001298 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001299 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001300 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001301 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001304 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001305 } else if (source.IsConstant()) {
1306 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001307 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1308 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001309 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001310 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1311 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001313 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001314 EmitParallelMoves(
1315 Location::StackSlot(source.GetStackIndex()),
1316 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001317 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001318 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001319 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1320 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 }
1322 }
1323}
1324
Calin Juravle175dc732015-08-25 15:42:32 +01001325void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1326 DCHECK(location.IsRegister());
1327 __ movl(location.AsRegister<Register>(), Immediate(value));
1328}
1329
Calin Juravlee460d1d2015-09-29 04:52:17 +01001330void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001331 HParallelMove move(GetGraph()->GetArena());
1332 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1333 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1334 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001335 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001336 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001338 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001339}
1340
1341void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1342 if (location.IsRegister()) {
1343 locations->AddTemp(location);
1344 } else if (location.IsRegisterPair()) {
1345 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1346 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1347 } else {
1348 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1349 }
1350}
1351
David Brazdilfc6a86a2015-06-26 10:33:45 +00001352void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001353 DCHECK(!successor->IsExitBlock());
1354
1355 HBasicBlock* block = got->GetBlock();
1356 HInstruction* previous = got->GetPrevious();
1357
1358 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001359 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001360 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1361 return;
1362 }
1363
1364 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1365 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1366 }
1367 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001368 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369 }
1370}
1371
David Brazdilfc6a86a2015-06-26 10:33:45 +00001372void LocationsBuilderX86::VisitGoto(HGoto* got) {
1373 got->SetLocations(nullptr);
1374}
1375
1376void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1377 HandleGoto(got, got->GetSuccessor());
1378}
1379
1380void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1381 try_boundary->SetLocations(nullptr);
1382}
1383
1384void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1385 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1386 if (!successor->IsExitBlock()) {
1387 HandleGoto(try_boundary, successor);
1388 }
1389}
1390
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001392 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393}
1394
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001395void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001396}
1397
Mark Mendell152408f2015-12-31 12:28:50 -05001398template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001399void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001400 LabelType* true_label,
1401 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 if (cond->IsFPConditionTrueIfNaN()) {
1403 __ j(kUnordered, true_label);
1404 } else if (cond->IsFPConditionFalseIfNaN()) {
1405 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001406 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001408}
1409
Mark Mendell152408f2015-12-31 12:28:50 -05001410template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001411void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001412 LabelType* true_label,
1413 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001414 LocationSummary* locations = cond->GetLocations();
1415 Location left = locations->InAt(0);
1416 Location right = locations->InAt(1);
1417 IfCondition if_cond = cond->GetCondition();
1418
Mark Mendellc4701932015-04-10 13:18:51 -04001419 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001421 IfCondition true_high_cond = if_cond;
1422 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001423 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001424
1425 // Set the conditions for the test, remembering that == needs to be
1426 // decided using the low words.
1427 switch (if_cond) {
1428 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001429 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001430 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001431 break;
1432 case kCondLT:
1433 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001434 break;
1435 case kCondLE:
1436 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001437 break;
1438 case kCondGT:
1439 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001440 break;
1441 case kCondGE:
1442 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001443 break;
Aart Bike9f37602015-10-09 11:15:55 -07001444 case kCondB:
1445 false_high_cond = kCondA;
1446 break;
1447 case kCondBE:
1448 true_high_cond = kCondB;
1449 break;
1450 case kCondA:
1451 false_high_cond = kCondB;
1452 break;
1453 case kCondAE:
1454 true_high_cond = kCondA;
1455 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001456 }
1457
1458 if (right.IsConstant()) {
1459 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001460 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001461 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001462
Aart Bika19616e2016-02-01 18:57:58 -08001463 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001464 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001465 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001466 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001467 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001468 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001469 __ j(X86Condition(true_high_cond), true_label);
1470 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 }
1472 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001473 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001474 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001475 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001476 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001477
1478 __ cmpl(left_high, right_high);
1479 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001480 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001481 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001482 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001483 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001484 __ j(X86Condition(true_high_cond), true_label);
1485 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 }
1487 // Must be equal high, so compare the lows.
1488 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001489 } else {
1490 DCHECK(right.IsDoubleStackSlot());
1491 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1492 if (if_cond == kCondNE) {
1493 __ j(X86Condition(true_high_cond), true_label);
1494 } else if (if_cond == kCondEQ) {
1495 __ j(X86Condition(false_high_cond), false_label);
1496 } else {
1497 __ j(X86Condition(true_high_cond), true_label);
1498 __ j(X86Condition(false_high_cond), false_label);
1499 }
1500 // Must be equal high, so compare the lows.
1501 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001502 }
1503 // The last comparison might be unsigned.
1504 __ j(final_condition, true_label);
1505}
1506
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001507void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1508 Location rhs,
1509 HInstruction* insn,
1510 bool is_double) {
1511 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1512 if (is_double) {
1513 if (rhs.IsFpuRegister()) {
1514 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1515 } else if (const_area != nullptr) {
1516 DCHECK(const_area->IsEmittedAtUseSite());
1517 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1518 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001519 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1520 const_area->GetBaseMethodAddress(),
1521 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001522 } else {
1523 DCHECK(rhs.IsDoubleStackSlot());
1524 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1525 }
1526 } else {
1527 if (rhs.IsFpuRegister()) {
1528 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1529 } else if (const_area != nullptr) {
1530 DCHECK(const_area->IsEmittedAtUseSite());
1531 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1532 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001533 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1534 const_area->GetBaseMethodAddress(),
1535 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001536 } else {
1537 DCHECK(rhs.IsStackSlot());
1538 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1539 }
1540 }
1541}
1542
Mark Mendell152408f2015-12-31 12:28:50 -05001543template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001544void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001545 LabelType* true_target_in,
1546 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001547 // Generated branching requires both targets to be explicit. If either of the
1548 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001549 LabelType fallthrough_target;
1550 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1551 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001552
Mark Mendellc4701932015-04-10 13:18:51 -04001553 LocationSummary* locations = condition->GetLocations();
1554 Location left = locations->InAt(0);
1555 Location right = locations->InAt(1);
1556
Mark Mendellc4701932015-04-10 13:18:51 -04001557 Primitive::Type type = condition->InputAt(0)->GetType();
1558 switch (type) {
1559 case Primitive::kPrimLong:
1560 GenerateLongComparesAndJumps(condition, true_target, false_target);
1561 break;
1562 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001563 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001564 GenerateFPJumps(condition, true_target, false_target);
1565 break;
1566 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001567 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001568 GenerateFPJumps(condition, true_target, false_target);
1569 break;
1570 default:
1571 LOG(FATAL) << "Unexpected compare type " << type;
1572 }
1573
David Brazdil0debae72015-11-12 18:37:00 +00001574 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001575 __ jmp(false_target);
1576 }
David Brazdil0debae72015-11-12 18:37:00 +00001577
1578 if (fallthrough_target.IsLinked()) {
1579 __ Bind(&fallthrough_target);
1580 }
Mark Mendellc4701932015-04-10 13:18:51 -04001581}
1582
David Brazdil0debae72015-11-12 18:37:00 +00001583static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1584 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1585 // are set only strictly before `branch`. We can't use the eflags on long/FP
1586 // conditions if they are materialized due to the complex branching.
1587 return cond->IsCondition() &&
1588 cond->GetNext() == branch &&
1589 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1590 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1591}
1592
Mark Mendell152408f2015-12-31 12:28:50 -05001593template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001594void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001595 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001596 LabelType* true_target,
1597 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001598 HInstruction* cond = instruction->InputAt(condition_input_index);
1599
1600 if (true_target == nullptr && false_target == nullptr) {
1601 // Nothing to do. The code always falls through.
1602 return;
1603 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001604 // Constant condition, statically compared against "true" (integer value 1).
1605 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001606 if (true_target != nullptr) {
1607 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001608 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001609 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001610 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001611 if (false_target != nullptr) {
1612 __ jmp(false_target);
1613 }
1614 }
1615 return;
1616 }
1617
1618 // The following code generates these patterns:
1619 // (1) true_target == nullptr && false_target != nullptr
1620 // - opposite condition true => branch to false_target
1621 // (2) true_target != nullptr && false_target == nullptr
1622 // - condition true => branch to true_target
1623 // (3) true_target != nullptr && false_target != nullptr
1624 // - condition true => branch to true_target
1625 // - branch to false_target
1626 if (IsBooleanValueOrMaterializedCondition(cond)) {
1627 if (AreEflagsSetFrom(cond, instruction)) {
1628 if (true_target == nullptr) {
1629 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1630 } else {
1631 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1632 }
1633 } else {
1634 // Materialized condition, compare against 0.
1635 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1636 if (lhs.IsRegister()) {
1637 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1638 } else {
1639 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1640 }
1641 if (true_target == nullptr) {
1642 __ j(kEqual, false_target);
1643 } else {
1644 __ j(kNotEqual, true_target);
1645 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001646 }
1647 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001648 // Condition has not been materialized, use its inputs as the comparison and
1649 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001650 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001651
1652 // If this is a long or FP comparison that has been folded into
1653 // the HCondition, generate the comparison directly.
1654 Primitive::Type type = condition->InputAt(0)->GetType();
1655 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1656 GenerateCompareTestAndBranch(condition, true_target, false_target);
1657 return;
1658 }
1659
1660 Location lhs = condition->GetLocations()->InAt(0);
1661 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001663 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001664 if (true_target == nullptr) {
1665 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1666 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001667 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001668 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001669 }
David Brazdil0debae72015-11-12 18:37:00 +00001670
1671 // If neither branch falls through (case 3), the conditional branch to `true_target`
1672 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1673 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001675 }
1676}
1677
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001678void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1680 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001681 locations->SetInAt(0, Location::Any());
1682 }
1683}
1684
1685void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001686 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1687 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1688 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1689 nullptr : codegen_->GetLabelOf(true_successor);
1690 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1691 nullptr : codegen_->GetLabelOf(false_successor);
1692 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693}
1694
1695void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1696 LocationSummary* locations = new (GetGraph()->GetArena())
1697 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001698 InvokeRuntimeCallingConvention calling_convention;
1699 RegisterSet caller_saves = RegisterSet::Empty();
1700 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1701 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001702 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001703 locations->SetInAt(0, Location::Any());
1704 }
1705}
1706
1707void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001708 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001709 GenerateTestAndBranch<Label>(deoptimize,
1710 /* condition_input_index */ 0,
1711 slow_path->GetEntryLabel(),
1712 /* false_target */ nullptr);
1713}
1714
Mingyao Yang063fc772016-08-02 11:02:54 -07001715void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1716 LocationSummary* locations = new (GetGraph()->GetArena())
1717 LocationSummary(flag, LocationSummary::kNoCall);
1718 locations->SetOut(Location::RequiresRegister());
1719}
1720
1721void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1722 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1723 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1724}
1725
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001726static bool SelectCanUseCMOV(HSelect* select) {
1727 // There are no conditional move instructions for XMMs.
1728 if (Primitive::IsFloatingPointType(select->GetType())) {
1729 return false;
1730 }
1731
1732 // A FP condition doesn't generate the single CC that we need.
1733 // In 32 bit mode, a long condition doesn't generate a single CC either.
1734 HInstruction* condition = select->GetCondition();
1735 if (condition->IsCondition()) {
1736 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1737 if (compare_type == Primitive::kPrimLong ||
1738 Primitive::IsFloatingPointType(compare_type)) {
1739 return false;
1740 }
1741 }
1742
1743 // We can generate a CMOV for this Select.
1744 return true;
1745}
1746
David Brazdil74eb1b22015-12-14 11:44:01 +00001747void LocationsBuilderX86::VisitSelect(HSelect* select) {
1748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001749 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001750 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001751 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001752 } else {
1753 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001754 if (SelectCanUseCMOV(select)) {
1755 if (select->InputAt(1)->IsConstant()) {
1756 // Cmov can't handle a constant value.
1757 locations->SetInAt(1, Location::RequiresRegister());
1758 } else {
1759 locations->SetInAt(1, Location::Any());
1760 }
1761 } else {
1762 locations->SetInAt(1, Location::Any());
1763 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001764 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001765 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1766 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001767 }
1768 locations->SetOut(Location::SameAsFirstInput());
1769}
1770
1771void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1772 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001773 DCHECK(locations->InAt(0).Equals(locations->Out()));
1774 if (SelectCanUseCMOV(select)) {
1775 // If both the condition and the source types are integer, we can generate
1776 // a CMOV to implement Select.
1777
1778 HInstruction* select_condition = select->GetCondition();
1779 Condition cond = kNotEqual;
1780
1781 // Figure out how to test the 'condition'.
1782 if (select_condition->IsCondition()) {
1783 HCondition* condition = select_condition->AsCondition();
1784 if (!condition->IsEmittedAtUseSite()) {
1785 // This was a previously materialized condition.
1786 // Can we use the existing condition code?
1787 if (AreEflagsSetFrom(condition, select)) {
1788 // Materialization was the previous instruction. Condition codes are right.
1789 cond = X86Condition(condition->GetCondition());
1790 } else {
1791 // No, we have to recreate the condition code.
1792 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1793 __ testl(cond_reg, cond_reg);
1794 }
1795 } else {
1796 // We can't handle FP or long here.
1797 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1798 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1799 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001800 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001801 cond = X86Condition(condition->GetCondition());
1802 }
1803 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001804 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001805 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1806 __ testl(cond_reg, cond_reg);
1807 }
1808
1809 // If the condition is true, overwrite the output, which already contains false.
1810 Location false_loc = locations->InAt(0);
1811 Location true_loc = locations->InAt(1);
1812 if (select->GetType() == Primitive::kPrimLong) {
1813 // 64 bit conditional move.
1814 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1815 Register false_low = false_loc.AsRegisterPairLow<Register>();
1816 if (true_loc.IsRegisterPair()) {
1817 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1818 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1819 } else {
1820 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1821 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1822 }
1823 } else {
1824 // 32 bit conditional move.
1825 Register false_reg = false_loc.AsRegister<Register>();
1826 if (true_loc.IsRegister()) {
1827 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1828 } else {
1829 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1830 }
1831 }
1832 } else {
1833 NearLabel false_target;
1834 GenerateTestAndBranch<NearLabel>(
1835 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1836 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1837 __ Bind(&false_target);
1838 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001839}
1840
David Srbecky0cf44932015-12-09 14:09:59 +00001841void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1842 new (GetGraph()->GetArena()) LocationSummary(info);
1843}
1844
David Srbeckyd28f4a02016-03-14 17:14:24 +00001845void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1846 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001847}
1848
1849void CodeGeneratorX86::GenerateNop() {
1850 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001851}
1852
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001854 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001855 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001856 // Handle the long/FP comparisons made in instruction simplification.
1857 switch (cond->InputAt(0)->GetType()) {
1858 case Primitive::kPrimLong: {
1859 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001860 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001861 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001862 locations->SetOut(Location::RequiresRegister());
1863 }
1864 break;
1865 }
1866 case Primitive::kPrimFloat:
1867 case Primitive::kPrimDouble: {
1868 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001869 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1870 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1871 } else if (cond->InputAt(1)->IsConstant()) {
1872 locations->SetInAt(1, Location::RequiresFpuRegister());
1873 } else {
1874 locations->SetInAt(1, Location::Any());
1875 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001876 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001877 locations->SetOut(Location::RequiresRegister());
1878 }
1879 break;
1880 }
1881 default:
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001884 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001885 // We need a byte register.
1886 locations->SetOut(Location::RegisterLocation(ECX));
1887 }
1888 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001890}
1891
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001893 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001894 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001895 }
Mark Mendellc4701932015-04-10 13:18:51 -04001896
1897 LocationSummary* locations = cond->GetLocations();
1898 Location lhs = locations->InAt(0);
1899 Location rhs = locations->InAt(1);
1900 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001901 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001902
1903 switch (cond->InputAt(0)->GetType()) {
1904 default: {
1905 // Integer case.
1906
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001907 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001908 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001909 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001910 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001911 return;
1912 }
1913 case Primitive::kPrimLong:
1914 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1915 break;
1916 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001917 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001918 GenerateFPJumps(cond, &true_label, &false_label);
1919 break;
1920 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001921 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001922 GenerateFPJumps(cond, &true_label, &false_label);
1923 break;
1924 }
1925
1926 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001927 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001928
Roland Levillain4fa13f62015-07-06 18:11:54 +01001929 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001930 __ Bind(&false_label);
1931 __ xorl(reg, reg);
1932 __ jmp(&done_label);
1933
Roland Levillain4fa13f62015-07-06 18:11:54 +01001934 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001935 __ Bind(&true_label);
1936 __ movl(reg, Immediate(1));
1937 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001946}
1947
1948void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001954}
1955
1956void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001962}
1963
1964void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001970}
1971
1972void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001974}
1975
1976void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001978}
1979
1980void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001982}
1983
1984void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001986}
1987
Aart Bike9f37602015-10-09 11:15:55 -07001988void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001990}
1991
1992void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001994}
1995
1996void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001998}
1999
2000void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002002}
2003
2004void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002006}
2007
2008void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002010}
2011
2012void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002014}
2015
2016void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002018}
2019
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002020void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002027 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028}
2029
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002030void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2033 locations->SetOut(Location::ConstantLocation(constant));
2034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002037 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002038}
2039
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002043 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044}
2045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 // Will be generated at use site.
2048}
2049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002050void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2051 LocationSummary* locations =
2052 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2053 locations->SetOut(Location::ConstantLocation(constant));
2054}
2055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002056void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 // Will be generated at use site.
2058}
2059
2060void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2063 locations->SetOut(Location::ConstantLocation(constant));
2064}
2065
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002066void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067 // Will be generated at use site.
2068}
2069
Igor Murashkind01745e2017-04-05 16:40:31 -07002070void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2071 constructor_fence->SetLocations(nullptr);
2072}
2073
2074void InstructionCodeGeneratorX86::VisitConstructorFence(
2075 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2076 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2077}
2078
Calin Juravle27df7582015-04-17 19:12:31 +01002079void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2080 memory_barrier->SetLocations(nullptr);
2081}
2082
2083void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002084 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002085}
2086
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002087void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002088 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002089}
2090
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002091void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002092 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002093}
2094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002095void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002098 switch (ret->InputAt(0)->GetType()) {
2099 case Primitive::kPrimBoolean:
2100 case Primitive::kPrimByte:
2101 case Primitive::kPrimChar:
2102 case Primitive::kPrimShort:
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002106 break;
2107
2108 case Primitive::kPrimLong:
2109 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002110 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002111 break;
2112
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002113 case Primitive::kPrimFloat:
2114 case Primitive::kPrimDouble:
2115 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002116 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002117 break;
2118
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002120 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122}
2123
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002124void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 if (kIsDebugBuild) {
2126 switch (ret->InputAt(0)->GetType()) {
2127 case Primitive::kPrimBoolean:
2128 case Primitive::kPrimByte:
2129 case Primitive::kPrimChar:
2130 case Primitive::kPrimShort:
2131 case Primitive::kPrimInt:
2132 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134 break;
2135
2136 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2138 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002139 break;
2140
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002141 case Primitive::kPrimFloat:
2142 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002144 break;
2145
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002146 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002147 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 }
2149 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002150 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151}
2152
Calin Juravle175dc732015-08-25 15:42:32 +01002153void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2154 // The trampoline uses the same calling convention as dex calling conventions,
2155 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2156 // the method_idx.
2157 HandleInvoke(invoke);
2158}
2159
2160void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2161 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2162}
2163
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002164void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002165 // Explicit clinit checks triggered by static invokes must have been pruned by
2166 // art::PrepareForRegisterAllocation.
2167 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002168
Mark Mendellfb8d2792015-03-31 22:16:59 -04002169 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002170 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002171 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002172 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002173 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002174 return;
2175 }
2176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002177 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002178
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002179 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002180 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002181 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002182 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183}
2184
Mark Mendell09ed1a32015-03-25 08:30:06 -04002185static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2186 if (invoke->GetLocations()->Intrinsified()) {
2187 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2188 intrinsic.Dispatch(invoke);
2189 return true;
2190 }
2191 return false;
2192}
2193
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002194void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002195 // Explicit clinit checks triggered by static invokes must have been pruned by
2196 // art::PrepareForRegisterAllocation.
2197 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002198
Mark Mendell09ed1a32015-03-25 08:30:06 -04002199 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2200 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002201 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002202
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002203 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002204 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002205 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002206}
2207
2208void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002209 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2210 if (intrinsic.TryDispatch(invoke)) {
2211 return;
2212 }
2213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002214 HandleInvoke(invoke);
2215}
2216
2217void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002218 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002219 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002220}
2221
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002222void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002223 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2224 return;
2225 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002226
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002227 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002228 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002229}
2230
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002231void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002232 // This call to HandleInvoke allocates a temporary (core) register
2233 // which is also used to transfer the hidden argument from FP to
2234 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002235 HandleInvoke(invoke);
2236 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002237 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238}
2239
2240void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2241 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002242 LocationSummary* locations = invoke->GetLocations();
2243 Register temp = locations->GetTemp(0).AsRegister<Register>();
2244 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002245 Location receiver = locations->InAt(0);
2246 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2247
Roland Levillain0d5a2812015-11-13 10:07:31 +00002248 // Set the hidden argument. This is safe to do this here, as XMM7
2249 // won't be modified thereafter, before the `call` instruction.
2250 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002251 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002252 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254 if (receiver.IsStackSlot()) {
2255 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002256 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257 __ movl(temp, Address(temp, class_offset));
2258 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002259 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002260 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261 }
Roland Levillain4d027112015-07-01 15:41:14 +01002262 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002263 // Instead of simply (possibly) unpoisoning `temp` here, we should
2264 // emit a read barrier for the previous class reference load.
2265 // However this is not required in practice, as this is an
2266 // intermediate/temporary reference and because the current
2267 // concurrent copying collector keeps the from-space memory
2268 // intact/accessible until the end of the marking phase (the
2269 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002270 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002271 // temp = temp->GetAddressOfIMT()
2272 __ movl(temp,
2273 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002275 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002276 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 __ movl(temp, Address(temp, method_offset));
2278 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002279 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002280 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281
2282 DCHECK(!codegen_->IsLeafMethod());
2283 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2284}
2285
Orion Hodsonac141392017-01-13 11:53:47 +00002286void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2287 HandleInvoke(invoke);
2288}
2289
2290void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2291 codegen_->GenerateInvokePolymorphicCall(invoke);
2292}
2293
Roland Levillain88cb1752014-10-20 16:36:47 +01002294void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2297 switch (neg->GetResultType()) {
2298 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002299 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002300 locations->SetInAt(0, Location::RequiresRegister());
2301 locations->SetOut(Location::SameAsFirstInput());
2302 break;
2303
Roland Levillain88cb1752014-10-20 16:36:47 +01002304 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002305 locations->SetInAt(0, Location::RequiresFpuRegister());
2306 locations->SetOut(Location::SameAsFirstInput());
2307 locations->AddTemp(Location::RequiresRegister());
2308 locations->AddTemp(Location::RequiresFpuRegister());
2309 break;
2310
Roland Levillain88cb1752014-10-20 16:36:47 +01002311 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002312 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002313 locations->SetOut(Location::SameAsFirstInput());
2314 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2319 }
2320}
2321
2322void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2323 LocationSummary* locations = neg->GetLocations();
2324 Location out = locations->Out();
2325 Location in = locations->InAt(0);
2326 switch (neg->GetResultType()) {
2327 case Primitive::kPrimInt:
2328 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002329 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002330 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002331 break;
2332
2333 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002334 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002335 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002336 __ negl(out.AsRegisterPairLow<Register>());
2337 // Negation is similar to subtraction from zero. The least
2338 // significant byte triggers a borrow when it is different from
2339 // zero; to take it into account, add 1 to the most significant
2340 // byte if the carry flag (CF) is set to 1 after the first NEGL
2341 // operation.
2342 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2343 __ negl(out.AsRegisterPairHigh<Register>());
2344 break;
2345
Roland Levillain5368c212014-11-27 15:03:41 +00002346 case Primitive::kPrimFloat: {
2347 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002348 Register constant = locations->GetTemp(0).AsRegister<Register>();
2349 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002350 // Implement float negation with an exclusive or with value
2351 // 0x80000000 (mask for bit 31, representing the sign of a
2352 // single-precision floating-point number).
2353 __ movl(constant, Immediate(INT32_C(0x80000000)));
2354 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002356 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002357 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002358
Roland Levillain5368c212014-11-27 15:03:41 +00002359 case Primitive::kPrimDouble: {
2360 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002362 // Implement double negation with an exclusive or with value
2363 // 0x8000000000000000 (mask for bit 63, representing the sign of
2364 // a double-precision floating-point number).
2365 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002366 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002367 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002368 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002369
2370 default:
2371 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2372 }
2373}
2374
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002375void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2376 LocationSummary* locations =
2377 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2378 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2379 locations->SetInAt(0, Location::RequiresFpuRegister());
2380 locations->SetInAt(1, Location::RequiresRegister());
2381 locations->SetOut(Location::SameAsFirstInput());
2382 locations->AddTemp(Location::RequiresFpuRegister());
2383}
2384
2385void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2386 LocationSummary* locations = neg->GetLocations();
2387 Location out = locations->Out();
2388 DCHECK(locations->InAt(0).Equals(out));
2389
2390 Register constant_area = locations->InAt(1).AsRegister<Register>();
2391 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2392 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002393 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2394 neg->GetBaseMethodAddress(),
2395 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002396 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2397 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002398 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2399 neg->GetBaseMethodAddress(),
2400 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002401 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2402 }
2403}
2404
Roland Levillaindff1f282014-11-05 14:15:05 +00002405void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002406 Primitive::Type result_type = conversion->GetResultType();
2407 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002408 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002409
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002410 // The float-to-long and double-to-long type conversions rely on a
2411 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002412 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002413 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2414 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002415 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002416 : LocationSummary::kNoCall;
2417 LocationSummary* locations =
2418 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2419
David Brazdilb2bd1c52015-03-25 11:17:37 +00002420 // The Java language does not allow treating boolean as an integral type but
2421 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002422
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002424 case Primitive::kPrimByte:
2425 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002426 case Primitive::kPrimLong: {
2427 // Type conversion from long to byte is a result of code transformations.
2428 HInstruction* input = conversion->InputAt(0);
2429 Location input_location = input->IsConstant()
2430 ? Location::ConstantLocation(input->AsConstant())
2431 : Location::RegisterPairLocation(EAX, EDX);
2432 locations->SetInAt(0, input_location);
2433 // Make the output overlap to please the register allocator. This greatly simplifies
2434 // the validation of the linear scan implementation
2435 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2436 break;
2437 }
David Brazdil46e2a392015-03-16 17:31:52 +00002438 case Primitive::kPrimBoolean:
2439 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002440 case Primitive::kPrimShort:
2441 case Primitive::kPrimInt:
2442 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002443 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002444 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2445 // Make the output overlap to please the register allocator. This greatly simplifies
2446 // the validation of the linear scan implementation
2447 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002448 break;
2449
2450 default:
2451 LOG(FATAL) << "Unexpected type conversion from " << input_type
2452 << " to " << result_type;
2453 }
2454 break;
2455
Roland Levillain01a8d712014-11-14 16:27:39 +00002456 case Primitive::kPrimShort:
2457 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002458 case Primitive::kPrimLong:
2459 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimInt:
2464 case Primitive::kPrimChar:
2465 // Processing a Dex `int-to-short' instruction.
2466 locations->SetInAt(0, Location::Any());
2467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillain946e1432014-11-11 17:35:19 +00002476 case Primitive::kPrimInt:
2477 switch (input_type) {
2478 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002479 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002480 locations->SetInAt(0, Location::Any());
2481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2482 break;
2483
2484 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002485 // Processing a Dex `float-to-int' instruction.
2486 locations->SetInAt(0, Location::RequiresFpuRegister());
2487 locations->SetOut(Location::RequiresRegister());
2488 locations->AddTemp(Location::RequiresFpuRegister());
2489 break;
2490
Roland Levillain946e1432014-11-11 17:35:19 +00002491 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002492 // Processing a Dex `double-to-int' instruction.
2493 locations->SetInAt(0, Location::RequiresFpuRegister());
2494 locations->SetOut(Location::RequiresRegister());
2495 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002496 break;
2497
2498 default:
2499 LOG(FATAL) << "Unexpected type conversion from " << input_type
2500 << " to " << result_type;
2501 }
2502 break;
2503
Roland Levillaindff1f282014-11-05 14:15:05 +00002504 case Primitive::kPrimLong:
2505 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002506 case Primitive::kPrimBoolean:
2507 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002508 case Primitive::kPrimByte:
2509 case Primitive::kPrimShort:
2510 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002511 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002512 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 locations->SetInAt(0, Location::RegisterLocation(EAX));
2514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2515 break;
2516
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002517 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002518 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002519 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002520 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002521 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2522 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2523
Vladimir Marko949c91f2015-01-27 10:48:44 +00002524 // The runtime helper puts the result in EAX, EDX.
2525 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002526 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002527 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 }
2533 break;
2534
Roland Levillain981e4542014-11-14 11:47:14 +00002535 case Primitive::kPrimChar:
2536 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002537 case Primitive::kPrimLong:
2538 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002539 case Primitive::kPrimBoolean:
2540 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002541 case Primitive::kPrimByte:
2542 case Primitive::kPrimShort:
2543 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002544 // Processing a Dex `int-to-char' instruction.
2545 locations->SetInAt(0, Location::Any());
2546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2547 break;
2548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
2552 }
2553 break;
2554
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002556 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002557 case Primitive::kPrimBoolean:
2558 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002559 case Primitive::kPrimByte:
2560 case Primitive::kPrimShort:
2561 case Primitive::kPrimInt:
2562 case Primitive::kPrimChar:
2563 // Processing a Dex `int-to-float' instruction.
2564 locations->SetInAt(0, Location::RequiresRegister());
2565 locations->SetOut(Location::RequiresFpuRegister());
2566 break;
2567
2568 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002569 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002570 locations->SetInAt(0, Location::Any());
2571 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002572 break;
2573
Roland Levillaincff13742014-11-17 14:32:17 +00002574 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002575 // Processing a Dex `double-to-float' instruction.
2576 locations->SetInAt(0, Location::RequiresFpuRegister());
2577 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unexpected type conversion from " << input_type
2582 << " to " << result_type;
2583 };
2584 break;
2585
Roland Levillaindff1f282014-11-05 14:15:05 +00002586 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002587 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002588 case Primitive::kPrimBoolean:
2589 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002590 case Primitive::kPrimByte:
2591 case Primitive::kPrimShort:
2592 case Primitive::kPrimInt:
2593 case Primitive::kPrimChar:
2594 // Processing a Dex `int-to-double' instruction.
2595 locations->SetInAt(0, Location::RequiresRegister());
2596 locations->SetOut(Location::RequiresFpuRegister());
2597 break;
2598
2599 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002600 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002601 locations->SetInAt(0, Location::Any());
2602 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002603 break;
2604
Roland Levillaincff13742014-11-17 14:32:17 +00002605 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002606 // Processing a Dex `float-to-double' instruction.
2607 locations->SetInAt(0, Location::RequiresFpuRegister());
2608 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002609 break;
2610
2611 default:
2612 LOG(FATAL) << "Unexpected type conversion from " << input_type
2613 << " to " << result_type;
2614 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002615 break;
2616
2617 default:
2618 LOG(FATAL) << "Unexpected type conversion from " << input_type
2619 << " to " << result_type;
2620 }
2621}
2622
2623void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2624 LocationSummary* locations = conversion->GetLocations();
2625 Location out = locations->Out();
2626 Location in = locations->InAt(0);
2627 Primitive::Type result_type = conversion->GetResultType();
2628 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002629 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002630 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002631 case Primitive::kPrimByte:
2632 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002633 case Primitive::kPrimLong:
2634 // Type conversion from long to byte is a result of code transformations.
2635 if (in.IsRegisterPair()) {
2636 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2637 } else {
2638 DCHECK(in.GetConstant()->IsLongConstant());
2639 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2640 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2641 }
2642 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002643 case Primitive::kPrimBoolean:
2644 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002645 case Primitive::kPrimShort:
2646 case Primitive::kPrimInt:
2647 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002648 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002649 if (in.IsRegister()) {
2650 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002651 } else {
2652 DCHECK(in.GetConstant()->IsIntConstant());
2653 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2654 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2655 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002656 break;
2657
2658 default:
2659 LOG(FATAL) << "Unexpected type conversion from " << input_type
2660 << " to " << result_type;
2661 }
2662 break;
2663
Roland Levillain01a8d712014-11-14 16:27:39 +00002664 case Primitive::kPrimShort:
2665 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002666 case Primitive::kPrimLong:
2667 // Type conversion from long to short is a result of code transformations.
2668 if (in.IsRegisterPair()) {
2669 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2670 } else if (in.IsDoubleStackSlot()) {
2671 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2672 } else {
2673 DCHECK(in.GetConstant()->IsLongConstant());
2674 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2675 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2676 }
2677 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002678 case Primitive::kPrimBoolean:
2679 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002680 case Primitive::kPrimByte:
2681 case Primitive::kPrimInt:
2682 case Primitive::kPrimChar:
2683 // Processing a Dex `int-to-short' instruction.
2684 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002686 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 } else {
2689 DCHECK(in.GetConstant()->IsIntConstant());
2690 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002692 }
2693 break;
2694
2695 default:
2696 LOG(FATAL) << "Unexpected type conversion from " << input_type
2697 << " to " << result_type;
2698 }
2699 break;
2700
Roland Levillain946e1432014-11-11 17:35:19 +00002701 case Primitive::kPrimInt:
2702 switch (input_type) {
2703 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002704 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002705 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002706 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002707 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002709 } else {
2710 DCHECK(in.IsConstant());
2711 DCHECK(in.GetConstant()->IsLongConstant());
2712 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002714 }
2715 break;
2716
Roland Levillain3f8f9362014-12-02 17:45:01 +00002717 case Primitive::kPrimFloat: {
2718 // Processing a Dex `float-to-int' instruction.
2719 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2720 Register output = out.AsRegister<Register>();
2721 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002722 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002723
2724 __ movl(output, Immediate(kPrimIntMax));
2725 // temp = int-to-float(output)
2726 __ cvtsi2ss(temp, output);
2727 // if input >= temp goto done
2728 __ comiss(input, temp);
2729 __ j(kAboveEqual, &done);
2730 // if input == NaN goto nan
2731 __ j(kUnordered, &nan);
2732 // output = float-to-int-truncate(input)
2733 __ cvttss2si(output, input);
2734 __ jmp(&done);
2735 __ Bind(&nan);
2736 // output = 0
2737 __ xorl(output, output);
2738 __ Bind(&done);
2739 break;
2740 }
2741
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002742 case Primitive::kPrimDouble: {
2743 // Processing a Dex `double-to-int' instruction.
2744 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2745 Register output = out.AsRegister<Register>();
2746 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002747 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002748
2749 __ movl(output, Immediate(kPrimIntMax));
2750 // temp = int-to-double(output)
2751 __ cvtsi2sd(temp, output);
2752 // if input >= temp goto done
2753 __ comisd(input, temp);
2754 __ j(kAboveEqual, &done);
2755 // if input == NaN goto nan
2756 __ j(kUnordered, &nan);
2757 // output = double-to-int-truncate(input)
2758 __ cvttsd2si(output, input);
2759 __ jmp(&done);
2760 __ Bind(&nan);
2761 // output = 0
2762 __ xorl(output, output);
2763 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002764 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002765 }
Roland Levillain946e1432014-11-11 17:35:19 +00002766
2767 default:
2768 LOG(FATAL) << "Unexpected type conversion from " << input_type
2769 << " to " << result_type;
2770 }
2771 break;
2772
Roland Levillaindff1f282014-11-05 14:15:05 +00002773 case Primitive::kPrimLong:
2774 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002775 case Primitive::kPrimBoolean:
2776 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002777 case Primitive::kPrimByte:
2778 case Primitive::kPrimShort:
2779 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002780 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002781 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2783 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 __ cdq();
2786 break;
2787
2788 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002789 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002790 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002791 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002792 break;
2793
Roland Levillaindff1f282014-11-05 14:15:05 +00002794 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002795 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002796 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002797 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002798 break;
2799
2800 default:
2801 LOG(FATAL) << "Unexpected type conversion from " << input_type
2802 << " to " << result_type;
2803 }
2804 break;
2805
Roland Levillain981e4542014-11-14 11:47:14 +00002806 case Primitive::kPrimChar:
2807 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002808 case Primitive::kPrimLong:
2809 // Type conversion from long to short is a result of code transformations.
2810 if (in.IsRegisterPair()) {
2811 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2812 } else if (in.IsDoubleStackSlot()) {
2813 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2814 } else {
2815 DCHECK(in.GetConstant()->IsLongConstant());
2816 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2817 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2818 }
2819 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002820 case Primitive::kPrimBoolean:
2821 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002822 case Primitive::kPrimByte:
2823 case Primitive::kPrimShort:
2824 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002825 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2826 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002827 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002828 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002829 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002830 } else {
2831 DCHECK(in.GetConstant()->IsIntConstant());
2832 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002833 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002834 }
2835 break;
2836
2837 default:
2838 LOG(FATAL) << "Unexpected type conversion from " << input_type
2839 << " to " << result_type;
2840 }
2841 break;
2842
Roland Levillaindff1f282014-11-05 14:15:05 +00002843 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002844 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002845 case Primitive::kPrimBoolean:
2846 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002847 case Primitive::kPrimByte:
2848 case Primitive::kPrimShort:
2849 case Primitive::kPrimInt:
2850 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002851 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002853 break;
2854
Roland Levillain6d0e4832014-11-27 18:31:21 +00002855 case Primitive::kPrimLong: {
2856 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002857 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002858
Roland Levillain232ade02015-04-20 15:14:36 +01002859 // Create stack space for the call to
2860 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2861 // TODO: enhance register allocator to ask for stack temporaries.
2862 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2863 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2864 __ subl(ESP, Immediate(adjustment));
2865 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002866
Roland Levillain232ade02015-04-20 15:14:36 +01002867 // Load the value to the FP stack, using temporaries if needed.
2868 PushOntoFPStack(in, 0, adjustment, false, true);
2869
2870 if (out.IsStackSlot()) {
2871 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2872 } else {
2873 __ fstps(Address(ESP, 0));
2874 Location stack_temp = Location::StackSlot(0);
2875 codegen_->Move32(out, stack_temp);
2876 }
2877
2878 // Remove the temporary stack space we allocated.
2879 if (adjustment != 0) {
2880 __ addl(ESP, Immediate(adjustment));
2881 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002882 break;
2883 }
2884
Roland Levillaincff13742014-11-17 14:32:17 +00002885 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002886 // Processing a Dex `double-to-float' instruction.
2887 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002888 break;
2889
2890 default:
2891 LOG(FATAL) << "Unexpected type conversion from " << input_type
2892 << " to " << result_type;
2893 };
2894 break;
2895
Roland Levillaindff1f282014-11-05 14:15:05 +00002896 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002897 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002898 case Primitive::kPrimBoolean:
2899 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002900 case Primitive::kPrimByte:
2901 case Primitive::kPrimShort:
2902 case Primitive::kPrimInt:
2903 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002904 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002906 break;
2907
Roland Levillain647b9ed2014-11-27 12:06:00 +00002908 case Primitive::kPrimLong: {
2909 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002910 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002911
Roland Levillain232ade02015-04-20 15:14:36 +01002912 // Create stack space for the call to
2913 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2914 // TODO: enhance register allocator to ask for stack temporaries.
2915 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2916 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2917 __ subl(ESP, Immediate(adjustment));
2918 }
2919
2920 // Load the value to the FP stack, using temporaries if needed.
2921 PushOntoFPStack(in, 0, adjustment, false, true);
2922
2923 if (out.IsDoubleStackSlot()) {
2924 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2925 } else {
2926 __ fstpl(Address(ESP, 0));
2927 Location stack_temp = Location::DoubleStackSlot(0);
2928 codegen_->Move64(out, stack_temp);
2929 }
2930
2931 // Remove the temporary stack space we allocated.
2932 if (adjustment != 0) {
2933 __ addl(ESP, Immediate(adjustment));
2934 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002935 break;
2936 }
2937
Roland Levillaincff13742014-11-17 14:32:17 +00002938 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002939 // Processing a Dex `float-to-double' instruction.
2940 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002941 break;
2942
2943 default:
2944 LOG(FATAL) << "Unexpected type conversion from " << input_type
2945 << " to " << result_type;
2946 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002947 break;
2948
2949 default:
2950 LOG(FATAL) << "Unexpected type conversion from " << input_type
2951 << " to " << result_type;
2952 }
2953}
2954
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002955void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002959 case Primitive::kPrimInt: {
2960 locations->SetInAt(0, Location::RequiresRegister());
2961 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2963 break;
2964 }
2965
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002966 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002967 locations->SetInAt(0, Location::RequiresRegister());
2968 locations->SetInAt(1, Location::Any());
2969 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002970 break;
2971 }
2972
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002973 case Primitive::kPrimFloat:
2974 case Primitive::kPrimDouble: {
2975 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002976 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2977 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002978 } else if (add->InputAt(1)->IsConstant()) {
2979 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002980 } else {
2981 locations->SetInAt(1, Location::Any());
2982 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002984 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002985 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002986
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002987 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2989 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002991}
2992
2993void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2994 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 Location first = locations->InAt(0);
2996 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002997 Location out = locations->Out();
2998
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002999 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003000 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003001 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003002 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3003 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003004 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3005 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003006 } else {
3007 __ leal(out.AsRegister<Register>(), Address(
3008 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3009 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003010 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003011 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3012 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3013 __ addl(out.AsRegister<Register>(), Immediate(value));
3014 } else {
3015 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3016 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003017 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003018 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003019 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003021 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003022 }
3023
3024 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003025 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003026 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3027 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003028 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003029 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3030 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003031 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003032 } else {
3033 DCHECK(second.IsConstant()) << second;
3034 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3035 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3036 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003037 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003038 break;
3039 }
3040
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003041 case Primitive::kPrimFloat: {
3042 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003044 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3045 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003046 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003047 __ addss(first.AsFpuRegister<XmmRegister>(),
3048 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003049 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3050 const_area->GetBaseMethodAddress(),
3051 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003052 } else {
3053 DCHECK(second.IsStackSlot());
3054 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003055 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003056 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003057 }
3058
3059 case Primitive::kPrimDouble: {
3060 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003061 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003062 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3063 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003064 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003065 __ addsd(first.AsFpuRegister<XmmRegister>(),
3066 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003067 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3068 const_area->GetBaseMethodAddress(),
3069 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003070 } else {
3071 DCHECK(second.IsDoubleStackSlot());
3072 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003073 }
3074 break;
3075 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003076
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003077 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003078 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003079 }
3080}
3081
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003082void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003086 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003087 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003088 locations->SetInAt(0, Location::RequiresRegister());
3089 locations->SetInAt(1, Location::Any());
3090 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003091 break;
3092 }
Calin Juravle11351682014-10-23 15:38:15 +01003093 case Primitive::kPrimFloat:
3094 case Primitive::kPrimDouble: {
3095 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003096 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3097 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003098 } else if (sub->InputAt(1)->IsConstant()) {
3099 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003100 } else {
3101 locations->SetInAt(1, Location::Any());
3102 }
Calin Juravle11351682014-10-23 15:38:15 +01003103 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003104 break;
Calin Juravle11351682014-10-23 15:38:15 +01003105 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003106
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003107 default:
Calin Juravle11351682014-10-23 15:38:15 +01003108 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003109 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110}
3111
3112void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3113 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003114 Location first = locations->InAt(0);
3115 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003116 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003117 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003118 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003119 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003121 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003122 __ subl(first.AsRegister<Register>(),
3123 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003124 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003125 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003126 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003127 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003128 }
3129
3130 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003131 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003132 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3133 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003134 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003135 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003136 __ sbbl(first.AsRegisterPairHigh<Register>(),
3137 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003138 } else {
3139 DCHECK(second.IsConstant()) << second;
3140 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3141 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3142 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003143 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003144 break;
3145 }
3146
Calin Juravle11351682014-10-23 15:38:15 +01003147 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 if (second.IsFpuRegister()) {
3149 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3150 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3151 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003152 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003153 __ subss(first.AsFpuRegister<XmmRegister>(),
3154 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003155 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3156 const_area->GetBaseMethodAddress(),
3157 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003158 } else {
3159 DCHECK(second.IsStackSlot());
3160 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3161 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003162 break;
Calin Juravle11351682014-10-23 15:38:15 +01003163 }
3164
3165 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003166 if (second.IsFpuRegister()) {
3167 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3168 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3169 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003170 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003171 __ subsd(first.AsFpuRegister<XmmRegister>(),
3172 codegen_->LiteralDoubleAddress(
3173 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003174 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003175 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3176 } else {
3177 DCHECK(second.IsDoubleStackSlot());
3178 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3179 }
Calin Juravle11351682014-10-23 15:38:15 +01003180 break;
3181 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003182
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003183 default:
Calin Juravle11351682014-10-23 15:38:15 +01003184 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003185 }
3186}
3187
Calin Juravle34bacdf2014-10-07 20:23:36 +01003188void LocationsBuilderX86::VisitMul(HMul* mul) {
3189 LocationSummary* locations =
3190 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3191 switch (mul->GetResultType()) {
3192 case Primitive::kPrimInt:
3193 locations->SetInAt(0, Location::RequiresRegister());
3194 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003195 if (mul->InputAt(1)->IsIntConstant()) {
3196 // Can use 3 operand multiply.
3197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3198 } else {
3199 locations->SetOut(Location::SameAsFirstInput());
3200 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003201 break;
3202 case Primitive::kPrimLong: {
3203 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204 locations->SetInAt(1, Location::Any());
3205 locations->SetOut(Location::SameAsFirstInput());
3206 // Needed for imul on 32bits with 64bits output.
3207 locations->AddTemp(Location::RegisterLocation(EAX));
3208 locations->AddTemp(Location::RegisterLocation(EDX));
3209 break;
3210 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003211 case Primitive::kPrimFloat:
3212 case Primitive::kPrimDouble: {
3213 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003214 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3215 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003216 } else if (mul->InputAt(1)->IsConstant()) {
3217 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003218 } else {
3219 locations->SetInAt(1, Location::Any());
3220 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003221 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003222 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003223 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224
3225 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003226 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227 }
3228}
3229
3230void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3231 LocationSummary* locations = mul->GetLocations();
3232 Location first = locations->InAt(0);
3233 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003234 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003235
3236 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003237 case Primitive::kPrimInt:
3238 // The constant may have ended up in a register, so test explicitly to avoid
3239 // problems where the output may not be the same as the first operand.
3240 if (mul->InputAt(1)->IsIntConstant()) {
3241 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3242 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3243 } else if (second.IsRegister()) {
3244 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246 } else {
3247 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003248 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003250 }
3251 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252
3253 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003254 Register in1_hi = first.AsRegisterPairHigh<Register>();
3255 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003256 Register eax = locations->GetTemp(0).AsRegister<Register>();
3257 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003258
3259 DCHECK_EQ(EAX, eax);
3260 DCHECK_EQ(EDX, edx);
3261
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003262 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003263 // output: in1
3264 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3265 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3266 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003267 if (second.IsConstant()) {
3268 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003269
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003270 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3271 int32_t low_value = Low32Bits(value);
3272 int32_t high_value = High32Bits(value);
3273 Immediate low(low_value);
3274 Immediate high(high_value);
3275
3276 __ movl(eax, high);
3277 // eax <- in1.lo * in2.hi
3278 __ imull(eax, in1_lo);
3279 // in1.hi <- in1.hi * in2.lo
3280 __ imull(in1_hi, low);
3281 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3282 __ addl(in1_hi, eax);
3283 // move in2_lo to eax to prepare for double precision
3284 __ movl(eax, low);
3285 // edx:eax <- in1.lo * in2.lo
3286 __ mull(in1_lo);
3287 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3288 __ addl(in1_hi, edx);
3289 // in1.lo <- (in1.lo * in2.lo)[31:0];
3290 __ movl(in1_lo, eax);
3291 } else if (second.IsRegisterPair()) {
3292 Register in2_hi = second.AsRegisterPairHigh<Register>();
3293 Register in2_lo = second.AsRegisterPairLow<Register>();
3294
3295 __ movl(eax, in2_hi);
3296 // eax <- in1.lo * in2.hi
3297 __ imull(eax, in1_lo);
3298 // in1.hi <- in1.hi * in2.lo
3299 __ imull(in1_hi, in2_lo);
3300 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3301 __ addl(in1_hi, eax);
3302 // move in1_lo to eax to prepare for double precision
3303 __ movl(eax, in1_lo);
3304 // edx:eax <- in1.lo * in2.lo
3305 __ mull(in2_lo);
3306 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3307 __ addl(in1_hi, edx);
3308 // in1.lo <- (in1.lo * in2.lo)[31:0];
3309 __ movl(in1_lo, eax);
3310 } else {
3311 DCHECK(second.IsDoubleStackSlot()) << second;
3312 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3313 Address in2_lo(ESP, second.GetStackIndex());
3314
3315 __ movl(eax, in2_hi);
3316 // eax <- in1.lo * in2.hi
3317 __ imull(eax, in1_lo);
3318 // in1.hi <- in1.hi * in2.lo
3319 __ imull(in1_hi, in2_lo);
3320 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3321 __ addl(in1_hi, eax);
3322 // move in1_lo to eax to prepare for double precision
3323 __ movl(eax, in1_lo);
3324 // edx:eax <- in1.lo * in2.lo
3325 __ mull(in2_lo);
3326 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3327 __ addl(in1_hi, edx);
3328 // in1.lo <- (in1.lo * in2.lo)[31:0];
3329 __ movl(in1_lo, eax);
3330 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003331
3332 break;
3333 }
3334
Calin Juravleb5bfa962014-10-21 18:02:24 +01003335 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003336 DCHECK(first.Equals(locations->Out()));
3337 if (second.IsFpuRegister()) {
3338 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3339 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3340 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003341 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003342 __ mulss(first.AsFpuRegister<XmmRegister>(),
3343 codegen_->LiteralFloatAddress(
3344 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003345 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003346 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3347 } else {
3348 DCHECK(second.IsStackSlot());
3349 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3350 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003351 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003352 }
3353
3354 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003355 DCHECK(first.Equals(locations->Out()));
3356 if (second.IsFpuRegister()) {
3357 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3358 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3359 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003360 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003361 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3362 codegen_->LiteralDoubleAddress(
3363 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003364 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003365 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3366 } else {
3367 DCHECK(second.IsDoubleStackSlot());
3368 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3369 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003370 break;
3371 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003372
3373 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003374 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375 }
3376}
3377
Roland Levillain232ade02015-04-20 15:14:36 +01003378void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3379 uint32_t temp_offset,
3380 uint32_t stack_adjustment,
3381 bool is_fp,
3382 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003383 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003384 DCHECK(!is_wide);
3385 if (is_fp) {
3386 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3387 } else {
3388 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3389 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003390 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003391 DCHECK(is_wide);
3392 if (is_fp) {
3393 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3394 } else {
3395 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3396 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003397 } else {
3398 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003399 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003400 Location stack_temp = Location::StackSlot(temp_offset);
3401 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003402 if (is_fp) {
3403 __ flds(Address(ESP, temp_offset));
3404 } else {
3405 __ filds(Address(ESP, temp_offset));
3406 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003407 } else {
3408 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3409 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003410 if (is_fp) {
3411 __ fldl(Address(ESP, temp_offset));
3412 } else {
3413 __ fildl(Address(ESP, temp_offset));
3414 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003415 }
3416 }
3417}
3418
3419void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3420 Primitive::Type type = rem->GetResultType();
3421 bool is_float = type == Primitive::kPrimFloat;
3422 size_t elem_size = Primitive::ComponentSize(type);
3423 LocationSummary* locations = rem->GetLocations();
3424 Location first = locations->InAt(0);
3425 Location second = locations->InAt(1);
3426 Location out = locations->Out();
3427
3428 // Create stack space for 2 elements.
3429 // TODO: enhance register allocator to ask for stack temporaries.
3430 __ subl(ESP, Immediate(2 * elem_size));
3431
3432 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003433 const bool is_wide = !is_float;
3434 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3435 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003436
3437 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003438 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003439 __ Bind(&retry);
3440 __ fprem();
3441
3442 // Move FP status to AX.
3443 __ fstsw();
3444
3445 // And see if the argument reduction is complete. This is signaled by the
3446 // C2 FPU flag bit set to 0.
3447 __ andl(EAX, Immediate(kC2ConditionMask));
3448 __ j(kNotEqual, &retry);
3449
3450 // We have settled on the final value. Retrieve it into an XMM register.
3451 // Store FP top of stack to real stack.
3452 if (is_float) {
3453 __ fsts(Address(ESP, 0));
3454 } else {
3455 __ fstl(Address(ESP, 0));
3456 }
3457
3458 // Pop the 2 items from the FP stack.
3459 __ fucompp();
3460
3461 // Load the value from the stack into an XMM register.
3462 DCHECK(out.IsFpuRegister()) << out;
3463 if (is_float) {
3464 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3465 } else {
3466 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3467 }
3468
3469 // And remove the temporary stack space we allocated.
3470 __ addl(ESP, Immediate(2 * elem_size));
3471}
3472
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003473
3474void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3475 DCHECK(instruction->IsDiv() || instruction->IsRem());
3476
3477 LocationSummary* locations = instruction->GetLocations();
3478 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003479 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480
3481 Register out_register = locations->Out().AsRegister<Register>();
3482 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003483 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003484
3485 DCHECK(imm == 1 || imm == -1);
3486
3487 if (instruction->IsRem()) {
3488 __ xorl(out_register, out_register);
3489 } else {
3490 __ movl(out_register, input_register);
3491 if (imm == -1) {
3492 __ negl(out_register);
3493 }
3494 }
3495}
3496
3497
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003498void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003499 LocationSummary* locations = instruction->GetLocations();
3500
3501 Register out_register = locations->Out().AsRegister<Register>();
3502 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003503 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003504 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3505 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507 Register num = locations->GetTemp(0).AsRegister<Register>();
3508
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003509 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 __ testl(input_register, input_register);
3511 __ cmovl(kGreaterEqual, num, input_register);
3512 int shift = CTZ(imm);
3513 __ sarl(num, Immediate(shift));
3514
3515 if (imm < 0) {
3516 __ negl(num);
3517 }
3518
3519 __ movl(out_register, num);
3520}
3521
3522void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3523 DCHECK(instruction->IsDiv() || instruction->IsRem());
3524
3525 LocationSummary* locations = instruction->GetLocations();
3526 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3527
3528 Register eax = locations->InAt(0).AsRegister<Register>();
3529 Register out = locations->Out().AsRegister<Register>();
3530 Register num;
3531 Register edx;
3532
3533 if (instruction->IsDiv()) {
3534 edx = locations->GetTemp(0).AsRegister<Register>();
3535 num = locations->GetTemp(1).AsRegister<Register>();
3536 } else {
3537 edx = locations->Out().AsRegister<Register>();
3538 num = locations->GetTemp(0).AsRegister<Register>();
3539 }
3540
3541 DCHECK_EQ(EAX, eax);
3542 DCHECK_EQ(EDX, edx);
3543 if (instruction->IsDiv()) {
3544 DCHECK_EQ(EAX, out);
3545 } else {
3546 DCHECK_EQ(EDX, out);
3547 }
3548
3549 int64_t magic;
3550 int shift;
3551 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3552
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003553 // Save the numerator.
3554 __ movl(num, eax);
3555
3556 // EAX = magic
3557 __ movl(eax, Immediate(magic));
3558
3559 // EDX:EAX = magic * numerator
3560 __ imull(num);
3561
3562 if (imm > 0 && magic < 0) {
3563 // EDX += num
3564 __ addl(edx, num);
3565 } else if (imm < 0 && magic > 0) {
3566 __ subl(edx, num);
3567 }
3568
3569 // Shift if needed.
3570 if (shift != 0) {
3571 __ sarl(edx, Immediate(shift));
3572 }
3573
3574 // EDX += 1 if EDX < 0
3575 __ movl(eax, edx);
3576 __ shrl(edx, Immediate(31));
3577 __ addl(edx, eax);
3578
3579 if (instruction->IsRem()) {
3580 __ movl(eax, num);
3581 __ imull(edx, Immediate(imm));
3582 __ subl(eax, edx);
3583 __ movl(edx, eax);
3584 } else {
3585 __ movl(eax, edx);
3586 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003587}
3588
Calin Juravlebacfec32014-11-14 15:54:36 +00003589void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3590 DCHECK(instruction->IsDiv() || instruction->IsRem());
3591
3592 LocationSummary* locations = instruction->GetLocations();
3593 Location out = locations->Out();
3594 Location first = locations->InAt(0);
3595 Location second = locations->InAt(1);
3596 bool is_div = instruction->IsDiv();
3597
3598 switch (instruction->GetResultType()) {
3599 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 DCHECK_EQ(EAX, first.AsRegister<Register>());
3601 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003602
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003603 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003604 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003605
3606 if (imm == 0) {
3607 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3608 } else if (imm == 1 || imm == -1) {
3609 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003610 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003611 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003612 } else {
3613 DCHECK(imm <= -2 || imm >= 2);
3614 GenerateDivRemWithAnyConstant(instruction);
3615 }
3616 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003617 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3618 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003619 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003620
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003621 Register second_reg = second.AsRegister<Register>();
3622 // 0x80000000/-1 triggers an arithmetic exception!
3623 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3624 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003625
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003626 __ cmpl(second_reg, Immediate(-1));
3627 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003628
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 // edx:eax <- sign-extended of eax
3630 __ cdq();
3631 // eax = quotient, edx = remainder
3632 __ idivl(second_reg);
3633 __ Bind(slow_path->GetExitLabel());
3634 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003635 break;
3636 }
3637
3638 case Primitive::kPrimLong: {
3639 InvokeRuntimeCallingConvention calling_convention;
3640 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3641 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3642 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3643 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3644 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3645 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3646
3647 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003648 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003649 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003651 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003652 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003654 break;
3655 }
3656
3657 default:
3658 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3659 }
3660}
3661
Calin Juravle7c4954d2014-10-28 16:57:40 +00003662void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003663 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003664 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003665 : LocationSummary::kNoCall;
3666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3667
Calin Juravle7c4954d2014-10-28 16:57:40 +00003668 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003669 case Primitive::kPrimInt: {
3670 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003671 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003672 locations->SetOut(Location::SameAsFirstInput());
3673 // Intel uses edx:eax as the dividend.
3674 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003675 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3676 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3677 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003678 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003679 locations->AddTemp(Location::RequiresRegister());
3680 }
Calin Juravled0d48522014-11-04 16:40:20 +00003681 break;
3682 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003684 InvokeRuntimeCallingConvention calling_convention;
3685 locations->SetInAt(0, Location::RegisterPairLocation(
3686 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3687 locations->SetInAt(1, Location::RegisterPairLocation(
3688 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3689 // Runtime helper puts the result in EAX, EDX.
3690 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003691 break;
3692 }
3693 case Primitive::kPrimFloat:
3694 case Primitive::kPrimDouble: {
3695 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003696 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3697 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003698 } else if (div->InputAt(1)->IsConstant()) {
3699 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003700 } else {
3701 locations->SetInAt(1, Location::Any());
3702 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003703 locations->SetOut(Location::SameAsFirstInput());
3704 break;
3705 }
3706
3707 default:
3708 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3709 }
3710}
3711
3712void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3713 LocationSummary* locations = div->GetLocations();
3714 Location first = locations->InAt(0);
3715 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003716
3717 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003718 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003720 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003721 break;
3722 }
3723
3724 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003725 if (second.IsFpuRegister()) {
3726 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3727 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3728 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003729 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003730 __ divss(first.AsFpuRegister<XmmRegister>(),
3731 codegen_->LiteralFloatAddress(
3732 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003733 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003734 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3735 } else {
3736 DCHECK(second.IsStackSlot());
3737 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3738 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003739 break;
3740 }
3741
3742 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003743 if (second.IsFpuRegister()) {
3744 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3745 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3746 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003747 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003748 __ divsd(first.AsFpuRegister<XmmRegister>(),
3749 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003750 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3751 const_area->GetBaseMethodAddress(),
3752 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003753 } else {
3754 DCHECK(second.IsDoubleStackSlot());
3755 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3756 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003757 break;
3758 }
3759
3760 default:
3761 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3762 }
3763}
3764
Calin Juravlebacfec32014-11-14 15:54:36 +00003765void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003766 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003767
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003768 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003769 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003770 : LocationSummary::kNoCall;
3771 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003772
Calin Juravled2ec87d2014-12-08 14:24:46 +00003773 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003774 case Primitive::kPrimInt: {
3775 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003776 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003777 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003778 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3779 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3780 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003781 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003782 locations->AddTemp(Location::RequiresRegister());
3783 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003784 break;
3785 }
3786 case Primitive::kPrimLong: {
3787 InvokeRuntimeCallingConvention calling_convention;
3788 locations->SetInAt(0, Location::RegisterPairLocation(
3789 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3790 locations->SetInAt(1, Location::RegisterPairLocation(
3791 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3792 // Runtime helper puts the result in EAX, EDX.
3793 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3794 break;
3795 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003796 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003797 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003798 locations->SetInAt(0, Location::Any());
3799 locations->SetInAt(1, Location::Any());
3800 locations->SetOut(Location::RequiresFpuRegister());
3801 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003802 break;
3803 }
3804
3805 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003806 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003807 }
3808}
3809
3810void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3811 Primitive::Type type = rem->GetResultType();
3812 switch (type) {
3813 case Primitive::kPrimInt:
3814 case Primitive::kPrimLong: {
3815 GenerateDivRemIntegral(rem);
3816 break;
3817 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003818 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003819 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003820 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003821 break;
3822 }
3823 default:
3824 LOG(FATAL) << "Unexpected rem type " << type;
3825 }
3826}
3827
Calin Juravled0d48522014-11-04 16:40:20 +00003828void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003829 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003830 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003831 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003832 case Primitive::kPrimByte:
3833 case Primitive::kPrimChar:
3834 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003835 case Primitive::kPrimInt: {
3836 locations->SetInAt(0, Location::Any());
3837 break;
3838 }
3839 case Primitive::kPrimLong: {
3840 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3841 if (!instruction->IsConstant()) {
3842 locations->AddTemp(Location::RequiresRegister());
3843 }
3844 break;
3845 }
3846 default:
3847 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3848 }
Calin Juravled0d48522014-11-04 16:40:20 +00003849}
3850
3851void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003852 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003853 codegen_->AddSlowPath(slow_path);
3854
3855 LocationSummary* locations = instruction->GetLocations();
3856 Location value = locations->InAt(0);
3857
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003858 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003859 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003860 case Primitive::kPrimByte:
3861 case Primitive::kPrimChar:
3862 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003863 case Primitive::kPrimInt: {
3864 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003865 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003866 __ j(kEqual, slow_path->GetEntryLabel());
3867 } else if (value.IsStackSlot()) {
3868 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3869 __ j(kEqual, slow_path->GetEntryLabel());
3870 } else {
3871 DCHECK(value.IsConstant()) << value;
3872 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003873 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003874 }
3875 }
3876 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003877 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003878 case Primitive::kPrimLong: {
3879 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003880 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003881 __ movl(temp, value.AsRegisterPairLow<Register>());
3882 __ orl(temp, value.AsRegisterPairHigh<Register>());
3883 __ j(kEqual, slow_path->GetEntryLabel());
3884 } else {
3885 DCHECK(value.IsConstant()) << value;
3886 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3887 __ jmp(slow_path->GetEntryLabel());
3888 }
3889 }
3890 break;
3891 }
3892 default:
3893 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003894 }
Calin Juravled0d48522014-11-04 16:40:20 +00003895}
3896
Calin Juravle9aec02f2014-11-18 23:06:35 +00003897void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3898 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3899
3900 LocationSummary* locations =
3901 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3902
3903 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003904 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003905 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003906 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003908 // The shift count needs to be in CL or a constant.
3909 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910 locations->SetOut(Location::SameAsFirstInput());
3911 break;
3912 }
3913 default:
3914 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3915 }
3916}
3917
3918void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3919 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3920
3921 LocationSummary* locations = op->GetLocations();
3922 Location first = locations->InAt(0);
3923 Location second = locations->InAt(1);
3924 DCHECK(first.Equals(locations->Out()));
3925
3926 switch (op->GetResultType()) {
3927 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003928 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003929 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003930 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932 DCHECK_EQ(ECX, second_reg);
3933 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003934 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003935 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003936 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003937 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003938 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003939 }
3940 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003941 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003942 if (shift == 0) {
3943 return;
3944 }
3945 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003946 if (op->IsShl()) {
3947 __ shll(first_reg, imm);
3948 } else if (op->IsShr()) {
3949 __ sarl(first_reg, imm);
3950 } else {
3951 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003952 }
3953 }
3954 break;
3955 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003956 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003957 if (second.IsRegister()) {
3958 Register second_reg = second.AsRegister<Register>();
3959 DCHECK_EQ(ECX, second_reg);
3960 if (op->IsShl()) {
3961 GenerateShlLong(first, second_reg);
3962 } else if (op->IsShr()) {
3963 GenerateShrLong(first, second_reg);
3964 } else {
3965 GenerateUShrLong(first, second_reg);
3966 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003967 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003968 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003969 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003970 // Nothing to do if the shift is 0, as the input is already the output.
3971 if (shift != 0) {
3972 if (op->IsShl()) {
3973 GenerateShlLong(first, shift);
3974 } else if (op->IsShr()) {
3975 GenerateShrLong(first, shift);
3976 } else {
3977 GenerateUShrLong(first, shift);
3978 }
3979 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003980 }
3981 break;
3982 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003983 default:
3984 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3985 }
3986}
3987
Mark P Mendell73945692015-04-29 14:56:17 +00003988void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3989 Register low = loc.AsRegisterPairLow<Register>();
3990 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003991 if (shift == 1) {
3992 // This is just an addition.
3993 __ addl(low, low);
3994 __ adcl(high, high);
3995 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003996 // Shift by 32 is easy. High gets low, and low gets 0.
3997 codegen_->EmitParallelMoves(
3998 loc.ToLow(),
3999 loc.ToHigh(),
4000 Primitive::kPrimInt,
4001 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4002 loc.ToLow(),
4003 Primitive::kPrimInt);
4004 } else if (shift > 32) {
4005 // Low part becomes 0. High part is low part << (shift-32).
4006 __ movl(high, low);
4007 __ shll(high, Immediate(shift - 32));
4008 __ xorl(low, low);
4009 } else {
4010 // Between 1 and 31.
4011 __ shld(high, low, Immediate(shift));
4012 __ shll(low, Immediate(shift));
4013 }
4014}
4015
Calin Juravle9aec02f2014-11-18 23:06:35 +00004016void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004017 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4019 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4020 __ testl(shifter, Immediate(32));
4021 __ j(kEqual, &done);
4022 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4023 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4024 __ Bind(&done);
4025}
4026
Mark P Mendell73945692015-04-29 14:56:17 +00004027void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4028 Register low = loc.AsRegisterPairLow<Register>();
4029 Register high = loc.AsRegisterPairHigh<Register>();
4030 if (shift == 32) {
4031 // Need to copy the sign.
4032 DCHECK_NE(low, high);
4033 __ movl(low, high);
4034 __ sarl(high, Immediate(31));
4035 } else if (shift > 32) {
4036 DCHECK_NE(low, high);
4037 // High part becomes sign. Low part is shifted by shift - 32.
4038 __ movl(low, high);
4039 __ sarl(high, Immediate(31));
4040 __ sarl(low, Immediate(shift - 32));
4041 } else {
4042 // Between 1 and 31.
4043 __ shrd(low, high, Immediate(shift));
4044 __ sarl(high, Immediate(shift));
4045 }
4046}
4047
Calin Juravle9aec02f2014-11-18 23:06:35 +00004048void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004049 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004050 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4051 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4052 __ testl(shifter, Immediate(32));
4053 __ j(kEqual, &done);
4054 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4055 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4056 __ Bind(&done);
4057}
4058
Mark P Mendell73945692015-04-29 14:56:17 +00004059void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4060 Register low = loc.AsRegisterPairLow<Register>();
4061 Register high = loc.AsRegisterPairHigh<Register>();
4062 if (shift == 32) {
4063 // Shift by 32 is easy. Low gets high, and high gets 0.
4064 codegen_->EmitParallelMoves(
4065 loc.ToHigh(),
4066 loc.ToLow(),
4067 Primitive::kPrimInt,
4068 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4069 loc.ToHigh(),
4070 Primitive::kPrimInt);
4071 } else if (shift > 32) {
4072 // Low part is high >> (shift - 32). High part becomes 0.
4073 __ movl(low, high);
4074 __ shrl(low, Immediate(shift - 32));
4075 __ xorl(high, high);
4076 } else {
4077 // Between 1 and 31.
4078 __ shrd(low, high, Immediate(shift));
4079 __ shrl(high, Immediate(shift));
4080 }
4081}
4082
Calin Juravle9aec02f2014-11-18 23:06:35 +00004083void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004084 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004085 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4086 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4087 __ testl(shifter, Immediate(32));
4088 __ j(kEqual, &done);
4089 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4090 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4091 __ Bind(&done);
4092}
4093
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004094void LocationsBuilderX86::VisitRor(HRor* ror) {
4095 LocationSummary* locations =
4096 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4097
4098 switch (ror->GetResultType()) {
4099 case Primitive::kPrimLong:
4100 // Add the temporary needed.
4101 locations->AddTemp(Location::RequiresRegister());
4102 FALLTHROUGH_INTENDED;
4103 case Primitive::kPrimInt:
4104 locations->SetInAt(0, Location::RequiresRegister());
4105 // The shift count needs to be in CL (unless it is a constant).
4106 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4107 locations->SetOut(Location::SameAsFirstInput());
4108 break;
4109 default:
4110 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4111 UNREACHABLE();
4112 }
4113}
4114
4115void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4116 LocationSummary* locations = ror->GetLocations();
4117 Location first = locations->InAt(0);
4118 Location second = locations->InAt(1);
4119
4120 if (ror->GetResultType() == Primitive::kPrimInt) {
4121 Register first_reg = first.AsRegister<Register>();
4122 if (second.IsRegister()) {
4123 Register second_reg = second.AsRegister<Register>();
4124 __ rorl(first_reg, second_reg);
4125 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004126 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004127 __ rorl(first_reg, imm);
4128 }
4129 return;
4130 }
4131
4132 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4133 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4134 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4135 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4136 if (second.IsRegister()) {
4137 Register second_reg = second.AsRegister<Register>();
4138 DCHECK_EQ(second_reg, ECX);
4139 __ movl(temp_reg, first_reg_hi);
4140 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4141 __ shrd(first_reg_lo, temp_reg, second_reg);
4142 __ movl(temp_reg, first_reg_hi);
4143 __ testl(second_reg, Immediate(32));
4144 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4145 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4146 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004147 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004148 if (shift_amt == 0) {
4149 // Already fine.
4150 return;
4151 }
4152 if (shift_amt == 32) {
4153 // Just swap.
4154 __ movl(temp_reg, first_reg_lo);
4155 __ movl(first_reg_lo, first_reg_hi);
4156 __ movl(first_reg_hi, temp_reg);
4157 return;
4158 }
4159
4160 Immediate imm(shift_amt);
4161 // Save the constents of the low value.
4162 __ movl(temp_reg, first_reg_lo);
4163
4164 // Shift right into low, feeding bits from high.
4165 __ shrd(first_reg_lo, first_reg_hi, imm);
4166
4167 // Shift right into high, feeding bits from the original low.
4168 __ shrd(first_reg_hi, temp_reg, imm);
4169
4170 // Swap if needed.
4171 if (shift_amt > 32) {
4172 __ movl(temp_reg, first_reg_lo);
4173 __ movl(first_reg_lo, first_reg_hi);
4174 __ movl(first_reg_hi, temp_reg);
4175 }
4176 }
4177}
4178
Calin Juravle9aec02f2014-11-18 23:06:35 +00004179void LocationsBuilderX86::VisitShl(HShl* shl) {
4180 HandleShift(shl);
4181}
4182
4183void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4184 HandleShift(shl);
4185}
4186
4187void LocationsBuilderX86::VisitShr(HShr* shr) {
4188 HandleShift(shr);
4189}
4190
4191void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4192 HandleShift(shr);
4193}
4194
4195void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4196 HandleShift(ushr);
4197}
4198
4199void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4200 HandleShift(ushr);
4201}
4202
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004203void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004204 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004206 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004207 if (instruction->IsStringAlloc()) {
4208 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4209 } else {
4210 InvokeRuntimeCallingConvention calling_convention;
4211 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004212 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004213}
4214
4215void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004216 // Note: if heap poisoning is enabled, the entry point takes cares
4217 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004218 if (instruction->IsStringAlloc()) {
4219 // String is allocated through StringFactory. Call NewEmptyString entry point.
4220 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004221 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004222 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4223 __ call(Address(temp, code_offset.Int32Value()));
4224 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4225 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004226 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004227 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004228 DCHECK(!codegen_->IsLeafMethod());
4229 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004230}
4231
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004232void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4233 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004234 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235 locations->SetOut(Location::RegisterLocation(EAX));
4236 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004237 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4238 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004239}
4240
4241void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004242 // Note: if heap poisoning is enabled, the entry point takes cares
4243 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004244 QuickEntrypointEnum entrypoint =
4245 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4246 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004247 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004248 DCHECK(!codegen_->IsLeafMethod());
4249}
4250
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004251void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004252 LocationSummary* locations =
4253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004254 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4255 if (location.IsStackSlot()) {
4256 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4257 } else if (location.IsDoubleStackSlot()) {
4258 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004259 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004260 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004261}
4262
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004263void InstructionCodeGeneratorX86::VisitParameterValue(
4264 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4265}
4266
4267void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4268 LocationSummary* locations =
4269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4270 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4271}
4272
4273void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004274}
4275
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004276void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4277 LocationSummary* locations =
4278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4279 locations->SetInAt(0, Location::RequiresRegister());
4280 locations->SetOut(Location::RequiresRegister());
4281}
4282
4283void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4284 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004285 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004286 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004287 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004288 __ movl(locations->Out().AsRegister<Register>(),
4289 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004290 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004291 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004292 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004293 __ movl(locations->Out().AsRegister<Register>(),
4294 Address(locations->InAt(0).AsRegister<Register>(),
4295 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4296 // temp = temp->GetImtEntryAt(method_offset);
4297 __ movl(locations->Out().AsRegister<Register>(),
4298 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004299 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004300}
4301
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004302void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004303 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004304 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004305 locations->SetInAt(0, Location::RequiresRegister());
4306 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004307}
4308
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004309void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4310 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004311 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004312 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004313 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004314 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004315 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004317 break;
4318
4319 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004320 __ notl(out.AsRegisterPairLow<Register>());
4321 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004322 break;
4323
4324 default:
4325 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4326 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004327}
4328
David Brazdil66d126e2015-04-03 16:02:44 +01004329void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4330 LocationSummary* locations =
4331 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4332 locations->SetInAt(0, Location::RequiresRegister());
4333 locations->SetOut(Location::SameAsFirstInput());
4334}
4335
4336void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004337 LocationSummary* locations = bool_not->GetLocations();
4338 Location in = locations->InAt(0);
4339 Location out = locations->Out();
4340 DCHECK(in.Equals(out));
4341 __ xorl(out.AsRegister<Register>(), Immediate(1));
4342}
4343
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004344void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004345 LocationSummary* locations =
4346 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004347 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004348 case Primitive::kPrimBoolean:
4349 case Primitive::kPrimByte:
4350 case Primitive::kPrimShort:
4351 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004352 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004353 case Primitive::kPrimLong: {
4354 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004355 locations->SetInAt(1, Location::Any());
4356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4357 break;
4358 }
4359 case Primitive::kPrimFloat:
4360 case Primitive::kPrimDouble: {
4361 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004362 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4363 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4364 } else if (compare->InputAt(1)->IsConstant()) {
4365 locations->SetInAt(1, Location::RequiresFpuRegister());
4366 } else {
4367 locations->SetInAt(1, Location::Any());
4368 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004369 locations->SetOut(Location::RequiresRegister());
4370 break;
4371 }
4372 default:
4373 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4374 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004375}
4376
4377void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004378 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004379 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004380 Location left = locations->InAt(0);
4381 Location right = locations->InAt(1);
4382
Mark Mendell0c9497d2015-08-21 09:30:05 -04004383 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004384 Condition less_cond = kLess;
4385
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004386 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004387 case Primitive::kPrimBoolean:
4388 case Primitive::kPrimByte:
4389 case Primitive::kPrimShort:
4390 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004391 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004392 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004393 break;
4394 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004395 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004396 Register left_low = left.AsRegisterPairLow<Register>();
4397 Register left_high = left.AsRegisterPairHigh<Register>();
4398 int32_t val_low = 0;
4399 int32_t val_high = 0;
4400 bool right_is_const = false;
4401
4402 if (right.IsConstant()) {
4403 DCHECK(right.GetConstant()->IsLongConstant());
4404 right_is_const = true;
4405 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4406 val_low = Low32Bits(val);
4407 val_high = High32Bits(val);
4408 }
4409
Calin Juravleddb7df22014-11-25 20:56:51 +00004410 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004411 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004412 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004413 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004414 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004415 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004416 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004417 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004418 __ j(kLess, &less); // Signed compare.
4419 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004420 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004421 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004422 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004423 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004424 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004425 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004426 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004427 }
Aart Bika19616e2016-02-01 18:57:58 -08004428 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004429 break;
4430 }
4431 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004432 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004433 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004434 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004435 break;
4436 }
4437 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004438 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004439 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004440 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004441 break;
4442 }
4443 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004444 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004445 }
Aart Bika19616e2016-02-01 18:57:58 -08004446
Calin Juravleddb7df22014-11-25 20:56:51 +00004447 __ movl(out, Immediate(0));
4448 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004449 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004450
4451 __ Bind(&greater);
4452 __ movl(out, Immediate(1));
4453 __ jmp(&done);
4454
4455 __ Bind(&less);
4456 __ movl(out, Immediate(-1));
4457
4458 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004459}
4460
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004461void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004462 LocationSummary* locations =
4463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004464 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004465 locations->SetInAt(i, Location::Any());
4466 }
4467 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004468}
4469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004470void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004471 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004472}
4473
Roland Levillain7c1559a2015-12-15 10:55:36 +00004474void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004475 /*
4476 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4477 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4478 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4479 */
4480 switch (kind) {
4481 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004482 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004483 break;
4484 }
4485 case MemBarrierKind::kAnyStore:
4486 case MemBarrierKind::kLoadAny:
4487 case MemBarrierKind::kStoreStore: {
4488 // nop
4489 break;
4490 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004491 case MemBarrierKind::kNTStoreStore:
4492 // Non-Temporal Store/Store needs an explicit fence.
4493 MemoryFence(/* non-temporal */ true);
4494 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004495 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004496}
4497
Vladimir Markodc151b22015-10-15 18:02:30 +01004498HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4499 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004500 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004501 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004502}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004503
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004504Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4505 Register temp) {
4506 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004507 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004508 if (!invoke->GetLocations()->Intrinsified()) {
4509 return location.AsRegister<Register>();
4510 }
4511 // For intrinsics we allow any location, so it may be on the stack.
4512 if (!location.IsRegister()) {
4513 __ movl(temp, Address(ESP, location.GetStackIndex()));
4514 return temp;
4515 }
4516 // For register locations, check if the register was saved. If so, get it from the stack.
4517 // Note: There is a chance that the register was saved but not overwritten, so we could
4518 // save one load. However, since this is just an intrinsic slow path we prefer this
4519 // simple and more robust approach rather that trying to determine if that's the case.
4520 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004521 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4522 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4523 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4524 __ movl(temp, Address(ESP, stack_offset));
4525 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004526 }
4527 return location.AsRegister<Register>();
4528}
4529
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004530void CodeGeneratorX86::GenerateStaticOrDirectCall(
4531 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004532 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4533 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004534 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004535 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004536 uint32_t offset =
4537 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4538 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004539 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004540 }
Vladimir Marko58155012015-08-19 12:49:41 +00004541 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004542 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004543 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004544 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4545 DCHECK(GetCompilerOptions().IsBootImage());
4546 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4547 temp.AsRegister<Register>());
4548 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4549 RecordBootMethodPatch(invoke);
4550 break;
4551 }
Vladimir Marko58155012015-08-19 12:49:41 +00004552 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4553 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4554 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004555 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004556 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4557 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004558 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004559 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004560 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004561 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004562 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004563 break;
4564 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004565 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4566 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4567 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004568 }
Vladimir Marko58155012015-08-19 12:49:41 +00004569 }
4570
4571 switch (invoke->GetCodePtrLocation()) {
4572 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4573 __ call(GetFrameEntryLabel());
4574 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004575 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4576 // (callee_method + offset_of_quick_compiled_code)()
4577 __ call(Address(callee_method.AsRegister<Register>(),
4578 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004579 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004580 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004581 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004582 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004583
4584 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004585}
4586
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004587void CodeGeneratorX86::GenerateVirtualCall(
4588 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004589 Register temp = temp_in.AsRegister<Register>();
4590 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4591 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004592
4593 // Use the calling convention instead of the location of the receiver, as
4594 // intrinsics may have put the receiver in a different register. In the intrinsics
4595 // slow path, the arguments have been moved to the right place, so here we are
4596 // guaranteed that the receiver is the first register of the calling convention.
4597 InvokeDexCallingConvention calling_convention;
4598 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004599 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004600 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004601 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004602 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004603 // Instead of simply (possibly) unpoisoning `temp` here, we should
4604 // emit a read barrier for the previous class reference load.
4605 // However this is not required in practice, as this is an
4606 // intermediate/temporary reference and because the current
4607 // concurrent copying collector keeps the from-space memory
4608 // intact/accessible until the end of the marking phase (the
4609 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004610 __ MaybeUnpoisonHeapReference(temp);
4611 // temp = temp->GetMethodAt(method_offset);
4612 __ movl(temp, Address(temp, method_offset));
4613 // call temp->GetEntryPoint();
4614 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004615 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004616 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004617}
4618
Vladimir Marko65979462017-05-19 17:25:12 +01004619void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4620 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4621 HX86ComputeBaseMethodAddress* address =
4622 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4623 boot_image_method_patches_.emplace_back(address,
4624 *invoke->GetTargetMethod().dex_file,
4625 invoke->GetTargetMethod().dex_method_index);
4626 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004627}
4628
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004629Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4630 HX86ComputeBaseMethodAddress* method_address,
4631 MethodReference target_method) {
4632 // Add the patch entry and bind its label at the end of the instruction.
4633 method_bss_entry_patches_.emplace_back(method_address,
4634 *target_method.dex_file,
4635 target_method.dex_method_index);
4636 return &method_bss_entry_patches_.back().label;
4637}
4638
Vladimir Marko1998cd02017-01-13 13:02:58 +00004639void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004640 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004641 boot_image_type_patches_.emplace_back(address,
4642 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004643 load_class->GetTypeIndex().index_);
4644 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004645}
4646
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004647Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004648 HX86ComputeBaseMethodAddress* address =
4649 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4650 type_bss_entry_patches_.emplace_back(
4651 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004652 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004653}
4654
Vladimir Marko65979462017-05-19 17:25:12 +01004655void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004656 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();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004667 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004668 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004669 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004670}
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() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004694 string_patches_.size() +
4695 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004696 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004697 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01004698 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
4699 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004700 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4701 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004702 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004703 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004704 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01004705 DCHECK(boot_image_type_patches_.empty());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004706 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(string_patches_,
4707 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004708 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004709 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4710 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004711 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4712 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004713 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
4714 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004715 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004716}
4717
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004718void CodeGeneratorX86::MarkGCCard(Register temp,
4719 Register card,
4720 Register object,
4721 Register value,
4722 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004723 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004724 if (value_can_be_null) {
4725 __ testl(value, value);
4726 __ j(kEqual, &is_null);
4727 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004728 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729 __ movl(temp, object);
4730 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004731 __ movb(Address(temp, card, TIMES_1, 0),
4732 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004733 if (value_can_be_null) {
4734 __ Bind(&is_null);
4735 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004736}
4737
Calin Juravle52c48962014-12-16 17:02:57 +00004738void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4739 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004740
4741 bool object_field_get_with_read_barrier =
4742 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004743 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004744 new (GetGraph()->GetArena()) LocationSummary(instruction,
4745 kEmitCompilerReadBarrier ?
4746 LocationSummary::kCallOnSlowPath :
4747 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004748 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004749 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004750 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004751 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004752
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004753 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4754 locations->SetOut(Location::RequiresFpuRegister());
4755 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004756 // The output overlaps in case of long: we don't want the low move
4757 // to overwrite the object's location. Likewise, in the case of
4758 // an object field get with read barriers enabled, we do not want
4759 // the move to overwrite the object's location, as we need it to emit
4760 // the read barrier.
4761 locations->SetOut(
4762 Location::RequiresRegister(),
4763 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4764 Location::kOutputOverlap :
4765 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004766 }
Calin Juravle52c48962014-12-16 17:02:57 +00004767
4768 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4769 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004770 // So we use an XMM register as a temp to achieve atomicity (first
4771 // load the temp into the XMM and then copy the XMM into the
4772 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004773 locations->AddTemp(Location::RequiresFpuRegister());
4774 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004775}
4776
Calin Juravle52c48962014-12-16 17:02:57 +00004777void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4778 const FieldInfo& field_info) {
4779 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780
Calin Juravle52c48962014-12-16 17:02:57 +00004781 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004782 Location base_loc = locations->InAt(0);
4783 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004784 Location out = locations->Out();
4785 bool is_volatile = field_info.IsVolatile();
4786 Primitive::Type field_type = field_info.GetFieldType();
4787 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4788
4789 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004790 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004791 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004792 break;
4793 }
4794
4795 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004796 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004797 break;
4798 }
4799
4800 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004801 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004802 break;
4803 }
4804
4805 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004806 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004807 break;
4808 }
4809
4810 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004811 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004812 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004813
4814 case Primitive::kPrimNot: {
4815 // /* HeapReference<Object> */ out = *(base + offset)
4816 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004817 // Note that a potential implicit null check is handled in this
4818 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4819 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004820 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004821 if (is_volatile) {
4822 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4823 }
4824 } else {
4825 __ movl(out.AsRegister<Register>(), Address(base, offset));
4826 codegen_->MaybeRecordImplicitNullCheck(instruction);
4827 if (is_volatile) {
4828 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4829 }
4830 // If read barriers are enabled, emit read barriers other than
4831 // Baker's using a slow path (and also unpoison the loaded
4832 // reference, if heap poisoning is enabled).
4833 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4834 }
4835 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836 }
4837
4838 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004839 if (is_volatile) {
4840 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4841 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004842 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004843 __ movd(out.AsRegisterPairLow<Register>(), temp);
4844 __ psrlq(temp, Immediate(32));
4845 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4846 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004847 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004848 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004849 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004850 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4851 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004852 break;
4853 }
4854
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004855 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004856 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004857 break;
4858 }
4859
4860 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004861 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004862 break;
4863 }
4864
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004865 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004866 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004867 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004868 }
Calin Juravle52c48962014-12-16 17:02:57 +00004869
Roland Levillain7c1559a2015-12-15 10:55:36 +00004870 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4871 // Potential implicit null checks, in the case of reference or
4872 // long fields, are handled in the previous switch statement.
4873 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
4875 }
4876
Calin Juravle52c48962014-12-16 17:02:57 +00004877 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004878 if (field_type == Primitive::kPrimNot) {
4879 // Memory barriers, in the case of references, are also handled
4880 // in the previous switch statement.
4881 } else {
4882 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4883 }
Roland Levillain4d027112015-07-01 15:41:14 +01004884 }
Calin Juravle52c48962014-12-16 17:02:57 +00004885}
4886
4887void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4888 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4889
4890 LocationSummary* locations =
4891 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4892 locations->SetInAt(0, Location::RequiresRegister());
4893 bool is_volatile = field_info.IsVolatile();
4894 Primitive::Type field_type = field_info.GetFieldType();
4895 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4896 || (field_type == Primitive::kPrimByte);
4897
4898 // The register allocator does not support multiple
4899 // inputs that die at entry with one in a specific register.
4900 if (is_byte_type) {
4901 // Ensure the value is in a byte register.
4902 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004903 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004904 if (is_volatile && field_type == Primitive::kPrimDouble) {
4905 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4906 locations->SetInAt(1, Location::RequiresFpuRegister());
4907 } else {
4908 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4909 }
4910 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4911 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004912 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004913
Calin Juravle52c48962014-12-16 17:02:57 +00004914 // 64bits value can be atomically written to an address with movsd and an XMM register.
4915 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4916 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4917 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4918 // isolated cases when we need this it isn't worth adding the extra complexity.
4919 locations->AddTemp(Location::RequiresFpuRegister());
4920 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004921 } else {
4922 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4923
4924 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4925 // Temporary registers for the write barrier.
4926 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4927 // Ensure the card is in a byte register.
4928 locations->AddTemp(Location::RegisterLocation(ECX));
4929 }
Calin Juravle52c48962014-12-16 17:02:57 +00004930 }
4931}
4932
4933void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004934 const FieldInfo& field_info,
4935 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004936 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4937
4938 LocationSummary* locations = instruction->GetLocations();
4939 Register base = locations->InAt(0).AsRegister<Register>();
4940 Location value = locations->InAt(1);
4941 bool is_volatile = field_info.IsVolatile();
4942 Primitive::Type field_type = field_info.GetFieldType();
4943 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004944 bool needs_write_barrier =
4945 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004946
4947 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004948 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004949 }
4950
Mark Mendell81489372015-11-04 11:30:41 -05004951 bool maybe_record_implicit_null_check_done = false;
4952
Calin Juravle52c48962014-12-16 17:02:57 +00004953 switch (field_type) {
4954 case Primitive::kPrimBoolean:
4955 case Primitive::kPrimByte: {
4956 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4957 break;
4958 }
4959
4960 case Primitive::kPrimShort:
4961 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004962 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004963 __ movw(Address(base, offset),
4964 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004965 } else {
4966 __ movw(Address(base, offset), value.AsRegister<Register>());
4967 }
Calin Juravle52c48962014-12-16 17:02:57 +00004968 break;
4969 }
4970
4971 case Primitive::kPrimInt:
4972 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004973 if (kPoisonHeapReferences && needs_write_barrier) {
4974 // Note that in the case where `value` is a null reference,
4975 // we do not enter this block, as the reference does not
4976 // need poisoning.
4977 DCHECK_EQ(field_type, Primitive::kPrimNot);
4978 Register temp = locations->GetTemp(0).AsRegister<Register>();
4979 __ movl(temp, value.AsRegister<Register>());
4980 __ PoisonHeapReference(temp);
4981 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004982 } else if (value.IsConstant()) {
4983 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4984 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004985 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004986 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004987 __ movl(Address(base, offset), value.AsRegister<Register>());
4988 }
Calin Juravle52c48962014-12-16 17:02:57 +00004989 break;
4990 }
4991
4992 case Primitive::kPrimLong: {
4993 if (is_volatile) {
4994 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4995 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4996 __ movd(temp1, value.AsRegisterPairLow<Register>());
4997 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4998 __ punpckldq(temp1, temp2);
4999 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005000 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005001 } else if (value.IsConstant()) {
5002 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5003 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5004 codegen_->MaybeRecordImplicitNullCheck(instruction);
5005 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005006 } else {
5007 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005008 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005009 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5010 }
Mark Mendell81489372015-11-04 11:30:41 -05005011 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005012 break;
5013 }
5014
5015 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005016 if (value.IsConstant()) {
5017 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5018 __ movl(Address(base, offset), Immediate(v));
5019 } else {
5020 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5021 }
Calin Juravle52c48962014-12-16 17:02:57 +00005022 break;
5023 }
5024
5025 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005026 if (value.IsConstant()) {
5027 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5028 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5029 codegen_->MaybeRecordImplicitNullCheck(instruction);
5030 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5031 maybe_record_implicit_null_check_done = true;
5032 } else {
5033 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5034 }
Calin Juravle52c48962014-12-16 17:02:57 +00005035 break;
5036 }
5037
5038 case Primitive::kPrimVoid:
5039 LOG(FATAL) << "Unreachable type " << field_type;
5040 UNREACHABLE();
5041 }
5042
Mark Mendell81489372015-11-04 11:30:41 -05005043 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005044 codegen_->MaybeRecordImplicitNullCheck(instruction);
5045 }
5046
Roland Levillain4d027112015-07-01 15:41:14 +01005047 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005048 Register temp = locations->GetTemp(0).AsRegister<Register>();
5049 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005050 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005051 }
5052
Calin Juravle52c48962014-12-16 17:02:57 +00005053 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005054 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005055 }
5056}
5057
5058void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5059 HandleFieldGet(instruction, instruction->GetFieldInfo());
5060}
5061
5062void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5063 HandleFieldGet(instruction, instruction->GetFieldInfo());
5064}
5065
5066void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5067 HandleFieldSet(instruction, instruction->GetFieldInfo());
5068}
5069
5070void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005071 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005072}
5073
5074void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5075 HandleFieldSet(instruction, instruction->GetFieldInfo());
5076}
5077
5078void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005079 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005080}
5081
5082void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5083 HandleFieldGet(instruction, instruction->GetFieldInfo());
5084}
5085
5086void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5087 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005088}
5089
Calin Juravlee460d1d2015-09-29 04:52:17 +01005090void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5091 HUnresolvedInstanceFieldGet* instruction) {
5092 FieldAccessCallingConventionX86 calling_convention;
5093 codegen_->CreateUnresolvedFieldLocationSummary(
5094 instruction, instruction->GetFieldType(), calling_convention);
5095}
5096
5097void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5098 HUnresolvedInstanceFieldGet* instruction) {
5099 FieldAccessCallingConventionX86 calling_convention;
5100 codegen_->GenerateUnresolvedFieldAccess(instruction,
5101 instruction->GetFieldType(),
5102 instruction->GetFieldIndex(),
5103 instruction->GetDexPc(),
5104 calling_convention);
5105}
5106
5107void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5108 HUnresolvedInstanceFieldSet* instruction) {
5109 FieldAccessCallingConventionX86 calling_convention;
5110 codegen_->CreateUnresolvedFieldLocationSummary(
5111 instruction, instruction->GetFieldType(), calling_convention);
5112}
5113
5114void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5115 HUnresolvedInstanceFieldSet* instruction) {
5116 FieldAccessCallingConventionX86 calling_convention;
5117 codegen_->GenerateUnresolvedFieldAccess(instruction,
5118 instruction->GetFieldType(),
5119 instruction->GetFieldIndex(),
5120 instruction->GetDexPc(),
5121 calling_convention);
5122}
5123
5124void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5125 HUnresolvedStaticFieldGet* instruction) {
5126 FieldAccessCallingConventionX86 calling_convention;
5127 codegen_->CreateUnresolvedFieldLocationSummary(
5128 instruction, instruction->GetFieldType(), calling_convention);
5129}
5130
5131void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5132 HUnresolvedStaticFieldGet* instruction) {
5133 FieldAccessCallingConventionX86 calling_convention;
5134 codegen_->GenerateUnresolvedFieldAccess(instruction,
5135 instruction->GetFieldType(),
5136 instruction->GetFieldIndex(),
5137 instruction->GetDexPc(),
5138 calling_convention);
5139}
5140
5141void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5142 HUnresolvedStaticFieldSet* instruction) {
5143 FieldAccessCallingConventionX86 calling_convention;
5144 codegen_->CreateUnresolvedFieldLocationSummary(
5145 instruction, instruction->GetFieldType(), calling_convention);
5146}
5147
5148void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5149 HUnresolvedStaticFieldSet* instruction) {
5150 FieldAccessCallingConventionX86 calling_convention;
5151 codegen_->GenerateUnresolvedFieldAccess(instruction,
5152 instruction->GetFieldType(),
5153 instruction->GetFieldIndex(),
5154 instruction->GetDexPc(),
5155 calling_convention);
5156}
5157
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005159 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5160 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5161 ? Location::RequiresRegister()
5162 : Location::Any();
5163 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164}
5165
Calin Juravle2ae48182016-03-16 14:05:09 +00005166void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5167 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005168 return;
5169 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005170 LocationSummary* locations = instruction->GetLocations();
5171 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005172
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005173 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005174 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005175}
5176
Calin Juravle2ae48182016-03-16 14:05:09 +00005177void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005178 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005179 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005180
5181 LocationSummary* locations = instruction->GetLocations();
5182 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183
5184 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005185 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005186 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005188 } else {
5189 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005190 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005191 __ jmp(slow_path->GetEntryLabel());
5192 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005193 }
5194 __ j(kEqual, slow_path->GetEntryLabel());
5195}
5196
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005197void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005198 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005199}
5200
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005201void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005202 bool object_array_get_with_read_barrier =
5203 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005204 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005205 new (GetGraph()->GetArena()) LocationSummary(instruction,
5206 object_array_get_with_read_barrier ?
5207 LocationSummary::kCallOnSlowPath :
5208 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005209 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005210 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005211 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005212 locations->SetInAt(0, Location::RequiresRegister());
5213 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005214 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5215 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5216 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005217 // The output overlaps in case of long: we don't want the low move
5218 // to overwrite the array's location. Likewise, in the case of an
5219 // object array get with read barriers enabled, we do not want the
5220 // move to overwrite the array's location, as we need it to emit
5221 // the read barrier.
5222 locations->SetOut(
5223 Location::RequiresRegister(),
5224 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5225 Location::kOutputOverlap :
5226 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005227 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228}
5229
5230void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5231 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005232 Location obj_loc = locations->InAt(0);
5233 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005235 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005236 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237
Calin Juravle77520bc2015-01-12 18:45:46 +00005238 Primitive::Type type = instruction->GetType();
5239 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005241 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005242 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 break;
5244 }
5245
5246 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005247 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005248 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249 break;
5250 }
5251
5252 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005253 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005254 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255 break;
5256 }
5257
5258 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005259 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005260 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5261 // Branch cases into compressed and uncompressed for each index's type.
5262 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5263 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005264 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005265 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005266 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5267 "Expecting 0=compressed, 1=uncompressed");
5268 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005269 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5270 __ jmp(&done);
5271 __ Bind(&not_compressed);
5272 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5273 __ Bind(&done);
5274 } else {
5275 // Common case for charAt of array of char or when string compression's
5276 // feature is turned off.
5277 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5278 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 break;
5280 }
5281
Roland Levillain7c1559a2015-12-15 10:55:36 +00005282 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005283 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005284 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 break;
5286 }
5287
Roland Levillain7c1559a2015-12-15 10:55:36 +00005288 case Primitive::kPrimNot: {
5289 static_assert(
5290 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5291 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005292 // /* HeapReference<Object> */ out =
5293 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5294 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005295 // Note that a potential implicit null check is handled in this
5296 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5297 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005298 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005299 } else {
5300 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005301 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5302 codegen_->MaybeRecordImplicitNullCheck(instruction);
5303 // If read barriers are enabled, emit read barriers other than
5304 // Baker's using a slow path (and also unpoison the loaded
5305 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005306 if (index.IsConstant()) {
5307 uint32_t offset =
5308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005309 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5310 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005311 codegen_->MaybeGenerateReadBarrierSlow(
5312 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5313 }
5314 }
5315 break;
5316 }
5317
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005319 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005320 __ movl(out_loc.AsRegisterPairLow<Register>(),
5321 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5322 codegen_->MaybeRecordImplicitNullCheck(instruction);
5323 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5324 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 break;
5326 }
5327
Mark Mendell7c8d0092015-01-26 11:21:33 -05005328 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005329 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005330 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005331 break;
5332 }
5333
5334 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005335 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005336 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005337 break;
5338 }
5339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005341 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005342 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005344
Roland Levillain7c1559a2015-12-15 10:55:36 +00005345 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5346 // Potential implicit null checks, in the case of reference or
5347 // long arrays, are handled in the previous switch statement.
5348 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005349 codegen_->MaybeRecordImplicitNullCheck(instruction);
5350 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351}
5352
5353void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005354 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005355
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005356 bool needs_write_barrier =
5357 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005358 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005359
Nicolas Geoffray39468442014-09-02 15:17:15 +01005360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5361 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005362 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005363 LocationSummary::kCallOnSlowPath :
5364 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005365
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005366 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5367 || (value_type == Primitive::kPrimByte);
5368 // We need the inputs to be different than the output in case of long operation.
5369 // In case of a byte operation, the register allocator does not support multiple
5370 // inputs that die at entry with one in a specific register.
5371 locations->SetInAt(0, Location::RequiresRegister());
5372 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5373 if (is_byte_type) {
5374 // Ensure the value is in a byte register.
5375 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5376 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005377 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005379 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5380 }
5381 if (needs_write_barrier) {
5382 // Temporary registers for the write barrier.
5383 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5384 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005385 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387}
5388
5389void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5390 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005391 Location array_loc = locations->InAt(0);
5392 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005394 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005395 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5397 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5398 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005399 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005400 bool needs_write_barrier =
5401 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402
5403 switch (value_type) {
5404 case Primitive::kPrimBoolean:
5405 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005406 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005407 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 if (value.IsRegister()) {
5409 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005411 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005413 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414 break;
5415 }
5416
5417 case Primitive::kPrimShort:
5418 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005420 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 if (value.IsRegister()) {
5422 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005424 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005426 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005427 break;
5428 }
5429
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005430 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005432 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005433
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 if (!value.IsRegister()) {
5435 // Just setting null.
5436 DCHECK(instruction->InputAt(2)->IsNullConstant());
5437 DCHECK(value.IsConstant()) << value;
5438 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005441 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005442 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444
5445 DCHECK(needs_write_barrier);
5446 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005447 // We cannot use a NearLabel for `done`, as its range may be too
5448 // short when Baker read barriers are enabled.
5449 Label done;
5450 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005451 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005452 Location temp_loc = locations->GetTemp(0);
5453 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005454 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005455 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5456 codegen_->AddSlowPath(slow_path);
5457 if (instruction->GetValueCanBeNull()) {
5458 __ testl(register_value, register_value);
5459 __ j(kNotEqual, &not_null);
5460 __ movl(address, Immediate(0));
5461 codegen_->MaybeRecordImplicitNullCheck(instruction);
5462 __ jmp(&done);
5463 __ Bind(&not_null);
5464 }
5465
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005466 // Note that when Baker read barriers are enabled, the type
5467 // checks are performed without read barriers. This is fine,
5468 // even in the case where a class object is in the from-space
5469 // after the flip, as a comparison involving such a type would
5470 // not produce a false positive; it may of course produce a
5471 // false negative, in which case we would take the ArraySet
5472 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005473
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005474 // /* HeapReference<Class> */ temp = array->klass_
5475 __ movl(temp, Address(array, class_offset));
5476 codegen_->MaybeRecordImplicitNullCheck(instruction);
5477 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005478
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005479 // /* HeapReference<Class> */ temp = temp->component_type_
5480 __ movl(temp, Address(temp, component_offset));
5481 // If heap poisoning is enabled, no need to unpoison `temp`
5482 // nor the object reference in `register_value->klass`, as
5483 // we are comparing two poisoned references.
5484 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005485
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005486 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5487 __ j(kEqual, &do_put);
5488 // If heap poisoning is enabled, the `temp` reference has
5489 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005490 __ MaybeUnpoisonHeapReference(temp);
5491
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005492 // If heap poisoning is enabled, no need to unpoison the
5493 // heap reference loaded below, as it is only used for a
5494 // comparison with null.
5495 __ cmpl(Address(temp, super_offset), Immediate(0));
5496 __ j(kNotEqual, slow_path->GetEntryLabel());
5497 __ Bind(&do_put);
5498 } else {
5499 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005500 }
5501 }
5502
5503 if (kPoisonHeapReferences) {
5504 __ movl(temp, register_value);
5505 __ PoisonHeapReference(temp);
5506 __ movl(address, temp);
5507 } else {
5508 __ movl(address, register_value);
5509 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005510 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005511 codegen_->MaybeRecordImplicitNullCheck(instruction);
5512 }
5513
5514 Register card = locations->GetTemp(1).AsRegister<Register>();
5515 codegen_->MarkGCCard(
5516 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5517 __ Bind(&done);
5518
5519 if (slow_path != nullptr) {
5520 __ Bind(slow_path->GetExitLabel());
5521 }
5522
5523 break;
5524 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005525
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005526 case Primitive::kPrimInt: {
5527 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005528 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005529 if (value.IsRegister()) {
5530 __ movl(address, value.AsRegister<Register>());
5531 } else {
5532 DCHECK(value.IsConstant()) << value;
5533 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5534 __ movl(address, Immediate(v));
5535 }
5536 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005537 break;
5538 }
5539
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540 case Primitive::kPrimLong: {
5541 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005542 if (value.IsRegisterPair()) {
5543 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5544 value.AsRegisterPairLow<Register>());
5545 codegen_->MaybeRecordImplicitNullCheck(instruction);
5546 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5547 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005548 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005549 DCHECK(value.IsConstant());
5550 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5551 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5552 Immediate(Low32Bits(val)));
5553 codegen_->MaybeRecordImplicitNullCheck(instruction);
5554 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5555 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556 }
5557 break;
5558 }
5559
Mark Mendell7c8d0092015-01-26 11:21:33 -05005560 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005561 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005562 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005563 if (value.IsFpuRegister()) {
5564 __ movss(address, value.AsFpuRegister<XmmRegister>());
5565 } else {
5566 DCHECK(value.IsConstant());
5567 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5568 __ movl(address, Immediate(v));
5569 }
5570 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005571 break;
5572 }
5573
5574 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005575 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005576 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005577 if (value.IsFpuRegister()) {
5578 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5579 } else {
5580 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005581 Address address_hi =
5582 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005583 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5584 __ movl(address, Immediate(Low32Bits(v)));
5585 codegen_->MaybeRecordImplicitNullCheck(instruction);
5586 __ movl(address_hi, Immediate(High32Bits(v)));
5587 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005588 break;
5589 }
5590
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 case Primitive::kPrimVoid:
5592 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005593 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 }
5595}
5596
5597void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005599 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005600 if (!instruction->IsEmittedAtUseSite()) {
5601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5602 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005603}
5604
5605void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005606 if (instruction->IsEmittedAtUseSite()) {
5607 return;
5608 }
5609
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005610 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005611 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 Register obj = locations->InAt(0).AsRegister<Register>();
5613 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005614 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005615 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005616 // Mask out most significant bit in case the array is String's array of char.
5617 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005618 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005619 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620}
5621
5622void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005623 RegisterSet caller_saves = RegisterSet::Empty();
5624 InvokeRuntimeCallingConvention calling_convention;
5625 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5626 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5627 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005628 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005629 HInstruction* length = instruction->InputAt(1);
5630 if (!length->IsEmittedAtUseSite()) {
5631 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5632 }
jessicahandojo4877b792016-09-08 19:49:13 -07005633 // Need register to see array's length.
5634 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5635 locations->AddTemp(Location::RequiresRegister());
5636 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005637}
5638
5639void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005640 const bool is_string_compressed_char_at =
5641 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005642 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005643 Location index_loc = locations->InAt(0);
5644 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005645 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005646 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005647
Mark Mendell99dbd682015-04-22 16:18:52 -04005648 if (length_loc.IsConstant()) {
5649 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5650 if (index_loc.IsConstant()) {
5651 // BCE will remove the bounds check if we are guarenteed to pass.
5652 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5653 if (index < 0 || index >= length) {
5654 codegen_->AddSlowPath(slow_path);
5655 __ jmp(slow_path->GetEntryLabel());
5656 } else {
5657 // Some optimization after BCE may have generated this, and we should not
5658 // generate a bounds check if it is a valid range.
5659 }
5660 return;
5661 }
5662
5663 // We have to reverse the jump condition because the length is the constant.
5664 Register index_reg = index_loc.AsRegister<Register>();
5665 __ cmpl(index_reg, Immediate(length));
5666 codegen_->AddSlowPath(slow_path);
5667 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005668 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005669 HInstruction* array_length = instruction->InputAt(1);
5670 if (array_length->IsEmittedAtUseSite()) {
5671 // Address the length field in the array.
5672 DCHECK(array_length->IsArrayLength());
5673 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5674 Location array_loc = array_length->GetLocations()->InAt(0);
5675 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005676 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005677 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5678 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005679 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5680 __ movl(length_reg, array_len);
5681 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005682 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005683 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005684 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005685 // Checking bounds for general case:
5686 // Array of char or string's array with feature compression off.
5687 if (index_loc.IsConstant()) {
5688 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5689 __ cmpl(array_len, Immediate(value));
5690 } else {
5691 __ cmpl(array_len, index_loc.AsRegister<Register>());
5692 }
5693 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005694 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005695 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005696 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005697 }
5698 codegen_->AddSlowPath(slow_path);
5699 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005700 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005701}
5702
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005703void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005704 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005705}
5706
5707void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005708 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5709}
5710
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005711void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005712 LocationSummary* locations =
5713 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005714 // In suspend check slow path, usually there are no caller-save registers at all.
5715 // If SIMD instructions are present, however, we force spilling all live SIMD
5716 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005717 locations->SetCustomSlowPathCallerSaves(
5718 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005719}
5720
5721void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005722 HBasicBlock* block = instruction->GetBlock();
5723 if (block->GetLoopInformation() != nullptr) {
5724 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5725 // The back edge will generate the suspend check.
5726 return;
5727 }
5728 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5729 // The goto will generate the suspend check.
5730 return;
5731 }
5732 GenerateSuspendCheck(instruction, nullptr);
5733}
5734
5735void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5736 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005737 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005738 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5739 if (slow_path == nullptr) {
5740 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5741 instruction->SetSlowPath(slow_path);
5742 codegen_->AddSlowPath(slow_path);
5743 if (successor != nullptr) {
5744 DCHECK(successor->IsLoopHeader());
5745 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5746 }
5747 } else {
5748 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5749 }
5750
Andreas Gampe542451c2016-07-26 09:02:02 -07005751 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005752 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005753 if (successor == nullptr) {
5754 __ j(kNotEqual, slow_path->GetEntryLabel());
5755 __ Bind(slow_path->GetReturnLabel());
5756 } else {
5757 __ j(kEqual, codegen_->GetLabelOf(successor));
5758 __ jmp(slow_path->GetEntryLabel());
5759 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005760}
5761
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5763 return codegen_->GetAssembler();
5764}
5765
Mark Mendell7c8d0092015-01-26 11:21:33 -05005766void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005767 ScratchRegisterScope ensure_scratch(
5768 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5769 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5770 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5771 __ movl(temp_reg, Address(ESP, src + stack_offset));
5772 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005773}
5774
5775void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005776 ScratchRegisterScope ensure_scratch(
5777 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5778 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5779 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5780 __ movl(temp_reg, Address(ESP, src + stack_offset));
5781 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5782 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5783 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005784}
5785
5786void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005787 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005788 Location source = move->GetSource();
5789 Location destination = move->GetDestination();
5790
5791 if (source.IsRegister()) {
5792 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005793 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005794 } else if (destination.IsFpuRegister()) {
5795 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796 } else {
5797 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005798 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005799 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005800 } else if (source.IsRegisterPair()) {
5801 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5802 // Create stack space for 2 elements.
5803 __ subl(ESP, Immediate(2 * elem_size));
5804 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5805 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5806 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5807 // And remove the temporary stack space we allocated.
5808 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005809 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005810 if (destination.IsRegister()) {
5811 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5812 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005813 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005814 } else if (destination.IsRegisterPair()) {
5815 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5816 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5817 __ psrlq(src_reg, Immediate(32));
5818 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 } else if (destination.IsStackSlot()) {
5820 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005821 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005822 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005823 } else {
5824 DCHECK(destination.IsSIMDStackSlot());
5825 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005826 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005827 } else if (source.IsStackSlot()) {
5828 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005829 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 } else if (destination.IsFpuRegister()) {
5831 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005832 } else {
5833 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005834 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5835 }
5836 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005837 if (destination.IsRegisterPair()) {
5838 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5839 __ movl(destination.AsRegisterPairHigh<Register>(),
5840 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5841 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005842 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5843 } else {
5844 DCHECK(destination.IsDoubleStackSlot()) << destination;
5845 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005846 }
Aart Bik5576f372017-03-23 16:17:37 -07005847 } else if (source.IsSIMDStackSlot()) {
5848 DCHECK(destination.IsFpuRegister());
5849 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005850 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005851 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005852 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005853 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005854 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005855 if (value == 0) {
5856 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5857 } else {
5858 __ movl(destination.AsRegister<Register>(), Immediate(value));
5859 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005860 } else {
5861 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005862 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005863 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005864 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005865 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005866 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005867 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005868 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005869 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5870 if (value == 0) {
5871 // Easy handling of 0.0.
5872 __ xorps(dest, dest);
5873 } else {
5874 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005875 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5876 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5877 __ movl(temp, Immediate(value));
5878 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005879 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005880 } else {
5881 DCHECK(destination.IsStackSlot()) << destination;
5882 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5883 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005884 } else if (constant->IsLongConstant()) {
5885 int64_t value = constant->AsLongConstant()->GetValue();
5886 int32_t low_value = Low32Bits(value);
5887 int32_t high_value = High32Bits(value);
5888 Immediate low(low_value);
5889 Immediate high(high_value);
5890 if (destination.IsDoubleStackSlot()) {
5891 __ movl(Address(ESP, destination.GetStackIndex()), low);
5892 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5893 } else {
5894 __ movl(destination.AsRegisterPairLow<Register>(), low);
5895 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5896 }
5897 } else {
5898 DCHECK(constant->IsDoubleConstant());
5899 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005900 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005901 int32_t low_value = Low32Bits(value);
5902 int32_t high_value = High32Bits(value);
5903 Immediate low(low_value);
5904 Immediate high(high_value);
5905 if (destination.IsFpuRegister()) {
5906 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5907 if (value == 0) {
5908 // Easy handling of 0.0.
5909 __ xorpd(dest, dest);
5910 } else {
5911 __ pushl(high);
5912 __ pushl(low);
5913 __ movsd(dest, Address(ESP, 0));
5914 __ addl(ESP, Immediate(8));
5915 }
5916 } else {
5917 DCHECK(destination.IsDoubleStackSlot()) << destination;
5918 __ movl(Address(ESP, destination.GetStackIndex()), low);
5919 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5920 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005921 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005922 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005923 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005924 }
5925}
5926
Mark Mendella5c19ce2015-04-01 12:51:05 -04005927void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005928 Register suggested_scratch = reg == EAX ? EBX : EAX;
5929 ScratchRegisterScope ensure_scratch(
5930 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5931
5932 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5933 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5934 __ movl(Address(ESP, mem + stack_offset), reg);
5935 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005936}
5937
Mark Mendell7c8d0092015-01-26 11:21:33 -05005938void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005939 ScratchRegisterScope ensure_scratch(
5940 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5941
5942 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5943 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5944 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5945 __ movss(Address(ESP, mem + stack_offset), reg);
5946 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005947}
5948
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005950 ScratchRegisterScope ensure_scratch1(
5951 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005952
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005953 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5954 ScratchRegisterScope ensure_scratch2(
5955 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005956
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005957 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5958 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5959 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5960 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5961 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5962 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005963}
5964
5965void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005966 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005967 Location source = move->GetSource();
5968 Location destination = move->GetDestination();
5969
5970 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005971 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5972 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5973 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5974 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5975 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005976 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005977 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005978 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005979 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005980 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5981 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005982 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5983 // Use XOR Swap algorithm to avoid a temporary.
5984 DCHECK_NE(source.reg(), destination.reg());
5985 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5986 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5987 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5988 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5989 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5990 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5991 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005992 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5993 // Take advantage of the 16 bytes in the XMM register.
5994 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5995 Address stack(ESP, destination.GetStackIndex());
5996 // Load the double into the high doubleword.
5997 __ movhpd(reg, stack);
5998
5999 // Store the low double into the destination.
6000 __ movsd(stack, reg);
6001
6002 // Move the high double to the low double.
6003 __ psrldq(reg, Immediate(8));
6004 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6005 // Take advantage of the 16 bytes in the XMM register.
6006 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6007 Address stack(ESP, source.GetStackIndex());
6008 // Load the double into the high doubleword.
6009 __ movhpd(reg, stack);
6010
6011 // Store the low double into the destination.
6012 __ movsd(stack, reg);
6013
6014 // Move the high double to the low double.
6015 __ psrldq(reg, Immediate(8));
6016 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6017 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6018 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006019 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006020 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006021 }
6022}
6023
6024void ParallelMoveResolverX86::SpillScratch(int reg) {
6025 __ pushl(static_cast<Register>(reg));
6026}
6027
6028void ParallelMoveResolverX86::RestoreScratch(int reg) {
6029 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006030}
6031
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006032HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6033 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006035 case HLoadClass::LoadKind::kInvalid:
6036 LOG(FATAL) << "UNREACHABLE";
6037 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006038 case HLoadClass::LoadKind::kReferrersClass:
6039 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006041 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006042 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006044 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 DCHECK(Runtime::Current()->UseJitCompilation());
6046 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006047 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006048 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 break;
6050 }
6051 return desired_class_load_kind;
6052}
6053
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006054void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006055 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006056 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006058 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006059 cls,
6060 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006061 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006062 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 return;
6064 }
Vladimir Marko41559982017-01-06 14:04:23 +00006065 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006067 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6068 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069 ? LocationSummary::kCallOnSlowPath
6070 : LocationSummary::kNoCall;
6071 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006072 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006073 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006074 }
6075
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006076 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006077 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6078 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006079 locations->SetInAt(0, Location::RequiresRegister());
6080 }
6081 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006082 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6083 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6084 // Rely on the type resolution and/or initialization to save everything.
6085 RegisterSet caller_saves = RegisterSet::Empty();
6086 InvokeRuntimeCallingConvention calling_convention;
6087 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6088 locations->SetCustomSlowPathCallerSaves(caller_saves);
6089 } else {
6090 // For non-Baker read barrier we have a temp-clobbering call.
6091 }
6092 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006093}
6094
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006095Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6096 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006097 Handle<mirror::Class> handle) {
6098 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6099 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006100 // Add a patch entry and return the label.
6101 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6102 PatchInfo<Label>* info = &jit_class_patches_.back();
6103 return &info->label;
6104}
6105
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006106// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6107// move.
6108void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006109 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006110 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006111 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006112 return;
6113 }
Vladimir Marko41559982017-01-06 14:04:23 +00006114 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006115
Vladimir Marko41559982017-01-06 14:04:23 +00006116 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117 Location out_loc = locations->Out();
6118 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006120 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006121 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6122 ? kWithoutReadBarrier
6123 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006124 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 case HLoadClass::LoadKind::kReferrersClass: {
6126 DCHECK(!cls->CanCallRuntime());
6127 DCHECK(!cls->MustGenerateClinitCheck());
6128 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6129 Register current_method = locations->InAt(0).AsRegister<Register>();
6130 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006131 cls,
6132 out_loc,
6133 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006134 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006135 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136 break;
6137 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006139 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006140 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 Register method_address = locations->InAt(0).AsRegister<Register>();
6142 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006143 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006144 break;
6145 }
6146 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006147 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006148 uint32_t address = dchecked_integral_cast<uint32_t>(
6149 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6150 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006151 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006152 break;
6153 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006154 case HLoadClass::LoadKind::kBssEntry: {
6155 Register method_address = locations->InAt(0).AsRegister<Register>();
6156 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6157 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6158 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6159 generate_null_check = true;
6160 break;
6161 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006162 case HLoadClass::LoadKind::kJitTableAddress: {
6163 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6164 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006165 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006167 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006168 break;
6169 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006170 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006171 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006172 LOG(FATAL) << "UNREACHABLE";
6173 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006174 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006175
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6177 DCHECK(cls->CanCallRuntime());
6178 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6179 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6180 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006181
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006182 if (generate_null_check) {
6183 __ testl(out, out);
6184 __ j(kEqual, slow_path->GetEntryLabel());
6185 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006186
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 if (cls->MustGenerateClinitCheck()) {
6188 GenerateClassInitializationCheck(slow_path, out);
6189 } else {
6190 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006191 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006192 }
6193}
6194
6195void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6196 LocationSummary* locations =
6197 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6198 locations->SetInAt(0, Location::RequiresRegister());
6199 if (check->HasUses()) {
6200 locations->SetOut(Location::SameAsFirstInput());
6201 }
6202}
6203
6204void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006205 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006206 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006207 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006208 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006209 GenerateClassInitializationCheck(slow_path,
6210 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006211}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006213void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006214 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006215 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6216 Immediate(mirror::Class::kStatusInitialized));
6217 __ j(kLess, slow_path->GetEntryLabel());
6218 __ Bind(slow_path->GetExitLabel());
6219 // No need for memory fence, thanks to the X86 memory model.
6220}
6221
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006222HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6223 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006224 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006226 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006227 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006228 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006229 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006230 case HLoadString::LoadKind::kJitTableAddress:
6231 DCHECK(Runtime::Current()->UseJitCompilation());
6232 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006233 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006234 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006235 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006236 }
6237 return desired_string_load_kind;
6238}
6239
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006240void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006241 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006244 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006245 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006246 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 locations->SetInAt(0, Location::RequiresRegister());
6248 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006249 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006250 locations->SetOut(Location::RegisterLocation(EAX));
6251 } else {
6252 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006253 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6254 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006255 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006256 RegisterSet caller_saves = RegisterSet::Empty();
6257 InvokeRuntimeCallingConvention calling_convention;
6258 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6259 locations->SetCustomSlowPathCallerSaves(caller_saves);
6260 } else {
6261 // For non-Baker read barrier we have a temp-clobbering call.
6262 }
6263 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006264 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006265}
6266
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006267Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006268 dex::StringIndex dex_index,
6269 Handle<mirror::String> handle) {
6270 jit_string_roots_.Overwrite(
6271 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006272 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006273 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006274 PatchInfo<Label>* info = &jit_string_patches_.back();
6275 return &info->label;
6276}
6277
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006278// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6279// move.
6280void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006281 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006282 Location out_loc = locations->Out();
6283 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006284
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006285 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006287 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006288 Register method_address = locations->InAt(0).AsRegister<Register>();
6289 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006290 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006291 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006292 }
6293 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006294 uint32_t address = dchecked_integral_cast<uint32_t>(
6295 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6296 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006297 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006298 return;
6299 }
6300 case HLoadString::LoadKind::kBootImageInternTable: {
6301 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6302 Register method_address = locations->InAt(0).AsRegister<Register>();
6303 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6304 codegen_->RecordBootStringPatch(load);
6305 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006307 case HLoadString::LoadKind::kBssEntry: {
6308 Register method_address = locations->InAt(0).AsRegister<Register>();
6309 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6310 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006311 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006312 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006313 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6314 codegen_->AddSlowPath(slow_path);
6315 __ testl(out, out);
6316 __ j(kEqual, slow_path->GetEntryLabel());
6317 __ Bind(slow_path->GetExitLabel());
6318 return;
6319 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006320 case HLoadString::LoadKind::kJitTableAddress: {
6321 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6322 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006323 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006324 // /* GcRoot<mirror::String> */ out = *address
6325 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6326 return;
6327 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006328 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006329 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006330 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006331
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006332 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006333 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006334 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006335 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006336 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6337 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006338}
6339
David Brazdilcb1c0552015-08-04 16:22:25 +01006340static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006341 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006342}
6343
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006344void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6345 LocationSummary* locations =
6346 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6347 locations->SetOut(Location::RequiresRegister());
6348}
6349
6350void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006351 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6352}
6353
6354void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6355 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6356}
6357
6358void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6359 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006360}
6361
6362void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6363 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006364 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006365 InvokeRuntimeCallingConvention calling_convention;
6366 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6367}
6368
6369void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006370 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006371 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006372}
6373
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006374// Temp is used for read barrier.
6375static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6376 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006377 !kUseBakerReadBarrier &&
6378 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006379 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006380 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6381 return 1;
6382 }
6383 return 0;
6384}
6385
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006386// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006387// interface pointer, one for loading the current interface.
6388// The other checks have one temp for loading the object's class.
6389static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6390 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6391 return 2;
6392 }
6393 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006394}
6395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006396void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006399 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 case TypeCheckKind::kExactCheck:
6402 case TypeCheckKind::kAbstractClassCheck:
6403 case TypeCheckKind::kClassHierarchyCheck:
6404 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 call_kind =
6406 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006407 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 break;
6409 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 case TypeCheckKind::kUnresolvedCheck:
6411 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 call_kind = LocationSummary::kCallOnSlowPath;
6413 break;
6414 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006416 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006417 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006418 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006419 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 locations->SetInAt(0, Location::RequiresRegister());
6421 locations->SetInAt(1, Location::Any());
6422 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6423 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006424 // When read barriers are enabled, we need a temporary register for some cases.
6425 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006426}
6427
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006428void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006429 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006430 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 Location obj_loc = locations->InAt(0);
6432 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006433 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434 Location out_loc = locations->Out();
6435 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006436 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6437 DCHECK_LE(num_temps, 1u);
6438 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006439 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6441 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6442 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006443 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006445
6446 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006447 // Avoid null check if we know obj is not null.
6448 if (instruction->MustDoNullCheck()) {
6449 __ testl(obj, obj);
6450 __ j(kEqual, &zero);
6451 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452
Roland Levillain7c1559a2015-12-15 10:55:36 +00006453 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006454 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006455 // /* HeapReference<Class> */ out = obj->klass_
6456 GenerateReferenceLoadTwoRegisters(instruction,
6457 out_loc,
6458 obj_loc,
6459 class_offset,
6460 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 if (cls.IsRegister()) {
6462 __ cmpl(out, cls.AsRegister<Register>());
6463 } else {
6464 DCHECK(cls.IsStackSlot()) << cls;
6465 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6466 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 // Classes must be equal for the instanceof to succeed.
6469 __ j(kNotEqual, &zero);
6470 __ movl(out, Immediate(1));
6471 __ jmp(&done);
6472 break;
6473 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006476 // /* HeapReference<Class> */ out = obj->klass_
6477 GenerateReferenceLoadTwoRegisters(instruction,
6478 out_loc,
6479 obj_loc,
6480 class_offset,
6481 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 // If the class is abstract, we eagerly fetch the super class of the
6483 // object to avoid doing a comparison we know will fail.
6484 NearLabel loop;
6485 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006486 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006487 GenerateReferenceLoadOneRegister(instruction,
6488 out_loc,
6489 super_offset,
6490 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006491 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 __ testl(out, out);
6493 // If `out` is null, we use it for the result, and jump to `done`.
6494 __ j(kEqual, &done);
6495 if (cls.IsRegister()) {
6496 __ cmpl(out, cls.AsRegister<Register>());
6497 } else {
6498 DCHECK(cls.IsStackSlot()) << cls;
6499 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6500 }
6501 __ j(kNotEqual, &loop);
6502 __ movl(out, Immediate(1));
6503 if (zero.IsLinked()) {
6504 __ jmp(&done);
6505 }
6506 break;
6507 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006509 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006510 // /* HeapReference<Class> */ out = obj->klass_
6511 GenerateReferenceLoadTwoRegisters(instruction,
6512 out_loc,
6513 obj_loc,
6514 class_offset,
6515 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 // Walk over the class hierarchy to find a match.
6517 NearLabel loop, success;
6518 __ Bind(&loop);
6519 if (cls.IsRegister()) {
6520 __ cmpl(out, cls.AsRegister<Register>());
6521 } else {
6522 DCHECK(cls.IsStackSlot()) << cls;
6523 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6524 }
6525 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006527 GenerateReferenceLoadOneRegister(instruction,
6528 out_loc,
6529 super_offset,
6530 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006531 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 __ testl(out, out);
6533 __ j(kNotEqual, &loop);
6534 // If `out` is null, we use it for the result, and jump to `done`.
6535 __ jmp(&done);
6536 __ Bind(&success);
6537 __ movl(out, Immediate(1));
6538 if (zero.IsLinked()) {
6539 __ jmp(&done);
6540 }
6541 break;
6542 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006545 // /* HeapReference<Class> */ out = obj->klass_
6546 GenerateReferenceLoadTwoRegisters(instruction,
6547 out_loc,
6548 obj_loc,
6549 class_offset,
6550 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006551 // Do an exact check.
6552 NearLabel exact_check;
6553 if (cls.IsRegister()) {
6554 __ cmpl(out, cls.AsRegister<Register>());
6555 } else {
6556 DCHECK(cls.IsStackSlot()) << cls;
6557 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6558 }
6559 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006562 GenerateReferenceLoadOneRegister(instruction,
6563 out_loc,
6564 component_offset,
6565 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006566 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 __ testl(out, out);
6568 // If `out` is null, we use it for the result, and jump to `done`.
6569 __ j(kEqual, &done);
6570 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6571 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006572 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 __ movl(out, Immediate(1));
6574 __ jmp(&done);
6575 break;
6576 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006579 // No read barrier since the slow path will retry upon failure.
6580 // /* HeapReference<Class> */ out = obj->klass_
6581 GenerateReferenceLoadTwoRegisters(instruction,
6582 out_loc,
6583 obj_loc,
6584 class_offset,
6585 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006586 if (cls.IsRegister()) {
6587 __ cmpl(out, cls.AsRegister<Register>());
6588 } else {
6589 DCHECK(cls.IsStackSlot()) << cls;
6590 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6591 }
6592 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006593 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6594 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006595 codegen_->AddSlowPath(slow_path);
6596 __ j(kNotEqual, slow_path->GetEntryLabel());
6597 __ movl(out, Immediate(1));
6598 if (zero.IsLinked()) {
6599 __ jmp(&done);
6600 }
6601 break;
6602 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603
Calin Juravle98893e12015-10-02 21:05:03 +01006604 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605 case TypeCheckKind::kInterfaceCheck: {
6606 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006607 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 // cases.
6609 //
6610 // We cannot directly call the InstanceofNonTrivial runtime
6611 // entry point without resorting to a type checking slow path
6612 // here (i.e. by calling InvokeRuntime directly), as it would
6613 // require to assign fixed registers for the inputs of this
6614 // HInstanceOf instruction (following the runtime calling
6615 // convention), which might be cluttered by the potential first
6616 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006617 //
6618 // TODO: Introduce a new runtime entry point taking the object
6619 // to test (instead of its class) as argument, and let it deal
6620 // with the read barrier issues. This will let us refactor this
6621 // case of the `switch` code as it was previously (with a direct
6622 // call to the runtime not using a type checking slow path).
6623 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624 DCHECK(locations->OnlyCallsOnSlowPath());
6625 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6626 /* is_fatal */ false);
6627 codegen_->AddSlowPath(slow_path);
6628 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006629 if (zero.IsLinked()) {
6630 __ jmp(&done);
6631 }
6632 break;
6633 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006634 }
6635
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006636 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006637 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006638 __ xorl(out, out);
6639 }
6640
6641 if (done.IsLinked()) {
6642 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006643 }
6644
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006645 if (slow_path != nullptr) {
6646 __ Bind(slow_path->GetExitLabel());
6647 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006648}
6649
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006650static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6651 switch (type_check_kind) {
6652 case TypeCheckKind::kExactCheck:
6653 case TypeCheckKind::kAbstractClassCheck:
6654 case TypeCheckKind::kClassHierarchyCheck:
6655 case TypeCheckKind::kArrayObjectCheck:
6656 return !throws_into_catch && !kEmitCompilerReadBarrier;
6657 case TypeCheckKind::kInterfaceCheck:
6658 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6659 case TypeCheckKind::kArrayCheck:
6660 case TypeCheckKind::kUnresolvedCheck:
6661 return false;
6662 }
6663 LOG(FATAL) << "Unreachable";
6664 UNREACHABLE();
6665}
6666
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006667void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006670 LocationSummary::CallKind call_kind =
6671 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6672 ? LocationSummary::kNoCall
6673 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6675 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006676 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6677 // Require a register for the interface check since there is a loop that compares the class to
6678 // a memory address.
6679 locations->SetInAt(1, Location::RequiresRegister());
6680 } else {
6681 locations->SetInAt(1, Location::Any());
6682 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6684 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006685 // When read barriers are enabled, we need an additional temporary register for some cases.
6686 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6687}
6688
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006689void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006690 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006691 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006692 Location obj_loc = locations->InAt(0);
6693 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006694 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695 Location temp_loc = locations->GetTemp(0);
6696 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006697 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6698 DCHECK_GE(num_temps, 1u);
6699 DCHECK_LE(num_temps, 2u);
6700 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6701 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6702 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6703 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6704 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6705 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6706 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6707 const uint32_t object_array_data_offset =
6708 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006710 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6711 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6712 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006714 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6715
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 SlowPathCode* type_check_slow_path =
6717 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6718 is_type_check_slow_path_fatal);
6719 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006720
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006722 // Avoid null check if we know obj is not null.
6723 if (instruction->MustDoNullCheck()) {
6724 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006726 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006729 case TypeCheckKind::kExactCheck:
6730 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006731 // /* HeapReference<Class> */ temp = obj->klass_
6732 GenerateReferenceLoadTwoRegisters(instruction,
6733 temp_loc,
6734 obj_loc,
6735 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006736 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006737
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738 if (cls.IsRegister()) {
6739 __ cmpl(temp, cls.AsRegister<Register>());
6740 } else {
6741 DCHECK(cls.IsStackSlot()) << cls;
6742 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6743 }
6744 // Jump to slow path for throwing the exception or doing a
6745 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006746 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747 break;
6748 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006750 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006751 // /* HeapReference<Class> */ temp = obj->klass_
6752 GenerateReferenceLoadTwoRegisters(instruction,
6753 temp_loc,
6754 obj_loc,
6755 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006756 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006758 // If the class is abstract, we eagerly fetch the super class of the
6759 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006760 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006762 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006763 GenerateReferenceLoadOneRegister(instruction,
6764 temp_loc,
6765 super_offset,
6766 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006767 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006769 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6770 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006771 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006772 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006773
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006774 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006775 if (cls.IsRegister()) {
6776 __ cmpl(temp, cls.AsRegister<Register>());
6777 } else {
6778 DCHECK(cls.IsStackSlot()) << cls;
6779 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6780 }
6781 __ j(kNotEqual, &loop);
6782 break;
6783 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006786 // /* HeapReference<Class> */ temp = obj->klass_
6787 GenerateReferenceLoadTwoRegisters(instruction,
6788 temp_loc,
6789 obj_loc,
6790 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006791 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006792
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006793 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006794 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795 __ Bind(&loop);
6796 if (cls.IsRegister()) {
6797 __ cmpl(temp, cls.AsRegister<Register>());
6798 } else {
6799 DCHECK(cls.IsStackSlot()) << cls;
6800 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6801 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006802 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006805 GenerateReferenceLoadOneRegister(instruction,
6806 temp_loc,
6807 super_offset,
6808 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006809 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006810
6811 // If the class reference currently in `temp` is not null, jump
6812 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006814 __ j(kNotZero, &loop);
6815 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006816 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006817 break;
6818 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006819
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006820 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006821 // /* HeapReference<Class> */ temp = obj->klass_
6822 GenerateReferenceLoadTwoRegisters(instruction,
6823 temp_loc,
6824 obj_loc,
6825 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006826 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006827
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006828 // Do an exact check.
6829 if (cls.IsRegister()) {
6830 __ cmpl(temp, cls.AsRegister<Register>());
6831 } else {
6832 DCHECK(cls.IsStackSlot()) << cls;
6833 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6834 }
6835 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836
6837 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006838 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006839 GenerateReferenceLoadOneRegister(instruction,
6840 temp_loc,
6841 component_offset,
6842 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006843 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006845 // If the component type is null (i.e. the object not an array), jump to the slow path to
6846 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006847 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006848 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006849
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006850 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006851 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006852 break;
6853 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006854
Calin Juravle98893e12015-10-02 21:05:03 +01006855 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006856 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006857 // We cannot directly call the CheckCast runtime entry point
6858 // without resorting to a type checking slow path here (i.e. by
6859 // calling InvokeRuntime directly), as it would require to
6860 // assign fixed registers for the inputs of this HInstanceOf
6861 // instruction (following the runtime calling convention), which
6862 // might be cluttered by the potential first read barrier
6863 // emission at the beginning of this method.
6864 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006865 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006866
6867 case TypeCheckKind::kInterfaceCheck: {
6868 // Fast path for the interface check. Since we compare with a memory location in the inner
6869 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6870 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006871 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006872 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006873 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6874 // doing this.
6875 // /* HeapReference<Class> */ temp = obj->klass_
6876 GenerateReferenceLoadTwoRegisters(instruction,
6877 temp_loc,
6878 obj_loc,
6879 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006880 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006881
6882 // /* HeapReference<Class> */ temp = temp->iftable_
6883 GenerateReferenceLoadTwoRegisters(instruction,
6884 temp_loc,
6885 temp_loc,
6886 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006887 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006888 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006889 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006890 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006891 NearLabel start_loop;
6892 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006893 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006894 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006895 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6896 // Go to next interface if the classes do not match.
6897 __ cmpl(cls.AsRegister<Register>(),
6898 CodeGeneratorX86::ArrayAddress(temp,
6899 maybe_temp2_loc,
6900 TIMES_4,
6901 object_array_data_offset));
6902 __ j(kNotEqual, &start_loop);
6903 } else {
6904 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006905 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006906 break;
6907 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006908 }
6909 __ Bind(&done);
6910
Roland Levillain0d5a2812015-11-13 10:07:31 +00006911 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006912}
6913
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006914void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6915 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006917 InvokeRuntimeCallingConvention calling_convention;
6918 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6919}
6920
6921void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006922 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6923 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006924 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006925 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006926 if (instruction->IsEnter()) {
6927 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6928 } else {
6929 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6930 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006931}
6932
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006933void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6934void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6935void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6936
6937void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6938 LocationSummary* locations =
6939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6940 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6941 || instruction->GetResultType() == Primitive::kPrimLong);
6942 locations->SetInAt(0, Location::RequiresRegister());
6943 locations->SetInAt(1, Location::Any());
6944 locations->SetOut(Location::SameAsFirstInput());
6945}
6946
6947void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6948 HandleBitwiseOperation(instruction);
6949}
6950
6951void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6952 HandleBitwiseOperation(instruction);
6953}
6954
6955void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6956 HandleBitwiseOperation(instruction);
6957}
6958
6959void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6960 LocationSummary* locations = instruction->GetLocations();
6961 Location first = locations->InAt(0);
6962 Location second = locations->InAt(1);
6963 DCHECK(first.Equals(locations->Out()));
6964
6965 if (instruction->GetResultType() == Primitive::kPrimInt) {
6966 if (second.IsRegister()) {
6967 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006968 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006970 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006971 } else {
6972 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006973 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 }
6975 } else if (second.IsConstant()) {
6976 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006977 __ andl(first.AsRegister<Register>(),
6978 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006979 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006980 __ orl(first.AsRegister<Register>(),
6981 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006982 } else {
6983 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006984 __ xorl(first.AsRegister<Register>(),
6985 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 }
6987 } else {
6988 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006989 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006990 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006991 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006992 } else {
6993 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006994 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006995 }
6996 }
6997 } else {
6998 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6999 if (second.IsRegisterPair()) {
7000 if (instruction->IsAnd()) {
7001 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7002 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7003 } else if (instruction->IsOr()) {
7004 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7005 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7006 } else {
7007 DCHECK(instruction->IsXor());
7008 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7009 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7010 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007011 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007012 if (instruction->IsAnd()) {
7013 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7014 __ andl(first.AsRegisterPairHigh<Register>(),
7015 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7016 } else if (instruction->IsOr()) {
7017 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7018 __ orl(first.AsRegisterPairHigh<Register>(),
7019 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7020 } else {
7021 DCHECK(instruction->IsXor());
7022 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7023 __ xorl(first.AsRegisterPairHigh<Register>(),
7024 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7025 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007026 } else {
7027 DCHECK(second.IsConstant()) << second;
7028 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007029 int32_t low_value = Low32Bits(value);
7030 int32_t high_value = High32Bits(value);
7031 Immediate low(low_value);
7032 Immediate high(high_value);
7033 Register first_low = first.AsRegisterPairLow<Register>();
7034 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007035 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007036 if (low_value == 0) {
7037 __ xorl(first_low, first_low);
7038 } else if (low_value != -1) {
7039 __ andl(first_low, low);
7040 }
7041 if (high_value == 0) {
7042 __ xorl(first_high, first_high);
7043 } else if (high_value != -1) {
7044 __ andl(first_high, high);
7045 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007046 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007047 if (low_value != 0) {
7048 __ orl(first_low, low);
7049 }
7050 if (high_value != 0) {
7051 __ orl(first_high, high);
7052 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007053 } else {
7054 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007055 if (low_value != 0) {
7056 __ xorl(first_low, low);
7057 }
7058 if (high_value != 0) {
7059 __ xorl(first_high, high);
7060 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007061 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007062 }
7063 }
7064}
7065
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007066void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7067 HInstruction* instruction,
7068 Location out,
7069 uint32_t offset,
7070 Location maybe_temp,
7071 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007072 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007073 if (read_barrier_option == kWithReadBarrier) {
7074 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007075 if (kUseBakerReadBarrier) {
7076 // Load with fast path based Baker's read barrier.
7077 // /* HeapReference<Object> */ out = *(out + offset)
7078 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007079 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007080 } else {
7081 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007082 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007083 // in the following move operation, as we will need it for the
7084 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007085 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007086 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 // /* HeapReference<Object> */ out = *(out + offset)
7088 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007089 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007090 }
7091 } else {
7092 // Plain load with no read barrier.
7093 // /* HeapReference<Object> */ out = *(out + offset)
7094 __ movl(out_reg, Address(out_reg, offset));
7095 __ MaybeUnpoisonHeapReference(out_reg);
7096 }
7097}
7098
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007099void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7100 HInstruction* instruction,
7101 Location out,
7102 Location obj,
7103 uint32_t offset,
7104 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007105 Register out_reg = out.AsRegister<Register>();
7106 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007107 if (read_barrier_option == kWithReadBarrier) {
7108 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007109 if (kUseBakerReadBarrier) {
7110 // Load with fast path based Baker's read barrier.
7111 // /* HeapReference<Object> */ out = *(obj + offset)
7112 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007113 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007114 } else {
7115 // Load with slow path based read barrier.
7116 // /* HeapReference<Object> */ out = *(obj + offset)
7117 __ movl(out_reg, Address(obj_reg, offset));
7118 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7119 }
7120 } else {
7121 // Plain load with no read barrier.
7122 // /* HeapReference<Object> */ out = *(obj + offset)
7123 __ movl(out_reg, Address(obj_reg, offset));
7124 __ MaybeUnpoisonHeapReference(out_reg);
7125 }
7126}
7127
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007128void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7129 HInstruction* instruction,
7130 Location root,
7131 const Address& address,
7132 Label* fixup_label,
7133 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007134 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007135 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007136 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007137 if (kUseBakerReadBarrier) {
7138 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7139 // Baker's read barrier are used:
7140 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007141 // root = obj.field;
7142 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7143 // if (temp != null) {
7144 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145 // }
7146
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007147 // /* GcRoot<mirror::Object> */ root = *address
7148 __ movl(root_reg, address);
7149 if (fixup_label != nullptr) {
7150 __ Bind(fixup_label);
7151 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152 static_assert(
7153 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7154 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7155 "have different sizes.");
7156 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7157 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7158 "have different sizes.");
7159
Vladimir Marko953437b2016-08-24 08:30:46 +00007160 // Slow path marking the GC root `root`.
7161 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007162 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007163 codegen_->AddSlowPath(slow_path);
7164
Roland Levillaind966ce72017-02-09 16:20:14 +00007165 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7166 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007167 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007168 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7169 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 __ j(kNotEqual, slow_path->GetEntryLabel());
7171 __ Bind(slow_path->GetExitLabel());
7172 } else {
7173 // GC root loaded through a slow path for read barriers other
7174 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007175 // /* GcRoot<mirror::Object>* */ root = address
7176 __ leal(root_reg, address);
7177 if (fixup_label != nullptr) {
7178 __ Bind(fixup_label);
7179 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180 // /* mirror::Object* */ root = root->Read()
7181 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7182 }
7183 } else {
7184 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007185 // /* GcRoot<mirror::Object> */ root = *address
7186 __ movl(root_reg, address);
7187 if (fixup_label != nullptr) {
7188 __ Bind(fixup_label);
7189 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007190 // Note that GC roots are not affected by heap poisoning, thus we
7191 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007192 }
7193}
7194
7195void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7196 Location ref,
7197 Register obj,
7198 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007199 bool needs_null_check) {
7200 DCHECK(kEmitCompilerReadBarrier);
7201 DCHECK(kUseBakerReadBarrier);
7202
7203 // /* HeapReference<Object> */ ref = *(obj + offset)
7204 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007205 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007206}
7207
7208void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7209 Location ref,
7210 Register obj,
7211 uint32_t data_offset,
7212 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007213 bool needs_null_check) {
7214 DCHECK(kEmitCompilerReadBarrier);
7215 DCHECK(kUseBakerReadBarrier);
7216
Roland Levillain3d312422016-06-23 13:53:42 +01007217 static_assert(
7218 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7219 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007220 // /* HeapReference<Object> */ ref =
7221 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007222 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007223 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224}
7225
7226void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7227 Location ref,
7228 Register obj,
7229 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007230 bool needs_null_check,
7231 bool always_update_field,
7232 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007233 DCHECK(kEmitCompilerReadBarrier);
7234 DCHECK(kUseBakerReadBarrier);
7235
7236 // In slow path based read barriers, the read barrier call is
7237 // inserted after the original load. However, in fast path based
7238 // Baker's read barriers, we need to perform the load of
7239 // mirror::Object::monitor_ *before* the original reference load.
7240 // This load-load ordering is required by the read barrier.
7241 // The fast path/slow path (for Baker's algorithm) should look like:
7242 //
7243 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7244 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7245 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007246 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007247 // if (is_gray) {
7248 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7249 // }
7250 //
7251 // Note: the original implementation in ReadBarrier::Barrier is
7252 // slightly more complex as:
7253 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007254 // the high-bits of rb_state, which are expected to be all zeroes
7255 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7256 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007257 // - it performs additional checks that we do not do here for
7258 // performance reasons.
7259
7260 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007261 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7262
Vladimir Marko953437b2016-08-24 08:30:46 +00007263 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007264 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7265 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007266 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7267 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7268 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7269
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007270 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007271 // ref = ReadBarrier::Mark(ref);
7272 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7273 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007274 if (needs_null_check) {
7275 MaybeRecordImplicitNullCheck(instruction);
7276 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007277
7278 // Load fence to prevent load-load reordering.
7279 // Note that this is a no-op, thanks to the x86 memory model.
7280 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7281
7282 // The actual reference load.
7283 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007284 __ movl(ref_reg, src); // Flags are unaffected.
7285
7286 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7287 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007288 SlowPathCode* slow_path;
7289 if (always_update_field) {
7290 DCHECK(temp != nullptr);
7291 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7292 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7293 } else {
7294 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7295 instruction, ref, /* unpoison_ref_before_marking */ true);
7296 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007297 AddSlowPath(slow_path);
7298
7299 // We have done the "if" of the gray bit check above, now branch based on the flags.
7300 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007301
7302 // Object* ref = ref_addr->AsMirrorPtr()
7303 __ MaybeUnpoisonHeapReference(ref_reg);
7304
Roland Levillain7c1559a2015-12-15 10:55:36 +00007305 __ Bind(slow_path->GetExitLabel());
7306}
7307
7308void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7309 Location out,
7310 Location ref,
7311 Location obj,
7312 uint32_t offset,
7313 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007314 DCHECK(kEmitCompilerReadBarrier);
7315
Roland Levillain7c1559a2015-12-15 10:55:36 +00007316 // Insert a slow path based read barrier *after* the reference load.
7317 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007318 // If heap poisoning is enabled, the unpoisoning of the loaded
7319 // reference will be carried out by the runtime within the slow
7320 // path.
7321 //
7322 // Note that `ref` currently does not get unpoisoned (when heap
7323 // poisoning is enabled), which is alright as the `ref` argument is
7324 // not used by the artReadBarrierSlow entry point.
7325 //
7326 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7327 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7328 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7329 AddSlowPath(slow_path);
7330
Roland Levillain0d5a2812015-11-13 10:07:31 +00007331 __ jmp(slow_path->GetEntryLabel());
7332 __ Bind(slow_path->GetExitLabel());
7333}
7334
Roland Levillain7c1559a2015-12-15 10:55:36 +00007335void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7336 Location out,
7337 Location ref,
7338 Location obj,
7339 uint32_t offset,
7340 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007341 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007342 // Baker's read barriers shall be handled by the fast path
7343 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7344 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007345 // If heap poisoning is enabled, unpoisoning will be taken care of
7346 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007347 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348 } else if (kPoisonHeapReferences) {
7349 __ UnpoisonHeapReference(out.AsRegister<Register>());
7350 }
7351}
7352
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7354 Location out,
7355 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 DCHECK(kEmitCompilerReadBarrier);
7357
Roland Levillain7c1559a2015-12-15 10:55:36 +00007358 // Insert a slow path based read barrier *after* the GC root load.
7359 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007360 // Note that GC roots are not affected by heap poisoning, so we do
7361 // not need to do anything special for this here.
7362 SlowPathCode* slow_path =
7363 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7364 AddSlowPath(slow_path);
7365
Roland Levillain0d5a2812015-11-13 10:07:31 +00007366 __ jmp(slow_path->GetEntryLabel());
7367 __ Bind(slow_path->GetExitLabel());
7368}
7369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007370void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007372 LOG(FATAL) << "Unreachable";
7373}
7374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007375void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007376 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 LOG(FATAL) << "Unreachable";
7378}
7379
Mark Mendellfe57faa2015-09-18 09:26:15 -04007380// Simple implementation of packed switch - generate cascaded compare/jumps.
7381void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7382 LocationSummary* locations =
7383 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7384 locations->SetInAt(0, Location::RequiresRegister());
7385}
7386
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007387void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7388 int32_t lower_bound,
7389 uint32_t num_entries,
7390 HBasicBlock* switch_block,
7391 HBasicBlock* default_block) {
7392 // Figure out the correct compare values and jump conditions.
7393 // Handle the first compare/branch as a special case because it might
7394 // jump to the default case.
7395 DCHECK_GT(num_entries, 2u);
7396 Condition first_condition;
7397 uint32_t index;
7398 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7399 if (lower_bound != 0) {
7400 first_condition = kLess;
7401 __ cmpl(value_reg, Immediate(lower_bound));
7402 __ j(first_condition, codegen_->GetLabelOf(default_block));
7403 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007404
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007405 index = 1;
7406 } else {
7407 // Handle all the compare/jumps below.
7408 first_condition = kBelow;
7409 index = 0;
7410 }
7411
7412 // Handle the rest of the compare/jumps.
7413 for (; index + 1 < num_entries; index += 2) {
7414 int32_t compare_to_value = lower_bound + index + 1;
7415 __ cmpl(value_reg, Immediate(compare_to_value));
7416 // Jump to successors[index] if value < case_value[index].
7417 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7418 // Jump to successors[index + 1] if value == case_value[index + 1].
7419 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7420 }
7421
7422 if (index != num_entries) {
7423 // There are an odd number of entries. Handle the last one.
7424 DCHECK_EQ(index + 1, num_entries);
7425 __ cmpl(value_reg, Immediate(lower_bound + index));
7426 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007427 }
7428
7429 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007430 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7431 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007432 }
7433}
7434
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007435void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7436 int32_t lower_bound = switch_instr->GetStartValue();
7437 uint32_t num_entries = switch_instr->GetNumEntries();
7438 LocationSummary* locations = switch_instr->GetLocations();
7439 Register value_reg = locations->InAt(0).AsRegister<Register>();
7440
7441 GenPackedSwitchWithCompares(value_reg,
7442 lower_bound,
7443 num_entries,
7444 switch_instr->GetBlock(),
7445 switch_instr->GetDefaultBlock());
7446}
7447
Mark Mendell805b3b52015-09-18 14:10:29 -04007448void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7449 LocationSummary* locations =
7450 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7451 locations->SetInAt(0, Location::RequiresRegister());
7452
7453 // Constant area pointer.
7454 locations->SetInAt(1, Location::RequiresRegister());
7455
7456 // And the temporary we need.
7457 locations->AddTemp(Location::RequiresRegister());
7458}
7459
7460void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7461 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007462 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007463 LocationSummary* locations = switch_instr->GetLocations();
7464 Register value_reg = locations->InAt(0).AsRegister<Register>();
7465 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7466
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007467 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7468 GenPackedSwitchWithCompares(value_reg,
7469 lower_bound,
7470 num_entries,
7471 switch_instr->GetBlock(),
7472 default_block);
7473 return;
7474 }
7475
Mark Mendell805b3b52015-09-18 14:10:29 -04007476 // Optimizing has a jump area.
7477 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7478 Register constant_area = locations->InAt(1).AsRegister<Register>();
7479
7480 // Remove the bias, if needed.
7481 if (lower_bound != 0) {
7482 __ leal(temp_reg, Address(value_reg, -lower_bound));
7483 value_reg = temp_reg;
7484 }
7485
7486 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007487 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007488 __ cmpl(value_reg, Immediate(num_entries - 1));
7489 __ j(kAbove, codegen_->GetLabelOf(default_block));
7490
7491 // We are in the range of the table.
7492 // Load (target-constant_area) from the jump table, indexing by the value.
7493 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7494
7495 // Compute the actual target address by adding in constant_area.
7496 __ addl(temp_reg, constant_area);
7497
7498 // And jump.
7499 __ jmp(temp_reg);
7500}
7501
Mark Mendell0616ae02015-04-17 12:49:27 -04007502void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7503 HX86ComputeBaseMethodAddress* insn) {
7504 LocationSummary* locations =
7505 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7506 locations->SetOut(Location::RequiresRegister());
7507}
7508
7509void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7510 HX86ComputeBaseMethodAddress* insn) {
7511 LocationSummary* locations = insn->GetLocations();
7512 Register reg = locations->Out().AsRegister<Register>();
7513
7514 // Generate call to next instruction.
7515 Label next_instruction;
7516 __ call(&next_instruction);
7517 __ Bind(&next_instruction);
7518
7519 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007520 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007521
7522 // Grab the return address off the stack.
7523 __ popl(reg);
7524}
7525
7526void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7527 HX86LoadFromConstantTable* insn) {
7528 LocationSummary* locations =
7529 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7530
7531 locations->SetInAt(0, Location::RequiresRegister());
7532 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7533
7534 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007535 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007536 return;
7537 }
7538
7539 switch (insn->GetType()) {
7540 case Primitive::kPrimFloat:
7541 case Primitive::kPrimDouble:
7542 locations->SetOut(Location::RequiresFpuRegister());
7543 break;
7544
7545 case Primitive::kPrimInt:
7546 locations->SetOut(Location::RequiresRegister());
7547 break;
7548
7549 default:
7550 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7551 }
7552}
7553
7554void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007555 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007556 return;
7557 }
7558
7559 LocationSummary* locations = insn->GetLocations();
7560 Location out = locations->Out();
7561 Register const_area = locations->InAt(0).AsRegister<Register>();
7562 HConstant *value = insn->GetConstant();
7563
7564 switch (insn->GetType()) {
7565 case Primitive::kPrimFloat:
7566 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007567 codegen_->LiteralFloatAddress(
7568 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007569 break;
7570
7571 case Primitive::kPrimDouble:
7572 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007573 codegen_->LiteralDoubleAddress(
7574 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007575 break;
7576
7577 case Primitive::kPrimInt:
7578 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007579 codegen_->LiteralInt32Address(
7580 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007581 break;
7582
7583 default:
7584 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7585 }
7586}
7587
Mark Mendell0616ae02015-04-17 12:49:27 -04007588/**
7589 * Class to handle late fixup of offsets into constant area.
7590 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007591class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007592 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007593 RIPFixup(CodeGeneratorX86& codegen,
7594 HX86ComputeBaseMethodAddress* base_method_address,
7595 size_t offset)
7596 : codegen_(&codegen),
7597 base_method_address_(base_method_address),
7598 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007599
7600 protected:
7601 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7602
7603 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007604 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007605
7606 private:
7607 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7608 // Patch the correct offset for the instruction. The place to patch is the
7609 // last 4 bytes of the instruction.
7610 // The value to patch is the distance from the offset in the constant area
7611 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007612 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007613 int32_t relative_position =
7614 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007615
7616 // Patch in the right value.
7617 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7618 }
7619
Mark Mendell0616ae02015-04-17 12:49:27 -04007620 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007621 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007622};
7623
Mark Mendell805b3b52015-09-18 14:10:29 -04007624/**
7625 * Class to handle late fixup of offsets to a jump table that will be created in the
7626 * constant area.
7627 */
7628class JumpTableRIPFixup : public RIPFixup {
7629 public:
7630 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007631 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7632 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007633
7634 void CreateJumpTable() {
7635 X86Assembler* assembler = codegen_->GetAssembler();
7636
7637 // Ensure that the reference to the jump table has the correct offset.
7638 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7639 SetOffset(offset_in_constant_table);
7640
7641 // The label values in the jump table are computed relative to the
7642 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007643 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007644
7645 // Populate the jump table with the correct values for the jump table.
7646 int32_t num_entries = switch_instr_->GetNumEntries();
7647 HBasicBlock* block = switch_instr_->GetBlock();
7648 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7649 // The value that we want is the target offset - the position of the table.
7650 for (int32_t i = 0; i < num_entries; i++) {
7651 HBasicBlock* b = successors[i];
7652 Label* l = codegen_->GetLabelOf(b);
7653 DCHECK(l->IsBound());
7654 int32_t offset_to_block = l->Position() - relative_offset;
7655 assembler->AppendInt32(offset_to_block);
7656 }
7657 }
7658
7659 private:
7660 const HX86PackedSwitch* switch_instr_;
7661};
7662
7663void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7664 // Generate the constant area if needed.
7665 X86Assembler* assembler = GetAssembler();
7666 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7667 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7668 // byte values.
7669 assembler->Align(4, 0);
7670 constant_area_start_ = assembler->CodeSize();
7671
7672 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007673 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007674 jump_table->CreateJumpTable();
7675 }
7676
7677 // And now add the constant area to the generated code.
7678 assembler->AddConstantArea();
7679 }
7680
7681 // And finish up.
7682 CodeGenerator::Finalize(allocator);
7683}
7684
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007685Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7686 HX86ComputeBaseMethodAddress* method_base,
7687 Register reg) {
7688 AssemblerFixup* fixup =
7689 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007690 return Address(reg, kDummy32BitOffset, fixup);
7691}
7692
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007693Address CodeGeneratorX86::LiteralFloatAddress(float v,
7694 HX86ComputeBaseMethodAddress* method_base,
7695 Register reg) {
7696 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007697 return Address(reg, kDummy32BitOffset, fixup);
7698}
7699
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007700Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7701 HX86ComputeBaseMethodAddress* method_base,
7702 Register reg) {
7703 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007704 return Address(reg, kDummy32BitOffset, fixup);
7705}
7706
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007707Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7708 HX86ComputeBaseMethodAddress* method_base,
7709 Register reg) {
7710 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007711 return Address(reg, kDummy32BitOffset, fixup);
7712}
7713
Aart Bika19616e2016-02-01 18:57:58 -08007714void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7715 if (value == 0) {
7716 __ xorl(dest, dest);
7717 } else {
7718 __ movl(dest, Immediate(value));
7719 }
7720}
7721
7722void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7723 if (value == 0) {
7724 __ testl(dest, dest);
7725 } else {
7726 __ cmpl(dest, Immediate(value));
7727 }
7728}
7729
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007730void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7731 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007732 GenerateIntCompare(lhs_reg, rhs);
7733}
7734
7735void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007736 if (rhs.IsConstant()) {
7737 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007738 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007739 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007740 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007741 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007742 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007743 }
7744}
7745
7746Address CodeGeneratorX86::ArrayAddress(Register obj,
7747 Location index,
7748 ScaleFactor scale,
7749 uint32_t data_offset) {
7750 return index.IsConstant() ?
7751 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7752 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7753}
7754
Mark Mendell805b3b52015-09-18 14:10:29 -04007755Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7756 Register reg,
7757 Register value) {
7758 // Create a fixup to be used to create and address the jump table.
7759 JumpTableRIPFixup* table_fixup =
7760 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7761
7762 // We have to populate the jump tables.
7763 fixups_to_jump_tables_.push_back(table_fixup);
7764
7765 // We want a scaled address, as we are extracting the correct offset from the table.
7766 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7767}
7768
Andreas Gampe85b62f22015-09-09 13:15:38 -07007769// TODO: target as memory.
7770void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7771 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007772 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007773 return;
7774 }
7775
7776 DCHECK_NE(type, Primitive::kPrimVoid);
7777
7778 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7779 if (target.Equals(return_loc)) {
7780 return;
7781 }
7782
7783 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7784 // with the else branch.
7785 if (type == Primitive::kPrimLong) {
7786 HParallelMove parallel_move(GetGraph()->GetArena());
7787 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7788 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7789 GetMoveResolver()->EmitNativeCode(&parallel_move);
7790 } else {
7791 // Let the parallel move resolver take care of all of this.
7792 HParallelMove parallel_move(GetGraph()->GetArena());
7793 parallel_move.AddMove(return_loc, target, type, nullptr);
7794 GetMoveResolver()->EmitNativeCode(&parallel_move);
7795 }
7796}
7797
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007798void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7799 const uint8_t* roots_data,
7800 const PatchInfo<Label>& info,
7801 uint64_t index_in_table) const {
7802 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7803 uintptr_t address =
7804 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7805 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7806 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7807 dchecked_integral_cast<uint32_t>(address);
7808}
7809
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007810void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7811 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007812 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007813 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007814 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007815 uint64_t index_in_table = it->second;
7816 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007817 }
7818
7819 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007820 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007821 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7822 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007823 uint64_t index_in_table = it->second;
7824 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007825 }
7826}
7827
Roland Levillain4d027112015-07-01 15:41:14 +01007828#undef __
7829
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007830} // namespace x86
7831} // namespace art